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 |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 475 | * error if the argument is not a "function". |
| 476 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 477 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 478 | __LJMP int hlua_checkfunction(lua_State *L, int argno) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 479 | { |
| 480 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 481 | 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] | 482 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 483 | } |
| 484 | lua_pushvalue(L, argno); |
| 485 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 486 | } |
| 487 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 488 | /* Used to check an Lua table type in the stack. It creates and |
| 489 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 490 | * error if the argument is not a "table". |
| 491 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 492 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 493 | __LJMP int hlua_checktable(lua_State *L, int argno) |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 494 | { |
| 495 | if (!lua_istable(L, argno)) { |
| 496 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 497 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 498 | } |
| 499 | lua_pushvalue(L, argno); |
| 500 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 501 | } |
| 502 | |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 503 | /* Get a reference to the object that is at the top of the stack |
| 504 | * The referenced object will be popped from the stack |
| 505 | * |
| 506 | * The function returns the reference to the object which must |
| 507 | * be cleared using hlua_unref() when no longer used |
| 508 | */ |
| 509 | __LJMP int hlua_ref(lua_State *L) |
| 510 | { |
| 511 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 512 | } |
| 513 | |
| 514 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 515 | * on <L> stack |
| 516 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 517 | * |
| 518 | * When the reference is no longer used, it should be released by calling |
| 519 | * hlua_unref() |
| 520 | * |
| 521 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 522 | * parent state that the one used to get the reference. |
| 523 | */ |
| 524 | void hlua_pushref(lua_State *L, int ref) |
| 525 | { |
| 526 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 527 | } |
| 528 | |
| 529 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 530 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 531 | * |
| 532 | * This will allow the reference to be reused and the referred object |
| 533 | * to be garbage collected. |
| 534 | * |
| 535 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 536 | * parent state that the one used to get the reference. |
| 537 | */ |
| 538 | void hlua_unref(lua_State *L, int ref) |
| 539 | { |
| 540 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 541 | } |
| 542 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 543 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 544 | { |
| 545 | lua_Debug ar; |
| 546 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 547 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 548 | |
| 549 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 550 | /* Fill fields: |
| 551 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 552 | * 'l': fills in the field currentline; |
| 553 | * 'n': fills in the field name and namewhat; |
| 554 | * 't': fills in the field istailcall; |
| 555 | */ |
| 556 | lua_getinfo(L, "Slnt", &ar); |
| 557 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 558 | /* skip these empty entries, usually they come from deep C functions */ |
| 559 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 560 | continue; |
| 561 | |
| 562 | /* Add separator */ |
| 563 | if (b_data(msg)) |
| 564 | chunk_appendf(msg, "%s", sep); |
| 565 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 566 | /* Append code localisation */ |
| 567 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 568 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 569 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 570 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 571 | |
| 572 | /* |
| 573 | * Get function name |
| 574 | * |
| 575 | * if namewhat is no empty, name is defined. |
| 576 | * what contains "Lua" for Lua function, "C" for C function, |
| 577 | * or "main" for main code. |
| 578 | */ |
| 579 | 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] | 580 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 581 | |
| 582 | 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] | 583 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 584 | |
| 585 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 586 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 587 | |
| 588 | else /* nothing left... */ |
| 589 | chunk_appendf(msg, "?"); |
| 590 | |
| 591 | |
| 592 | /* Display tailed call */ |
| 593 | if (ar.istailcall) |
| 594 | chunk_appendf(msg, " ..."); |
| 595 | } |
| 596 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 597 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 601 | /* This function check the number of arguments available in the |
| 602 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 603 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 604 | */ |
| 605 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 606 | { |
| 607 | if (lua_gettop(L) == nb) |
| 608 | return; |
| 609 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 610 | } |
| 611 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 612 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 613 | * and the line number where the error is encountered. |
| 614 | */ |
| 615 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 616 | { |
| 617 | va_list argp; |
| 618 | va_start(argp, fmt); |
| 619 | luaL_where(L, 1); |
| 620 | lua_pushvfstring(L, fmt, argp); |
| 621 | va_end(argp); |
| 622 | lua_concat(L, 2); |
| 623 | return 1; |
| 624 | } |
| 625 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 626 | /* This functions is used with sample fetch and converters. It |
| 627 | * converts the HAProxy configuration argument in a lua stack |
| 628 | * values. |
| 629 | * |
| 630 | * It takes an array of "arg", and each entry of the array is |
| 631 | * converted and pushed in the LUA stack. |
| 632 | */ |
| 633 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 634 | { |
| 635 | switch (arg->type) { |
| 636 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 637 | case ARGT_TIME: |
| 638 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 639 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 640 | break; |
| 641 | |
| 642 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 643 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 644 | break; |
| 645 | |
| 646 | case ARGT_IPV4: |
| 647 | case ARGT_IPV6: |
| 648 | case ARGT_MSK4: |
| 649 | case ARGT_MSK6: |
| 650 | case ARGT_FE: |
| 651 | case ARGT_BE: |
| 652 | case ARGT_TAB: |
| 653 | case ARGT_SRV: |
| 654 | case ARGT_USR: |
| 655 | case ARGT_MAP: |
| 656 | default: |
| 657 | lua_pushnil(L); |
| 658 | break; |
| 659 | } |
| 660 | return 1; |
| 661 | } |
| 662 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 663 | /* 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] | 664 | * 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] | 665 | * with sample fetch wrappers. The input arguments are given to the |
| 666 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | */ |
| 668 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 669 | { |
| 670 | switch (lua_type(L, ud)) { |
| 671 | |
| 672 | case LUA_TNUMBER: |
| 673 | case LUA_TBOOLEAN: |
| 674 | arg->type = ARGT_SINT; |
| 675 | arg->data.sint = lua_tointeger(L, ud); |
| 676 | break; |
| 677 | |
| 678 | case LUA_TSTRING: |
| 679 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 680 | 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] | 681 | /* 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] | 682 | 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] | 683 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case LUA_TUSERDATA: |
| 687 | case LUA_TNIL: |
| 688 | case LUA_TTABLE: |
| 689 | case LUA_TFUNCTION: |
| 690 | case LUA_TTHREAD: |
| 691 | case LUA_TLIGHTUSERDATA: |
| 692 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 693 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 694 | break; |
| 695 | } |
| 696 | return 1; |
| 697 | } |
| 698 | |
| 699 | /* the following functions are used to convert a struct sample |
| 700 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 701 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 702 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 703 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 704 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 705 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 706 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 707 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 708 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 709 | break; |
| 710 | |
| 711 | case SMP_T_BIN: |
| 712 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | 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] | 714 | break; |
| 715 | |
| 716 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 717 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 718 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 719 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 720 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 721 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 722 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 723 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 724 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 725 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 726 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 727 | 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] | 728 | break; |
| 729 | default: |
| 730 | lua_pushnil(L); |
| 731 | break; |
| 732 | } |
| 733 | break; |
| 734 | |
| 735 | case SMP_T_IPV4: |
| 736 | case SMP_T_IPV6: |
| 737 | 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] | 738 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 739 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 740 | 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] | 741 | else |
| 742 | lua_pushnil(L); |
| 743 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 744 | default: |
| 745 | lua_pushnil(L); |
| 746 | break; |
| 747 | } |
| 748 | return 1; |
| 749 | } |
| 750 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 751 | /* the following functions are used to convert a struct sample |
| 752 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 753 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 754 | */ |
| 755 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 756 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 757 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 758 | |
| 759 | case SMP_T_BIN: |
| 760 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 761 | 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] | 762 | break; |
| 763 | |
| 764 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 765 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 766 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 767 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 768 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 769 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 770 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 771 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 772 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 773 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 774 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | 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] | 776 | break; |
| 777 | default: |
| 778 | lua_pushstring(L, ""); |
| 779 | break; |
| 780 | } |
| 781 | break; |
| 782 | |
| 783 | case SMP_T_SINT: |
| 784 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 785 | case SMP_T_IPV4: |
| 786 | case SMP_T_IPV6: |
| 787 | 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] | 788 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 789 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 790 | 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] | 791 | else |
| 792 | lua_pushstring(L, ""); |
| 793 | break; |
| 794 | default: |
| 795 | lua_pushstring(L, ""); |
| 796 | break; |
| 797 | } |
| 798 | return 1; |
| 799 | } |
| 800 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 801 | /* the following functions are used to convert an Lua type in a |
| 802 | * struct sample. This is useful to provide data from a converter |
| 803 | * to the LUA code. |
| 804 | */ |
| 805 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 806 | { |
| 807 | switch (lua_type(L, ud)) { |
| 808 | |
| 809 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 810 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 811 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 812 | break; |
| 813 | |
| 814 | |
| 815 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 816 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 817 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 818 | break; |
| 819 | |
| 820 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 821 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 822 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 823 | 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] | 824 | /* 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] | 825 | 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] | 826 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | break; |
| 828 | |
| 829 | case LUA_TUSERDATA: |
| 830 | case LUA_TNIL: |
| 831 | case LUA_TTABLE: |
| 832 | case LUA_TFUNCTION: |
| 833 | case LUA_TTHREAD: |
| 834 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 835 | case LUA_TNONE: |
| 836 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 837 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 838 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 839 | break; |
| 840 | } |
| 841 | return 1; |
| 842 | } |
| 843 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 844 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 845 | * 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] | 846 | * 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] | 847 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 848 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 849 | * 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] | 850 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 851 | __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] | 852 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 853 | { |
| 854 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 855 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 856 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 857 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 858 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 859 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 860 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 861 | |
| 862 | idx = 0; |
| 863 | min_arg = ARGM(mask); |
| 864 | mask >>= ARGM_BITS; |
| 865 | |
| 866 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 867 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 868 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 869 | /* Check for mandatory arguments. */ |
| 870 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 871 | if (idx < min_arg) { |
| 872 | |
| 873 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | if (idx > 0) { |
| 875 | msg = "Mandatory argument expected"; |
| 876 | goto error; |
| 877 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 878 | |
| 879 | /* If first argument have a certain type, some default values |
| 880 | * may be used. See the function smp_resolve_args(). |
| 881 | */ |
| 882 | switch (mask & ARGT_MASK) { |
| 883 | |
| 884 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 885 | if (!(p->cap & PR_CAP_FE)) { |
| 886 | msg = "Mandatory argument expected"; |
| 887 | goto error; |
| 888 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 889 | argp[idx].data.prx = p; |
| 890 | argp[idx].type = ARGT_FE; |
| 891 | argp[idx+1].type = ARGT_STOP; |
| 892 | break; |
| 893 | |
| 894 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 895 | if (!(p->cap & PR_CAP_BE)) { |
| 896 | msg = "Mandatory argument expected"; |
| 897 | goto error; |
| 898 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 899 | argp[idx].data.prx = p; |
| 900 | argp[idx].type = ARGT_BE; |
| 901 | argp[idx+1].type = ARGT_STOP; |
| 902 | break; |
| 903 | |
| 904 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 905 | if (!p->table) { |
| 906 | msg = "Mandatory argument expected"; |
| 907 | goto error; |
| 908 | } |
| 909 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 910 | argp[idx].type = ARGT_TAB; |
| 911 | argp[idx+1].type = ARGT_STOP; |
| 912 | break; |
| 913 | |
| 914 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 915 | msg = "Mandatory argument expected"; |
| 916 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 917 | break; |
| 918 | } |
| 919 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 920 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 921 | } |
| 922 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 923 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 924 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 925 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 926 | msg = "Last argument expected"; |
| 927 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 928 | } |
| 929 | |
| 930 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 931 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 932 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 933 | } |
| 934 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | /* Convert some argument types. All string in argp[] are for not |
| 936 | * duplicated yet. |
| 937 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 938 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 939 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | if (argp[idx].type != ARGT_SINT) { |
| 941 | msg = "integer expected"; |
| 942 | goto error; |
| 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | argp[idx].type = ARGT_SINT; |
| 945 | break; |
| 946 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 948 | if (argp[idx].type != ARGT_SINT) { |
| 949 | msg = "integer expected"; |
| 950 | goto error; |
| 951 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 952 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | break; |
| 954 | |
| 955 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 956 | if (argp[idx].type != ARGT_SINT) { |
| 957 | msg = "integer expected"; |
| 958 | goto error; |
| 959 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 960 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 961 | break; |
| 962 | |
| 963 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 964 | if (argp[idx].type != ARGT_STR) { |
| 965 | msg = "string expected"; |
| 966 | goto error; |
| 967 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | 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] | 969 | if (!argp[idx].data.prx) { |
| 970 | msg = "frontend doesn't exist"; |
| 971 | goto error; |
| 972 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 973 | argp[idx].type = ARGT_FE; |
| 974 | break; |
| 975 | |
| 976 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 977 | if (argp[idx].type != ARGT_STR) { |
| 978 | msg = "string expected"; |
| 979 | goto error; |
| 980 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 981 | 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] | 982 | if (!argp[idx].data.prx) { |
| 983 | msg = "backend doesn't exist"; |
| 984 | goto error; |
| 985 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 986 | argp[idx].type = ARGT_BE; |
| 987 | break; |
| 988 | |
| 989 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 990 | if (argp[idx].type != ARGT_STR) { |
| 991 | msg = "string expected"; |
| 992 | goto error; |
| 993 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 994 | 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] | 995 | if (!argp[idx].data.t) { |
| 996 | msg = "table doesn't exist"; |
| 997 | goto error; |
| 998 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 999 | argp[idx].type = ARGT_TAB; |
| 1000 | break; |
| 1001 | |
| 1002 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1003 | if (argp[idx].type != ARGT_STR) { |
| 1004 | msg = "string expected"; |
| 1005 | goto error; |
| 1006 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1007 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1008 | if (sname) { |
| 1009 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1010 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1011 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1012 | if (!px) { |
| 1013 | msg = "backend doesn't exist"; |
| 1014 | goto error; |
| 1015 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1016 | } |
| 1017 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1018 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1019 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1020 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1021 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1022 | if (!argp[idx].data.srv) { |
| 1023 | msg = "server doesn't exist"; |
| 1024 | goto error; |
| 1025 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1026 | argp[idx].type = ARGT_SRV; |
| 1027 | break; |
| 1028 | |
| 1029 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1030 | if (argp[idx].type != ARGT_STR) { |
| 1031 | msg = "string expected"; |
| 1032 | goto error; |
| 1033 | } |
| 1034 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1035 | msg = "invalid IPv4 address"; |
| 1036 | goto error; |
| 1037 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1038 | argp[idx].type = ARGT_IPV4; |
| 1039 | break; |
| 1040 | |
| 1041 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1042 | if (argp[idx].type == ARGT_SINT) |
| 1043 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1044 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1045 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1046 | msg = "invalid IPv4 mask"; |
| 1047 | goto error; |
| 1048 | } |
| 1049 | } |
| 1050 | else { |
| 1051 | msg = "integer or string expected"; |
| 1052 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1053 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1054 | argp[idx].type = ARGT_MSK4; |
| 1055 | break; |
| 1056 | |
| 1057 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1058 | if (argp[idx].type != ARGT_STR) { |
| 1059 | msg = "string expected"; |
| 1060 | goto error; |
| 1061 | } |
| 1062 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1063 | msg = "invalid IPv6 address"; |
| 1064 | goto error; |
| 1065 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1066 | argp[idx].type = ARGT_IPV6; |
| 1067 | break; |
| 1068 | |
| 1069 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1070 | if (argp[idx].type == ARGT_SINT) |
| 1071 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1072 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1073 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1074 | msg = "invalid IPv6 mask"; |
| 1075 | goto error; |
| 1076 | } |
| 1077 | } |
| 1078 | else { |
| 1079 | msg = "integer or string expected"; |
| 1080 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1081 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1082 | argp[idx].type = ARGT_MSK6; |
| 1083 | break; |
| 1084 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1085 | case ARGT_REG: |
| 1086 | if (argp[idx].type != ARGT_STR) { |
| 1087 | msg = "string expected"; |
| 1088 | goto error; |
| 1089 | } |
| 1090 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1091 | if (!reg) { |
| 1092 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1093 | argp[idx].data.str.area, err); |
| 1094 | free(err); |
| 1095 | goto error; |
| 1096 | } |
| 1097 | argp[idx].type = ARGT_REG; |
| 1098 | argp[idx].data.reg = reg; |
| 1099 | break; |
| 1100 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1101 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1102 | if (argp[idx].type != ARGT_STR) { |
| 1103 | msg = "string expected"; |
| 1104 | goto error; |
| 1105 | } |
| 1106 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1107 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1108 | ul = p->uri_auth->userlist; |
| 1109 | else |
| 1110 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1111 | |
| 1112 | if (!ul) { |
| 1113 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1114 | goto error; |
| 1115 | } |
| 1116 | argp[idx].type = ARGT_USR; |
| 1117 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1118 | break; |
| 1119 | |
| 1120 | case ARGT_STR: |
| 1121 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1122 | msg = "unable to duplicate string arg"; |
| 1123 | goto error; |
| 1124 | } |
| 1125 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1126 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1127 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1128 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1129 | msg = "type not yet supported"; |
| 1130 | goto error; |
| 1131 | break; |
| 1132 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1133 | } |
| 1134 | |
| 1135 | /* Check for type of argument. */ |
| 1136 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1137 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1138 | arg_type_names[(mask & ARGT_MASK)], |
| 1139 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1140 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1141 | } |
| 1142 | |
| 1143 | /* Next argument. */ |
| 1144 | mask >>= ARGT_BITS; |
| 1145 | idx++; |
| 1146 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1147 | return 0; |
| 1148 | |
| 1149 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1150 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1151 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1152 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1153 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1154 | } |
| 1155 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1156 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1157 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1158 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | * |
| 1160 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1162 | */ |
| 1163 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1164 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1165 | struct hlua **hlua = lua_getextraspace(L); |
| 1166 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | } |
| 1168 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1169 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1170 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1171 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | } |
| 1173 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1174 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1175 | * currently is executing from within a Lua function. One line per entry will |
| 1176 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1177 | * Lua function is not detected, NULL is returned. |
| 1178 | */ |
| 1179 | const char *hlua_show_current_location(const char *pfx) |
| 1180 | { |
| 1181 | lua_State *L; |
| 1182 | lua_Debug ar; |
| 1183 | |
| 1184 | /* global or per-thread stack initializing ? */ |
| 1185 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1186 | return hlua_traceback(L, pfx); |
| 1187 | |
| 1188 | /* per-thread stack running ? */ |
| 1189 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1190 | return hlua_traceback(L, pfx); |
| 1191 | |
| 1192 | /* global stack running ? */ |
| 1193 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1194 | return hlua_traceback(L, pfx); |
| 1195 | |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1199 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1200 | * and on the default syslog server. |
| 1201 | */ |
| 1202 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1203 | { |
| 1204 | struct tm tm; |
| 1205 | char *p; |
| 1206 | |
| 1207 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1208 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1209 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1210 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1211 | /* Break the message if exceed the buffer size. */ |
| 1212 | *(p-4) = ' '; |
| 1213 | *(p-3) = '.'; |
| 1214 | *(p-2) = '.'; |
| 1215 | *(p-1) = '.'; |
| 1216 | break; |
| 1217 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1218 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1219 | *p = *msg; |
| 1220 | else |
| 1221 | *p = '.'; |
| 1222 | } |
| 1223 | *p = '\0'; |
| 1224 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1225 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1226 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1227 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1228 | return; |
| 1229 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1230 | get_localtime(date.tv_sec, &tm); |
| 1231 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1232 | 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] | 1233 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1234 | fflush(stderr); |
| 1235 | } |
| 1236 | } |
| 1237 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1238 | /* This function just ensure that the yield will be always |
| 1239 | * returned with a timeout and permit to set some flags |
| 1240 | */ |
| 1241 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1242 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1243 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1244 | struct hlua *hlua; |
| 1245 | |
| 1246 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1247 | hlua = hlua_gethlua(L); |
| 1248 | if (!hlua) { |
| 1249 | return; |
| 1250 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1251 | |
| 1252 | /* Set the wake timeout. If timeout is required, we set |
| 1253 | * the expiration time. |
| 1254 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1255 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1256 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1257 | hlua->flags |= flags; |
| 1258 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1259 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1260 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1261 | } |
| 1262 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1263 | /* This function initialises the Lua environment stored in the stream. |
| 1264 | * 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] | 1265 | * 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] | 1266 | * |
| 1267 | * This function is particular. it initialises a new Lua thread. If the |
| 1268 | * initialisation fails (example: out of memory error), the lua function |
| 1269 | * throws an error (longjmp). |
| 1270 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1271 | * 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] | 1272 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1273 | * threads appear, the safe environment set a lock to ensure only one |
| 1274 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1275 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1276 | * |
| 1277 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1278 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1279 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1280 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1281 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1282 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1283 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1285 | 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] | 1286 | { |
| 1287 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1288 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1289 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1290 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1291 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1292 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1293 | MT_LIST_INIT(&lua->hc_list); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1294 | if (!already_safe) { |
| 1295 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1296 | lua->Tref = LUA_REFNIL; |
| 1297 | return 0; |
| 1298 | } |
| 1299 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1300 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1301 | if (!lua->T) { |
| 1302 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1303 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1304 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1305 | return 0; |
| 1306 | } |
| 1307 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1308 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1309 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1310 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1311 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1312 | return 1; |
| 1313 | } |
| 1314 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1315 | /* kill all associated httpclient to this hlua task |
| 1316 | * We must take extra precautions as we're manipulating lua-exposed |
| 1317 | * objects without the main lua lock. |
| 1318 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1319 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1320 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1321 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1322 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1323 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1324 | * another thread sharing the same main lua stack (lua coroutine) |
| 1325 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1326 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1327 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1328 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1329 | * be thread safe in Lua. (From lua co-author: |
| 1330 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1331 | * |
| 1332 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1333 | * since in this case coroutines exclusively run on the same thread |
| 1334 | * (main stack is not shared between OS threads). |
| 1335 | */ |
| 1336 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1337 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1338 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1339 | } |
| 1340 | } |
| 1341 | |
| 1342 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1343 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1344 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1345 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1346 | */ |
| 1347 | void hlua_ctx_destroy(struct hlua *lua) |
| 1348 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1349 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1350 | return; |
| 1351 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1352 | if (!lua->T) |
| 1353 | goto end; |
| 1354 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1355 | /* clean all running httpclient */ |
| 1356 | hlua_httpclient_destroy_all(lua); |
| 1357 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1358 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1359 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1360 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1361 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1362 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1363 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1364 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1365 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1366 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1367 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1368 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1369 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1370 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1371 | * without error, we run the GC on the thread pointer. Its freed all |
| 1372 | * the unused memory. |
| 1373 | * If the thread is finnish with an error or is currently yielded, |
| 1374 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1375 | * so e run the GC on the main thread. |
| 1376 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1377 | * the garbage collection. |
| 1378 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1379 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1380 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1381 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1382 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1383 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1384 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1385 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1386 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1387 | |
| 1388 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1389 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1390 | } |
| 1391 | |
| 1392 | /* This function is used to restore the Lua context when a coroutine |
| 1393 | * fails. This function copy the common memory between old coroutine |
| 1394 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1395 | * replaced by the new coroutine. |
| 1396 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1397 | * as string error message and it is copied in the new stack. |
| 1398 | */ |
| 1399 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1400 | { |
| 1401 | lua_State *T; |
| 1402 | int new_ref; |
| 1403 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1404 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1405 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1406 | if (!T) |
| 1407 | return 0; |
| 1408 | |
| 1409 | /* Copy last error message. */ |
| 1410 | if (keep_msg) |
| 1411 | lua_xmove(lua->T, T, 1); |
| 1412 | |
| 1413 | /* Copy data between the coroutines. */ |
| 1414 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1415 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1416 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1417 | |
| 1418 | /* Destroy old data. */ |
| 1419 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1420 | |
| 1421 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1422 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | |
| 1424 | /* Fill the struct with the new coroutine values. */ |
| 1425 | lua->Mref = new_ref; |
| 1426 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1427 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1428 | |
| 1429 | /* Set context. */ |
| 1430 | hlua_sethlua(lua); |
| 1431 | |
| 1432 | return 1; |
| 1433 | } |
| 1434 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1435 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1436 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1437 | struct hlua *hlua; |
| 1438 | |
| 1439 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1440 | hlua = hlua_gethlua(L); |
| 1441 | if (!hlua) |
| 1442 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1443 | |
| 1444 | /* Lua cannot yield when its returning from a function, |
| 1445 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1446 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1447 | */ |
| 1448 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1449 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1450 | return; |
| 1451 | } |
| 1452 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1453 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1454 | * If the state is not yieldable, trying yield causes an error. |
| 1455 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1456 | if (lua_isyieldable(L)) { |
| 1457 | /* note: for converters/fetches.. where yielding is not allowed |
| 1458 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1459 | * instead of rescheduling hlua->task. |
| 1460 | * also: hlua_ctx_resume() will take care of checking execution |
| 1461 | * timeout and re-applying the hook as needed. |
| 1462 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1463 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1464 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1465 | * |
| 1466 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1467 | * |
| 1468 | * Moreover, it seems that we don't want to continue after the yield |
| 1469 | * because the end of the function is about handling unyieldable function, |
| 1470 | * which is not the case here. |
| 1471 | * |
| 1472 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1473 | * in the function. |
| 1474 | */ |
| 1475 | return; |
| 1476 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1477 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1478 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1479 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1480 | hlua->run_time += now_ms - hlua->start_time; |
| 1481 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1482 | lua_pushfstring(L, "execution timeout"); |
| 1483 | WILL_LJMP(lua_error(L)); |
| 1484 | } |
| 1485 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1486 | /* Update the start time. */ |
| 1487 | hlua->start_time = now_ms; |
| 1488 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1489 | /* Try to interrupt the process at the end of the current |
| 1490 | * unyieldable function. |
| 1491 | */ |
| 1492 | 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] | 1493 | } |
| 1494 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1495 | /* This function start or resumes the Lua stack execution. If the flag |
| 1496 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1497 | * The function return an error. |
| 1498 | * |
| 1499 | * The function can returns 4 values: |
| 1500 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1501 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1502 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1503 | * - 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] | 1504 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1505 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1506 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1507 | * 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] | 1508 | * LUA code. |
| 1509 | */ |
| 1510 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1511 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1512 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1513 | int nres; |
| 1514 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1515 | int ret; |
| 1516 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1517 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1518 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1519 | /* Initialise run time counter. */ |
| 1520 | if (!HLUA_IS_RUNNING(lua)) |
| 1521 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1522 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1523 | /* Lock the whole Lua execution. This lock must be before the |
| 1524 | * label "resume_execution". |
| 1525 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1526 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1527 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1528 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1529 | resume_execution: |
| 1530 | |
| 1531 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1532 | * instructions. it is used for preventing infinite loops. |
| 1533 | */ |
| 1534 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1535 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1536 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1537 | HLUA_SET_RUN(lua); |
| 1538 | HLUA_CLR_CTRLYIELD(lua); |
| 1539 | HLUA_CLR_WAKERESWR(lua); |
| 1540 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1541 | HLUA_CLR_NOYIELD(lua); |
| 1542 | if (!yield_allowed) |
| 1543 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1544 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1545 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1546 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1547 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1548 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1550 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1551 | 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] | 1552 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1553 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1554 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1555 | switch (ret) { |
| 1556 | |
| 1557 | case LUA_OK: |
| 1558 | ret = HLUA_E_OK; |
| 1559 | break; |
| 1560 | |
| 1561 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1562 | /* Check if the execution timeout is expired. It it is the case, we |
| 1563 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1564 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1565 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1566 | lua->run_time += now_ms - lua->start_time; |
| 1567 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1568 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1569 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1570 | break; |
| 1571 | } |
| 1572 | /* Process the forced yield. if the general yield is not allowed or |
| 1573 | * if no task were associated this the current Lua execution |
| 1574 | * coroutine, we resume the execution. Else we want to return in the |
| 1575 | * scheduler and we want to be waked up again, to continue the |
| 1576 | * current Lua execution. So we schedule our own task. |
| 1577 | */ |
| 1578 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1579 | if (!yield_allowed || !lua->task) |
| 1580 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1581 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1582 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1583 | if (!yield_allowed) { |
| 1584 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1585 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1586 | break; |
| 1587 | } |
| 1588 | ret = HLUA_E_AGAIN; |
| 1589 | break; |
| 1590 | |
| 1591 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1592 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1593 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1594 | * because the errors ares the only one mean to return immediately |
| 1595 | * from and lua execution. |
| 1596 | */ |
| 1597 | if (lua->flags & HLUA_EXIT) { |
| 1598 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1599 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1600 | break; |
| 1601 | } |
| 1602 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1603 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1604 | if (!lua_checkstack(lua->T, 1)) { |
| 1605 | ret = HLUA_E_ERR; |
| 1606 | break; |
| 1607 | } |
| 1608 | msg = lua_tostring(lua->T, -1); |
| 1609 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1610 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1611 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1612 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1613 | 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] | 1614 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1615 | 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] | 1616 | ret = HLUA_E_ERRMSG; |
| 1617 | break; |
| 1618 | |
| 1619 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1620 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1621 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1622 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1623 | break; |
| 1624 | |
| 1625 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1626 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1627 | if (!lua_checkstack(lua->T, 1)) { |
| 1628 | ret = HLUA_E_ERR; |
| 1629 | break; |
| 1630 | } |
| 1631 | msg = lua_tostring(lua->T, -1); |
| 1632 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1633 | lua_pop(lua->T, 1); |
| 1634 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1635 | 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] | 1636 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1637 | 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] | 1638 | ret = HLUA_E_ERRMSG; |
| 1639 | break; |
| 1640 | |
| 1641 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1642 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1643 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1644 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1645 | break; |
| 1646 | } |
| 1647 | |
| 1648 | switch (ret) { |
| 1649 | case HLUA_E_AGAIN: |
| 1650 | break; |
| 1651 | |
| 1652 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1653 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1654 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1655 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1656 | break; |
| 1657 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1658 | case HLUA_E_ETMOUT: |
| 1659 | case HLUA_E_NOMEM: |
| 1660 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1661 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1662 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1663 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1664 | hlua_ctx_renew(lua, 0); |
| 1665 | break; |
| 1666 | |
| 1667 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1668 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1669 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1670 | break; |
| 1671 | } |
| 1672 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1673 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1674 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1675 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1676 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1677 | return ret; |
| 1678 | } |
| 1679 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1680 | /* This function exit the current code. */ |
| 1681 | __LJMP static int hlua_done(lua_State *L) |
| 1682 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1683 | struct hlua *hlua; |
| 1684 | |
| 1685 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1686 | hlua = hlua_gethlua(L); |
| 1687 | if (!hlua) |
| 1688 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1689 | |
| 1690 | hlua->flags |= HLUA_EXIT; |
| 1691 | WILL_LJMP(lua_error(L)); |
| 1692 | |
| 1693 | return 0; |
| 1694 | } |
| 1695 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1696 | /* This function is an LUA binding. It provides a function |
| 1697 | * for deleting ACL from a referenced ACL file. |
| 1698 | */ |
| 1699 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1700 | { |
| 1701 | const char *name; |
| 1702 | const char *key; |
| 1703 | struct pat_ref *ref; |
| 1704 | |
| 1705 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1706 | |
| 1707 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1708 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1709 | |
| 1710 | ref = pat_ref_lookup(name); |
| 1711 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1712 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1713 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1714 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1715 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1716 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1717 | return 0; |
| 1718 | } |
| 1719 | |
| 1720 | /* This function is an LUA binding. It provides a function |
| 1721 | * for deleting map entry from a referenced map file. |
| 1722 | */ |
| 1723 | static int hlua_del_map(lua_State *L) |
| 1724 | { |
| 1725 | const char *name; |
| 1726 | const char *key; |
| 1727 | struct pat_ref *ref; |
| 1728 | |
| 1729 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1730 | |
| 1731 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1732 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1733 | |
| 1734 | ref = pat_ref_lookup(name); |
| 1735 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1736 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1737 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1738 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1739 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1740 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1741 | return 0; |
| 1742 | } |
| 1743 | |
| 1744 | /* This function is an LUA binding. It provides a function |
| 1745 | * for adding ACL pattern from a referenced ACL file. |
| 1746 | */ |
| 1747 | static int hlua_add_acl(lua_State *L) |
| 1748 | { |
| 1749 | const char *name; |
| 1750 | const char *key; |
| 1751 | struct pat_ref *ref; |
| 1752 | |
| 1753 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1754 | |
| 1755 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1756 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1757 | |
| 1758 | ref = pat_ref_lookup(name); |
| 1759 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1760 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1761 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1762 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1763 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1764 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1765 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1766 | return 0; |
| 1767 | } |
| 1768 | |
| 1769 | /* This function is an LUA binding. It provides a function |
| 1770 | * for setting map pattern and sample from a referenced map |
| 1771 | * file. |
| 1772 | */ |
| 1773 | static int hlua_set_map(lua_State *L) |
| 1774 | { |
| 1775 | const char *name; |
| 1776 | const char *key; |
| 1777 | const char *value; |
| 1778 | struct pat_ref *ref; |
| 1779 | |
| 1780 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1781 | |
| 1782 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1783 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1784 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1785 | |
| 1786 | ref = pat_ref_lookup(name); |
| 1787 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1788 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1789 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1790 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1791 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1792 | pat_ref_set(ref, key, value, NULL); |
| 1793 | else |
| 1794 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1795 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1796 | return 0; |
| 1797 | } |
| 1798 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1799 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1800 | * 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] | 1801 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1802 | * the name of the object (_G[<name>] = <metable> ). |
| 1803 | * |
| 1804 | * A metable is a table that modify the standard behavior of a standard |
| 1805 | * access to the associated data. The entries of this new metatable are |
| 1806 | * defined as is: |
| 1807 | * |
| 1808 | * http://lua-users.org/wiki/MetatableEvents |
| 1809 | * |
| 1810 | * __index |
| 1811 | * |
| 1812 | * we access an absent field in a table, the result is nil. This is |
| 1813 | * true, but it is not the whole truth. Actually, such access triggers |
| 1814 | * the interpreter to look for an __index metamethod: If there is no |
| 1815 | * such method, as usually happens, then the access results in nil; |
| 1816 | * otherwise, the metamethod will provide the result. |
| 1817 | * |
| 1818 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1819 | * the key does not appear in the table, but the metatable has an __index |
| 1820 | * property: |
| 1821 | * |
| 1822 | * - if the value is a function, the function is called, passing in the |
| 1823 | * table and the key; the return value of that function is returned as |
| 1824 | * the result. |
| 1825 | * |
| 1826 | * - if the value is another table, the value of the key in that table is |
| 1827 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1828 | * table's metatable has an __index property, then it continues on up) |
| 1829 | * |
| 1830 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1831 | * |
| 1832 | * http://www.lua.org/pil/13.4.1.html |
| 1833 | * |
| 1834 | * __newindex |
| 1835 | * |
| 1836 | * Like __index, but control property assignment. |
| 1837 | * |
| 1838 | * __mode - Control weak references. A string value with one or both |
| 1839 | * of the characters 'k' and 'v' which specifies that the the |
| 1840 | * keys and/or values in the table are weak references. |
| 1841 | * |
| 1842 | * __call - Treat a table like a function. When a table is followed by |
| 1843 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1844 | * a __call key pointing to a function, that function is invoked |
| 1845 | * (passing any specified arguments) and the return value is |
| 1846 | * returned. |
| 1847 | * |
| 1848 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1849 | * called, if the metatable for myTable has a __metatable |
| 1850 | * key, the value of that key is returned instead of the |
| 1851 | * actual metatable. |
| 1852 | * |
| 1853 | * __tostring - Control string representation. When the builtin |
| 1854 | * "tostring( myTable )" function is called, if the metatable |
| 1855 | * for myTable has a __tostring property set to a function, |
| 1856 | * that function is invoked (passing myTable to it) and the |
| 1857 | * return value is used as the string representation. |
| 1858 | * |
| 1859 | * __len - Control table length. When the table length is requested using |
| 1860 | * the length operator ( '#' ), if the metatable for myTable has |
| 1861 | * a __len key pointing to a function, that function is invoked |
| 1862 | * (passing myTable to it) and the return value used as the value |
| 1863 | * of "#myTable". |
| 1864 | * |
| 1865 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1866 | * collected, if the metatable has a __gc field pointing to a |
| 1867 | * function, that function is first invoked, passing the userdata |
| 1868 | * to it. The __gc metamethod is not called for tables. |
| 1869 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1870 | * |
| 1871 | * Special metamethods for redefining standard operators: |
| 1872 | * http://www.lua.org/pil/13.1.html |
| 1873 | * |
| 1874 | * __add "+" |
| 1875 | * __sub "-" |
| 1876 | * __mul "*" |
| 1877 | * __div "/" |
| 1878 | * __unm "!" |
| 1879 | * __pow "^" |
| 1880 | * __concat ".." |
| 1881 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1882 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1883 | * http://www.lua.org/pil/13.2.html |
| 1884 | * |
| 1885 | * __eq "==" |
| 1886 | * __lt "<" |
| 1887 | * __le "<=" |
| 1888 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1889 | |
| 1890 | /* |
| 1891 | * |
| 1892 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1893 | * Class Map |
| 1894 | * |
| 1895 | * |
| 1896 | */ |
| 1897 | |
| 1898 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1899 | * a class session, otherwise it throws an error. |
| 1900 | */ |
| 1901 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1902 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1903 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1904 | } |
| 1905 | |
| 1906 | /* This function is the map constructor. It don't need |
| 1907 | * the class Map object. It creates and return a new Map |
| 1908 | * object. It must be called only during "body" or "init" |
| 1909 | * context because it process some filesystem accesses. |
| 1910 | */ |
| 1911 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1912 | { |
| 1913 | const char *fn; |
| 1914 | int match = PAT_MATCH_STR; |
| 1915 | struct sample_conv conv; |
| 1916 | const char *file = ""; |
| 1917 | int line = 0; |
| 1918 | lua_Debug ar; |
| 1919 | char *err = NULL; |
| 1920 | struct arg args[2]; |
| 1921 | |
| 1922 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1923 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1924 | |
| 1925 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1926 | |
| 1927 | if (lua_gettop(L) >= 2) { |
| 1928 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1929 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1930 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1931 | } |
| 1932 | |
| 1933 | /* Get Lua filename and line number. */ |
| 1934 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1935 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1936 | if (ar.currentline > 0) { /* is there info? */ |
| 1937 | file = ar.short_src; |
| 1938 | line = ar.currentline; |
| 1939 | } |
| 1940 | } |
| 1941 | |
| 1942 | /* fill fake sample_conv struct. */ |
| 1943 | conv.kw = ""; /* unused. */ |
| 1944 | conv.process = NULL; /* unused. */ |
| 1945 | conv.arg_mask = 0; /* unused. */ |
| 1946 | conv.val_args = NULL; /* unused. */ |
| 1947 | conv.out_type = SMP_T_STR; |
| 1948 | conv.private = (void *)(long)match; |
| 1949 | switch (match) { |
| 1950 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1951 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1952 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1953 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1954 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1955 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1956 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1957 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1958 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1959 | default: |
| 1960 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1961 | } |
| 1962 | |
| 1963 | /* fill fake args. */ |
| 1964 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1965 | args[0].data.str.area = strdup(fn); |
| 1966 | args[0].data.str.data = strlen(fn); |
| 1967 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1968 | args[1].type = ARGT_STOP; |
| 1969 | |
| 1970 | /* load the map. */ |
| 1971 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1972 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1973 | * free the err variable. |
| 1974 | */ |
| 1975 | luaL_where(L, 1); |
| 1976 | lua_pushfstring(L, "'new': %s.", err); |
| 1977 | lua_concat(L, 2); |
| 1978 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1979 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1980 | WILL_LJMP(lua_error(L)); |
| 1981 | } |
| 1982 | |
| 1983 | /* create the lua object. */ |
| 1984 | lua_newtable(L); |
| 1985 | lua_pushlightuserdata(L, args[0].data.map); |
| 1986 | lua_rawseti(L, -2, 0); |
| 1987 | |
| 1988 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1989 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1990 | lua_setmetatable(L, -2); |
| 1991 | |
| 1992 | |
| 1993 | return 1; |
| 1994 | } |
| 1995 | |
| 1996 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1997 | { |
| 1998 | struct map_descriptor *desc; |
| 1999 | struct pattern *pat; |
| 2000 | struct sample smp; |
| 2001 | |
| 2002 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2003 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2004 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2005 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2006 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2007 | } |
| 2008 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2009 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2010 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2011 | 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] | 2012 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2013 | } |
| 2014 | |
| 2015 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2016 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2017 | if (str) |
| 2018 | lua_pushstring(L, ""); |
| 2019 | else |
| 2020 | lua_pushnil(L); |
| 2021 | return 1; |
| 2022 | } |
| 2023 | |
| 2024 | /* 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] | 2025 | 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] | 2026 | return 1; |
| 2027 | } |
| 2028 | |
| 2029 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2030 | { |
| 2031 | return _hlua_map_lookup(L, 0); |
| 2032 | } |
| 2033 | |
| 2034 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2035 | { |
| 2036 | return _hlua_map_lookup(L, 1); |
| 2037 | } |
| 2038 | |
| 2039 | /* |
| 2040 | * |
| 2041 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | * Class Socket |
| 2043 | * |
| 2044 | * |
| 2045 | */ |
| 2046 | |
| 2047 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2048 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2049 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | } |
| 2051 | |
| 2052 | /* 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] | 2053 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | * received. |
| 2055 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2056 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2058 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2059 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2060 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2061 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2062 | notification_wake(&ctx->wake_on_read); |
| 2063 | notification_wake(&ctx->wake_on_write); |
| 2064 | return; |
| 2065 | } |
| 2066 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2067 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2068 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2069 | notification_wake(&ctx->wake_on_read); |
| 2070 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2071 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2072 | } |
| 2073 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2074 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2075 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2076 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2077 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2078 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2079 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2080 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2081 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2082 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2083 | * to be notified whenever the connection completes. |
| 2084 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2085 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2086 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2087 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2088 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2089 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2090 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2091 | |
| 2092 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2093 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2095 | /* 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] | 2096 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2097 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2100 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2101 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2102 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2103 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2104 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2105 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2106 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2107 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | } |
| 2109 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2110 | static int hlua_socket_init(struct appctx *appctx) |
| 2111 | { |
| 2112 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2113 | struct stream *s; |
| 2114 | |
| 2115 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2116 | goto error; |
| 2117 | |
| 2118 | s = appctx_strm(appctx); |
| 2119 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2120 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2121 | * and retrieve data from the server. The connection is initialized |
| 2122 | * with the "struct server". |
| 2123 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2124 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2125 | |
| 2126 | /* Force destination server. */ |
| 2127 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2128 | s->target = &socket_tcp->obj_type; |
| 2129 | |
| 2130 | ctx->appctx = appctx; |
| 2131 | return 0; |
| 2132 | |
| 2133 | error: |
| 2134 | return -1; |
| 2135 | } |
| 2136 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2137 | /* This function is called when the "struct stream" is destroyed. |
| 2138 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2139 | * Wake all the pending signals. |
| 2140 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2141 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2142 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2143 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2144 | struct xref *peer; |
| 2145 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2146 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2147 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2148 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2149 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | |
| 2151 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2152 | notification_wake(&ctx->wake_on_read); |
| 2153 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | } |
| 2155 | |
| 2156 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2157 | * uses this object. If the stream does not exists, just quit. |
| 2158 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | * pending signal can rest in the read and write lists. destroy |
| 2160 | * it. |
| 2161 | */ |
| 2162 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2163 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2164 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2165 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2168 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2169 | |
| 2170 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2171 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2172 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2174 | |
| 2175 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2176 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2177 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2178 | ctx->die = 1; |
| 2179 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2181 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2182 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | return 0; |
| 2184 | } |
| 2185 | |
| 2186 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2187 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2188 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2189 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2191 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2192 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2193 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2194 | struct hlua *hlua; |
| 2195 | |
| 2196 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2197 | hlua = hlua_gethlua(L); |
| 2198 | if (!hlua) |
| 2199 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2201 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2202 | |
| 2203 | /* Check if we run on the same thread than the xreator thread. |
| 2204 | * We cannot access to the socket if the thread is different. |
| 2205 | */ |
| 2206 | if (socket->tid != tid) |
| 2207 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2208 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2209 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2210 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2211 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2212 | |
| 2213 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2214 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
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 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2217 | ctx->die = 1; |
| 2218 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2219 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2221 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2222 | return 0; |
| 2223 | } |
| 2224 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2225 | /* The close function calls close_helper. |
| 2226 | */ |
| 2227 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2228 | { |
| 2229 | MAY_LJMP(check_args(L, 1, "close")); |
| 2230 | return hlua_socket_close_helper(L); |
| 2231 | } |
| 2232 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | /* This Lua function assumes that the stack contain three parameters. |
| 2234 | * 1 - USERDATA containing a struct socket |
| 2235 | * 2 - INTEGER with values of the macro defined below |
| 2236 | * If the integer is -1, we must read at most one line. |
| 2237 | * If the integer is -2, we ust read all the data until the |
| 2238 | * end of the stream. |
| 2239 | * If the integer is positive value, we must read a number of |
| 2240 | * bytes corresponding to this value. |
| 2241 | */ |
| 2242 | #define HLSR_READ_LINE (-1) |
| 2243 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2244 | __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] | 2245 | { |
| 2246 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2247 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2248 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2249 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2251 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2253 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2254 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2255 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2256 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2257 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2258 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2259 | struct stream *s; |
| 2260 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2261 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2262 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2263 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2264 | hlua = hlua_gethlua(L); |
| 2265 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2266 | /* Check if this lua stack is schedulable. */ |
| 2267 | if (!hlua || !hlua->task) |
| 2268 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2269 | "'frontend', 'backend' or 'task'")); |
| 2270 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2271 | /* Check if we run on the same thread than the xreator thread. |
| 2272 | * We cannot access to the socket if the thread is different. |
| 2273 | */ |
| 2274 | if (socket->tid != tid) |
| 2275 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2276 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2277 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2279 | if (!peer) |
| 2280 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2281 | |
| 2282 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2283 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2284 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2285 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2289 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | if (nblk < 0) /* Connection close. */ |
| 2291 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2292 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2294 | |
| 2295 | /* remove final \r\n. */ |
| 2296 | if (nblk == 1) { |
| 2297 | if (blk1[len1-1] == '\n') { |
| 2298 | len1--; |
| 2299 | skip_at_end++; |
| 2300 | if (blk1[len1-1] == '\r') { |
| 2301 | len1--; |
| 2302 | skip_at_end++; |
| 2303 | } |
| 2304 | } |
| 2305 | } |
| 2306 | else { |
| 2307 | if (blk2[len2-1] == '\n') { |
| 2308 | len2--; |
| 2309 | skip_at_end++; |
| 2310 | if (blk2[len2-1] == '\r') { |
| 2311 | len2--; |
| 2312 | skip_at_end++; |
| 2313 | } |
| 2314 | } |
| 2315 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | } |
| 2317 | |
| 2318 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2320 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | if (nblk < 0) /* Connection close. */ |
| 2322 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2323 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2324 | goto connection_empty; |
| 2325 | } |
| 2326 | |
| 2327 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2329 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2330 | if (nblk < 0) /* Connection close. */ |
| 2331 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2332 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | goto connection_empty; |
| 2334 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2335 | missing_bytes = wanted - socket->b.n; |
| 2336 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2338 | len1 = missing_bytes; |
| 2339 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2340 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | } |
| 2342 | |
| 2343 | len = len1; |
| 2344 | |
| 2345 | luaL_addlstring(&socket->b, blk1, len1); |
| 2346 | if (nblk == 2) { |
| 2347 | len += len2; |
| 2348 | luaL_addlstring(&socket->b, blk2, len2); |
| 2349 | } |
| 2350 | |
| 2351 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2352 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | |
| 2354 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2355 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2356 | |
| 2357 | /* If the pattern reclaim to read all the data |
| 2358 | * in the connection, got out. |
| 2359 | */ |
| 2360 | if (wanted == HLSR_READ_ALL) |
| 2361 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2362 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | goto connection_empty; |
| 2364 | |
| 2365 | /* Return result. */ |
| 2366 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2367 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2368 | return 1; |
| 2369 | |
| 2370 | connection_closed: |
| 2371 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | xref_unlock(&socket->xref, peer); |
| 2373 | |
| 2374 | no_peer: |
| 2375 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | /* If the buffer containds data. */ |
| 2377 | if (socket->b.n > 0) { |
| 2378 | luaL_pushresult(&socket->b); |
| 2379 | return 1; |
| 2380 | } |
| 2381 | lua_pushnil(L); |
| 2382 | lua_pushstring(L, "connection closed."); |
| 2383 | return 2; |
| 2384 | |
| 2385 | connection_empty: |
| 2386 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2387 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2388 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2390 | } |
| 2391 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2392 | 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] | 2393 | return 0; |
| 2394 | } |
| 2395 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2396 | /* This Lua function gets two parameters. The first one can be string |
| 2397 | * or a number. If the string is "*l", the user requires one line. If |
| 2398 | * 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] | 2399 | * If the value is a number, the user require a number of bytes equal |
| 2400 | * to the value. The default value is "*l" (a line). |
| 2401 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2402 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | * integer takes this values: |
| 2404 | * -1 : read a line |
| 2405 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2406 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2408 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2409 | * concatenated with the read data. |
| 2410 | */ |
| 2411 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2412 | { |
| 2413 | int wanted = HLSR_READ_LINE; |
| 2414 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2415 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | char *error; |
| 2417 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2418 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | |
| 2420 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2421 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2422 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2423 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2424 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2425 | /* Check if we run on the same thread than the xreator thread. |
| 2426 | * We cannot access to the socket if the thread is different. |
| 2427 | */ |
| 2428 | if (socket->tid != tid) |
| 2429 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2430 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | /* check for pattern. */ |
| 2432 | if (lua_gettop(L) >= 2) { |
| 2433 | type = lua_type(L, 2); |
| 2434 | if (type == LUA_TSTRING) { |
| 2435 | pattern = lua_tostring(L, 2); |
| 2436 | if (strcmp(pattern, "*a") == 0) |
| 2437 | wanted = HLSR_READ_ALL; |
| 2438 | else if (strcmp(pattern, "*l") == 0) |
| 2439 | wanted = HLSR_READ_LINE; |
| 2440 | else { |
| 2441 | wanted = strtoll(pattern, &error, 10); |
| 2442 | if (*error != '\0') |
| 2443 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2444 | } |
| 2445 | } |
| 2446 | else if (type == LUA_TNUMBER) { |
| 2447 | wanted = lua_tointeger(L, 2); |
| 2448 | if (wanted < 0) |
| 2449 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2450 | } |
| 2451 | } |
| 2452 | |
| 2453 | /* Set pattern. */ |
| 2454 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2455 | |
| 2456 | /* Check if we would replace the top by itself. */ |
| 2457 | if (lua_gettop(L) != 2) |
| 2458 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2459 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2460 | /* Save index of the top of the stack because since buffers are used, it |
| 2461 | * may change |
| 2462 | */ |
| 2463 | lastarg = lua_gettop(L); |
| 2464 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2465 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | luaL_buffinit(L, &socket->b); |
| 2467 | |
| 2468 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2469 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2471 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2472 | pattern = lua_tolstring(L, 3, &len); |
| 2473 | luaL_addlstring(&socket->b, pattern, len); |
| 2474 | } |
| 2475 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2476 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | } |
| 2478 | |
| 2479 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2480 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | 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] | 2483 | { |
| 2484 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2485 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2486 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2487 | struct appctx *appctx; |
| 2488 | size_t buf_len; |
| 2489 | const char *buf; |
| 2490 | int len; |
| 2491 | int send_len; |
| 2492 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2494 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2495 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2497 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2498 | hlua = hlua_gethlua(L); |
| 2499 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | /* Check if this lua stack is schedulable. */ |
| 2501 | if (!hlua || !hlua->task) |
| 2502 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2503 | "'frontend', 'backend' or 'task'")); |
| 2504 | |
| 2505 | /* Get object */ |
| 2506 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2507 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2508 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2510 | /* Check if we run on the same thread than the xreator thread. |
| 2511 | * We cannot access to the socket if the thread is different. |
| 2512 | */ |
| 2513 | if (socket->tid != tid) |
| 2514 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2515 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2516 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2517 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2518 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2519 | lua_pushinteger(L, -1); |
| 2520 | return 1; |
| 2521 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2522 | |
| 2523 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2524 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2525 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2526 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2527 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2528 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2530 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | lua_pushinteger(L, -1); |
| 2532 | return 1; |
| 2533 | } |
| 2534 | |
| 2535 | /* Update the input buffer data. */ |
| 2536 | buf += sent; |
| 2537 | send_len = buf_len - sent; |
| 2538 | |
| 2539 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2540 | if (sent >= buf_len) { |
| 2541 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2542 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2543 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2545 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2546 | * the request buffer if its not required. |
| 2547 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2548 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2549 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2550 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2551 | } |
| 2552 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2553 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2554 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2555 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2556 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2557 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2558 | |
| 2559 | /* send data */ |
| 2560 | if (len < send_len) |
| 2561 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2562 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | |
| 2564 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2565 | * the data" (-2) are not expected because the available length |
| 2566 | * is tested. |
| 2567 | * Other unknown error are also not expected. |
| 2568 | */ |
| 2569 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2570 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2572 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2573 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2574 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2575 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2576 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | return 1; |
| 2578 | } |
| 2579 | |
| 2580 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2581 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2582 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2583 | /* Update length sent. */ |
| 2584 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2585 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2586 | |
| 2587 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2588 | if (sent + len >= buf_len) { |
| 2589 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2591 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | |
| 2593 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2594 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2595 | xref_unlock(&socket->xref, peer); |
| 2596 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2597 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2598 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2599 | 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] | 2600 | return 0; |
| 2601 | } |
| 2602 | |
| 2603 | /* This function initiate the send of data. It just check the input |
| 2604 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2605 | * 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] | 2606 | * "hlua_socket_write_yield" that can yield. |
| 2607 | * |
| 2608 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2609 | * the associated object. The second is a string buffer. The third is |
| 2610 | * a facultative integer that represents where is the buffer position |
| 2611 | * of the start of the data that can send. The first byte is the |
| 2612 | * position "1". The default value is "1". The fourth argument is a |
| 2613 | * facultative integer that represents where is the buffer position |
| 2614 | * of the end of the data that can send. The default is the last byte. |
| 2615 | */ |
| 2616 | static int hlua_socket_send(struct lua_State *L) |
| 2617 | { |
| 2618 | int i; |
| 2619 | int j; |
| 2620 | const char *buf; |
| 2621 | size_t buf_len; |
| 2622 | |
| 2623 | /* Check number of arguments. */ |
| 2624 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2625 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2626 | |
| 2627 | /* Get the string. */ |
| 2628 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2629 | |
| 2630 | /* Get and check j. */ |
| 2631 | if (lua_gettop(L) == 4) { |
| 2632 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2633 | if (j < 0) |
| 2634 | j = buf_len + j + 1; |
| 2635 | if (j > buf_len) |
| 2636 | j = buf_len + 1; |
| 2637 | lua_pop(L, 1); |
| 2638 | } |
| 2639 | else |
| 2640 | j = buf_len; |
| 2641 | |
| 2642 | /* Get and check i. */ |
| 2643 | if (lua_gettop(L) == 3) { |
| 2644 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2645 | if (i < 0) |
| 2646 | i = buf_len + i + 1; |
| 2647 | if (i > buf_len) |
| 2648 | i = buf_len + 1; |
| 2649 | lua_pop(L, 1); |
| 2650 | } else |
| 2651 | i = 1; |
| 2652 | |
| 2653 | /* Check bth i and j. */ |
| 2654 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2655 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | return 1; |
| 2657 | } |
| 2658 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2659 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2660 | return 1; |
| 2661 | } |
| 2662 | if (i == 0) |
| 2663 | i = 1; |
| 2664 | if (j == 0) |
| 2665 | j = 1; |
| 2666 | |
| 2667 | /* Pop the string. */ |
| 2668 | lua_pop(L, 1); |
| 2669 | |
| 2670 | /* Update the buffer length. */ |
| 2671 | buf += i - 1; |
| 2672 | buf_len = j - i + 1; |
| 2673 | lua_pushlstring(L, buf, buf_len); |
| 2674 | |
| 2675 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2676 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2678 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | } |
| 2680 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2681 | #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] | 2682 | __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] | 2683 | { |
| 2684 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2685 | int ret; |
| 2686 | int len; |
| 2687 | char *p; |
| 2688 | |
| 2689 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2690 | if (ret <= 0) { |
| 2691 | lua_pushnil(L); |
| 2692 | return 1; |
| 2693 | } |
| 2694 | |
| 2695 | if (ret == AF_UNIX) { |
| 2696 | lua_pushstring(L, buffer+1); |
| 2697 | return 1; |
| 2698 | } |
| 2699 | else if (ret == AF_INET6) { |
| 2700 | buffer[0] = '['; |
| 2701 | len = strlen(buffer); |
| 2702 | buffer[len] = ']'; |
| 2703 | len++; |
| 2704 | buffer[len] = ':'; |
| 2705 | len++; |
| 2706 | p = buffer; |
| 2707 | } |
| 2708 | else if (ret == AF_INET) { |
| 2709 | p = buffer + 1; |
| 2710 | len = strlen(p); |
| 2711 | p[len] = ':'; |
| 2712 | len++; |
| 2713 | } |
| 2714 | else { |
| 2715 | lua_pushnil(L); |
| 2716 | return 1; |
| 2717 | } |
| 2718 | |
| 2719 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2720 | lua_pushnil(L); |
| 2721 | return 1; |
| 2722 | } |
| 2723 | |
| 2724 | lua_pushstring(L, p); |
| 2725 | return 1; |
| 2726 | } |
| 2727 | |
| 2728 | /* Returns information about the peer of the connection. */ |
| 2729 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2730 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2731 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | struct xref *peer; |
| 2733 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2734 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2735 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2736 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2737 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2738 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2739 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2740 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2741 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2742 | /* Check if we run on the same thread than the xreator thread. |
| 2743 | * We cannot access to the socket if the thread is different. |
| 2744 | */ |
| 2745 | if (socket->tid != tid) |
| 2746 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2747 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2748 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2749 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2751 | lua_pushnil(L); |
| 2752 | return 1; |
| 2753 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2754 | |
| 2755 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2756 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2757 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2758 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2759 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2760 | lua_pushnil(L); |
| 2761 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2762 | } |
| 2763 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2764 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | xref_unlock(&socket->xref, peer); |
| 2766 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | /* Returns information about my connection side. */ |
| 2770 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2771 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2772 | struct hlua_socket *socket; |
| 2773 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2774 | struct appctx *appctx; |
| 2775 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2776 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2777 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2778 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2780 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2781 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2782 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2783 | /* Check if we run on the same thread than the xreator thread. |
| 2784 | * We cannot access to the socket if the thread is different. |
| 2785 | */ |
| 2786 | if (socket->tid != tid) |
| 2787 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2788 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2789 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2790 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2791 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2792 | lua_pushnil(L); |
| 2793 | return 1; |
| 2794 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2795 | |
| 2796 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2797 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2798 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2799 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2800 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2801 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2802 | lua_pushnil(L); |
| 2803 | return 1; |
| 2804 | } |
| 2805 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2806 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2807 | xref_unlock(&socket->xref, peer); |
| 2808 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | } |
| 2810 | |
| 2811 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2812 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | .obj_type = OBJ_TYPE_APPLET, |
| 2814 | .name = "<LUA_TCP>", |
| 2815 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2816 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2817 | .release = hlua_socket_release, |
| 2818 | }; |
| 2819 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2820 | __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] | 2821 | { |
| 2822 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2823 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2824 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2825 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2826 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2827 | struct stream *s; |
| 2828 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2829 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2830 | hlua = hlua_gethlua(L); |
| 2831 | if (!hlua) |
| 2832 | return 0; |
| 2833 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2834 | /* Check if we run on the same thread than the xreator thread. |
| 2835 | * We cannot access to the socket if the thread is different. |
| 2836 | */ |
| 2837 | if (socket->tid != tid) |
| 2838 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2839 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2840 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2841 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2842 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2843 | lua_pushnil(L); |
| 2844 | lua_pushstring(L, "Can't connect"); |
| 2845 | return 2; |
| 2846 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2847 | |
| 2848 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2849 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2850 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2851 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2852 | /* Check if we run on the same thread than the xreator thread. |
| 2853 | * We cannot access to the socket if the thread is different. |
| 2854 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2855 | if (socket->tid != tid) { |
| 2856 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2857 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2858 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2859 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2860 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2861 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2862 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2863 | lua_pushnil(L); |
| 2864 | lua_pushstring(L, "Can't connect"); |
| 2865 | return 2; |
| 2866 | } |
| 2867 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2868 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
| 2870 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2871 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2872 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2873 | lua_pushinteger(L, 1); |
| 2874 | return 1; |
| 2875 | } |
| 2876 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2877 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2878 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2879 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2880 | } |
| 2881 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2882 | 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] | 2883 | return 0; |
| 2884 | } |
| 2885 | |
| 2886 | /* This function fail or initite the connection. */ |
| 2887 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2888 | { |
| 2889 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2890 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2891 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2892 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2893 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2894 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2895 | int low, high; |
| 2896 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2897 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2898 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2899 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2900 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2901 | if (lua_gettop(L) < 2) |
| 2902 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2903 | |
| 2904 | /* Get args. */ |
| 2905 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2906 | |
| 2907 | /* Check if we run on the same thread than the xreator thread. |
| 2908 | * We cannot access to the socket if the thread is different. |
| 2909 | */ |
| 2910 | if (socket->tid != tid) |
| 2911 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2912 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2913 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2914 | if (lua_gettop(L) >= 3) { |
| 2915 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2916 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2917 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2918 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2919 | * that are not enclosed within square brackets. |
| 2920 | */ |
| 2921 | if (port > 0) { |
| 2922 | luaL_buffinit(L, &b); |
| 2923 | luaL_addstring(&b, ip); |
| 2924 | luaL_addchar(&b, ':'); |
| 2925 | luaL_pushresult(&b); |
| 2926 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2927 | } |
| 2928 | } |
| 2929 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2930 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2931 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2932 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2933 | lua_pushnil(L); |
| 2934 | return 1; |
| 2935 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2936 | |
| 2937 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2938 | 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] | 2939 | if (!addr) { |
| 2940 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2941 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2942 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2943 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2944 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2945 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2946 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2947 | if (port == -1) { |
| 2948 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2949 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2950 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2951 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2952 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2953 | if (port == -1) { |
| 2954 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2955 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2956 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2957 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2958 | } |
| 2959 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2960 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2961 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2962 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2963 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2964 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2965 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2966 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2967 | xref_unlock(&socket->xref, peer); |
| 2968 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2969 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2971 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2972 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2973 | if (!hlua) |
| 2974 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2975 | |
| 2976 | /* inform the stream that we want to be notified whenever the |
| 2977 | * connection completes. |
| 2978 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2979 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2980 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2981 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2982 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2983 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2984 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2985 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2986 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2987 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2988 | } |
| 2989 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2990 | |
| 2991 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2992 | /* Return yield waiting for connection. */ |
| 2993 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2994 | 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] | 2995 | |
| 2996 | return 0; |
| 2997 | } |
| 2998 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2999 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3000 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3001 | { |
| 3002 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3003 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3004 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3005 | |
| 3006 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3007 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3008 | |
| 3009 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3010 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3011 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3012 | lua_pushnil(L); |
| 3013 | return 1; |
| 3014 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3015 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3016 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3017 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3018 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3019 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3020 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3021 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3022 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3023 | |
| 3024 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3025 | { |
| 3026 | return 0; |
| 3027 | } |
| 3028 | |
| 3029 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3030 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3031 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3032 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3033 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3034 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3035 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3036 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3037 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3038 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3039 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3040 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3041 | /* convert the timeout to millis */ |
| 3042 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3043 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3044 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3045 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3046 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3047 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3048 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3049 | 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] | 3050 | |
| 3051 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3052 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3053 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3054 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3055 | /* Check if we run on the same thread than the xreator thread. |
| 3056 | * We cannot access to the socket if the thread is different. |
| 3057 | */ |
| 3058 | if (socket->tid != tid) |
| 3059 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3060 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3061 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3062 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3063 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3064 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3065 | WILL_LJMP(lua_error(L)); |
| 3066 | return 0; |
| 3067 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3068 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3069 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3070 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3071 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3072 | s->scf->ioto = tmout; |
| 3073 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3074 | |
| 3075 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3076 | task_queue(s->task); |
| 3077 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3078 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3079 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3080 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3081 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3082 | } |
| 3083 | |
| 3084 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3085 | { |
| 3086 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3087 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3088 | struct appctx *appctx; |
| 3089 | |
| 3090 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3091 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3092 | hlua_pusherror(L, "socket: full stack"); |
| 3093 | goto out_fail_conf; |
| 3094 | } |
| 3095 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3096 | /* Create the object: obj[0] = userdata. */ |
| 3097 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3098 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3099 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3100 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3101 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3102 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3103 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3104 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3105 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3106 | goto out_fail_conf; |
| 3107 | } |
| 3108 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3109 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3110 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3111 | lua_setmetatable(L, -2); |
| 3112 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3113 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3114 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3115 | if (!appctx) { |
| 3116 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3117 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3118 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3119 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3120 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3121 | ctx->die = 0; |
| 3122 | LIST_INIT(&ctx->wake_on_write); |
| 3123 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3124 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3125 | if (appctx_init(appctx) == -1) { |
| 3126 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3127 | goto out_fail_appctx; |
| 3128 | } |
| 3129 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3130 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3131 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3132 | return 1; |
| 3133 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3134 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3135 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3136 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3137 | WILL_LJMP(lua_error(L)); |
| 3138 | return 0; |
| 3139 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3140 | |
| 3141 | /* |
| 3142 | * |
| 3143 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3144 | * Class Channel |
| 3145 | * |
| 3146 | * |
| 3147 | */ |
| 3148 | |
| 3149 | /* Returns the struct hlua_channel join to the class channel in the |
| 3150 | * stack entry "ud" or throws an argument error. |
| 3151 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3152 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3154 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | } |
| 3156 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3157 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3158 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3160 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3161 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3163 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | return 0; |
| 3165 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3166 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3167 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3168 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | |
| 3170 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3171 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3172 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3173 | return 1; |
| 3174 | } |
| 3175 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3176 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3177 | * 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] | 3178 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3179 | * initialized. |
| 3180 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3181 | 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] | 3182 | { |
| 3183 | struct filter *filter = NULL; |
| 3184 | |
| 3185 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3186 | struct hlua_flt_ctx *flt_ctx; |
| 3187 | |
| 3188 | filter = lua_touserdata (L, -1); |
| 3189 | flt_ctx = filter->ctx; |
| 3190 | if (hlua_filter_from_payload(filter)) { |
| 3191 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3192 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3193 | } |
| 3194 | } |
| 3195 | |
| 3196 | lua_pop(L, 1); |
| 3197 | return filter; |
| 3198 | } |
| 3199 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3200 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3201 | * 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] | 3202 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3203 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3204 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3205 | */ |
| 3206 | 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] | 3207 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3208 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3209 | luaL_Buffer b; |
| 3210 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3211 | block1 = len; |
| 3212 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3213 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3214 | block2 = len - block1; |
| 3215 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3217 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3218 | if (block2) |
| 3219 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3220 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3221 | return len; |
| 3222 | } |
| 3223 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3224 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3225 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3226 | * number of bytes copied. |
| 3227 | */ |
| 3228 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3229 | { |
| 3230 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3231 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3232 | /* Nothing to do, just return */ |
| 3233 | if (unlikely(istlen(str) == 0)) |
| 3234 | goto end; |
| 3235 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3236 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3237 | ret = -1; |
| 3238 | goto end; |
| 3239 | } |
| 3240 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3241 | |
| 3242 | end: |
| 3243 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3246 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3247 | */ |
| 3248 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3249 | { |
| 3250 | size_t end = offset + len; |
| 3251 | |
| 3252 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3253 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3254 | b_data(&chn->buf) - end, -len); |
| 3255 | b_sub(&chn->buf, len); |
| 3256 | } |
| 3257 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3258 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3259 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3260 | * offset by default). If there is not enough input data and more data can be |
| 3261 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3262 | * |
| 3263 | * From an action, All input data are considered. For a filter, the offset and |
| 3264 | * 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] | 3265 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3266 | __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] | 3267 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3268 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3269 | struct filter *filter; |
| 3270 | size_t input, output; |
| 3271 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3272 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3273 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3274 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3275 | output = co_data(chn); |
| 3276 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3277 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3278 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3279 | if (filter && !hlua_filter_from_payload(filter)) |
| 3280 | WILL_LJMP(lua_error(L)); |
| 3281 | |
| 3282 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3283 | if (lua_gettop(L) > 1) { |
| 3284 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3285 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3286 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3287 | offset += output; |
| 3288 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3289 | lua_pushfstring(L, "offset out of range."); |
| 3290 | WILL_LJMP(lua_error(L)); |
| 3291 | } |
| 3292 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3293 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3294 | if (lua_gettop(L) == 3) { |
| 3295 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3296 | if (!len) |
| 3297 | goto dup; |
| 3298 | if (len == -1) |
| 3299 | len = global.tune.bufsize; |
| 3300 | if (len < 0) { |
| 3301 | lua_pushfstring(L, "length out of range."); |
| 3302 | WILL_LJMP(lua_error(L)); |
| 3303 | } |
| 3304 | } |
| 3305 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3306 | /* Wait for more data if possible if no length was specified and there |
| 3307 | * is no data or not enough data was received. |
| 3308 | */ |
| 3309 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3310 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3311 | /* Yield waiting for more data, as requested */ |
| 3312 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3313 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3314 | |
| 3315 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3316 | if (!len) { |
| 3317 | lua_pushnil(L); |
| 3318 | return -1; |
| 3319 | } |
| 3320 | |
| 3321 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3322 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3323 | } |
| 3324 | |
| 3325 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3326 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3327 | return 1; |
| 3328 | } |
| 3329 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3330 | /* Copies the first line (including the trailing LF) of input data in the |
| 3331 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3332 | * a length (all remaining input data starting for the offset by default). If |
| 3333 | * there is not enough input data and more data can be received, the function |
| 3334 | * yields. If a length is explicitly specified, no more data are |
| 3335 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3336 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3337 | * |
| 3338 | * From an action, All input data are considered. For a filter, the offset and |
| 3339 | * 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] | 3340 | */ |
| 3341 | __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] | 3342 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3343 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3344 | struct filter *filter; |
| 3345 | size_t l, input, output; |
| 3346 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3347 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3348 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3349 | output = co_data(chn); |
| 3350 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3351 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3352 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3353 | if (filter && !hlua_filter_from_payload(filter)) |
| 3354 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3355 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3356 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3357 | if (lua_gettop(L) > 1) { |
| 3358 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3359 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3360 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3361 | offset += output; |
| 3362 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3363 | lua_pushfstring(L, "offset out of range."); |
| 3364 | WILL_LJMP(lua_error(L)); |
| 3365 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3366 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3367 | |
| 3368 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3369 | if (lua_gettop(L) == 3) { |
| 3370 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3371 | if (!len) |
| 3372 | goto dup; |
| 3373 | if (len == -1) |
| 3374 | len = global.tune.bufsize; |
| 3375 | if (len < 0) { |
| 3376 | lua_pushfstring(L, "length out of range."); |
| 3377 | WILL_LJMP(lua_error(L)); |
| 3378 | } |
| 3379 | } |
| 3380 | |
| 3381 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3382 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3383 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3384 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3385 | len = l+1; |
| 3386 | goto dup; |
| 3387 | } |
| 3388 | } |
| 3389 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3390 | /* Wait for more data if possible if no line is found and no length was |
| 3391 | * specified or not enough data was received. |
| 3392 | */ |
| 3393 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3394 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3395 | /* Yield waiting for more data */ |
| 3396 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3397 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3398 | |
| 3399 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3400 | if (!len) { |
| 3401 | lua_pushnil(L); |
| 3402 | return -1; |
| 3403 | } |
| 3404 | |
| 3405 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3406 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3407 | } |
| 3408 | |
| 3409 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3410 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3411 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3412 | } |
| 3413 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3414 | /* [ DEPRECATED ] |
| 3415 | * |
| 3416 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3417 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3418 | * |
| 3419 | * From an action, All input data are considered. For a filter, the offset and |
| 3420 | * 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] | 3421 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3422 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3423 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3424 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3425 | struct filter *filter; |
| 3426 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3427 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3428 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3429 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3430 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3431 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3432 | WILL_LJMP(lua_error(L)); |
| 3433 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3434 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3435 | offset = co_data(chn); |
| 3436 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3437 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3438 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3439 | if (filter && !hlua_filter_from_payload(filter)) |
| 3440 | WILL_LJMP(lua_error(L)); |
| 3441 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3442 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3443 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3444 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3445 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3446 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3447 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3448 | return 1; |
| 3449 | } |
| 3450 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3451 | /* [ DEPRECATED ] |
| 3452 | * |
| 3453 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3454 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3455 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3456 | * |
| 3457 | * From an action, All input data are considered. For a filter, the offset and |
| 3458 | * 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] | 3459 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3460 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3461 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3462 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3463 | struct filter *filter; |
| 3464 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3465 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3466 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3467 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3468 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3469 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3470 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3471 | WILL_LJMP(lua_error(L)); |
| 3472 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3473 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3474 | offset = co_data(chn); |
| 3475 | len = ci_data(chn); |
| 3476 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3477 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3478 | if (filter && !hlua_filter_from_payload(filter)) |
| 3479 | WILL_LJMP(lua_error(L)); |
| 3480 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3482 | lua_pushnil(L); |
| 3483 | return 1; |
| 3484 | } |
| 3485 | |
| 3486 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3487 | _hlua_channel_delete(chn, offset, ret); |
| 3488 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3489 | } |
| 3490 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3491 | /* This functions consumes and returns one line. If the channel is closed, |
| 3492 | * 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] | 3493 | * 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] | 3494 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3495 | * |
| 3496 | * From an action, All input data are considered. For a filter, the offset and |
| 3497 | * 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] | 3498 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3499 | __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] | 3500 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3501 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3502 | struct filter *filter; |
| 3503 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3504 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3505 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3506 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3507 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3508 | offset = co_data(chn); |
| 3509 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3510 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3511 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3512 | if (filter && !hlua_filter_from_payload(filter)) |
| 3513 | WILL_LJMP(lua_error(L)); |
| 3514 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3515 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3516 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3517 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3518 | } |
| 3519 | |
| 3520 | for (l = 0; l < len; l++) { |
| 3521 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3522 | len = l+1; |
| 3523 | goto dup; |
| 3524 | } |
| 3525 | } |
| 3526 | |
| 3527 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3528 | /* Yield waiting for more data */ |
| 3529 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3530 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3531 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3532 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3533 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3534 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3535 | return 1; |
| 3536 | } |
| 3537 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3538 | /* [ DEPRECATED ] |
| 3539 | * |
| 3540 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3541 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3542 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3543 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3544 | struct channel *chn; |
| 3545 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3546 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3547 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3548 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3549 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3550 | WILL_LJMP(lua_error(L)); |
| 3551 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3552 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3553 | } |
| 3554 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3555 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3556 | * available input data are returned. The offset may be negactive to start from |
| 3557 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3558 | * size. |
| 3559 | */ |
| 3560 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3561 | { |
| 3562 | struct channel *chn; |
| 3563 | |
| 3564 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3565 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3566 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3567 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3568 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3569 | WILL_LJMP(lua_error(L)); |
| 3570 | } |
| 3571 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3572 | } |
| 3573 | |
| 3574 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3575 | * available input data are returned. The offset may be negactive to start from |
| 3576 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3577 | * size. |
| 3578 | */ |
| 3579 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3580 | { |
| 3581 | struct channel *chn; |
| 3582 | |
| 3583 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3584 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3585 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3586 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3587 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3588 | WILL_LJMP(lua_error(L)); |
| 3589 | } |
| 3590 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3591 | } |
| 3592 | |
| 3593 | /* Appends a string into the input side of channel. It returns the length of the |
| 3594 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3595 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3596 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3597 | * |
| 3598 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3599 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3600 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3601 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3602 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3603 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3604 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3605 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3606 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3607 | |
| 3608 | MAY_LJMP(check_args(L, 2, "append")); |
| 3609 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3610 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3611 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3612 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3613 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3614 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3615 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3616 | offset = co_data(chn); |
| 3617 | len = ci_data(chn); |
| 3618 | |
| 3619 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3620 | if (filter && !hlua_filter_from_payload(filter)) |
| 3621 | WILL_LJMP(lua_error(L)); |
| 3622 | |
| 3623 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3624 | if (ret > 0 && filter) { |
| 3625 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3626 | |
| 3627 | flt_update_offsets(filter, chn, ret); |
| 3628 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3629 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3630 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3631 | return 1; |
| 3632 | } |
| 3633 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3634 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3635 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3636 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3637 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3638 | * |
| 3639 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3640 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3641 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3642 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3643 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3644 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3645 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3646 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3647 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3648 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3649 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3650 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3652 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3653 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3654 | WILL_LJMP(lua_error(L)); |
| 3655 | } |
| 3656 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3657 | offset = co_data(chn); |
| 3658 | len = ci_data(chn); |
| 3659 | |
| 3660 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3661 | if (filter && !hlua_filter_from_payload(filter)) |
| 3662 | WILL_LJMP(lua_error(L)); |
| 3663 | |
| 3664 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3665 | if (ret > 0 && filter) { |
| 3666 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3667 | |
| 3668 | flt_update_offsets(filter, chn, ret); |
| 3669 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3670 | } |
| 3671 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3672 | lua_pushinteger(L, ret); |
| 3673 | return 1; |
| 3674 | } |
| 3675 | |
| 3676 | /* 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] | 3677 | * 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] | 3678 | * returns the length of the written string, or -1 if the channel is closed or |
| 3679 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3680 | * |
| 3681 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3682 | */ |
| 3683 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3684 | { |
| 3685 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3686 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3687 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3688 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3689 | int ret, offset; |
| 3690 | |
| 3691 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3692 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3693 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3694 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3695 | |
| 3696 | output = co_data(chn); |
| 3697 | input = ci_data(chn); |
| 3698 | |
| 3699 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3700 | if (filter && !hlua_filter_from_payload(filter)) |
| 3701 | WILL_LJMP(lua_error(L)); |
| 3702 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3703 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3704 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3705 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3706 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3707 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3708 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3709 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3710 | lua_pushfstring(L, "offset out of range."); |
| 3711 | WILL_LJMP(lua_error(L)); |
| 3712 | } |
| 3713 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3714 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3715 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3716 | WILL_LJMP(lua_error(L)); |
| 3717 | } |
| 3718 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3719 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3720 | if (ret > 0 && filter) { |
| 3721 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3722 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3723 | flt_update_offsets(filter, chn, ret); |
| 3724 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3725 | } |
| 3726 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3727 | lua_pushinteger(L, ret); |
| 3728 | return 1; |
| 3729 | } |
| 3730 | /* Replaces a given amount of input data at the given offset by a string |
| 3731 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3732 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3733 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3734 | * |
| 3735 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3736 | */ |
| 3737 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3738 | { |
| 3739 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3740 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3741 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3742 | size_t sz, input, output; |
| 3743 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3744 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3745 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3746 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3747 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3748 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3750 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3751 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3752 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3753 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3754 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3755 | output = co_data(chn); |
| 3756 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3757 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3759 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3760 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3761 | |
| 3762 | offset = output; |
| 3763 | if (lua_gettop(L) > 2) { |
| 3764 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3765 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3766 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3767 | offset += output; |
| 3768 | if (offset < output || offset > input + output) { |
| 3769 | lua_pushfstring(L, "offset out of range."); |
| 3770 | WILL_LJMP(lua_error(L)); |
| 3771 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3772 | } |
| 3773 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3774 | len = output + input - offset; |
| 3775 | if (lua_gettop(L) == 4) { |
| 3776 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3777 | if (!len) |
| 3778 | goto set; |
| 3779 | if (len == -1) |
| 3780 | len = output + input - offset; |
| 3781 | if (len < 0 || offset + len > output + input) { |
| 3782 | lua_pushfstring(L, "length out of range."); |
| 3783 | WILL_LJMP(lua_error(L)); |
| 3784 | } |
| 3785 | } |
| 3786 | |
| 3787 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3788 | /* 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] | 3789 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3790 | lua_pushinteger(L, -1); |
| 3791 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3792 | _hlua_channel_delete(chn, offset, len); |
| 3793 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3794 | if (filter) { |
| 3795 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3796 | |
| 3797 | len -= (ret > 0 ? ret : 0); |
| 3798 | flt_update_offsets(filter, chn, -len); |
| 3799 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3800 | } |
| 3801 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3803 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3804 | return 1; |
| 3805 | } |
| 3806 | |
| 3807 | /* 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] | 3808 | * 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] | 3809 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3810 | * |
| 3811 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3812 | */ |
| 3813 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3814 | { |
| 3815 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3816 | struct filter *filter; |
| 3817 | size_t input, output; |
| 3818 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3819 | |
| 3820 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3821 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3822 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3823 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3824 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3825 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3826 | WILL_LJMP(lua_error(L)); |
| 3827 | } |
| 3828 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3829 | output = co_data(chn); |
| 3830 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3831 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3832 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3833 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3834 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3835 | |
| 3836 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3837 | if (lua_gettop(L) > 1) { |
| 3838 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3839 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3840 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3841 | offset += output; |
| 3842 | if (offset < output || offset > input + output) { |
| 3843 | lua_pushfstring(L, "offset out of range."); |
| 3844 | WILL_LJMP(lua_error(L)); |
| 3845 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3846 | } |
| 3847 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3848 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3849 | if (lua_gettop(L) == 3) { |
| 3850 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3851 | if (!len) |
| 3852 | goto end; |
| 3853 | if (len == -1) |
| 3854 | len = output + input - offset; |
| 3855 | if (len < 0 || offset + len > output + input) { |
| 3856 | lua_pushfstring(L, "length out of range."); |
| 3857 | WILL_LJMP(lua_error(L)); |
| 3858 | } |
| 3859 | } |
| 3860 | |
| 3861 | _hlua_channel_delete(chn, offset, len); |
| 3862 | if (filter) { |
| 3863 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3864 | |
| 3865 | flt_update_offsets(filter, chn, -len); |
| 3866 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3867 | } |
| 3868 | |
| 3869 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3870 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3871 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3872 | } |
| 3873 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3874 | /* Append data in the output side of the buffer. This data is immediately |
| 3875 | * sent. The function returns the amount of data written. If the buffer |
| 3876 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3877 | * if the channel is closed. |
| 3878 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3879 | __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] | 3880 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3881 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3882 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3883 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3884 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3885 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3886 | struct hlua *hlua; |
| 3887 | |
| 3888 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3889 | hlua = hlua_gethlua(L); |
| 3890 | if (!hlua) { |
| 3891 | lua_pushnil(L); |
| 3892 | return 1; |
| 3893 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3894 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3895 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3896 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3897 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3898 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3899 | offset = co_data(chn); |
| 3900 | len = ci_data(chn); |
| 3901 | |
| 3902 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3903 | if (filter && !hlua_filter_from_payload(filter)) |
| 3904 | WILL_LJMP(lua_error(L)); |
| 3905 | |
| 3906 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3907 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3908 | lua_pushinteger(L, -1); |
| 3909 | return 1; |
| 3910 | } |
| 3911 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3912 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3913 | if (len > sz -l) { |
| 3914 | if (filter) { |
| 3915 | lua_pushinteger(L, -1); |
| 3916 | return 1; |
| 3917 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3918 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3919 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3920 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3921 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3922 | if (ret == -1) { |
| 3923 | lua_pop(L, 1); |
| 3924 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3925 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3926 | } |
| 3927 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3928 | if (filter) { |
| 3929 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3930 | |
| 3931 | |
| 3932 | flt_update_offsets(filter, chn, ret); |
| 3933 | FLT_OFF(filter, chn) += ret; |
| 3934 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3935 | } |
| 3936 | else |
| 3937 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3938 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3939 | l += ret; |
| 3940 | lua_pop(L, 1); |
| 3941 | lua_pushinteger(L, l); |
| 3942 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3943 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3944 | if (l < sz) { |
| 3945 | /* Yield only if the channel's output is not empty. |
| 3946 | * Otherwise it means we cannot add more data. */ |
| 3947 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3948 | return 1; |
| 3949 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3950 | /* If we are waiting for space in the response buffer, we |
| 3951 | * must set the flag WAKERESWR. This flag required the task |
| 3952 | * wake up if any activity is detected on the response buffer. |
| 3953 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3954 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3955 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3956 | else |
| 3957 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3958 | 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] | 3959 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3960 | |
| 3961 | return 1; |
| 3962 | } |
| 3963 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3964 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3965 | * yield the LUA process, and resume it without checking the |
| 3966 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3967 | * |
| 3968 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3969 | */ |
| 3970 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3971 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3972 | struct channel *chn; |
| 3973 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3974 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3975 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3976 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3977 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3978 | WILL_LJMP(lua_error(L)); |
| 3979 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3980 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3981 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3982 | } |
| 3983 | |
| 3984 | /* This function forward and amount of butes. The data pass from |
| 3985 | * the input side of the buffer to the output side, and can be |
| 3986 | * forwarded. This function never fails. |
| 3987 | * |
| 3988 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3989 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3990 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3991 | __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] | 3992 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3993 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3994 | struct filter *filter; |
| 3995 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3996 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3997 | struct hlua *hlua; |
| 3998 | |
| 3999 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4000 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4001 | if (!hlua) { |
| 4002 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4003 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4004 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4005 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4006 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4007 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4008 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4009 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4010 | offset = co_data(chn); |
| 4011 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4012 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4013 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4014 | if (filter && !hlua_filter_from_payload(filter)) |
| 4015 | WILL_LJMP(lua_error(L)); |
| 4016 | |
| 4017 | max = fwd - l; |
| 4018 | if (max > len) |
| 4019 | max = len; |
| 4020 | |
| 4021 | if (filter) { |
| 4022 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4023 | |
| 4024 | FLT_OFF(filter, chn) += max; |
| 4025 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4026 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4027 | } |
| 4028 | else |
| 4029 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4030 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4031 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4032 | lua_pop(L, 1); |
| 4033 | lua_pushinteger(L, l); |
| 4034 | |
| 4035 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4036 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4037 | /* The the input channel or the output channel are closed, we |
| 4038 | * must return the amount of data forwarded. |
| 4039 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4040 | 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] | 4041 | return 1; |
| 4042 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4043 | /* If we are waiting for space data in the response buffer, we |
| 4044 | * must set the flag WAKERESWR. This flag required the task |
| 4045 | * wake up if any activity is detected on the response buffer. |
| 4046 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4047 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4048 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4049 | else |
| 4050 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4051 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4052 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4053 | 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] | 4054 | } |
| 4055 | |
| 4056 | return 1; |
| 4057 | } |
| 4058 | |
| 4059 | /* Just check the input and prepare the stack for the previous |
| 4060 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4061 | * |
| 4062 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4063 | */ |
| 4064 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4065 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4066 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4067 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4068 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4069 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4070 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4071 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4072 | WILL_LJMP(lua_error(L)); |
| 4073 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4074 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4075 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4076 | } |
| 4077 | |
| 4078 | /* Just returns the number of bytes available in the input |
| 4079 | * side of the buffer. This function never fails. |
| 4080 | */ |
| 4081 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4082 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4083 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4084 | struct filter *filter; |
| 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, "input")); |
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 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4093 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4094 | lua_pushinteger(L, input); |
| 4095 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4096 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4097 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4098 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4099 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4100 | return 1; |
| 4101 | } |
| 4102 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4103 | /* Returns true if the channel is full. */ |
| 4104 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4105 | { |
| 4106 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4107 | |
| 4108 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4109 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4110 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4111 | * applet). |
| 4112 | */ |
| 4113 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4114 | return 1; |
| 4115 | } |
| 4116 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4117 | /* Returns true if the channel may still receive data. */ |
| 4118 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4119 | { |
| 4120 | struct channel *chn; |
| 4121 | |
| 4122 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4123 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4124 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4125 | return 1; |
| 4126 | } |
| 4127 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4128 | /* Returns true if the channel is the response channel. */ |
| 4129 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4130 | { |
| 4131 | struct channel *chn; |
| 4132 | |
| 4133 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4134 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4135 | |
| 4136 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4137 | return 1; |
| 4138 | } |
| 4139 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4140 | /* Just returns the number of bytes available in the output |
| 4141 | * side of the buffer. This function never fails. |
| 4142 | */ |
| 4143 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4144 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4145 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4146 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4147 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4148 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4149 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4150 | |
| 4151 | output = co_data(chn); |
| 4152 | input = ci_data(chn); |
| 4153 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4154 | |
| 4155 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4156 | return 1; |
| 4157 | } |
| 4158 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4159 | /* |
| 4160 | * |
| 4161 | * |
| 4162 | * Class Fetches |
| 4163 | * |
| 4164 | * |
| 4165 | */ |
| 4166 | |
| 4167 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4168 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4169 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4170 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4171 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4172 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4173 | } |
| 4174 | |
| 4175 | /* This function creates and push in the stack a fetch object according |
| 4176 | * with a current TXN. |
| 4177 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4178 | 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] | 4179 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4180 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4181 | |
| 4182 | /* Check stack size. */ |
| 4183 | if (!lua_checkstack(L, 3)) |
| 4184 | return 0; |
| 4185 | |
| 4186 | /* Create the object: obj[0] = userdata. |
| 4187 | * Note that the base of the Fetches object is the |
| 4188 | * transaction object. |
| 4189 | */ |
| 4190 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4191 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4192 | lua_rawseti(L, -2, 0); |
| 4193 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4194 | hsmp->s = txn->s; |
| 4195 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4196 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4197 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4198 | |
| 4199 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4200 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4201 | lua_setmetatable(L, -2); |
| 4202 | |
| 4203 | return 1; |
| 4204 | } |
| 4205 | |
| 4206 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4207 | * It uses closure argument to store the associated sample-fetch. It |
| 4208 | * returns only one argument or throws an error. An error is thrown |
| 4209 | * only if an error is encountered during the argument parsing. If |
| 4210 | * the "sample-fetch" function fails, nil is returned. |
| 4211 | */ |
| 4212 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4213 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4214 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4215 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4216 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4217 | int i; |
| 4218 | struct sample smp; |
| 4219 | |
| 4220 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4221 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4222 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4223 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4224 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4225 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4226 | /* Check execution authorization. */ |
| 4227 | if (f->use & SMP_USE_HTTP_ANY && |
| 4228 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4229 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4230 | "is not available in Lua services", f->kw); |
| 4231 | WILL_LJMP(lua_error(L)); |
| 4232 | } |
| 4233 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4234 | /* Get extra arguments. */ |
| 4235 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4236 | if (i >= ARGM_NBARGS) |
| 4237 | break; |
| 4238 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4239 | } |
| 4240 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4241 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4242 | |
| 4243 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4244 | 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] | 4245 | |
| 4246 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4247 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4248 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4249 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4250 | } |
| 4251 | |
| 4252 | /* Initialise the sample. */ |
| 4253 | memset(&smp, 0, sizeof(smp)); |
| 4254 | |
| 4255 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4256 | 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] | 4257 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4258 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4259 | lua_pushstring(L, ""); |
| 4260 | else |
| 4261 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4262 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4266 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4267 | hlua_smp2lua_str(L, &smp); |
| 4268 | else |
| 4269 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4270 | |
| 4271 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4272 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4273 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4274 | |
| 4275 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4276 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4277 | WILL_LJMP(lua_error(L)); |
| 4278 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4279 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4280 | |
| 4281 | /* |
| 4282 | * |
| 4283 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4284 | * Class Converters |
| 4285 | * |
| 4286 | * |
| 4287 | */ |
| 4288 | |
| 4289 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4290 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4291 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4292 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4293 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4294 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4295 | } |
| 4296 | |
| 4297 | /* This function creates and push in the stack a Converters object |
| 4298 | * according with a current TXN. |
| 4299 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4300 | 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] | 4301 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4302 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4303 | |
| 4304 | /* Check stack size. */ |
| 4305 | if (!lua_checkstack(L, 3)) |
| 4306 | return 0; |
| 4307 | |
| 4308 | /* Create the object: obj[0] = userdata. |
| 4309 | * Note that the base of the Converters object is the |
| 4310 | * same than the TXN object. |
| 4311 | */ |
| 4312 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4313 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4314 | lua_rawseti(L, -2, 0); |
| 4315 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4316 | hsmp->s = txn->s; |
| 4317 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4318 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4319 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4320 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4321 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4322 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4323 | lua_setmetatable(L, -2); |
| 4324 | |
| 4325 | return 1; |
| 4326 | } |
| 4327 | |
| 4328 | /* This function is an LUA binding. It is called with each converter. |
| 4329 | * It uses closure argument to store the associated converter. It |
| 4330 | * returns only one argument or throws an error. An error is thrown |
| 4331 | * only if an error is encountered during the argument parsing. If |
| 4332 | * the converter function function fails, nil is returned. |
| 4333 | */ |
| 4334 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4335 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4336 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4337 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4338 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4339 | int i; |
| 4340 | struct sample smp; |
| 4341 | |
| 4342 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4343 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4344 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4345 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4346 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4347 | |
| 4348 | /* Get extra arguments. */ |
| 4349 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4350 | if (i >= ARGM_NBARGS) |
| 4351 | break; |
| 4352 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4353 | } |
| 4354 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4355 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4356 | |
| 4357 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4358 | 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] | 4359 | |
| 4360 | /* Run the special args checker. */ |
| 4361 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4362 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4363 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4364 | } |
| 4365 | |
| 4366 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4367 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4368 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4369 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4370 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4371 | } |
| 4372 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4373 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4374 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4375 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4376 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4377 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4378 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4379 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4380 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4381 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4382 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4383 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4384 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4385 | } |
| 4386 | |
| 4387 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4388 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4389 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4390 | lua_pushstring(L, ""); |
| 4391 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4392 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4393 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4394 | } |
| 4395 | |
| 4396 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4397 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4398 | hlua_smp2lua_str(L, &smp); |
| 4399 | else |
| 4400 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4401 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4402 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4403 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4404 | |
| 4405 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4406 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4407 | WILL_LJMP(lua_error(L)); |
| 4408 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4409 | } |
| 4410 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4411 | /* |
| 4412 | * |
| 4413 | * |
| 4414 | * Class AppletTCP |
| 4415 | * |
| 4416 | * |
| 4417 | */ |
| 4418 | |
| 4419 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4420 | * a class stream, otherwise it throws an error. |
| 4421 | */ |
| 4422 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4423 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4424 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4425 | } |
| 4426 | |
| 4427 | /* This function creates and push in the stack an Applet object |
| 4428 | * according with a current TXN. |
| 4429 | */ |
| 4430 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4431 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4432 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4433 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4434 | struct proxy *p; |
| 4435 | |
| 4436 | ALREADY_CHECKED(s); |
| 4437 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4438 | |
| 4439 | /* Check stack size. */ |
| 4440 | if (!lua_checkstack(L, 3)) |
| 4441 | return 0; |
| 4442 | |
| 4443 | /* Create the object: obj[0] = userdata. |
| 4444 | * Note that the base of the Converters object is the |
| 4445 | * same than the TXN object. |
| 4446 | */ |
| 4447 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4448 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4449 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4450 | luactx->appctx = ctx; |
| 4451 | luactx->htxn.s = s; |
| 4452 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4453 | |
| 4454 | /* Create the "f" field that contains a list of fetches. */ |
| 4455 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4456 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4457 | return 0; |
| 4458 | lua_settable(L, -3); |
| 4459 | |
| 4460 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4461 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4462 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4463 | return 0; |
| 4464 | lua_settable(L, -3); |
| 4465 | |
| 4466 | /* Create the "c" field that contains a list of converters. */ |
| 4467 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4468 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4469 | return 0; |
| 4470 | lua_settable(L, -3); |
| 4471 | |
| 4472 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4473 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4474 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4475 | return 0; |
| 4476 | lua_settable(L, -3); |
| 4477 | |
| 4478 | /* Pop a class stream metatable and affect it to the table. */ |
| 4479 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4480 | lua_setmetatable(L, -2); |
| 4481 | |
| 4482 | return 1; |
| 4483 | } |
| 4484 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4485 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4486 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4487 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4488 | struct stream *s; |
| 4489 | const char *name; |
| 4490 | size_t len; |
| 4491 | struct sample smp; |
| 4492 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4493 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4494 | 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] | 4495 | |
| 4496 | /* It is useles to retrieve the stream, but this function |
| 4497 | * runs only in a stream context. |
| 4498 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4499 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4500 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4501 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4502 | |
| 4503 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4504 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4505 | hlua_lua2smp(L, 3, &smp); |
| 4506 | |
| 4507 | /* Store the sample in a variable. */ |
| 4508 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4509 | |
| 4510 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4511 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4512 | else |
| 4513 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4514 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4515 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4516 | } |
| 4517 | |
| 4518 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4519 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4520 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4521 | struct stream *s; |
| 4522 | const char *name; |
| 4523 | size_t len; |
| 4524 | struct sample smp; |
| 4525 | |
| 4526 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4527 | |
| 4528 | /* It is useles to retrieve the stream, but this function |
| 4529 | * runs only in a stream context. |
| 4530 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4531 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4532 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4533 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4534 | |
| 4535 | /* Unset the variable. */ |
| 4536 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4537 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4538 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4539 | } |
| 4540 | |
| 4541 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4542 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4543 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4544 | struct stream *s; |
| 4545 | const char *name; |
| 4546 | size_t len; |
| 4547 | struct sample smp; |
| 4548 | |
| 4549 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4550 | |
| 4551 | /* It is useles to retrieve the stream, but this function |
| 4552 | * runs only in a stream context. |
| 4553 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4554 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4555 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4556 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4557 | |
| 4558 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4559 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4560 | lua_pushnil(L); |
| 4561 | return 1; |
| 4562 | } |
| 4563 | |
| 4564 | return hlua_smp2lua(L, &smp); |
| 4565 | } |
| 4566 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4567 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4568 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4569 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4570 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4571 | struct hlua *hlua; |
| 4572 | |
| 4573 | /* 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] | 4574 | if (!s->hlua) |
| 4575 | return 0; |
| 4576 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4577 | |
| 4578 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4579 | |
| 4580 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4581 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4582 | |
| 4583 | /* Get and store new value. */ |
| 4584 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4585 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4586 | |
| 4587 | return 0; |
| 4588 | } |
| 4589 | |
| 4590 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4591 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4592 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4593 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4594 | struct hlua *hlua; |
| 4595 | |
| 4596 | /* 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] | 4597 | if (!s->hlua) { |
| 4598 | lua_pushnil(L); |
| 4599 | return 1; |
| 4600 | } |
| 4601 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4602 | |
| 4603 | /* Push configuration index in the stack. */ |
| 4604 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4605 | |
| 4606 | return 1; |
| 4607 | } |
| 4608 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4609 | /* If expected data not yet available, it returns a yield. This function |
| 4610 | * consumes the data in the buffer. It returns a string containing the |
| 4611 | * data. This string can be empty. |
| 4612 | */ |
| 4613 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4614 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4615 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4616 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4617 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4618 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4619 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4620 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4621 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4622 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4623 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4624 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4625 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4626 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4627 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4628 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4629 | 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] | 4630 | } |
| 4631 | |
| 4632 | /* End of data: commit the total strings and return. */ |
| 4633 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4634 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4635 | return 1; |
| 4636 | } |
| 4637 | |
| 4638 | /* Ensure that the block 2 length is usable. */ |
| 4639 | if (ret == 1) |
| 4640 | len2 = 0; |
| 4641 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4642 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4643 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4644 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4645 | |
| 4646 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4647 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4648 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4649 | return 1; |
| 4650 | } |
| 4651 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4652 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4653 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4654 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4655 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4656 | |
| 4657 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4658 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4659 | |
| 4660 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4661 | } |
| 4662 | |
| 4663 | /* If expected data not yet available, it returns a yield. This function |
| 4664 | * consumes the data in the buffer. It returns a string containing the |
| 4665 | * data. This string can be empty. |
| 4666 | */ |
| 4667 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4668 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4669 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4670 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4671 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4672 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4673 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4674 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4675 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4676 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4677 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4678 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4679 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4680 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4681 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4682 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4683 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4684 | 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] | 4685 | } |
| 4686 | |
| 4687 | /* End of data: commit the total strings and return. */ |
| 4688 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4689 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4690 | return 1; |
| 4691 | } |
| 4692 | |
| 4693 | /* Ensure that the block 2 length is usable. */ |
| 4694 | if (ret == 1) |
| 4695 | len2 = 0; |
| 4696 | |
| 4697 | if (len == -1) { |
| 4698 | |
| 4699 | /* If len == -1, catenate all the data avalaile and |
| 4700 | * yield because we want to get all the data until |
| 4701 | * the end of data stream. |
| 4702 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4703 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4704 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4705 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4706 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4707 | 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] | 4708 | |
| 4709 | } else { |
| 4710 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4711 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4712 | if (len1 > len) |
| 4713 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4714 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4715 | len -= len1; |
| 4716 | |
| 4717 | /* Copy the second block. */ |
| 4718 | if (len2 > len) |
| 4719 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4720 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4721 | len -= len2; |
| 4722 | |
| 4723 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4724 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4725 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4726 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4727 | if (len > 0) { |
| 4728 | lua_pushinteger(L, len); |
| 4729 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4730 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4731 | 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] | 4732 | } |
| 4733 | |
| 4734 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4735 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4736 | return 1; |
| 4737 | } |
| 4738 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4739 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4740 | hlua_pusherror(L, "Lua: internal error"); |
| 4741 | WILL_LJMP(lua_error(L)); |
| 4742 | return 0; |
| 4743 | } |
| 4744 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4745 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4746 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4747 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4748 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4749 | int len = -1; |
| 4750 | |
| 4751 | if (lua_gettop(L) > 2) |
| 4752 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4753 | if (lua_gettop(L) >= 2) { |
| 4754 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4755 | lua_pop(L, 1); |
| 4756 | } |
| 4757 | |
| 4758 | /* Confirm or set the required length */ |
| 4759 | lua_pushinteger(L, len); |
| 4760 | |
| 4761 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4763 | |
| 4764 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4765 | } |
| 4766 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4767 | /* Append data in the output side of the buffer. This data is immediately |
| 4768 | * sent. The function returns the amount of data written. If the buffer |
| 4769 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4770 | * if the channel is closed. |
| 4771 | */ |
| 4772 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4773 | { |
| 4774 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4775 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4776 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4777 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4778 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4779 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4780 | int max; |
| 4781 | |
| 4782 | /* Get the max amount of data which can write as input in the channel. */ |
| 4783 | max = channel_recv_max(chn); |
| 4784 | if (max > (len - l)) |
| 4785 | max = len - l; |
| 4786 | |
| 4787 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4788 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4789 | |
| 4790 | /* update counters. */ |
| 4791 | l += max; |
| 4792 | lua_pop(L, 1); |
| 4793 | lua_pushinteger(L, l); |
| 4794 | |
| 4795 | /* If some data is not send, declares the situation to the |
| 4796 | * applet, and returns a yield. |
| 4797 | */ |
| 4798 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4799 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4800 | 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] | 4801 | } |
| 4802 | |
| 4803 | return 1; |
| 4804 | } |
| 4805 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4806 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4807 | * yield the LUA process, and resume it without checking the |
| 4808 | * input arguments. |
| 4809 | */ |
| 4810 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4811 | { |
| 4812 | MAY_LJMP(check_args(L, 2, "send")); |
| 4813 | lua_pushinteger(L, 0); |
| 4814 | |
| 4815 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4816 | } |
| 4817 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4818 | /* |
| 4819 | * |
| 4820 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4821 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4822 | * |
| 4823 | * |
| 4824 | */ |
| 4825 | |
| 4826 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4827 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4828 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4829 | __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] | 4830 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4831 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4832 | } |
| 4833 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4834 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4835 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4836 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4837 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4838 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4839 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4840 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4841 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4842 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4843 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4844 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4845 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4846 | struct htx *htx; |
| 4847 | struct htx_blk *blk; |
| 4848 | struct htx_sl *sl; |
| 4849 | struct ist path; |
| 4850 | unsigned long long len = 0; |
| 4851 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4852 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4853 | |
| 4854 | /* Check stack size. */ |
| 4855 | if (!lua_checkstack(L, 3)) |
| 4856 | return 0; |
| 4857 | |
| 4858 | /* Create the object: obj[0] = userdata. |
| 4859 | * Note that the base of the Converters object is the |
| 4860 | * same than the TXN object. |
| 4861 | */ |
| 4862 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4863 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4864 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4865 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4866 | http_ctx->status = 200; /* Default status code returned. */ |
| 4867 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4868 | luactx->htxn.s = s; |
| 4869 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4870 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4871 | /* Create the "f" field that contains a list of fetches. */ |
| 4872 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4873 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4874 | return 0; |
| 4875 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4876 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4877 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4878 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4879 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4880 | return 0; |
| 4881 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4882 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4883 | /* Create the "c" field that contains a list of converters. */ |
| 4884 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4885 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4886 | return 0; |
| 4887 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4888 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4889 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4890 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4891 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4892 | return 0; |
| 4893 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | htx = htxbuf(&s->req.buf); |
| 4896 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4897 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4898 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4899 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4900 | /* Stores the request method. */ |
| 4901 | lua_pushstring(L, "method"); |
| 4902 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4903 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4904 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4905 | /* Stores the http version. */ |
| 4906 | lua_pushstring(L, "version"); |
| 4907 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4908 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4909 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4910 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4911 | * the array on the top of the stack. |
| 4912 | */ |
| 4913 | lua_pushstring(L, "headers"); |
| 4914 | htxn.s = s; |
| 4915 | htxn.p = px; |
| 4916 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4917 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4918 | return 0; |
| 4919 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4920 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4921 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4922 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4923 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4924 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4925 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4926 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4927 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4928 | q = p; |
| 4929 | while (q < end && *q != '?') |
| 4930 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4931 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4932 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4933 | lua_pushstring(L, "path"); |
| 4934 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4935 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4936 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4937 | /* Stores the query string. */ |
| 4938 | lua_pushstring(L, "qs"); |
| 4939 | if (*q == '?') |
| 4940 | q++; |
| 4941 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4942 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4943 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4944 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4945 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4946 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4947 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4948 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4949 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | break; |
| 4951 | if (type == HTX_BLK_DATA) |
| 4952 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4953 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 4954 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4955 | len += htx->extra; |
| 4956 | |
| 4957 | /* Stores the request path. */ |
| 4958 | lua_pushstring(L, "length"); |
| 4959 | lua_pushinteger(L, len); |
| 4960 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4961 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4962 | /* Create an empty array of HTTP request headers. */ |
| 4963 | lua_pushstring(L, "response"); |
| 4964 | lua_newtable(L); |
| 4965 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4966 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4967 | /* Pop a class stream metatable and affect it to the table. */ |
| 4968 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4969 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4970 | |
| 4971 | return 1; |
| 4972 | } |
| 4973 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4974 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4975 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4976 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4977 | struct stream *s; |
| 4978 | const char *name; |
| 4979 | size_t len; |
| 4980 | struct sample smp; |
| 4981 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4982 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4983 | 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] | 4984 | |
| 4985 | /* It is useles to retrieve the stream, but this function |
| 4986 | * runs only in a stream context. |
| 4987 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4988 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4989 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4990 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4991 | |
| 4992 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4993 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4994 | hlua_lua2smp(L, 3, &smp); |
| 4995 | |
| 4996 | /* Store the sample in a variable. */ |
| 4997 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4998 | |
| 4999 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5000 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5001 | else |
| 5002 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5003 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5004 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5005 | } |
| 5006 | |
| 5007 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5008 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5009 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5010 | struct stream *s; |
| 5011 | const char *name; |
| 5012 | size_t len; |
| 5013 | struct sample smp; |
| 5014 | |
| 5015 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5016 | |
| 5017 | /* It is useles to retrieve the stream, but this function |
| 5018 | * runs only in a stream context. |
| 5019 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5020 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5021 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5022 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5023 | |
| 5024 | /* Unset the variable. */ |
| 5025 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5026 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5027 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5028 | } |
| 5029 | |
| 5030 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5031 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5032 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5033 | struct stream *s; |
| 5034 | const char *name; |
| 5035 | size_t len; |
| 5036 | struct sample smp; |
| 5037 | |
| 5038 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5039 | |
| 5040 | /* It is useles to retrieve the stream, but this function |
| 5041 | * runs only in a stream context. |
| 5042 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5043 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5044 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5045 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5046 | |
| 5047 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5048 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5049 | lua_pushnil(L); |
| 5050 | return 1; |
| 5051 | } |
| 5052 | |
| 5053 | return hlua_smp2lua(L, &smp); |
| 5054 | } |
| 5055 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5056 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5057 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5058 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5059 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5060 | struct hlua *hlua; |
| 5061 | |
| 5062 | /* 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] | 5063 | if (!s->hlua) |
| 5064 | return 0; |
| 5065 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5066 | |
| 5067 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5068 | |
| 5069 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5070 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5071 | |
| 5072 | /* Get and store new value. */ |
| 5073 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5074 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5075 | |
| 5076 | return 0; |
| 5077 | } |
| 5078 | |
| 5079 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5080 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5081 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5082 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5083 | struct hlua *hlua; |
| 5084 | |
| 5085 | /* 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] | 5086 | if (!s->hlua) { |
| 5087 | lua_pushnil(L); |
| 5088 | return 1; |
| 5089 | } |
| 5090 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5091 | |
| 5092 | /* Push configuration index in the stack. */ |
| 5093 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5094 | |
| 5095 | return 1; |
| 5096 | } |
| 5097 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5098 | /* If expected data not yet available, it returns a yield. This function |
| 5099 | * consumes the data in the buffer. It returns a string containing the |
| 5100 | * data. This string can be empty. |
| 5101 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5102 | __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] | 5103 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5104 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5105 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5106 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5107 | struct htx *htx; |
| 5108 | struct htx_blk *blk; |
| 5109 | size_t count; |
| 5110 | int stop = 0; |
| 5111 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5112 | htx = htx_from_buf(&req->buf); |
| 5113 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5114 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5115 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5116 | while (count && !stop && blk) { |
| 5117 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5118 | uint32_t sz = htx_get_blksz(blk); |
| 5119 | struct ist v; |
| 5120 | uint32_t vlen; |
| 5121 | char *nl; |
| 5122 | |
| 5123 | vlen = sz; |
| 5124 | if (vlen > count) { |
| 5125 | if (type != HTX_BLK_DATA) |
| 5126 | break; |
| 5127 | vlen = count; |
| 5128 | } |
| 5129 | |
| 5130 | switch (type) { |
| 5131 | case HTX_BLK_UNUSED: |
| 5132 | break; |
| 5133 | |
| 5134 | case HTX_BLK_DATA: |
| 5135 | v = htx_get_blk_value(htx, blk); |
| 5136 | v.len = vlen; |
| 5137 | nl = istchr(v, '\n'); |
| 5138 | if (nl != NULL) { |
| 5139 | stop = 1; |
| 5140 | vlen = nl - v.ptr + 1; |
| 5141 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5142 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5143 | break; |
| 5144 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5145 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5146 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5147 | stop = 1; |
| 5148 | break; |
| 5149 | |
| 5150 | default: |
| 5151 | break; |
| 5152 | } |
| 5153 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5154 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5155 | count -= vlen; |
| 5156 | if (sz == vlen) |
| 5157 | blk = htx_remove_blk(htx, blk); |
| 5158 | else { |
| 5159 | htx_cut_data_blk(htx, blk, vlen); |
| 5160 | break; |
| 5161 | } |
| 5162 | } |
| 5163 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5164 | /* The message was fully consumed and no more data are expected |
| 5165 | * (EOM flag set). |
| 5166 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5167 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5168 | stop = 1; |
| 5169 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5170 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5171 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5172 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5173 | 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] | 5174 | } |
| 5175 | |
| 5176 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5177 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5178 | return 1; |
| 5179 | } |
| 5180 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5181 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5182 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5183 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5184 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5185 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5186 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5187 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5188 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5189 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5190 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5191 | } |
| 5192 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5193 | /* If expected data not yet available, it returns a yield. This function |
| 5194 | * consumes the data in the buffer. It returns a string containing the |
| 5195 | * data. This string can be empty. |
| 5196 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5197 | __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] | 5198 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5199 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5200 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5201 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5202 | struct htx *htx; |
| 5203 | struct htx_blk *blk; |
| 5204 | size_t count; |
| 5205 | int len; |
| 5206 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5207 | htx = htx_from_buf(&req->buf); |
| 5208 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5209 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5210 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5211 | while (count && len && blk) { |
| 5212 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5213 | uint32_t sz = htx_get_blksz(blk); |
| 5214 | struct ist v; |
| 5215 | uint32_t vlen; |
| 5216 | |
| 5217 | vlen = sz; |
| 5218 | if (len > 0 && vlen > len) |
| 5219 | vlen = len; |
| 5220 | if (vlen > count) { |
| 5221 | if (type != HTX_BLK_DATA) |
| 5222 | break; |
| 5223 | vlen = count; |
| 5224 | } |
| 5225 | |
| 5226 | switch (type) { |
| 5227 | case HTX_BLK_UNUSED: |
| 5228 | break; |
| 5229 | |
| 5230 | case HTX_BLK_DATA: |
| 5231 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5232 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5233 | break; |
| 5234 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5235 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5236 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5237 | len = 0; |
| 5238 | break; |
| 5239 | |
| 5240 | default: |
| 5241 | break; |
| 5242 | } |
| 5243 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5244 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5245 | count -= vlen; |
| 5246 | if (len > 0) |
| 5247 | len -= vlen; |
| 5248 | if (sz == vlen) |
| 5249 | blk = htx_remove_blk(htx, blk); |
| 5250 | else { |
| 5251 | htx_cut_data_blk(htx, blk, vlen); |
| 5252 | break; |
| 5253 | } |
| 5254 | } |
| 5255 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5256 | /* The message was fully consumed and no more data are expected |
| 5257 | * (EOM flag set). |
| 5258 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5259 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5260 | len = 0; |
| 5261 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5262 | htx_to_buf(htx, &req->buf); |
| 5263 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5264 | /* If we are no other data available, yield waiting for new data. */ |
| 5265 | if (len) { |
| 5266 | if (len > 0) { |
| 5267 | lua_pushinteger(L, len); |
| 5268 | lua_replace(L, 2); |
| 5269 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5270 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5271 | 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] | 5272 | } |
| 5273 | |
| 5274 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5275 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5276 | return 1; |
| 5277 | } |
| 5278 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5279 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5280 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5281 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5282 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5283 | int len = -1; |
| 5284 | |
| 5285 | /* Check arguments. */ |
| 5286 | if (lua_gettop(L) > 2) |
| 5287 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5288 | if (lua_gettop(L) >= 2) { |
| 5289 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5290 | lua_pop(L, 1); |
| 5291 | } |
| 5292 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5293 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5294 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5295 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5296 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5297 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5298 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5299 | } |
| 5300 | |
| 5301 | /* Append data in the output side of the buffer. This data is immediately |
| 5302 | * sent. The function returns the amount of data written. If the buffer |
| 5303 | * cannot contain the data, the function yields. The function returns -1 |
| 5304 | * if the channel is closed. |
| 5305 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5306 | __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] | 5307 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5308 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5309 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5310 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5311 | struct htx *htx = htx_from_buf(&res->buf); |
| 5312 | const char *data; |
| 5313 | size_t len; |
| 5314 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5315 | int max; |
| 5316 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5317 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5318 | if (!max) |
| 5319 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5320 | |
| 5321 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5322 | |
| 5323 | /* Get the max amount of data which can write as input in the channel. */ |
| 5324 | if (max > (len - l)) |
| 5325 | max = len - l; |
| 5326 | |
| 5327 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5328 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5329 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5330 | |
| 5331 | /* update counters. */ |
| 5332 | l += max; |
| 5333 | lua_pop(L, 1); |
| 5334 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5335 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5336 | /* If some data is not send, declares the situation to the |
| 5337 | * applet, and returns a yield. |
| 5338 | */ |
| 5339 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5340 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5341 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5342 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5343 | 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] | 5344 | } |
| 5345 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5346 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5347 | return 1; |
| 5348 | } |
| 5349 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5350 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5351 | * yield the LUA process, and resume it without checking the |
| 5352 | * input arguments. |
| 5353 | */ |
| 5354 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5355 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5356 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5357 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5358 | |
| 5359 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5360 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5361 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5362 | WILL_LJMP(lua_error(L)); |
| 5363 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5364 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5365 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5366 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5367 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5368 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5369 | } |
| 5370 | |
| 5371 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5372 | { |
| 5373 | const char *name; |
| 5374 | int ret; |
| 5375 | |
| 5376 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5377 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5378 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5379 | |
| 5380 | /* Push in the stack the "response" entry. */ |
| 5381 | ret = lua_getfield(L, 1, "response"); |
| 5382 | if (ret != LUA_TTABLE) { |
| 5383 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5384 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5385 | WILL_LJMP(lua_error(L)); |
| 5386 | } |
| 5387 | |
| 5388 | /* check if the header is already registered if it is not |
| 5389 | * the case, register it. |
| 5390 | */ |
| 5391 | ret = lua_getfield(L, -1, name); |
| 5392 | if (ret == LUA_TNIL) { |
| 5393 | |
| 5394 | /* Entry not found. */ |
| 5395 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5396 | |
| 5397 | /* Insert the new header name in the array in the top of the stack. |
| 5398 | * It left the new array in the top of the stack. |
| 5399 | */ |
| 5400 | lua_newtable(L); |
| 5401 | lua_pushvalue(L, 2); |
| 5402 | lua_pushvalue(L, -2); |
| 5403 | lua_settable(L, -4); |
| 5404 | |
| 5405 | } else if (ret != LUA_TTABLE) { |
| 5406 | |
| 5407 | /* corruption error. */ |
| 5408 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5409 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5410 | WILL_LJMP(lua_error(L)); |
| 5411 | } |
| 5412 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5413 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5414 | * the header value as new entry. |
| 5415 | */ |
| 5416 | lua_pushvalue(L, 3); |
| 5417 | ret = lua_rawlen(L, -2); |
| 5418 | lua_rawseti(L, -2, ret + 1); |
| 5419 | lua_pushboolean(L, 1); |
| 5420 | return 1; |
| 5421 | } |
| 5422 | |
| 5423 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5424 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5425 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5426 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5427 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5428 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5429 | |
| 5430 | if (status < 100 || status > 599) { |
| 5431 | lua_pushboolean(L, 0); |
| 5432 | return 1; |
| 5433 | } |
| 5434 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5435 | http_ctx->status = status; |
| 5436 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5437 | lua_pushboolean(L, 1); |
| 5438 | return 1; |
| 5439 | } |
| 5440 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5441 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5442 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5443 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5444 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5445 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5446 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5447 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5448 | struct htx *htx; |
| 5449 | struct htx_sl *sl; |
| 5450 | struct h1m h1m; |
| 5451 | const char *status, *reason; |
| 5452 | const char *name, *value; |
| 5453 | size_t nlen, vlen; |
| 5454 | unsigned int flags; |
| 5455 | |
| 5456 | /* Send the message at once. */ |
| 5457 | htx = htx_from_buf(&res->buf); |
| 5458 | h1m_init_res(&h1m); |
| 5459 | |
| 5460 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5461 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5462 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5463 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5464 | reason = http_get_reason(http_ctx->status); |
| 5465 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5466 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5467 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5468 | } |
| 5469 | else { |
| 5470 | flags = HTX_SL_F_IS_RESP; |
| 5471 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5472 | } |
| 5473 | if (!sl) { |
| 5474 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5475 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5476 | WILL_LJMP(lua_error(L)); |
| 5477 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5478 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5479 | |
| 5480 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5481 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5482 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5483 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5484 | WILL_LJMP(lua_error(L)); |
| 5485 | } |
| 5486 | |
| 5487 | /* Browse the list of headers. */ |
| 5488 | lua_pushnil(L); |
| 5489 | while(lua_next(L, -2) != 0) { |
| 5490 | /* We expect a string as -2. */ |
| 5491 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5492 | 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] | 5493 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5494 | lua_typename(L, lua_type(L, -2))); |
| 5495 | WILL_LJMP(lua_error(L)); |
| 5496 | } |
| 5497 | name = lua_tolstring(L, -2, &nlen); |
| 5498 | |
| 5499 | /* We expect an array as -1. */ |
| 5500 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5501 | 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] | 5502 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5503 | name, |
| 5504 | lua_typename(L, lua_type(L, -1))); |
| 5505 | WILL_LJMP(lua_error(L)); |
| 5506 | } |
| 5507 | |
| 5508 | /* Browse the table who is on the top of the stack. */ |
| 5509 | lua_pushnil(L); |
| 5510 | while(lua_next(L, -2) != 0) { |
| 5511 | int id; |
| 5512 | |
| 5513 | /* We expect a number as -2. */ |
| 5514 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5515 | 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] | 5516 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5517 | name, |
| 5518 | lua_typename(L, lua_type(L, -2))); |
| 5519 | WILL_LJMP(lua_error(L)); |
| 5520 | } |
| 5521 | id = lua_tointeger(L, -2); |
| 5522 | |
| 5523 | /* We expect a string as -2. */ |
| 5524 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5525 | 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] | 5526 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5527 | name, id, |
| 5528 | lua_typename(L, lua_type(L, -1))); |
| 5529 | WILL_LJMP(lua_error(L)); |
| 5530 | } |
| 5531 | value = lua_tolstring(L, -1, &vlen); |
| 5532 | |
| 5533 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5534 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5535 | int ret; |
| 5536 | |
| 5537 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5538 | if (ret < 0) { |
| 5539 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5540 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5541 | name); |
| 5542 | WILL_LJMP(lua_error(L)); |
| 5543 | } |
| 5544 | else if (ret == 0) |
| 5545 | goto next; /* Skip it */ |
| 5546 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5547 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5548 | struct ist v = ist2(value, vlen); |
| 5549 | int ret; |
| 5550 | |
| 5551 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5552 | if (ret < 0) { |
| 5553 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5554 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5555 | name); |
| 5556 | WILL_LJMP(lua_error(L)); |
| 5557 | } |
| 5558 | else if (ret == 0) |
| 5559 | goto next; /* Skip it */ |
| 5560 | } |
| 5561 | |
| 5562 | /* Add a new header */ |
| 5563 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5564 | 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] | 5565 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5566 | name); |
| 5567 | WILL_LJMP(lua_error(L)); |
| 5568 | } |
| 5569 | next: |
| 5570 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5571 | lua_pop(L, 1); |
| 5572 | } |
| 5573 | |
| 5574 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5575 | lua_pop(L, 1); |
| 5576 | } |
| 5577 | |
| 5578 | if (h1m.flags & H1_MF_CHNK) |
| 5579 | h1m.flags &= ~H1_MF_CLEN; |
| 5580 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5581 | h1m.flags |= H1_MF_XFER_LEN; |
| 5582 | |
| 5583 | /* Uset HTX start-line flags */ |
| 5584 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5585 | flags |= HTX_SL_F_XFER_ENC; |
| 5586 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5587 | flags |= HTX_SL_F_XFER_LEN; |
| 5588 | if (h1m.flags & H1_MF_CHNK) |
| 5589 | flags |= HTX_SL_F_CHNK; |
| 5590 | else if (h1m.flags & H1_MF_CLEN) |
| 5591 | flags |= HTX_SL_F_CLEN; |
| 5592 | if (h1m.body_len == 0) |
| 5593 | flags |= HTX_SL_F_BODYLESS; |
| 5594 | } |
| 5595 | sl->flags |= flags; |
| 5596 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5597 | /* 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] | 5598 | * and the status code implies the presence of a message body, we must |
| 5599 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5600 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5601 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5602 | */ |
| 5603 | 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] | 5604 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5605 | /* Add a new header */ |
| 5606 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5607 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5608 | 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] | 5609 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5610 | WILL_LJMP(lua_error(L)); |
| 5611 | } |
| 5612 | } |
| 5613 | |
| 5614 | /* Finalize headers. */ |
| 5615 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5616 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5617 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5618 | WILL_LJMP(lua_error(L)); |
| 5619 | } |
| 5620 | |
| 5621 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5622 | b_reset(&res->buf); |
| 5623 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5624 | WILL_LJMP(lua_error(L)); |
| 5625 | } |
| 5626 | |
| 5627 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5628 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5629 | |
| 5630 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5631 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5632 | return 0; |
| 5633 | |
| 5634 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5635 | /* We will build the status line and the headers of the HTTP response. |
| 5636 | * We will try send at once if its not possible, we give back the hand |
| 5637 | * waiting for more room. |
| 5638 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5639 | __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] | 5640 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5641 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5642 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5643 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5644 | |
| 5645 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5646 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5647 | 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] | 5648 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5649 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5650 | } |
| 5651 | |
| 5652 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5653 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5654 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5655 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5656 | } |
| 5657 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5658 | /* |
| 5659 | * |
| 5660 | * |
| 5661 | * Class HTTP |
| 5662 | * |
| 5663 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5664 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5665 | |
| 5666 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5667 | * a class stream, otherwise it throws an error. |
| 5668 | */ |
| 5669 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5670 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5671 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5672 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5673 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5674 | /* This function creates and push in the stack a HTTP object |
| 5675 | * according with a current TXN. |
| 5676 | */ |
| 5677 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5678 | { |
| 5679 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5680 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5681 | /* Check stack size. */ |
| 5682 | if (!lua_checkstack(L, 3)) |
| 5683 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5684 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5685 | /* Create the object: obj[0] = userdata. |
| 5686 | * Note that the base of the Converters object is the |
| 5687 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5688 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5689 | lua_newtable(L); |
| 5690 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5691 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5692 | |
| 5693 | htxn->s = txn->s; |
| 5694 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5695 | htxn->dir = txn->dir; |
| 5696 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5697 | |
| 5698 | /* Pop a class stream metatable and affect it to the table. */ |
| 5699 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5700 | lua_setmetatable(L, -2); |
| 5701 | |
| 5702 | return 1; |
| 5703 | } |
| 5704 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5705 | /* This function creates and returns an array containing the status-line |
| 5706 | * elements. This function does not fails. |
| 5707 | */ |
| 5708 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5709 | { |
| 5710 | /* Create the table. */ |
| 5711 | lua_newtable(L); |
| 5712 | |
| 5713 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5714 | lua_pushstring(L, "version"); |
| 5715 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5716 | lua_settable(L, -3); |
| 5717 | lua_pushstring(L, "code"); |
| 5718 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5719 | lua_settable(L, -3); |
| 5720 | lua_pushstring(L, "reason"); |
| 5721 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5722 | lua_settable(L, -3); |
| 5723 | } |
| 5724 | else { |
| 5725 | lua_pushstring(L, "method"); |
| 5726 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5727 | lua_settable(L, -3); |
| 5728 | lua_pushstring(L, "uri"); |
| 5729 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5730 | lua_settable(L, -3); |
| 5731 | lua_pushstring(L, "version"); |
| 5732 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5733 | lua_settable(L, -3); |
| 5734 | } |
| 5735 | return 1; |
| 5736 | } |
| 5737 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5738 | /* This function creates ans returns an array of HTTP headers. |
| 5739 | * This function does not fails. It is used as wrapper with the |
| 5740 | * 2 following functions. |
| 5741 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5742 | __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] | 5743 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5744 | struct htx *htx; |
| 5745 | int32_t pos; |
| 5746 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5747 | /* Create the table. */ |
| 5748 | lua_newtable(L); |
| 5749 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5750 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5751 | htx = htxbuf(&msg->chn->buf); |
| 5752 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5753 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5754 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5755 | struct ist n, v; |
| 5756 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5757 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5758 | if (type == HTX_BLK_HDR) { |
| 5759 | n = htx_get_blk_name(htx,blk); |
| 5760 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5761 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5762 | else if (type == HTX_BLK_EOH) |
| 5763 | break; |
| 5764 | else |
| 5765 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5766 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5767 | /* Check for existing entry: |
| 5768 | * assume that the table is on the top of the stack, and |
| 5769 | * push the key in the stack, the function lua_gettable() |
| 5770 | * perform the lookup. |
| 5771 | */ |
| 5772 | lua_pushlstring(L, n.ptr, n.len); |
| 5773 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5774 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5775 | switch (lua_type(L, -1)) { |
| 5776 | case LUA_TNIL: |
| 5777 | /* Table not found, create it. */ |
| 5778 | lua_pop(L, 1); /* remove the nil value. */ |
| 5779 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5780 | lua_newtable(L); /* create and push empty table. */ |
| 5781 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5782 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5783 | 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] | 5784 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5786 | case LUA_TTABLE: |
| 5787 | /* Entry found: push the value in the table. */ |
| 5788 | len = lua_rawlen(L, -1); |
| 5789 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5790 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5791 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5792 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5793 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5794 | default: |
| 5795 | /* Other cases are errors. */ |
| 5796 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5797 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5798 | } |
| 5799 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5800 | return 1; |
| 5801 | } |
| 5802 | |
| 5803 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5804 | { |
| 5805 | struct hlua_txn *htxn; |
| 5806 | |
| 5807 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 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_REQ || !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 | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5813 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5814 | } |
| 5815 | |
| 5816 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5817 | { |
| 5818 | struct hlua_txn *htxn; |
| 5819 | |
| 5820 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 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_RES || !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 | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5826 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5827 | } |
| 5828 | |
| 5829 | /* This function replace full header, or just a value in |
| 5830 | * the request or in the response. It is a wrapper fir the |
| 5831 | * 4 following functions. |
| 5832 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5833 | __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] | 5834 | { |
| 5835 | size_t name_len; |
| 5836 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5837 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5838 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5839 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5840 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5841 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5842 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5843 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5844 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5845 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5846 | 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] | 5847 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5848 | return 0; |
| 5849 | } |
| 5850 | |
| 5851 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5852 | { |
| 5853 | struct hlua_txn *htxn; |
| 5854 | |
| 5855 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5856 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5857 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5858 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5859 | WILL_LJMP(lua_error(L)); |
| 5860 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5861 | 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] | 5862 | } |
| 5863 | |
| 5864 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5865 | { |
| 5866 | struct hlua_txn *htxn; |
| 5867 | |
| 5868 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5869 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5870 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5871 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5872 | WILL_LJMP(lua_error(L)); |
| 5873 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5874 | 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] | 5875 | } |
| 5876 | |
| 5877 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5878 | { |
| 5879 | struct hlua_txn *htxn; |
| 5880 | |
| 5881 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5882 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5883 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5884 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5885 | WILL_LJMP(lua_error(L)); |
| 5886 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5887 | 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] | 5888 | } |
| 5889 | |
| 5890 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5891 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5892 | struct hlua_txn *htxn; |
| 5893 | |
| 5894 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5895 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5896 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5897 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5898 | WILL_LJMP(lua_error(L)); |
| 5899 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5900 | 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] | 5901 | } |
| 5902 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5903 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5904 | * It is a wrapper for the 2 following functions. |
| 5905 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5906 | __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] | 5907 | { |
| 5908 | size_t len; |
| 5909 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5910 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5911 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5912 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5913 | ctx.blk = NULL; |
| 5914 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5915 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5916 | return 0; |
| 5917 | } |
| 5918 | |
| 5919 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5920 | { |
| 5921 | struct hlua_txn *htxn; |
| 5922 | |
| 5923 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5924 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5925 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5926 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5927 | WILL_LJMP(lua_error(L)); |
| 5928 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5929 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5930 | } |
| 5931 | |
| 5932 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5933 | { |
| 5934 | struct hlua_txn *htxn; |
| 5935 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5936 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5937 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5938 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5939 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5940 | WILL_LJMP(lua_error(L)); |
| 5941 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5942 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5943 | } |
| 5944 | |
| 5945 | /* This function adds an header. It is a wrapper used by |
| 5946 | * the 2 following functions. |
| 5947 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5948 | __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] | 5949 | { |
| 5950 | size_t name_len; |
| 5951 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5952 | size_t value_len; |
| 5953 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5954 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5955 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5956 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5957 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5958 | return 0; |
| 5959 | } |
| 5960 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5961 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5962 | { |
| 5963 | struct hlua_txn *htxn; |
| 5964 | |
| 5965 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5966 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5967 | |
| 5968 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5969 | WILL_LJMP(lua_error(L)); |
| 5970 | |
| 5971 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5972 | } |
| 5973 | |
| 5974 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5975 | { |
| 5976 | struct hlua_txn *htxn; |
| 5977 | |
| 5978 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5979 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5980 | |
| 5981 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5982 | WILL_LJMP(lua_error(L)); |
| 5983 | |
| 5984 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5985 | } |
| 5986 | |
| 5987 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5988 | { |
| 5989 | struct hlua_txn *htxn; |
| 5990 | |
| 5991 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5992 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5993 | |
| 5994 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5995 | WILL_LJMP(lua_error(L)); |
| 5996 | |
| 5997 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5998 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5999 | } |
| 6000 | |
| 6001 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6002 | { |
| 6003 | struct hlua_txn *htxn; |
| 6004 | |
| 6005 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6006 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6007 | |
| 6008 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6009 | WILL_LJMP(lua_error(L)); |
| 6010 | |
| 6011 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6012 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6013 | } |
| 6014 | |
| 6015 | /* This function set the method. */ |
| 6016 | static int hlua_http_req_set_meth(lua_State *L) |
| 6017 | { |
| 6018 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6019 | size_t name_len; |
| 6020 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6021 | |
| 6022 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6023 | WILL_LJMP(lua_error(L)); |
| 6024 | |
| 6025 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6026 | return 1; |
| 6027 | } |
| 6028 | |
| 6029 | /* This function set the method. */ |
| 6030 | static int hlua_http_req_set_path(lua_State *L) |
| 6031 | { |
| 6032 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6033 | size_t name_len; |
| 6034 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6035 | |
| 6036 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6037 | WILL_LJMP(lua_error(L)); |
| 6038 | |
| 6039 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6040 | return 1; |
| 6041 | } |
| 6042 | |
| 6043 | /* This function set the query-string. */ |
| 6044 | static int hlua_http_req_set_query(lua_State *L) |
| 6045 | { |
| 6046 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6047 | size_t name_len; |
| 6048 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6049 | |
| 6050 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6051 | WILL_LJMP(lua_error(L)); |
| 6052 | |
| 6053 | /* Check length. */ |
| 6054 | if (name_len > trash.size - 1) { |
| 6055 | lua_pushboolean(L, 0); |
| 6056 | return 1; |
| 6057 | } |
| 6058 | |
| 6059 | /* Add the mark question as prefix. */ |
| 6060 | chunk_reset(&trash); |
| 6061 | trash.area[trash.data++] = '?'; |
| 6062 | memcpy(trash.area + trash.data, name, name_len); |
| 6063 | trash.data += name_len; |
| 6064 | |
| 6065 | lua_pushboolean(L, |
| 6066 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6067 | return 1; |
| 6068 | } |
| 6069 | |
| 6070 | /* This function set the uri. */ |
| 6071 | static int hlua_http_req_set_uri(lua_State *L) |
| 6072 | { |
| 6073 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6074 | size_t name_len; |
| 6075 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6076 | |
| 6077 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6078 | WILL_LJMP(lua_error(L)); |
| 6079 | |
| 6080 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6081 | return 1; |
| 6082 | } |
| 6083 | |
| 6084 | /* This function set the response code & optionally reason. */ |
| 6085 | static int hlua_http_res_set_status(lua_State *L) |
| 6086 | { |
| 6087 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6088 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6089 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6090 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6091 | |
| 6092 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6093 | WILL_LJMP(lua_error(L)); |
| 6094 | |
| 6095 | http_res_set_status(code, reason, htxn->s); |
| 6096 | return 0; |
| 6097 | } |
| 6098 | |
| 6099 | /* |
| 6100 | * |
| 6101 | * |
| 6102 | * Class HTTPMessage |
| 6103 | * |
| 6104 | * |
| 6105 | */ |
| 6106 | |
| 6107 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6108 | * otherwise it throws an error. |
| 6109 | */ |
| 6110 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6111 | { |
| 6112 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6113 | } |
| 6114 | |
| 6115 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6116 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6117 | 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] | 6118 | { |
| 6119 | /* Check stack size. */ |
| 6120 | if (!lua_checkstack(L, 3)) |
| 6121 | return 0; |
| 6122 | |
| 6123 | lua_newtable(L); |
| 6124 | lua_pushlightuserdata(L, msg); |
| 6125 | lua_rawseti(L, -2, 0); |
| 6126 | |
| 6127 | /* Create the "channel" field that contains the request channel object. */ |
| 6128 | lua_pushstring(L, "channel"); |
| 6129 | if (!hlua_channel_new(L, msg->chn)) |
| 6130 | return 0; |
| 6131 | lua_rawset(L, -3); |
| 6132 | |
| 6133 | /* Pop a class stream metatable and affect it to the table. */ |
| 6134 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6135 | lua_setmetatable(L, -2); |
| 6136 | |
| 6137 | return 1; |
| 6138 | } |
| 6139 | |
| 6140 | /* Helper function returning a filter attached to the HTTP message at the |
| 6141 | * 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] | 6142 | * 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] | 6143 | * filled with output and input length respectively. |
| 6144 | */ |
| 6145 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6146 | { |
| 6147 | struct channel *chn = msg->chn; |
| 6148 | struct htx *htx = htxbuf(&chn->buf); |
| 6149 | struct filter *filter = NULL; |
| 6150 | |
| 6151 | *offset = co_data(msg->chn); |
| 6152 | *len = htx->data - co_data(msg->chn); |
| 6153 | |
| 6154 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6155 | filter = lua_touserdata (L, -1); |
| 6156 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6157 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6158 | |
| 6159 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6160 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6161 | } |
| 6162 | } |
| 6163 | |
| 6164 | lua_pop(L, 1); |
| 6165 | return filter; |
| 6166 | } |
| 6167 | |
| 6168 | /* Returns true if the channel attached to the HTTP message is the response |
| 6169 | * channel. |
| 6170 | */ |
| 6171 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6172 | { |
| 6173 | struct http_msg *msg; |
| 6174 | |
| 6175 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6176 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6177 | |
| 6178 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6179 | return 1; |
| 6180 | } |
| 6181 | |
| 6182 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6183 | * on hlua_http_get_stline(). |
| 6184 | */ |
| 6185 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6186 | { |
| 6187 | struct http_msg *msg; |
| 6188 | struct htx *htx; |
| 6189 | struct htx_sl *sl; |
| 6190 | |
| 6191 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6192 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6193 | |
| 6194 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6195 | WILL_LJMP(lua_error(L)); |
| 6196 | |
| 6197 | htx = htxbuf(&msg->chn->buf); |
| 6198 | sl = http_get_stline(htx); |
| 6199 | if (!sl) |
| 6200 | return 0; |
| 6201 | return hlua_http_get_stline(L, sl); |
| 6202 | } |
| 6203 | |
| 6204 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6205 | * hlua_http_get_headers(). |
| 6206 | */ |
| 6207 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6208 | { |
| 6209 | struct http_msg *msg; |
| 6210 | |
| 6211 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6212 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6213 | |
| 6214 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6215 | WILL_LJMP(lua_error(L)); |
| 6216 | |
| 6217 | return hlua_http_get_headers(L, msg); |
| 6218 | } |
| 6219 | |
| 6220 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6221 | * name. It relies on hlua_http_del_hdr(). |
| 6222 | */ |
| 6223 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6224 | { |
| 6225 | struct http_msg *msg; |
| 6226 | |
| 6227 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6228 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6229 | |
| 6230 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6231 | WILL_LJMP(lua_error(L)); |
| 6232 | |
| 6233 | return hlua_http_del_hdr(L, msg); |
| 6234 | } |
| 6235 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6236 | /* 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] | 6237 | * message given its name against a regex and replaces it if it matches. It |
| 6238 | * relies on hlua_http_rep_hdr(). |
| 6239 | */ |
| 6240 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6241 | { |
| 6242 | struct http_msg *msg; |
| 6243 | |
| 6244 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6245 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6246 | |
| 6247 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6248 | WILL_LJMP(lua_error(L)); |
| 6249 | |
| 6250 | return hlua_http_rep_hdr(L, msg, 1); |
| 6251 | } |
| 6252 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6253 | /* 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] | 6254 | * message given its name against a regex and replaces it if it matches. It |
| 6255 | * relies on hlua_http_rep_hdr(). |
| 6256 | */ |
| 6257 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6258 | { |
| 6259 | struct http_msg *msg; |
| 6260 | |
| 6261 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6262 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6263 | |
| 6264 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6265 | WILL_LJMP(lua_error(L)); |
| 6266 | |
| 6267 | return hlua_http_rep_hdr(L, msg, 0); |
| 6268 | } |
| 6269 | |
| 6270 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6271 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6272 | { |
| 6273 | struct http_msg *msg; |
| 6274 | |
| 6275 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6276 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6277 | |
| 6278 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6279 | WILL_LJMP(lua_error(L)); |
| 6280 | |
| 6281 | return hlua_http_add_hdr(L, msg); |
| 6282 | } |
| 6283 | |
| 6284 | /* Add an header in the HTTP message removing existing headers with the same |
| 6285 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6286 | */ |
| 6287 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6288 | { |
| 6289 | struct http_msg *msg; |
| 6290 | |
| 6291 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6292 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6293 | |
| 6294 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6295 | WILL_LJMP(lua_error(L)); |
| 6296 | |
| 6297 | hlua_http_del_hdr(L, msg); |
| 6298 | return hlua_http_add_hdr(L, msg); |
| 6299 | } |
| 6300 | |
| 6301 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6302 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6303 | { |
| 6304 | struct stream *s; |
| 6305 | struct http_msg *msg; |
| 6306 | const char *name; |
| 6307 | size_t name_len; |
| 6308 | |
| 6309 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6310 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6311 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6312 | |
| 6313 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6314 | WILL_LJMP(lua_error(L)); |
| 6315 | |
| 6316 | s = chn_strm(msg->chn); |
| 6317 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6318 | return 1; |
| 6319 | } |
| 6320 | |
| 6321 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6322 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6323 | { |
| 6324 | struct stream *s; |
| 6325 | struct http_msg *msg; |
| 6326 | const char *name; |
| 6327 | size_t name_len; |
| 6328 | |
| 6329 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6330 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6331 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6332 | |
| 6333 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6334 | WILL_LJMP(lua_error(L)); |
| 6335 | |
| 6336 | s = chn_strm(msg->chn); |
| 6337 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6338 | return 1; |
| 6339 | } |
| 6340 | |
| 6341 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6342 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6343 | { |
| 6344 | struct stream *s; |
| 6345 | struct http_msg *msg; |
| 6346 | const char *name; |
| 6347 | size_t name_len; |
| 6348 | |
| 6349 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6350 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6351 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6352 | |
| 6353 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6354 | WILL_LJMP(lua_error(L)); |
| 6355 | |
| 6356 | /* Check length. */ |
| 6357 | if (name_len > trash.size - 1) { |
| 6358 | lua_pushboolean(L, 0); |
| 6359 | return 1; |
| 6360 | } |
| 6361 | |
| 6362 | /* Add the mark question as prefix. */ |
| 6363 | chunk_reset(&trash); |
| 6364 | trash.area[trash.data++] = '?'; |
| 6365 | memcpy(trash.area + trash.data, name, name_len); |
| 6366 | trash.data += name_len; |
| 6367 | |
| 6368 | s = chn_strm(msg->chn); |
| 6369 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6370 | return 1; |
| 6371 | } |
| 6372 | |
| 6373 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6374 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6375 | { |
| 6376 | struct stream *s; |
| 6377 | struct http_msg *msg; |
| 6378 | const char *name; |
| 6379 | size_t name_len; |
| 6380 | |
| 6381 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6382 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6383 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6384 | |
| 6385 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6386 | WILL_LJMP(lua_error(L)); |
| 6387 | |
| 6388 | s = chn_strm(msg->chn); |
| 6389 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6390 | return 1; |
| 6391 | } |
| 6392 | |
| 6393 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6394 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6395 | { |
| 6396 | struct http_msg *msg; |
| 6397 | unsigned int code; |
| 6398 | const char *reason; |
| 6399 | size_t reason_len; |
| 6400 | |
| 6401 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6402 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6403 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6404 | |
| 6405 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6406 | WILL_LJMP(lua_error(L)); |
| 6407 | |
| 6408 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6409 | return 1; |
| 6410 | } |
| 6411 | |
| 6412 | /* Returns true if the HTTP message is full. */ |
| 6413 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6414 | { |
| 6415 | struct http_msg *msg; |
| 6416 | |
| 6417 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6418 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6419 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6420 | return 1; |
| 6421 | } |
| 6422 | |
| 6423 | /* Returns true if the HTTP message may still receive data. */ |
| 6424 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6425 | { |
| 6426 | struct http_msg *msg; |
| 6427 | struct htx *htx; |
| 6428 | |
| 6429 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6430 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6431 | htx = htxbuf(&msg->chn->buf); |
| 6432 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6433 | return 1; |
| 6434 | } |
| 6435 | |
| 6436 | /* Returns true if the HTTP message EOM was received */ |
| 6437 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6438 | { |
| 6439 | struct http_msg *msg; |
| 6440 | struct htx *htx; |
| 6441 | |
| 6442 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6443 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6444 | htx = htxbuf(&msg->chn->buf); |
| 6445 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6446 | return 1; |
| 6447 | } |
| 6448 | |
| 6449 | /* Returns the number of bytes available in the input side of the HTTP |
| 6450 | * message. This function never fails. |
| 6451 | */ |
| 6452 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6453 | { |
| 6454 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6455 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6456 | |
| 6457 | MAY_LJMP(check_args(L, 1, "input")); |
| 6458 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6459 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6460 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6461 | return 1; |
| 6462 | } |
| 6463 | |
| 6464 | /* Returns the number of bytes available in the output side of the HTTP |
| 6465 | * message. This function never fails. |
| 6466 | */ |
| 6467 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6468 | { |
| 6469 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6470 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6471 | |
| 6472 | MAY_LJMP(check_args(L, 1, "output")); |
| 6473 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6474 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6475 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6476 | return 1; |
| 6477 | } |
| 6478 | |
| 6479 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6480 | * 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] | 6481 | * 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] | 6482 | * block. This function is called during the payload filtering, so the headers |
| 6483 | * are already scheduled for output (from the filter point of view). |
| 6484 | */ |
| 6485 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6486 | { |
| 6487 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6488 | struct htx_blk *blk; |
| 6489 | struct htx_ret htxret; |
| 6490 | luaL_Buffer b; |
| 6491 | int ret = 0; |
| 6492 | |
| 6493 | luaL_buffinit(L, &b); |
| 6494 | htxret = htx_find_offset(htx, offset); |
| 6495 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6496 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6497 | struct ist v; |
| 6498 | |
| 6499 | switch (type) { |
| 6500 | case HTX_BLK_UNUSED: |
| 6501 | break; |
| 6502 | |
| 6503 | case HTX_BLK_DATA: |
| 6504 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6505 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6506 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6507 | |
| 6508 | luaL_addlstring(&b, v.ptr, v.len); |
| 6509 | ret += v.len; |
| 6510 | break; |
| 6511 | |
| 6512 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6513 | if (!ret) |
| 6514 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6515 | goto end; |
| 6516 | } |
| 6517 | offset = 0; |
| 6518 | } |
| 6519 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6520 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6521 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6522 | goto no_data; |
| 6523 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6524 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6525 | |
| 6526 | no_data: |
| 6527 | /* Remove the empty string and push nil on the stack */ |
| 6528 | lua_pop(L, 1); |
| 6529 | lua_pushnil(L); |
| 6530 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6531 | } |
| 6532 | |
| 6533 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6534 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6535 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6536 | * the filter context. |
| 6537 | */ |
| 6538 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6539 | { |
| 6540 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6541 | struct htx_ret htxret; |
| 6542 | int /*max, */ret = 0; |
| 6543 | |
| 6544 | /* Nothing to do, just return */ |
| 6545 | if (unlikely(istlen(str) == 0)) |
| 6546 | goto end; |
| 6547 | |
| 6548 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6549 | ret = -1; |
| 6550 | goto end; |
| 6551 | } |
| 6552 | |
| 6553 | htxret = htx_find_offset(htx, offset); |
| 6554 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6555 | if (!htx_add_last_data(htx, str)) |
| 6556 | goto end; |
| 6557 | } |
| 6558 | else { |
| 6559 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6560 | v.ptr += htxret.ret; |
| 6561 | v.len = 0; |
| 6562 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6563 | goto end; |
| 6564 | } |
| 6565 | ret = str.len; |
| 6566 | if (ret) { |
| 6567 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6568 | flt_update_offsets(filter, msg->chn, ret); |
| 6569 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6570 | } |
| 6571 | |
| 6572 | end: |
| 6573 | htx_to_buf(htx, &msg->chn->buf); |
| 6574 | return ret; |
| 6575 | } |
| 6576 | |
| 6577 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6578 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6579 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6580 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6581 | * update the filter context. |
| 6582 | */ |
| 6583 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6584 | { |
| 6585 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6586 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6587 | struct htx_blk *blk; |
| 6588 | struct htx_ret htxret; |
| 6589 | size_t ret = 0; |
| 6590 | |
| 6591 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6592 | 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] | 6593 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6594 | * of special blocks like HTX_BLK_EOT. |
| 6595 | * We simply truncate after offset |
| 6596 | * (truncate targeted blk and discard the following ones) |
| 6597 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6598 | htx_truncate(htx, offset); |
| 6599 | ret = len; |
| 6600 | goto end; |
| 6601 | } |
| 6602 | |
| 6603 | htxret = htx_find_offset(htx, offset); |
| 6604 | blk = htxret.blk; |
| 6605 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6606 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6607 | struct ist v; |
| 6608 | |
| 6609 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6610 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6611 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6612 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6613 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6614 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6615 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6616 | /* trimming data in blk: discard everything after the offset |
| 6617 | * (replace 'v' with 'IST_NULL') |
| 6618 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6619 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6620 | if (blk && v.len < len) { |
| 6621 | /* In this case, caller wants to keep removing data, |
| 6622 | * but we need to spare current blk |
| 6623 | * because it was already trimmed |
| 6624 | */ |
| 6625 | blk = htx_get_next_blk(htx, blk); |
| 6626 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6627 | len -= v.len; |
| 6628 | ret += v.len; |
| 6629 | } |
| 6630 | |
| 6631 | |
| 6632 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6633 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6634 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6635 | uint32_t sz = htx_get_blksz(blk); |
| 6636 | |
| 6637 | switch (type) { |
| 6638 | case HTX_BLK_UNUSED: |
| 6639 | break; |
| 6640 | |
| 6641 | case HTX_BLK_DATA: |
| 6642 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6643 | /* don't discard whole blk, only part of it |
| 6644 | * (from the beginning) |
| 6645 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6646 | htx_cut_data_blk(htx, blk, len); |
| 6647 | ret += len; |
| 6648 | goto end; |
| 6649 | } |
| 6650 | break; |
| 6651 | |
| 6652 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6653 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6654 | goto end; |
| 6655 | } |
| 6656 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6657 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6658 | len -= sz; |
| 6659 | ret += sz; |
| 6660 | blk = htx_remove_blk(htx, blk); |
| 6661 | } |
| 6662 | |
| 6663 | end: |
| 6664 | flt_update_offsets(filter, msg->chn, -ret); |
| 6665 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6666 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6667 | * to loose the EOM flag if the message is empty. |
| 6668 | */ |
| 6669 | } |
| 6670 | |
| 6671 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6672 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6673 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6674 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6675 | * the stack. |
| 6676 | */ |
| 6677 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6678 | { |
| 6679 | struct http_msg *msg; |
| 6680 | struct filter *filter; |
| 6681 | size_t output, input; |
| 6682 | int offset, len; |
| 6683 | |
| 6684 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6685 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6686 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6687 | |
| 6688 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6689 | WILL_LJMP(lua_error(L)); |
| 6690 | |
| 6691 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6692 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6693 | WILL_LJMP(lua_error(L)); |
| 6694 | |
| 6695 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6696 | lua_pushnil(L); |
| 6697 | return 1; |
| 6698 | } |
| 6699 | |
| 6700 | offset = output; |
| 6701 | if (lua_gettop(L) > 1) { |
| 6702 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6703 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6704 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6705 | offset += output; |
| 6706 | if (offset < output || offset > input + output) { |
| 6707 | lua_pushfstring(L, "offset out of range."); |
| 6708 | WILL_LJMP(lua_error(L)); |
| 6709 | } |
| 6710 | } |
| 6711 | len = output + input - offset; |
| 6712 | if (lua_gettop(L) == 3) { |
| 6713 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6714 | if (!len) |
| 6715 | goto dup; |
| 6716 | if (len == -1) |
| 6717 | len = global.tune.bufsize; |
| 6718 | if (len < 0) { |
| 6719 | lua_pushfstring(L, "length out of range."); |
| 6720 | WILL_LJMP(lua_error(L)); |
| 6721 | } |
| 6722 | } |
| 6723 | |
| 6724 | dup: |
| 6725 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6726 | return 1; |
| 6727 | } |
| 6728 | |
| 6729 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6730 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6731 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6732 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6733 | * yield. An exception is returned if it is called from another callback. |
| 6734 | */ |
| 6735 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6736 | { |
| 6737 | struct http_msg *msg; |
| 6738 | struct filter *filter; |
| 6739 | const char *str; |
| 6740 | size_t offset, len, sz; |
| 6741 | int ret; |
| 6742 | |
| 6743 | MAY_LJMP(check_args(L, 2, "append")); |
| 6744 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6745 | |
| 6746 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6747 | WILL_LJMP(lua_error(L)); |
| 6748 | |
| 6749 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6750 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6751 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6752 | WILL_LJMP(lua_error(L)); |
| 6753 | |
| 6754 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6755 | lua_pushinteger(L, ret); |
| 6756 | return 1; |
| 6757 | } |
| 6758 | |
| 6759 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6760 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6761 | * channel function used to prepend data, this one can only be called inside a |
| 6762 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6763 | * returned if it is called from another callback. |
| 6764 | */ |
| 6765 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6766 | { |
| 6767 | struct http_msg *msg; |
| 6768 | struct filter *filter; |
| 6769 | const char *str; |
| 6770 | size_t offset, len, sz; |
| 6771 | int ret; |
| 6772 | |
| 6773 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6774 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6775 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6776 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6777 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6778 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6779 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6780 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6781 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6782 | WILL_LJMP(lua_error(L)); |
| 6783 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6784 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6785 | lua_pushinteger(L, ret); |
| 6786 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6787 | } |
| 6788 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6789 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6790 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6791 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6792 | * this one can only be called inside a filter, from http_payload callback. So |
| 6793 | * it cannot yield. An exception is returned if it is called from another |
| 6794 | * callback. |
| 6795 | */ |
| 6796 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6797 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6798 | struct http_msg *msg; |
| 6799 | struct filter *filter; |
| 6800 | const char *str; |
| 6801 | size_t input, output, sz; |
| 6802 | int offset; |
| 6803 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6804 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6805 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6806 | 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] | 6807 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6808 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6809 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6810 | WILL_LJMP(lua_error(L)); |
| 6811 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6813 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6814 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6815 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6816 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6817 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6818 | if (lua_gettop(L) > 2) { |
| 6819 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6820 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6821 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6822 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6823 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6824 | lua_pushfstring(L, "offset out of range."); |
| 6825 | WILL_LJMP(lua_error(L)); |
| 6826 | } |
| 6827 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6828 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6829 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6830 | lua_pushinteger(L, ret); |
| 6831 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6832 | } |
| 6833 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6834 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6835 | * default all DATA blocks are removed. It returns the amount of data |
| 6836 | * removed. Unlike the channel function used to remove data, this one can only |
| 6837 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6838 | * exception is returned if it is called from another callback. |
| 6839 | */ |
| 6840 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6841 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6842 | struct http_msg *msg; |
| 6843 | struct filter *filter; |
| 6844 | size_t input, output; |
| 6845 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6846 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6847 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6848 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6849 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6850 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6851 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6852 | WILL_LJMP(lua_error(L)); |
| 6853 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6854 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6855 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6856 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6857 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6858 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6859 | if (lua_gettop(L) > 1) { |
| 6860 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 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; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6864 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6865 | lua_pushfstring(L, "offset out of range."); |
| 6866 | WILL_LJMP(lua_error(L)); |
| 6867 | } |
| 6868 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6869 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6870 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6871 | if (lua_gettop(L) == 3) { |
| 6872 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6873 | if (!len) |
| 6874 | goto end; |
| 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 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6884 | |
| 6885 | end: |
| 6886 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6887 | return 1; |
| 6888 | } |
| 6889 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6890 | /* 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] | 6891 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6892 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6893 | * function used to replace data, this one can only be called inside a filter, |
| 6894 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6895 | * it is called from another callback. |
| 6896 | */ |
| 6897 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6898 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6899 | struct http_msg *msg; |
| 6900 | struct filter *filter; |
| 6901 | struct htx *htx; |
| 6902 | const char *str; |
| 6903 | size_t input, output, sz; |
| 6904 | int offset, len; |
| 6905 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6906 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6907 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6908 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6909 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6910 | |
| 6911 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6912 | WILL_LJMP(lua_error(L)); |
| 6913 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6914 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6915 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6916 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6917 | WILL_LJMP(lua_error(L)); |
| 6918 | |
| 6919 | offset = output; |
| 6920 | if (lua_gettop(L) > 2) { |
| 6921 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6922 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6923 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6924 | offset += output; |
| 6925 | if (offset < output || offset > input + output) { |
| 6926 | lua_pushfstring(L, "offset out of range."); |
| 6927 | WILL_LJMP(lua_error(L)); |
| 6928 | } |
| 6929 | } |
| 6930 | |
| 6931 | len = output + input - offset; |
| 6932 | if (lua_gettop(L) == 4) { |
| 6933 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6934 | if (!len) |
| 6935 | goto set; |
| 6936 | if (len == -1) |
| 6937 | len = output + input - offset; |
| 6938 | if (len < 0 || offset + len > output + input) { |
| 6939 | lua_pushfstring(L, "length out of range."); |
| 6940 | WILL_LJMP(lua_error(L)); |
| 6941 | } |
| 6942 | } |
| 6943 | |
| 6944 | set: |
| 6945 | /* Be sure we can copied the string once input data will be removed. */ |
| 6946 | htx = htx_from_buf(&msg->chn->buf); |
| 6947 | if (sz > htx_free_data_space(htx) + len) |
| 6948 | lua_pushinteger(L, -1); |
| 6949 | else { |
| 6950 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6951 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6952 | lua_pushinteger(L, ret); |
| 6953 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6954 | return 1; |
| 6955 | } |
| 6956 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6957 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6958 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6959 | * the channel function used to send data, this one can only be called inside a |
| 6960 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6961 | * returned if it is called from another callback. |
| 6962 | */ |
| 6963 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6964 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6965 | struct http_msg *msg; |
| 6966 | struct filter *filter; |
| 6967 | struct htx *htx; |
| 6968 | const char *str; |
| 6969 | size_t offset, len, sz; |
| 6970 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6971 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6972 | MAY_LJMP(check_args(L, 2, "send")); |
| 6973 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6974 | |
| 6975 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6976 | WILL_LJMP(lua_error(L)); |
| 6977 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6978 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6979 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6980 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6981 | WILL_LJMP(lua_error(L)); |
| 6982 | |
| 6983 | /* Return an error if the channel's output is closed */ |
| 6984 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6985 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6986 | return 1; |
| 6987 | } |
| 6988 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6989 | htx = htx_from_buf(&msg->chn->buf); |
| 6990 | if (sz > htx_free_data_space(htx)) { |
| 6991 | lua_pushinteger(L, -1); |
| 6992 | return 1; |
| 6993 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6994 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6995 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6996 | if (ret > 0) { |
| 6997 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6998 | |
| 6999 | FLT_OFF(filter, msg->chn) += ret; |
| 7000 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7001 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7002 | } |
| 7003 | |
| 7004 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7005 | return 1; |
| 7006 | } |
| 7007 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7008 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7009 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7010 | * channel function used to forward data, this one can only be called inside a |
| 7011 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7012 | * returned if it is called from another callback. All other functions deal with |
| 7013 | * DATA block, this one not. |
| 7014 | */ |
| 7015 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7016 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7017 | struct http_msg *msg; |
| 7018 | struct filter *filter; |
| 7019 | size_t offset, len; |
| 7020 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7021 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7022 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7023 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7024 | |
| 7025 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7026 | WILL_LJMP(lua_error(L)); |
| 7027 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7028 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7029 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7030 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7031 | WILL_LJMP(lua_error(L)); |
| 7032 | |
| 7033 | /* Nothing to do, just return */ |
| 7034 | if (!fwd) |
| 7035 | goto end; |
| 7036 | |
| 7037 | /* Return an error if the channel's output is closed */ |
| 7038 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7039 | ret = -1; |
| 7040 | goto end; |
| 7041 | } |
| 7042 | |
| 7043 | ret = fwd; |
| 7044 | if (ret > len) |
| 7045 | ret = len; |
| 7046 | |
| 7047 | if (ret) { |
| 7048 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7049 | |
| 7050 | FLT_OFF(filter, msg->chn) += ret; |
| 7051 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7052 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7053 | } |
| 7054 | |
| 7055 | end: |
| 7056 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7057 | return 1; |
| 7058 | } |
| 7059 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7060 | /* Set EOM flag on the HTX message. |
| 7061 | * |
| 7062 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7063 | * really know how to do without this feature. |
| 7064 | */ |
| 7065 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7066 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7067 | struct http_msg *msg; |
| 7068 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7069 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7070 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7071 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7072 | htx = htxbuf(&msg->chn->buf); |
| 7073 | htx->flags |= HTX_FL_EOM; |
| 7074 | return 0; |
| 7075 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7076 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7077 | /* Unset EOM flag on the HTX message. |
| 7078 | * |
| 7079 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7080 | * really know how to do without this feature. |
| 7081 | */ |
| 7082 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7083 | { |
| 7084 | struct http_msg *msg; |
| 7085 | struct htx *htx; |
| 7086 | |
| 7087 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7088 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7089 | htx = htxbuf(&msg->chn->buf); |
| 7090 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7091 | return 0; |
| 7092 | } |
| 7093 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7094 | /* |
| 7095 | * |
| 7096 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7097 | * Class HTTPClient |
| 7098 | * |
| 7099 | * |
| 7100 | */ |
| 7101 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7102 | { |
| 7103 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7104 | } |
| 7105 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7106 | |
| 7107 | /* stops the httpclient and ask it to kill itself */ |
| 7108 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7109 | { |
| 7110 | struct hlua_httpclient *hlua_hc; |
| 7111 | |
| 7112 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7113 | |
| 7114 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7115 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7116 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7117 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7118 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7119 | hlua_hc->hc = NULL; |
| 7120 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7121 | |
| 7122 | return 0; |
| 7123 | } |
| 7124 | |
| 7125 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7126 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7127 | { |
| 7128 | struct hlua_httpclient *hlua_hc; |
| 7129 | struct hlua *hlua; |
| 7130 | |
| 7131 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7132 | hlua = hlua_gethlua(L); |
| 7133 | if (!hlua) |
| 7134 | return 0; |
| 7135 | |
| 7136 | /* Check stack size. */ |
| 7137 | if (!lua_checkstack(L, 3)) { |
| 7138 | hlua_pusherror(L, "httpclient: full stack"); |
| 7139 | goto err; |
| 7140 | } |
| 7141 | /* Create the object: obj[0] = userdata. */ |
| 7142 | lua_newtable(L); |
| 7143 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7144 | lua_rawseti(L, -2, 0); |
| 7145 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7146 | |
| 7147 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7148 | if (!hlua_hc->hc) |
| 7149 | goto err; |
| 7150 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7151 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7152 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7153 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7154 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7155 | lua_setmetatable(L, -2); |
| 7156 | |
| 7157 | return 1; |
| 7158 | |
| 7159 | err: |
| 7160 | WILL_LJMP(lua_error(L)); |
| 7161 | return 0; |
| 7162 | } |
| 7163 | |
| 7164 | |
| 7165 | /* |
| 7166 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7167 | * httpclient receives some data |
| 7168 | * |
| 7169 | */ |
| 7170 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7171 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7172 | { |
| 7173 | struct hlua *hlua = hc->caller; |
| 7174 | |
| 7175 | if (!hlua || !hlua->task) |
| 7176 | return; |
| 7177 | |
| 7178 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7179 | } |
| 7180 | |
| 7181 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7182 | * Fill the lua stack with headers from the httpclient response |
| 7183 | * This works the same way as the hlua_http_get_headers() function |
| 7184 | */ |
| 7185 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7186 | { |
| 7187 | struct http_hdr *hdr; |
| 7188 | |
| 7189 | lua_newtable(L); |
| 7190 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7191 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7192 | struct ist n, v; |
| 7193 | int len; |
| 7194 | |
| 7195 | n = hdr->n; |
| 7196 | v = hdr->v; |
| 7197 | |
| 7198 | /* Check for existing entry: |
| 7199 | * assume that the table is on the top of the stack, and |
| 7200 | * push the key in the stack, the function lua_gettable() |
| 7201 | * perform the lookup. |
| 7202 | */ |
| 7203 | |
| 7204 | lua_pushlstring(L, n.ptr, n.len); |
| 7205 | lua_gettable(L, -2); |
| 7206 | |
| 7207 | switch (lua_type(L, -1)) { |
| 7208 | case LUA_TNIL: |
| 7209 | /* Table not found, create it. */ |
| 7210 | lua_pop(L, 1); /* remove the nil value. */ |
| 7211 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7212 | lua_newtable(L); /* create and push empty table. */ |
| 7213 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7214 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7215 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7216 | break; |
| 7217 | |
| 7218 | case LUA_TTABLE: |
| 7219 | /* Entry found: push the value in the table. */ |
| 7220 | len = lua_rawlen(L, -1); |
| 7221 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7222 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7223 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7224 | break; |
| 7225 | |
| 7226 | default: |
| 7227 | /* Other cases are errors. */ |
| 7228 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7229 | WILL_LJMP(lua_error(L)); |
| 7230 | } |
| 7231 | } |
| 7232 | return 1; |
| 7233 | } |
| 7234 | |
| 7235 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7236 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7237 | * |
| 7238 | * Caller must free the result |
| 7239 | */ |
| 7240 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7241 | { |
| 7242 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7243 | struct http_hdr *result = NULL; |
| 7244 | uint32_t hdr_num = 0; |
| 7245 | |
| 7246 | lua_pushnil(L); |
| 7247 | while (lua_next(L, -2) != 0) { |
| 7248 | struct ist name, value; |
| 7249 | const char *n, *v; |
| 7250 | size_t nlen, vlen; |
| 7251 | |
| 7252 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7253 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7254 | goto next_hdr; |
| 7255 | } |
| 7256 | |
| 7257 | n = lua_tolstring(L, -2, &nlen); |
| 7258 | name = ist2(n, nlen); |
| 7259 | |
| 7260 | /* Loop on header's values */ |
| 7261 | lua_pushnil(L); |
| 7262 | while (lua_next(L, -2)) { |
| 7263 | if (!lua_isstring(L, -1)) { |
| 7264 | /* Skip the value if it is not a string */ |
| 7265 | goto next_value; |
| 7266 | } |
| 7267 | |
| 7268 | v = lua_tolstring(L, -1, &vlen); |
| 7269 | value = ist2(v, vlen); |
| 7270 | name = ist2(n, nlen); |
| 7271 | |
| 7272 | hdrs[hdr_num].n = istdup(name); |
| 7273 | hdrs[hdr_num].v = istdup(value); |
| 7274 | |
| 7275 | hdr_num++; |
| 7276 | |
| 7277 | next_value: |
| 7278 | lua_pop(L, 1); |
| 7279 | } |
| 7280 | |
| 7281 | next_hdr: |
| 7282 | lua_pop(L, 1); |
| 7283 | |
| 7284 | } |
| 7285 | |
| 7286 | if (hdr_num) { |
| 7287 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7288 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7289 | if (!result) |
| 7290 | goto skip_headers; |
| 7291 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7292 | |
| 7293 | result[hdr_num].n = IST_NULL; |
| 7294 | result[hdr_num].v = IST_NULL; |
| 7295 | } |
| 7296 | |
| 7297 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7298 | |
| 7299 | return result; |
| 7300 | } |
| 7301 | |
| 7302 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7303 | /* |
| 7304 | * For each yield, checks if there is some data in the httpclient and push them |
| 7305 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7306 | * the stack |
| 7307 | */ |
| 7308 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7309 | { |
| 7310 | struct buffer *tr; |
| 7311 | int res; |
| 7312 | struct hlua *hlua = hlua_gethlua(L); |
| 7313 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7314 | |
| 7315 | |
| 7316 | tr = get_trash_chunk(); |
| 7317 | |
| 7318 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7319 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7320 | |
| 7321 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7322 | |
| 7323 | luaL_pushresult(&hlua_hc->b); |
| 7324 | lua_settable(L, -3); |
| 7325 | |
| 7326 | lua_pushstring(L, "status"); |
| 7327 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7328 | lua_settable(L, -3); |
| 7329 | |
| 7330 | |
| 7331 | lua_pushstring(L, "reason"); |
| 7332 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7333 | lua_settable(L, -3); |
| 7334 | |
| 7335 | lua_pushstring(L, "headers"); |
| 7336 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7337 | lua_settable(L, -3); |
| 7338 | |
| 7339 | return 1; |
| 7340 | } |
| 7341 | |
| 7342 | if (httpclient_data(hlua_hc->hc)) |
| 7343 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7344 | |
| 7345 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7346 | |
| 7347 | return 0; |
| 7348 | } |
| 7349 | |
| 7350 | /* |
| 7351 | * Call this when trying to stream a body during a request |
| 7352 | */ |
| 7353 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7354 | { |
| 7355 | struct hlua *hlua; |
| 7356 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7357 | const char *body_str = NULL; |
| 7358 | int ret; |
| 7359 | int end = 0; |
| 7360 | size_t buf_len; |
| 7361 | size_t to_send = 0; |
| 7362 | |
| 7363 | hlua = hlua_gethlua(L); |
| 7364 | |
| 7365 | if (!hlua || !hlua->task) |
| 7366 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7367 | "'frontend', 'backend' or 'task'")); |
| 7368 | |
| 7369 | ret = lua_getfield(L, -1, "body"); |
| 7370 | if (ret != LUA_TSTRING) |
| 7371 | goto rcv; |
| 7372 | |
| 7373 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7374 | lua_pop(L, 1); |
| 7375 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7376 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7377 | |
| 7378 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7379 | end = 1; |
| 7380 | |
| 7381 | /* the end flag is always set since we are using the whole remaining size */ |
| 7382 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7383 | |
| 7384 | if (buf_len > hlua_hc->sent) { |
| 7385 | /* still need to process the buffer */ |
| 7386 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7387 | } else { |
| 7388 | goto rcv; |
| 7389 | /* we sent the whole request buffer we can recv */ |
| 7390 | } |
| 7391 | return 0; |
| 7392 | |
| 7393 | rcv: |
| 7394 | |
| 7395 | /* we return a "res" object */ |
| 7396 | lua_newtable(L); |
| 7397 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7398 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7399 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7400 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7401 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7402 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7403 | |
| 7404 | return 1; |
| 7405 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7406 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7407 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7408 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7409 | */ |
| 7410 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7411 | __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] | 7412 | { |
| 7413 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7414 | struct http_hdr *hdrs = NULL; |
| 7415 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7416 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7417 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7418 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7419 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7420 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7421 | |
| 7422 | hlua = hlua_gethlua(L); |
| 7423 | |
| 7424 | if (!hlua || !hlua->task) |
| 7425 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7426 | "'frontend', 'backend' or 'task'")); |
| 7427 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7428 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7429 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7430 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7431 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7432 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7433 | lua_pushnil(L); /* first key */ |
| 7434 | while (lua_next(L, 2)) { |
| 7435 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7436 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7437 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7438 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7439 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7440 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7441 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7442 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7443 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7444 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7445 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7446 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7447 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7448 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7449 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7450 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7451 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7452 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7453 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7454 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7455 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7456 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7457 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7458 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7459 | } else { |
| 7460 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7461 | } |
| 7462 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7463 | lua_pop(L, 1); |
| 7464 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7465 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7466 | if (!url_str) { |
| 7467 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7468 | return 0; |
| 7469 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7470 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7471 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7472 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7473 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7474 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7475 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7476 | |
| 7477 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7478 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7479 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7480 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7481 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7482 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7483 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7484 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7485 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7486 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7487 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7488 | 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] | 7489 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7490 | /* free the temporary headers array */ |
| 7491 | hdrs_i = hdrs; |
| 7492 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7493 | istfree(&hdrs_i->n); |
| 7494 | istfree(&hdrs_i->v); |
| 7495 | hdrs_i++; |
| 7496 | } |
| 7497 | ha_free(&hdrs); |
| 7498 | |
| 7499 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7500 | if (ret != ERR_NONE) { |
| 7501 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7502 | return 0; |
| 7503 | } |
| 7504 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7505 | if (!httpclient_start(hlua_hc->hc)) |
| 7506 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7507 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7508 | 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] | 7509 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7510 | return 0; |
| 7511 | } |
| 7512 | |
| 7513 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7514 | * Sends an HTTP HEAD request and wait for a response |
| 7515 | * |
| 7516 | * httpclient:head(url, headers, payload) |
| 7517 | */ |
| 7518 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7519 | { |
| 7520 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7521 | } |
| 7522 | |
| 7523 | /* |
| 7524 | * Send an HTTP GET request and wait for a response |
| 7525 | * |
| 7526 | * httpclient:get(url, headers, payload) |
| 7527 | */ |
| 7528 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7529 | { |
| 7530 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7531 | |
| 7532 | } |
| 7533 | |
| 7534 | /* |
| 7535 | * Sends an HTTP PUT request and wait for a response |
| 7536 | * |
| 7537 | * httpclient:put(url, headers, payload) |
| 7538 | */ |
| 7539 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7540 | { |
| 7541 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7542 | } |
| 7543 | |
| 7544 | /* |
| 7545 | * Send an HTTP POST request and wait for a response |
| 7546 | * |
| 7547 | * httpclient:post(url, headers, payload) |
| 7548 | */ |
| 7549 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7550 | { |
| 7551 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7552 | } |
| 7553 | |
| 7554 | |
| 7555 | /* |
| 7556 | * Sends an HTTP DELETE request and wait for a response |
| 7557 | * |
| 7558 | * httpclient:delete(url, headers, payload) |
| 7559 | */ |
| 7560 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7561 | { |
| 7562 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7563 | } |
| 7564 | |
| 7565 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7566 | * |
| 7567 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7568 | * Class TXN |
| 7569 | * |
| 7570 | * |
| 7571 | */ |
| 7572 | |
| 7573 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7574 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7575 | */ |
| 7576 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7577 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7578 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7579 | } |
| 7580 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7581 | __LJMP static int hlua_set_var(lua_State *L) |
| 7582 | { |
| 7583 | struct hlua_txn *htxn; |
| 7584 | const char *name; |
| 7585 | size_t len; |
| 7586 | struct sample smp; |
| 7587 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7588 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7589 | 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] | 7590 | |
| 7591 | /* It is useles to retrieve the stream, but this function |
| 7592 | * runs only in a stream context. |
| 7593 | */ |
| 7594 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7595 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7596 | |
| 7597 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7598 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7599 | hlua_lua2smp(L, 3, &smp); |
| 7600 | |
| 7601 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7602 | 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] | 7603 | |
| 7604 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7605 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7606 | else |
| 7607 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7608 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7609 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7610 | } |
| 7611 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7612 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7613 | { |
| 7614 | struct hlua_txn *htxn; |
| 7615 | const char *name; |
| 7616 | size_t len; |
| 7617 | struct sample smp; |
| 7618 | |
| 7619 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7620 | |
| 7621 | /* It is useles to retrieve the stream, but this function |
| 7622 | * runs only in a stream context. |
| 7623 | */ |
| 7624 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7625 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7626 | |
| 7627 | /* Unset the variable. */ |
| 7628 | 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] | 7629 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7630 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7631 | } |
| 7632 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7633 | __LJMP static int hlua_get_var(lua_State *L) |
| 7634 | { |
| 7635 | struct hlua_txn *htxn; |
| 7636 | const char *name; |
| 7637 | size_t len; |
| 7638 | struct sample smp; |
| 7639 | |
| 7640 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7641 | |
| 7642 | /* It is useles to retrieve the stream, but this function |
| 7643 | * runs only in a stream context. |
| 7644 | */ |
| 7645 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7646 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7647 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7648 | 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] | 7649 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7650 | lua_pushnil(L); |
| 7651 | return 1; |
| 7652 | } |
| 7653 | |
| 7654 | return hlua_smp2lua(L, &smp); |
| 7655 | } |
| 7656 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7657 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7658 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7659 | struct hlua *hlua; |
| 7660 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7661 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7662 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7663 | /* It is useles to retrieve the stream, but this function |
| 7664 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7665 | */ |
| 7666 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7667 | |
| 7668 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7669 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7670 | if (!hlua) |
| 7671 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7672 | |
| 7673 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7674 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7675 | |
| 7676 | /* Get and store new value. */ |
| 7677 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7678 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7679 | |
| 7680 | return 0; |
| 7681 | } |
| 7682 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7683 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7684 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7685 | struct hlua *hlua; |
| 7686 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7687 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7688 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7689 | /* It is useles to retrieve the stream, but this function |
| 7690 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7691 | */ |
| 7692 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7693 | |
| 7694 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7695 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7696 | if (!hlua) { |
| 7697 | lua_pushnil(L); |
| 7698 | return 1; |
| 7699 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7700 | |
| 7701 | /* Push configuration index in the stack. */ |
| 7702 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7703 | |
| 7704 | return 1; |
| 7705 | } |
| 7706 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7707 | /* Create stack entry containing a class TXN. This function |
| 7708 | * return 0 if the stack does not contains free slots, |
| 7709 | * otherwise it returns 1. |
| 7710 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7711 | 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] | 7712 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7713 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7714 | |
| 7715 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7716 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7717 | return 0; |
| 7718 | |
| 7719 | /* NOTE: The allocation never fails. The failure |
| 7720 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7721 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7722 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7723 | /* Create the object: obj[0] = userdata. */ |
| 7724 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7725 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7726 | lua_rawseti(L, -2, 0); |
| 7727 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7728 | htxn->s = s; |
| 7729 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7730 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7731 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7732 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7733 | /* Create the "f" field that contains a list of fetches. */ |
| 7734 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7735 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7736 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7737 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7738 | |
| 7739 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7740 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7741 | 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] | 7742 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7743 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7744 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7745 | /* Create the "c" field that contains a list of converters. */ |
| 7746 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7747 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7748 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7749 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7750 | |
| 7751 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7752 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7753 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7754 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7755 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7756 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7757 | /* Create the "req" field that contains the request channel object. */ |
| 7758 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7759 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7760 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7761 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7762 | |
| 7763 | /* Create the "res" field that contains the response channel object. */ |
| 7764 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7765 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7766 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7767 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7768 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7769 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7770 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7771 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7772 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7773 | return 0; |
| 7774 | } |
| 7775 | else |
| 7776 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7777 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7778 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7779 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7780 | /* HTTPMessage object are created when a lua TXN is created from |
| 7781 | * a filter context only |
| 7782 | */ |
| 7783 | |
| 7784 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7785 | lua_pushstring(L, "http_req"); |
| 7786 | if (p->mode == PR_MODE_HTTP) { |
| 7787 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7788 | return 0; |
| 7789 | } |
| 7790 | else |
| 7791 | lua_pushnil(L); |
| 7792 | lua_rawset(L, -3); |
| 7793 | |
| 7794 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7795 | lua_pushstring(L, "http_res"); |
| 7796 | if (p->mode == PR_MODE_HTTP) { |
| 7797 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7798 | return 0; |
| 7799 | } |
| 7800 | else |
| 7801 | lua_pushnil(L); |
| 7802 | lua_rawset(L, -3); |
| 7803 | } |
| 7804 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7805 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7806 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7807 | lua_setmetatable(L, -2); |
| 7808 | |
| 7809 | return 1; |
| 7810 | } |
| 7811 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7812 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7813 | { |
| 7814 | const char *msg; |
| 7815 | struct hlua_txn *htxn; |
| 7816 | |
| 7817 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7818 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7819 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7820 | |
| 7821 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7822 | return 0; |
| 7823 | } |
| 7824 | |
| 7825 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7826 | { |
| 7827 | int level; |
| 7828 | const char *msg; |
| 7829 | struct hlua_txn *htxn; |
| 7830 | |
| 7831 | MAY_LJMP(check_args(L, 3, "log")); |
| 7832 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7833 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7834 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7835 | |
| 7836 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7837 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7838 | |
| 7839 | hlua_sendlog(htxn->s->be, level, msg); |
| 7840 | return 0; |
| 7841 | } |
| 7842 | |
| 7843 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7844 | { |
| 7845 | const char *msg; |
| 7846 | struct hlua_txn *htxn; |
| 7847 | |
| 7848 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7849 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7850 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7851 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7852 | return 0; |
| 7853 | } |
| 7854 | |
| 7855 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7856 | { |
| 7857 | const char *msg; |
| 7858 | struct hlua_txn *htxn; |
| 7859 | |
| 7860 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7861 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7862 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7863 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7864 | return 0; |
| 7865 | } |
| 7866 | |
| 7867 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7868 | { |
| 7869 | const char *msg; |
| 7870 | struct hlua_txn *htxn; |
| 7871 | |
| 7872 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7873 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7874 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7875 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7876 | return 0; |
| 7877 | } |
| 7878 | |
| 7879 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7880 | { |
| 7881 | const char *msg; |
| 7882 | struct hlua_txn *htxn; |
| 7883 | |
| 7884 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7885 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7886 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7887 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7888 | return 0; |
| 7889 | } |
| 7890 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7891 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7892 | { |
| 7893 | struct hlua_txn *htxn; |
| 7894 | int ll; |
| 7895 | |
| 7896 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7897 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7898 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7899 | |
| 7900 | if (ll < 0 || ll > 7) |
| 7901 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7902 | |
| 7903 | htxn->s->logs.level = ll; |
| 7904 | return 0; |
| 7905 | } |
| 7906 | |
| 7907 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7908 | { |
| 7909 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7910 | int tos; |
| 7911 | |
| 7912 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7913 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7914 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7915 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7916 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7917 | return 0; |
| 7918 | } |
| 7919 | |
| 7920 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7921 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7922 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7923 | int mark; |
| 7924 | |
| 7925 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7926 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7927 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7928 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7929 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7930 | return 0; |
| 7931 | } |
| 7932 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7933 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7934 | { |
| 7935 | struct hlua_txn *htxn; |
| 7936 | |
| 7937 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7938 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7939 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7940 | return 0; |
| 7941 | } |
| 7942 | |
| 7943 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7944 | { |
| 7945 | struct hlua_txn *htxn; |
| 7946 | |
| 7947 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7948 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7949 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7950 | return 0; |
| 7951 | } |
| 7952 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7953 | /* 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] | 7954 | * 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] | 7955 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7956 | * error. The Reply must be on top of the stack. |
| 7957 | */ |
| 7958 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7959 | { |
| 7960 | struct htx *htx; |
| 7961 | struct htx_sl *sl; |
| 7962 | struct h1m h1m; |
| 7963 | const char *status, *reason, *body; |
| 7964 | size_t status_len, reason_len, body_len; |
| 7965 | int ret, code, flags; |
| 7966 | |
| 7967 | code = 200; |
| 7968 | status = "200"; |
| 7969 | status_len = 3; |
| 7970 | ret = lua_getfield(L, -1, "status"); |
| 7971 | if (ret == LUA_TNUMBER) { |
| 7972 | code = lua_tointeger(L, -1); |
| 7973 | status = lua_tolstring(L, -1, &status_len); |
| 7974 | } |
| 7975 | lua_pop(L, 1); |
| 7976 | |
| 7977 | reason = http_get_reason(code); |
| 7978 | reason_len = strlen(reason); |
| 7979 | ret = lua_getfield(L, -1, "reason"); |
| 7980 | if (ret == LUA_TSTRING) |
| 7981 | reason = lua_tolstring(L, -1, &reason_len); |
| 7982 | lua_pop(L, 1); |
| 7983 | |
| 7984 | body = NULL; |
| 7985 | body_len = 0; |
| 7986 | ret = lua_getfield(L, -1, "body"); |
| 7987 | if (ret == LUA_TSTRING) |
| 7988 | body = lua_tolstring(L, -1, &body_len); |
| 7989 | lua_pop(L, 1); |
| 7990 | |
| 7991 | /* Prepare the response before inserting the headers */ |
| 7992 | h1m_init_res(&h1m); |
| 7993 | htx = htx_from_buf(&s->res.buf); |
| 7994 | channel_htx_truncate(&s->res, htx); |
| 7995 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7996 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7997 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7998 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7999 | } |
| 8000 | else { |
| 8001 | flags = HTX_SL_F_IS_RESP; |
| 8002 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8003 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8004 | } |
| 8005 | if (!sl) |
| 8006 | goto fail; |
| 8007 | sl->info.res.status = code; |
| 8008 | |
| 8009 | /* Push in the stack the "headers" entry. */ |
| 8010 | ret = lua_getfield(L, -1, "headers"); |
| 8011 | if (ret != LUA_TTABLE) |
| 8012 | goto skip_headers; |
| 8013 | |
| 8014 | lua_pushnil(L); |
| 8015 | while (lua_next(L, -2) != 0) { |
| 8016 | struct ist name, value; |
| 8017 | const char *n, *v; |
| 8018 | size_t nlen, vlen; |
| 8019 | |
| 8020 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8021 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8022 | goto next_hdr; |
| 8023 | } |
| 8024 | |
| 8025 | n = lua_tolstring(L, -2, &nlen); |
| 8026 | name = ist2(n, nlen); |
| 8027 | if (isteqi(name, ist("content-length"))) { |
| 8028 | /* Always skip content-length header. It will be added |
| 8029 | * later with the correct len |
| 8030 | */ |
| 8031 | goto next_hdr; |
| 8032 | } |
| 8033 | |
| 8034 | /* Loop on header's values */ |
| 8035 | lua_pushnil(L); |
| 8036 | while (lua_next(L, -2)) { |
| 8037 | if (!lua_isstring(L, -1)) { |
| 8038 | /* Skip the value if it is not a string */ |
| 8039 | goto next_value; |
| 8040 | } |
| 8041 | |
| 8042 | v = lua_tolstring(L, -1, &vlen); |
| 8043 | value = ist2(v, vlen); |
| 8044 | |
| 8045 | if (isteqi(name, ist("transfer-encoding"))) |
| 8046 | h1_parse_xfer_enc_header(&h1m, value); |
| 8047 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8048 | goto fail; |
| 8049 | |
| 8050 | next_value: |
| 8051 | lua_pop(L, 1); |
| 8052 | } |
| 8053 | |
| 8054 | next_hdr: |
| 8055 | lua_pop(L, 1); |
| 8056 | } |
| 8057 | skip_headers: |
| 8058 | lua_pop(L, 1); |
| 8059 | |
| 8060 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8061 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8062 | const char *clen = ultoa(body_len); |
| 8063 | |
| 8064 | h1m.flags |= H1_MF_CLEN; |
| 8065 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8066 | goto fail; |
| 8067 | } |
| 8068 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8069 | h1m.flags |= H1_MF_XFER_LEN; |
| 8070 | |
| 8071 | /* Update HTX start-line flags */ |
| 8072 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8073 | flags |= HTX_SL_F_XFER_ENC; |
| 8074 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8075 | flags |= HTX_SL_F_XFER_LEN; |
| 8076 | if (h1m.flags & H1_MF_CHNK) |
| 8077 | flags |= HTX_SL_F_CHNK; |
| 8078 | else if (h1m.flags & H1_MF_CLEN) |
| 8079 | flags |= HTX_SL_F_CLEN; |
| 8080 | if (h1m.body_len == 0) |
| 8081 | flags |= HTX_SL_F_BODYLESS; |
| 8082 | } |
| 8083 | sl->flags |= flags; |
| 8084 | |
| 8085 | |
| 8086 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8087 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8088 | goto fail; |
| 8089 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8090 | htx->flags |= HTX_FL_EOM; |
| 8091 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8092 | /* Now, forward the response and terminate the transaction */ |
| 8093 | s->txn->status = code; |
| 8094 | htx_to_buf(htx, &s->res.buf); |
| 8095 | if (!http_forward_proxy_resp(s, 1)) |
| 8096 | goto fail; |
| 8097 | |
| 8098 | return 1; |
| 8099 | |
| 8100 | fail: |
| 8101 | channel_htx_truncate(&s->res, htx); |
| 8102 | return 0; |
| 8103 | } |
| 8104 | |
| 8105 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8106 | * processing is just aborted. Nothing is returned to the client and all |
| 8107 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8108 | * is forwarded to the client before terminating the transaction. On success, |
| 8109 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8110 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8111 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8112 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8113 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8114 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8115 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8116 | struct stream *s; |
| 8117 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8118 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8119 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8120 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8121 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8122 | * just end the execution of the current lua code. */ |
| 8123 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8124 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8125 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8126 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8127 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8128 | struct channel *req = &s->req; |
| 8129 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8130 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8131 | channel_auto_read(req); |
| 8132 | channel_abort(req); |
| 8133 | channel_auto_close(req); |
| 8134 | channel_erase(req); |
| 8135 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8136 | channel_auto_read(res); |
| 8137 | channel_auto_close(res); |
| 8138 | channel_shutr_now(res); |
| 8139 | |
| 8140 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8141 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8142 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8143 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8144 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8145 | /* No reply or invalid reply */ |
| 8146 | s->txn->status = 0; |
| 8147 | http_reply_and_close(s, 0, NULL); |
| 8148 | } |
| 8149 | else { |
| 8150 | /* Remove extra args to have the reply on top of the stack */ |
| 8151 | if (lua_gettop(L) > 2) |
| 8152 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8153 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8154 | if (!hlua_txn_forward_reply(L, s)) { |
| 8155 | if (!(s->flags & SF_ERR_MASK)) |
| 8156 | s->flags |= SF_ERR_PRXCOND; |
| 8157 | lua_pushinteger(L, ACT_RET_ERR); |
| 8158 | WILL_LJMP(hlua_done(L)); |
| 8159 | return 0; /* Never reached */ |
| 8160 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8161 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8162 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8163 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8164 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8165 | /* let's log the request time */ |
| 8166 | s->logs.tv_request = now; |
| 8167 | 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] | 8168 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8169 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8170 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8171 | done: |
| 8172 | if (!(s->flags & SF_ERR_MASK)) |
| 8173 | s->flags |= SF_ERR_LOCAL; |
| 8174 | if (!(s->flags & SF_FINST_MASK)) |
| 8175 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8176 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8177 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8178 | lua_pushinteger(L, -1); |
| 8179 | else |
| 8180 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8181 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8182 | return 0; |
| 8183 | } |
| 8184 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8185 | /* |
| 8186 | * |
| 8187 | * |
| 8188 | * Class REPLY |
| 8189 | * |
| 8190 | * |
| 8191 | */ |
| 8192 | |
| 8193 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8194 | * free slots, the function fails and returns 0; |
| 8195 | */ |
| 8196 | static int hlua_txn_reply_new(lua_State *L) |
| 8197 | { |
| 8198 | struct hlua_txn *htxn; |
| 8199 | const char *reason, *body = NULL; |
| 8200 | int ret, status; |
| 8201 | |
| 8202 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8203 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8204 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8205 | WILL_LJMP(lua_error(L)); |
| 8206 | } |
| 8207 | |
| 8208 | /* Default value */ |
| 8209 | status = 200; |
| 8210 | reason = http_get_reason(status); |
| 8211 | |
| 8212 | if (lua_istable(L, 2)) { |
| 8213 | /* load status and reason from the table argument at index 2 */ |
| 8214 | ret = lua_getfield(L, 2, "status"); |
| 8215 | if (ret == LUA_TNIL) |
| 8216 | goto reason; |
| 8217 | else if (ret != LUA_TNUMBER) { |
| 8218 | /* invalid status: ignore the reason */ |
| 8219 | goto body; |
| 8220 | } |
| 8221 | status = lua_tointeger(L, -1); |
| 8222 | |
| 8223 | reason: |
| 8224 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8225 | ret = lua_getfield(L, 2, "reason"); |
| 8226 | if (ret == LUA_TSTRING) |
| 8227 | reason = lua_tostring(L, -1); |
| 8228 | |
| 8229 | body: |
| 8230 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8231 | ret = lua_getfield(L, 2, "body"); |
| 8232 | if (ret == LUA_TSTRING) |
| 8233 | body = lua_tostring(L, -1); |
| 8234 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8235 | } |
| 8236 | |
| 8237 | /* Create the Reply table */ |
| 8238 | lua_newtable(L); |
| 8239 | |
| 8240 | /* Add status element */ |
| 8241 | lua_pushstring(L, "status"); |
| 8242 | lua_pushinteger(L, status); |
| 8243 | lua_settable(L, -3); |
| 8244 | |
| 8245 | /* Add reason element */ |
| 8246 | reason = http_get_reason(status); |
| 8247 | lua_pushstring(L, "reason"); |
| 8248 | lua_pushstring(L, reason); |
| 8249 | lua_settable(L, -3); |
| 8250 | |
| 8251 | /* Add body element, nil if undefined */ |
| 8252 | lua_pushstring(L, "body"); |
| 8253 | if (body) |
| 8254 | lua_pushstring(L, body); |
| 8255 | else |
| 8256 | lua_pushnil(L); |
| 8257 | lua_settable(L, -3); |
| 8258 | |
| 8259 | /* Add headers element */ |
| 8260 | lua_pushstring(L, "headers"); |
| 8261 | lua_newtable(L); |
| 8262 | |
| 8263 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8264 | if (lua_istable(L, 2)) { |
| 8265 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8266 | ret = lua_getfield(L, 2, "headers"); |
| 8267 | if (ret == LUA_TTABLE) { |
| 8268 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8269 | lua_pushnil(L); |
| 8270 | while (lua_next(L, -2) != 0) { |
| 8271 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8272 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8273 | /* invalid value type, skip it */ |
| 8274 | lua_pop(L, 1); |
| 8275 | continue; |
| 8276 | } |
| 8277 | |
| 8278 | |
| 8279 | /* Duplicate the key and swap it with the value. */ |
| 8280 | lua_pushvalue(L, -2); |
| 8281 | lua_insert(L, -2); |
| 8282 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8283 | |
| 8284 | lua_newtable(L); |
| 8285 | lua_insert(L, -2); |
| 8286 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8287 | |
| 8288 | if (lua_isstring(L, -1)) { |
| 8289 | /* push the value in the inner table */ |
| 8290 | lua_rawseti(L, -2, 1); |
| 8291 | } |
| 8292 | else { /* table */ |
| 8293 | lua_pushnil(L); |
| 8294 | while (lua_next(L, -2) != 0) { |
| 8295 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8296 | if (!lua_isstring(L, -1)) { |
| 8297 | /* invalid value type, skip it*/ |
| 8298 | lua_pop(L, 1); |
| 8299 | continue; |
| 8300 | } |
| 8301 | /* push the value in the inner table */ |
| 8302 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8303 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8304 | } |
| 8305 | lua_pop(L, 1); |
| 8306 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8307 | } |
| 8308 | |
| 8309 | /* push (k,v) on the stack in the headers table: |
| 8310 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8311 | */ |
| 8312 | lua_settable(L, -5); |
| 8313 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8314 | } |
| 8315 | } |
| 8316 | lua_pop(L, 1); |
| 8317 | } |
| 8318 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8319 | lua_settable(L, -3); |
| 8320 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8321 | |
| 8322 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8323 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8324 | lua_setmetatable(L, -2); |
| 8325 | return 1; |
| 8326 | } |
| 8327 | |
| 8328 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8329 | * provided, the default one corresponding to the status code is used. |
| 8330 | */ |
| 8331 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8332 | { |
| 8333 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8334 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8335 | |
| 8336 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8337 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8338 | |
| 8339 | if (status < 100 || status > 599) { |
| 8340 | lua_pushboolean(L, 0); |
| 8341 | return 1; |
| 8342 | } |
| 8343 | if (!reason) |
| 8344 | reason = http_get_reason(status); |
| 8345 | |
| 8346 | lua_pushinteger(L, status); |
| 8347 | lua_setfield(L, 1, "status"); |
| 8348 | |
| 8349 | lua_pushstring(L, reason); |
| 8350 | lua_setfield(L, 1, "reason"); |
| 8351 | |
| 8352 | lua_pushboolean(L, 1); |
| 8353 | return 1; |
| 8354 | } |
| 8355 | |
| 8356 | /* Add a header into the reply object. Each header name is associated to an |
| 8357 | * array of values in the "headers" table. If the header name is not found, a |
| 8358 | * new entry is created. |
| 8359 | */ |
| 8360 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8361 | { |
| 8362 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8363 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8364 | int ret; |
| 8365 | |
| 8366 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8367 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8368 | |
| 8369 | /* Push in the stack the "headers" entry. */ |
| 8370 | ret = lua_getfield(L, 1, "headers"); |
| 8371 | if (ret != LUA_TTABLE) { |
| 8372 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8373 | WILL_LJMP(lua_error(L)); |
| 8374 | } |
| 8375 | |
| 8376 | /* check if the header is already registered. If not, register it. */ |
| 8377 | ret = lua_getfield(L, -1, name); |
| 8378 | if (ret == LUA_TNIL) { |
| 8379 | /* Entry not found. */ |
| 8380 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8381 | |
| 8382 | /* Insert the new header name in the array in the top of the stack. |
| 8383 | * It left the new array in the top of the stack. |
| 8384 | */ |
| 8385 | lua_newtable(L); |
| 8386 | lua_pushstring(L, name); |
| 8387 | lua_pushvalue(L, -2); |
| 8388 | lua_settable(L, -4); |
| 8389 | } |
| 8390 | else if (ret != LUA_TTABLE) { |
| 8391 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8392 | WILL_LJMP(lua_error(L)); |
| 8393 | } |
| 8394 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8395 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8396 | * the header value as new entry. |
| 8397 | */ |
| 8398 | lua_pushstring(L, value); |
| 8399 | ret = lua_rawlen(L, -2); |
| 8400 | lua_rawseti(L, -2, ret + 1); |
| 8401 | |
| 8402 | lua_pushboolean(L, 1); |
| 8403 | return 1; |
| 8404 | } |
| 8405 | |
| 8406 | /* Remove all occurrences of a given header name. */ |
| 8407 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8408 | { |
| 8409 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8410 | int ret; |
| 8411 | |
| 8412 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8413 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8414 | |
| 8415 | /* Push in the stack the "headers" entry. */ |
| 8416 | ret = lua_getfield(L, 1, "headers"); |
| 8417 | if (ret != LUA_TTABLE) { |
| 8418 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8419 | WILL_LJMP(lua_error(L)); |
| 8420 | } |
| 8421 | |
| 8422 | lua_pushstring(L, name); |
| 8423 | lua_pushnil(L); |
| 8424 | lua_settable(L, -3); |
| 8425 | |
| 8426 | lua_pushboolean(L, 1); |
| 8427 | return 1; |
| 8428 | } |
| 8429 | |
| 8430 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8431 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8432 | { |
| 8433 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8434 | |
| 8435 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8436 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8437 | |
| 8438 | lua_pushstring(L, payload); |
| 8439 | lua_setfield(L, 1, "body"); |
| 8440 | |
| 8441 | lua_pushboolean(L, 1); |
| 8442 | return 1; |
| 8443 | } |
| 8444 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8445 | __LJMP static int hlua_log(lua_State *L) |
| 8446 | { |
| 8447 | int level; |
| 8448 | const char *msg; |
| 8449 | |
| 8450 | MAY_LJMP(check_args(L, 2, "log")); |
| 8451 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8452 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8453 | |
| 8454 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8455 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8456 | |
| 8457 | hlua_sendlog(NULL, level, msg); |
| 8458 | return 0; |
| 8459 | } |
| 8460 | |
| 8461 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8462 | { |
| 8463 | const char *msg; |
| 8464 | |
| 8465 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8466 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8467 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8468 | return 0; |
| 8469 | } |
| 8470 | |
| 8471 | __LJMP static int hlua_log_info(lua_State *L) |
| 8472 | { |
| 8473 | const char *msg; |
| 8474 | |
| 8475 | MAY_LJMP(check_args(L, 1, "info")); |
| 8476 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8477 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
| 8481 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8482 | { |
| 8483 | const char *msg; |
| 8484 | |
| 8485 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8486 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8487 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8488 | return 0; |
| 8489 | } |
| 8490 | |
| 8491 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8492 | { |
| 8493 | const char *msg; |
| 8494 | |
| 8495 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8496 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8497 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8498 | return 0; |
| 8499 | } |
| 8500 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8501 | __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] | 8502 | { |
| 8503 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8504 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8505 | 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] | 8506 | return 0; |
| 8507 | } |
| 8508 | |
| 8509 | __LJMP static int hlua_sleep(lua_State *L) |
| 8510 | { |
| 8511 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8512 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8513 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8514 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8515 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8516 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8517 | wakeup_ms = tick_add(now_ms, delay); |
| 8518 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8519 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8520 | 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] | 8521 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8522 | } |
| 8523 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8524 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8525 | { |
| 8526 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8527 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8528 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8529 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8530 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8531 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8532 | wakeup_ms = tick_add(now_ms, delay); |
| 8533 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8534 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8535 | 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] | 8536 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8537 | } |
| 8538 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8539 | /* This functionis an LUA binding. it permits to give back |
| 8540 | * the hand at the HAProxy scheduler. It is used when the |
| 8541 | * LUA processing consumes a lot of time. |
| 8542 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8543 | __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] | 8544 | { |
| 8545 | return 0; |
| 8546 | } |
| 8547 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8548 | __LJMP static int hlua_yield(lua_State *L) |
| 8549 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8550 | 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] | 8551 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8552 | } |
| 8553 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8554 | /* This function change the nice of the currently executed |
| 8555 | * task. It is used set low or high priority at the current |
| 8556 | * task. |
| 8557 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8558 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8559 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8560 | struct hlua *hlua; |
| 8561 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8562 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8563 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8564 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8565 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8566 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8567 | hlua = hlua_gethlua(L); |
| 8568 | |
| 8569 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8570 | if (!hlua || !hlua->task) |
| 8571 | return 0; |
| 8572 | |
| 8573 | if (nice < -1024) |
| 8574 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8575 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8576 | nice = 1024; |
| 8577 | |
| 8578 | hlua->task->nice = nice; |
| 8579 | return 0; |
| 8580 | } |
| 8581 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8582 | /* 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] | 8583 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8584 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8585 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8586 | * |
| 8587 | * Task wrapper are longjmp safe because the only one Lua code |
| 8588 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8589 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8590 | 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] | 8591 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8592 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8593 | enum hlua_exec status; |
| 8594 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8595 | if (task->tid < 0) |
| 8596 | task->tid = tid; |
| 8597 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8598 | /* If it is the first call to the task, we must initialize the |
| 8599 | * execution timeouts. |
| 8600 | */ |
| 8601 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8602 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8603 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8604 | /* Execute the Lua code. */ |
| 8605 | status = hlua_ctx_resume(hlua, 1); |
| 8606 | |
| 8607 | switch (status) { |
| 8608 | /* finished or yield */ |
| 8609 | case HLUA_E_OK: |
| 8610 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8611 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8612 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8613 | break; |
| 8614 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8615 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8616 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8617 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8618 | break; |
| 8619 | |
| 8620 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8621 | case HLUA_E_ETMOUT: |
| 8622 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8623 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8624 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8625 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8626 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8627 | case HLUA_E_ERR: |
| 8628 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8629 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8630 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8631 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8632 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8633 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8634 | break; |
| 8635 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8636 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8637 | } |
| 8638 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8639 | /* This function is an LUA binding that register LUA function to be |
| 8640 | * executed after the HAProxy configuration parsing and before the |
| 8641 | * HAProxy scheduler starts. This function expect only one LUA |
| 8642 | * argument that is a function. This function returns nothing, but |
| 8643 | * throws if an error is encountered. |
| 8644 | */ |
| 8645 | __LJMP static int hlua_register_init(lua_State *L) |
| 8646 | { |
| 8647 | struct hlua_init_function *init; |
| 8648 | int ref; |
| 8649 | |
| 8650 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8651 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8652 | if (hlua_gethlua(L)) { |
| 8653 | /* runtime processing */ |
| 8654 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 8655 | } |
| 8656 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8657 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8658 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8659 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8660 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8661 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8662 | |
| 8663 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8664 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8665 | return 0; |
| 8666 | } |
| 8667 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8668 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8669 | * executed in parallel of the main HAroxy activity. The task is |
| 8670 | * created and it is set in the HAProxy scheduler. It can be called |
| 8671 | * from the "init" section, "post init" or during the runtime. |
| 8672 | * |
| 8673 | * Lua prototype: |
| 8674 | * |
| 8675 | * <none> core.register_task(<function>) |
| 8676 | */ |
| 8677 | static int hlua_register_task(lua_State *L) |
| 8678 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8679 | struct hlua *hlua = NULL; |
| 8680 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8681 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8682 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8683 | |
| 8684 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8685 | |
| 8686 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8687 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8688 | /* Get the reference state. If the reference is NULL, L is the master |
| 8689 | * state, otherwise hlua->T is. |
| 8690 | */ |
| 8691 | hlua = hlua_gethlua(L); |
| 8692 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8693 | /* we are in runtime processing */ |
| 8694 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8695 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8696 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8697 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8698 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8699 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8700 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8701 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8702 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8703 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8704 | /* We are in the common lua state, execute the task anywhere, |
| 8705 | * otherwise, inherit the current thread identifier |
| 8706 | */ |
| 8707 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8708 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8709 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8710 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8711 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8712 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8713 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8714 | task->context = hlua; |
| 8715 | task->process = hlua_process_task; |
| 8716 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8717 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8718 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8719 | |
| 8720 | /* Restore the function in the stack. */ |
| 8721 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 8722 | /* function ref not needed anymore since it was pushed to the substack */ |
| 8723 | hlua_unref(L, ref); |
| 8724 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8725 | hlua->nargs = 0; |
| 8726 | |
| 8727 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8728 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8729 | |
| 8730 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8731 | |
| 8732 | alloc_error: |
| 8733 | task_destroy(task); |
| 8734 | hlua_ctx_destroy(hlua); |
| 8735 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8736 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8737 | } |
| 8738 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8739 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8740 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8741 | * resume converters. |
| 8742 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8743 | 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] | 8744 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8745 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8746 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8747 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8748 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8749 | if (!stream) |
| 8750 | return 0; |
| 8751 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8752 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8753 | * Lua context can be not initialized. This behavior |
| 8754 | * permits to save performances because a systematic |
| 8755 | * Lua initialization cause 5% performances loss. |
| 8756 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8757 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8758 | struct hlua *hlua; |
| 8759 | |
| 8760 | hlua = pool_alloc(pool_head_hlua); |
| 8761 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8762 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8763 | return 0; |
| 8764 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8765 | HLUA_INIT(hlua); |
| 8766 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8767 | 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] | 8768 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8769 | return 0; |
| 8770 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8771 | } |
| 8772 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8773 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8774 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8775 | |
| 8776 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8777 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8778 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8779 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8780 | else |
| 8781 | error = "critical error"; |
| 8782 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8783 | return 0; |
| 8784 | } |
| 8785 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8786 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8787 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8788 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8789 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8790 | return 0; |
| 8791 | } |
| 8792 | |
| 8793 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8794 | 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] | 8795 | |
| 8796 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8797 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8798 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8799 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8800 | return 0; |
| 8801 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8802 | hlua_smp2lua(stream->hlua->T, smp); |
| 8803 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8804 | |
| 8805 | /* push keywords in the stack. */ |
| 8806 | if (arg_p) { |
| 8807 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8808 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8809 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8810 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8811 | return 0; |
| 8812 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8813 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8814 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8815 | } |
| 8816 | } |
| 8817 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8818 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8819 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8820 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8821 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8822 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8823 | } |
| 8824 | |
| 8825 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8826 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8827 | /* finished. */ |
| 8828 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8829 | /* If the stack is empty, the function fails. */ |
| 8830 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8831 | return 0; |
| 8832 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8833 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8834 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8835 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8836 | return 1; |
| 8837 | |
| 8838 | /* yield. */ |
| 8839 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8840 | 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] | 8841 | return 0; |
| 8842 | |
| 8843 | /* finished with error. */ |
| 8844 | case HLUA_E_ERRMSG: |
| 8845 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8846 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8847 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8848 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8849 | return 0; |
| 8850 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8851 | case HLUA_E_ETMOUT: |
| 8852 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8853 | return 0; |
| 8854 | |
| 8855 | case HLUA_E_NOMEM: |
| 8856 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8857 | return 0; |
| 8858 | |
| 8859 | case HLUA_E_YIELD: |
| 8860 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8861 | return 0; |
| 8862 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8863 | case HLUA_E_ERR: |
| 8864 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8865 | 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] | 8866 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8867 | |
| 8868 | default: |
| 8869 | return 0; |
| 8870 | } |
| 8871 | } |
| 8872 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8873 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8874 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8875 | * resume sample-fetches. This function will be called by the sample |
| 8876 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8877 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8878 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8879 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8880 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8881 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8882 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8883 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8884 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8885 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8886 | if (!stream) |
| 8887 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8888 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8889 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8890 | * Lua context can be not initialized. This behavior |
| 8891 | * permits to save performances because a systematic |
| 8892 | * Lua initialization cause 5% performances loss. |
| 8893 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8894 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8895 | struct hlua *hlua; |
| 8896 | |
| 8897 | hlua = pool_alloc(pool_head_hlua); |
| 8898 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8899 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8900 | return 0; |
| 8901 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8902 | hlua->T = NULL; |
| 8903 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8904 | 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] | 8905 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8906 | return 0; |
| 8907 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8908 | } |
| 8909 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8910 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8911 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8912 | |
| 8913 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8914 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8915 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8916 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8917 | else |
| 8918 | error = "critical error"; |
| 8919 | 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] | 8920 | return 0; |
| 8921 | } |
| 8922 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8923 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8924 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8925 | 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] | 8926 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8927 | return 0; |
| 8928 | } |
| 8929 | |
| 8930 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8931 | 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] | 8932 | |
| 8933 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8934 | 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] | 8935 | 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] | 8936 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8937 | return 0; |
| 8938 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8939 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8940 | |
| 8941 | /* push keywords in the stack. */ |
| 8942 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8943 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8944 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8945 | 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] | 8946 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8947 | return 0; |
| 8948 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8949 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8950 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8951 | } |
| 8952 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8953 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8954 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8955 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8956 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8957 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8958 | } |
| 8959 | |
| 8960 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8961 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8962 | /* finished. */ |
| 8963 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8964 | /* If the stack is empty, the function fails. */ |
| 8965 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8966 | return 0; |
| 8967 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8968 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8969 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8970 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8971 | |
| 8972 | /* Set the end of execution flag. */ |
| 8973 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8974 | return 1; |
| 8975 | |
| 8976 | /* yield. */ |
| 8977 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8978 | 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] | 8979 | return 0; |
| 8980 | |
| 8981 | /* finished with error. */ |
| 8982 | case HLUA_E_ERRMSG: |
| 8983 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8984 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8985 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8986 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8987 | return 0; |
| 8988 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8989 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8990 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8991 | return 0; |
| 8992 | |
| 8993 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8994 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8995 | return 0; |
| 8996 | |
| 8997 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8998 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8999 | return 0; |
| 9000 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9001 | case HLUA_E_ERR: |
| 9002 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9003 | 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] | 9004 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9005 | |
| 9006 | default: |
| 9007 | return 0; |
| 9008 | } |
| 9009 | } |
| 9010 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9011 | /* This function is an LUA binding used for registering |
| 9012 | * "sample-conv" functions. It expects a converter name used |
| 9013 | * in the haproxy configuration file, and an LUA function. |
| 9014 | */ |
| 9015 | __LJMP static int hlua_register_converters(lua_State *L) |
| 9016 | { |
| 9017 | struct sample_conv_kw_list *sck; |
| 9018 | const char *name; |
| 9019 | int ref; |
| 9020 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9021 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9022 | struct sample_conv *sc; |
| 9023 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9024 | |
| 9025 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 9026 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9027 | if (hlua_gethlua(L)) { |
| 9028 | /* runtime processing */ |
| 9029 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 9030 | } |
| 9031 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9032 | /* First argument : converter name. */ |
| 9033 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9034 | |
| 9035 | /* Second argument : lua function. */ |
| 9036 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9037 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9038 | /* Check if the converter is already registered */ |
| 9039 | trash = get_trash_chunk(); |
| 9040 | chunk_printf(trash, "lua.%s", name); |
| 9041 | sc = find_sample_conv(trash->area, trash->data); |
| 9042 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9043 | fcn = sc->private; |
| 9044 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9045 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 9046 | "This will become a hard error in version 2.5.\n", name); |
| 9047 | } |
| 9048 | fcn->function_ref[hlua_state_id] = ref; |
| 9049 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9050 | } |
| 9051 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9052 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9053 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9054 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9055 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9056 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9057 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9058 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9059 | |
| 9060 | /* Fill fcn. */ |
| 9061 | fcn->name = strdup(name); |
| 9062 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9063 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9064 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9065 | |
| 9066 | /* List head */ |
| 9067 | sck->list.n = sck->list.p = NULL; |
| 9068 | |
| 9069 | /* converter keyword. */ |
| 9070 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9071 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9072 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9073 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9074 | |
| 9075 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9076 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9077 | 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] | 9078 | sck->kw[0].val_args = NULL; |
| 9079 | sck->kw[0].in_type = SMP_T_STR; |
| 9080 | sck->kw[0].out_type = SMP_T_STR; |
| 9081 | sck->kw[0].private = fcn; |
| 9082 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9083 | /* Register this new converter */ |
| 9084 | sample_register_convs(sck); |
| 9085 | |
| 9086 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9087 | |
| 9088 | alloc_error: |
| 9089 | release_hlua_function(fcn); |
| 9090 | ha_free(&sck); |
| 9091 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9092 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9093 | } |
| 9094 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9095 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9096 | * "sample-fetch" functions. It expects a converter name used |
| 9097 | * in the haproxy configuration file, and an LUA function. |
| 9098 | */ |
| 9099 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9100 | { |
| 9101 | const char *name; |
| 9102 | int ref; |
| 9103 | int len; |
| 9104 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9105 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9106 | struct sample_fetch *sf; |
| 9107 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9108 | |
| 9109 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9110 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9111 | if (hlua_gethlua(L)) { |
| 9112 | /* runtime processing */ |
| 9113 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 9114 | } |
| 9115 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9116 | /* First argument : sample-fetch name. */ |
| 9117 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9118 | |
| 9119 | /* Second argument : lua function. */ |
| 9120 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9121 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9122 | /* Check if the sample-fetch is already registered */ |
| 9123 | trash = get_trash_chunk(); |
| 9124 | chunk_printf(trash, "lua.%s", name); |
| 9125 | sf = find_sample_fetch(trash->area, trash->data); |
| 9126 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9127 | fcn = sf->private; |
| 9128 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9129 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9130 | "This will become a hard error in version 2.5.\n", name); |
| 9131 | } |
| 9132 | fcn->function_ref[hlua_state_id] = ref; |
| 9133 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9134 | } |
| 9135 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9136 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9137 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9138 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9139 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9140 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9141 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9142 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9143 | |
| 9144 | /* Fill fcn. */ |
| 9145 | fcn->name = strdup(name); |
| 9146 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9147 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9148 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9149 | |
| 9150 | /* List head */ |
| 9151 | sfk->list.n = sfk->list.p = NULL; |
| 9152 | |
| 9153 | /* sample-fetch keyword. */ |
| 9154 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9155 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9156 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9157 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9158 | |
| 9159 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9160 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9161 | 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] | 9162 | sfk->kw[0].val_args = NULL; |
| 9163 | sfk->kw[0].out_type = SMP_T_STR; |
| 9164 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9165 | sfk->kw[0].val = 0; |
| 9166 | sfk->kw[0].private = fcn; |
| 9167 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9168 | /* Register this new fetch. */ |
| 9169 | sample_register_fetches(sfk); |
| 9170 | |
| 9171 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9172 | |
| 9173 | alloc_error: |
| 9174 | release_hlua_function(fcn); |
| 9175 | ha_free(&sfk); |
| 9176 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9177 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9178 | } |
| 9179 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9180 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9181 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9182 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9183 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9184 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9185 | unsigned int delay; |
| 9186 | unsigned int wakeup_ms; |
| 9187 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9188 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9189 | hlua = hlua_gethlua(L); |
| 9190 | if (!hlua) { |
| 9191 | return 0; |
| 9192 | } |
| 9193 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9194 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9195 | |
| 9196 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9197 | wakeup_ms = tick_add(now_ms, delay); |
| 9198 | hlua->wake_time = wakeup_ms; |
| 9199 | return 0; |
| 9200 | } |
| 9201 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9202 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9203 | * wrapper during the initialisation period. This function may return any |
| 9204 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9205 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9206 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9207 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9208 | 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] | 9209 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9210 | { |
| 9211 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9212 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9213 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9214 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9215 | |
| 9216 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9217 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9218 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9219 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9220 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9221 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9222 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9223 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9224 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9225 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9226 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9227 | * Lua context can be not initialized. This behavior |
| 9228 | * permits to save performances because a systematic |
| 9229 | * Lua initialization cause 5% performances loss. |
| 9230 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9231 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9232 | struct hlua *hlua; |
| 9233 | |
| 9234 | hlua = pool_alloc(pool_head_hlua); |
| 9235 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9236 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9237 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9238 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9239 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9240 | HLUA_INIT(hlua); |
| 9241 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9242 | 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] | 9243 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9244 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9245 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9246 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9247 | } |
| 9248 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9249 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9250 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9251 | |
| 9252 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9253 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9254 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9255 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9256 | else |
| 9257 | error = "critical error"; |
| 9258 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9259 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9260 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9261 | } |
| 9262 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9263 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9264 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9265 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9266 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9267 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9268 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9269 | } |
| 9270 | |
| 9271 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9272 | 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] | 9273 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9274 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9275 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9276 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9277 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9278 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9279 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9280 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9281 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9282 | |
| 9283 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9284 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9285 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9286 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9287 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9288 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9289 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9290 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9291 | lua_pushstring(s->hlua->T, *arg); |
| 9292 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9293 | } |
| 9294 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9295 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9296 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9297 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9298 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9299 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9300 | } |
| 9301 | |
| 9302 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9303 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9304 | /* finished. */ |
| 9305 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9306 | /* Catch the return value */ |
| 9307 | if (lua_gettop(s->hlua->T) > 0) |
| 9308 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9309 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9310 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9311 | if (act_ret == ACT_RET_YIELD) { |
| 9312 | if (flags & ACT_OPT_FINAL) |
| 9313 | goto err_yield; |
| 9314 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9315 | if (dir == SMP_OPT_DIR_REQ) |
| 9316 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9317 | s->hlua->wake_time); |
| 9318 | else |
| 9319 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9320 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9321 | } |
| 9322 | goto end; |
| 9323 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9324 | /* yield. */ |
| 9325 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9326 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9327 | if (dir == SMP_OPT_DIR_REQ) |
| 9328 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9329 | s->hlua->wake_time); |
| 9330 | else |
| 9331 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9332 | s->hlua->wake_time); |
| 9333 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9334 | /* Some actions can be wake up when a "write" event |
| 9335 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9336 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9337 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9338 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9339 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9340 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9341 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9342 | act_ret = ACT_RET_YIELD; |
| 9343 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9344 | |
| 9345 | /* finished with error. */ |
| 9346 | case HLUA_E_ERRMSG: |
| 9347 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9348 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9349 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9350 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9351 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9352 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9353 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9354 | 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] | 9355 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9356 | |
| 9357 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9358 | 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] | 9359 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9360 | |
| 9361 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9362 | err_yield: |
| 9363 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9364 | 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] | 9365 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9366 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9367 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9368 | case HLUA_E_ERR: |
| 9369 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9370 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9371 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9372 | |
| 9373 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9374 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9375 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9376 | |
| 9377 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9378 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9379 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9380 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9381 | } |
| 9382 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9383 | 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] | 9384 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9385 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9386 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9387 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9388 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9389 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9390 | } |
| 9391 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9392 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9393 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9394 | 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] | 9395 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9396 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9397 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9398 | struct task *task; |
| 9399 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9400 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9401 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9402 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9403 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9404 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9405 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9406 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9407 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9408 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9409 | tcp_ctx->hlua = hlua; |
| 9410 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9411 | |
| 9412 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9413 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9414 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9415 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9416 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9417 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9418 | } |
| 9419 | task->nice = 0; |
| 9420 | task->context = ctx; |
| 9421 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9422 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9423 | |
| 9424 | /* In the execution wrappers linked with a stream, the |
| 9425 | * Lua context can be not initialized. This behavior |
| 9426 | * permits to save performances because a systematic |
| 9427 | * Lua initialization cause 5% performances loss. |
| 9428 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9429 | 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] | 9430 | 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] | 9431 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9432 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9433 | } |
| 9434 | |
| 9435 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9436 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9437 | |
| 9438 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9439 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9440 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9441 | error = lua_tostring(hlua->T, -1); |
| 9442 | else |
| 9443 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9444 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9445 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9446 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9447 | } |
| 9448 | |
| 9449 | /* Check stack available size. */ |
| 9450 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9451 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9452 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9453 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9454 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9455 | } |
| 9456 | |
| 9457 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9458 | 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] | 9459 | |
| 9460 | /* Create and and push object stream in the stack. */ |
| 9461 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9462 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9463 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9464 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9465 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9466 | } |
| 9467 | hlua->nargs = 1; |
| 9468 | |
| 9469 | /* push keywords in the stack. */ |
| 9470 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9471 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9472 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9473 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9474 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9475 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9476 | } |
| 9477 | lua_pushstring(hlua->T, *arg); |
| 9478 | hlua->nargs++; |
| 9479 | } |
| 9480 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9481 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9482 | |
| 9483 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9484 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9485 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9486 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9487 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9488 | } |
| 9489 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9490 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9491 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9492 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9493 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9494 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9495 | struct act_rule *rule = ctx->rule; |
| 9496 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9497 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9498 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9499 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9500 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9501 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9502 | /* The applet execution is already done. */ |
| 9503 | if (tcp_ctx->flags & APPLET_DONE) |
| 9504 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9505 | |
| 9506 | /* Execute the function. */ |
| 9507 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9508 | /* finished. */ |
| 9509 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9510 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9511 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 9512 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9513 | |
| 9514 | /* yield. */ |
| 9515 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9516 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9517 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9518 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9519 | |
| 9520 | /* finished with error. */ |
| 9521 | case HLUA_E_ERRMSG: |
| 9522 | /* Display log. */ |
| 9523 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9524 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9525 | lua_pop(hlua->T, 1); |
| 9526 | goto error; |
| 9527 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9528 | case HLUA_E_ETMOUT: |
| 9529 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9530 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9531 | goto error; |
| 9532 | |
| 9533 | case HLUA_E_NOMEM: |
| 9534 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9535 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9536 | goto error; |
| 9537 | |
| 9538 | case HLUA_E_YIELD: /* unexpected */ |
| 9539 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9540 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9541 | goto error; |
| 9542 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9543 | case HLUA_E_ERR: |
| 9544 | /* Display log. */ |
| 9545 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9546 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9547 | goto error; |
| 9548 | |
| 9549 | default: |
| 9550 | goto error; |
| 9551 | } |
| 9552 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9553 | out: |
| 9554 | /* eat the whole request */ |
| 9555 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 9556 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9557 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9558 | error: |
| 9559 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9560 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9561 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9562 | } |
| 9563 | |
| 9564 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9565 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9566 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9567 | |
| 9568 | task_destroy(tcp_ctx->task); |
| 9569 | tcp_ctx->task = NULL; |
| 9570 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9571 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9572 | } |
| 9573 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9574 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9575 | * 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] | 9576 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9577 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9578 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9579 | 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] | 9580 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9581 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9582 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9583 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9584 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9585 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9586 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9587 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9588 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9589 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9590 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9591 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9592 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9593 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9594 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9595 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9596 | http_ctx->hlua = hlua; |
| 9597 | http_ctx->left_bytes = -1; |
| 9598 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9599 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9600 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9601 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9602 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9603 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9604 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9605 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9606 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9607 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9608 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9609 | } |
| 9610 | task->nice = 0; |
| 9611 | task->context = ctx; |
| 9612 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9613 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9614 | |
| 9615 | /* In the execution wrappers linked with a stream, the |
| 9616 | * Lua context can be not initialized. This behavior |
| 9617 | * permits to save performances because a systematic |
| 9618 | * Lua initialization cause 5% performances loss. |
| 9619 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9620 | 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] | 9621 | 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] | 9622 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9623 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9624 | } |
| 9625 | |
| 9626 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9627 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9628 | |
| 9629 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9630 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9631 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9632 | error = lua_tostring(hlua->T, -1); |
| 9633 | else |
| 9634 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9635 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9636 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9637 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9638 | } |
| 9639 | |
| 9640 | /* Check stack available size. */ |
| 9641 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9642 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9643 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9644 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9645 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9646 | } |
| 9647 | |
| 9648 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9649 | 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] | 9650 | |
| 9651 | /* Create and and push object stream in the stack. */ |
| 9652 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9653 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9654 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9655 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9656 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9657 | } |
| 9658 | hlua->nargs = 1; |
| 9659 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9660 | /* push keywords in the stack. */ |
| 9661 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9662 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9663 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9664 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9665 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9666 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9667 | } |
| 9668 | lua_pushstring(hlua->T, *arg); |
| 9669 | hlua->nargs++; |
| 9670 | } |
| 9671 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9672 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9673 | |
| 9674 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9675 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9676 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9677 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9678 | } |
| 9679 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9680 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9681 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9682 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9683 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9684 | struct stream *strm = __sc_strm(sc); |
| 9685 | struct channel *req = sc_oc(sc); |
| 9686 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9687 | struct act_rule *rule = ctx->rule; |
| 9688 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9689 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9690 | struct htx *req_htx, *res_htx; |
| 9691 | |
| 9692 | res_htx = htx_from_buf(&res->buf); |
| 9693 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9694 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9695 | goto out; |
| 9696 | |
| 9697 | /* The applet execution is already done. */ |
| 9698 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9699 | goto out; |
| 9700 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9701 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9702 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9703 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9704 | goto out; |
| 9705 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9706 | |
| 9707 | /* Set the currently running flag. */ |
| 9708 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9709 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9710 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9711 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9712 | goto out; |
| 9713 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9714 | } |
| 9715 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9716 | /* Execute the function. */ |
| 9717 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9718 | /* finished. */ |
| 9719 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9720 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9721 | break; |
| 9722 | |
| 9723 | /* yield. */ |
| 9724 | case HLUA_E_AGAIN: |
| 9725 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9726 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9727 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9728 | |
| 9729 | /* finished with error. */ |
| 9730 | case HLUA_E_ERRMSG: |
| 9731 | /* Display log. */ |
| 9732 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9733 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9734 | lua_pop(hlua->T, 1); |
| 9735 | goto error; |
| 9736 | |
| 9737 | case HLUA_E_ETMOUT: |
| 9738 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9739 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9740 | goto error; |
| 9741 | |
| 9742 | case HLUA_E_NOMEM: |
| 9743 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9744 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9745 | goto error; |
| 9746 | |
| 9747 | case HLUA_E_YIELD: /* unexpected */ |
| 9748 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9749 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9750 | goto error; |
| 9751 | |
| 9752 | case HLUA_E_ERR: |
| 9753 | /* Display log. */ |
| 9754 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9755 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9756 | goto error; |
| 9757 | |
| 9758 | default: |
| 9759 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9760 | } |
| 9761 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9762 | if (http_ctx->flags & APPLET_DONE) { |
| 9763 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9764 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9765 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9766 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9767 | goto error; |
| 9768 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9769 | /* no more data are expected. If the response buffer is empty |
| 9770 | * for a chunked message, be sure to add something (EOT block in |
| 9771 | * this case) to have something to send. It is important to be |
| 9772 | * sure the EOM flags will be handled by the endpoint. |
| 9773 | */ |
| 9774 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9775 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9776 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9777 | goto out; |
| 9778 | } |
| 9779 | channel_add_input(res, 1); |
| 9780 | } |
| 9781 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9782 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9783 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9784 | strm->txn->status = http_ctx->status; |
| 9785 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9786 | } |
| 9787 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9788 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9789 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9790 | /* eat the whole request */ |
| 9791 | if (co_data(req)) { |
| 9792 | req_htx = htx_from_buf(&req->buf); |
| 9793 | co_htx_skip(req, req_htx, co_data(req)); |
| 9794 | htx_to_buf(req_htx, &req->buf); |
| 9795 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9796 | return; |
| 9797 | |
| 9798 | error: |
| 9799 | |
| 9800 | /* If we are in HTTP mode, and we are not send any |
| 9801 | * data, return a 500 server error in best effort: |
| 9802 | * if there is no room available in the buffer, |
| 9803 | * just close the connection. |
| 9804 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9805 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9806 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9807 | |
| 9808 | channel_erase(res); |
| 9809 | res->buf.data = b_data(err); |
| 9810 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9811 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9812 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9813 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9814 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9815 | else |
| 9816 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 9817 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9818 | if (!(strm->flags & SF_ERR_MASK)) |
| 9819 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9820 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9821 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9822 | } |
| 9823 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9824 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9825 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9826 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9827 | |
| 9828 | task_destroy(http_ctx->task); |
| 9829 | http_ctx->task = NULL; |
| 9830 | hlua_ctx_destroy(http_ctx->hlua); |
| 9831 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9832 | } |
| 9833 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9834 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9835 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9836 | * |
| 9837 | * This function can fail with an abort() due to an Lua critical error. |
| 9838 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9839 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9840 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9841 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9842 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9843 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9844 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9845 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9846 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9847 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9848 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9849 | if (!rule->arg.hlua_rule) { |
| 9850 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9851 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9852 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9853 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9854 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9855 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9856 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9857 | if (!rule->arg.hlua_rule->args) { |
| 9858 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9859 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9860 | } |
| 9861 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9862 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9863 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9864 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9865 | /* Expect some arguments */ |
| 9866 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9867 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9868 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9869 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9870 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9871 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9872 | if (!rule->arg.hlua_rule->args[i]) { |
| 9873 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9874 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9875 | } |
| 9876 | (*cur_arg)++; |
| 9877 | } |
| 9878 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9879 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9880 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9881 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9882 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9883 | |
| 9884 | error: |
| 9885 | if (rule->arg.hlua_rule) { |
| 9886 | if (rule->arg.hlua_rule->args) { |
| 9887 | for (i = 0; i < fcn->nargs; i++) |
| 9888 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9889 | ha_free(&rule->arg.hlua_rule->args); |
| 9890 | } |
| 9891 | ha_free(&rule->arg.hlua_rule); |
| 9892 | } |
| 9893 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9894 | } |
| 9895 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9896 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9897 | struct act_rule *rule, char **err) |
| 9898 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9899 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9900 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9901 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9902 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9903 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9904 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9905 | * the call of this analyzer. |
| 9906 | */ |
| 9907 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9908 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9909 | return ACT_RET_PRS_ERR; |
| 9910 | } |
| 9911 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9912 | /* Memory for the rule. */ |
| 9913 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9914 | if (!rule->arg.hlua_rule) { |
| 9915 | memprintf(err, "out of memory error"); |
| 9916 | return ACT_RET_PRS_ERR; |
| 9917 | } |
| 9918 | |
| 9919 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9920 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9921 | |
| 9922 | /* TODO: later accept arguments. */ |
| 9923 | rule->arg.hlua_rule->args = NULL; |
| 9924 | |
| 9925 | /* Add applet pointer in the rule. */ |
| 9926 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9927 | rule->applet.name = fcn->name; |
| 9928 | rule->applet.init = hlua_applet_http_init; |
| 9929 | rule->applet.fct = hlua_applet_http_fct; |
| 9930 | rule->applet.release = hlua_applet_http_release; |
| 9931 | rule->applet.timeout = hlua_timeout_applet; |
| 9932 | |
| 9933 | return ACT_RET_PRS_OK; |
| 9934 | } |
| 9935 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9936 | /* This function is an LUA binding used for registering |
| 9937 | * "sample-conv" functions. It expects a converter name used |
| 9938 | * in the haproxy configuration file, and an LUA function. |
| 9939 | */ |
| 9940 | __LJMP static int hlua_register_action(lua_State *L) |
| 9941 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9942 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9943 | const char *name; |
| 9944 | int ref; |
| 9945 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9946 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9947 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9948 | struct buffer *trash; |
| 9949 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9950 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9951 | /* Initialise the number of expected arguments at 0. */ |
| 9952 | nargs = 0; |
| 9953 | |
| 9954 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9955 | 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] | 9956 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9957 | if (hlua_gethlua(L)) { |
| 9958 | /* runtime processing */ |
| 9959 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 9960 | } |
| 9961 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9962 | /* First argument : converter name. */ |
| 9963 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9964 | |
| 9965 | /* Second argument : environment. */ |
| 9966 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9967 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9968 | |
| 9969 | /* Third argument : lua function. */ |
| 9970 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9971 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9972 | /* 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] | 9973 | if (lua_gettop(L) >= 4) |
| 9974 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9975 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9976 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9977 | lua_pushnil(L); |
| 9978 | while (lua_next(L, 2) != 0) { |
| 9979 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9980 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9981 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9982 | /* Check if action exists */ |
| 9983 | trash = get_trash_chunk(); |
| 9984 | chunk_printf(trash, "lua.%s", name); |
| 9985 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9986 | akw = tcp_req_cont_action(trash->area); |
| 9987 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9988 | akw = tcp_res_cont_action(trash->area); |
| 9989 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9990 | akw = action_http_req_custom(trash->area); |
| 9991 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9992 | akw = action_http_res_custom(trash->area); |
| 9993 | } else { |
| 9994 | akw = NULL; |
| 9995 | } |
| 9996 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9997 | fcn = akw->private; |
| 9998 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9999 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 10000 | "This will become a hard error in version 2.5.\n", name); |
| 10001 | } |
| 10002 | fcn->function_ref[hlua_state_id] = ref; |
| 10003 | |
| 10004 | /* pop the environment string. */ |
| 10005 | lua_pop(L, 1); |
| 10006 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10007 | } |
| 10008 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10009 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 10010 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10011 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10012 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10013 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10014 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10015 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10016 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10017 | |
| 10018 | /* Fill fcn. */ |
| 10019 | fcn->name = strdup(name); |
| 10020 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10021 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10022 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10023 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10024 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10025 | fcn->nargs = nargs; |
| 10026 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10027 | /* List head */ |
| 10028 | akl->list.n = akl->list.p = NULL; |
| 10029 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10030 | /* action keyword. */ |
| 10031 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10032 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10033 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10034 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10035 | |
| 10036 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10037 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10038 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10039 | akl->kw[0].private = fcn; |
| 10040 | akl->kw[0].parse = action_register_lua; |
| 10041 | |
| 10042 | /* select the action registering point. */ |
| 10043 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 10044 | tcp_req_cont_keywords_register(akl); |
| 10045 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 10046 | tcp_res_cont_keywords_register(akl); |
| 10047 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 10048 | http_req_keywords_register(akl); |
| 10049 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 10050 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10051 | else { |
| 10052 | release_hlua_function(fcn); |
| 10053 | if (akl) |
| 10054 | ha_free((char **)&(akl->kw[0].kw)); |
| 10055 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10056 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10057 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 10058 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10059 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10060 | |
| 10061 | /* pop the environment string. */ |
| 10062 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10063 | |
| 10064 | /* reset for next loop */ |
| 10065 | akl = NULL; |
| 10066 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10067 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10068 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10069 | |
| 10070 | alloc_error: |
| 10071 | release_hlua_function(fcn); |
| 10072 | ha_free(&akl); |
| 10073 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10074 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10075 | } |
| 10076 | |
| 10077 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10078 | struct act_rule *rule, char **err) |
| 10079 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10080 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10081 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10082 | if (px->mode == PR_MODE_HTTP) { |
| 10083 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10084 | return ACT_RET_PRS_ERR; |
| 10085 | } |
| 10086 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10087 | /* Memory for the rule. */ |
| 10088 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10089 | if (!rule->arg.hlua_rule) { |
| 10090 | memprintf(err, "out of memory error"); |
| 10091 | return ACT_RET_PRS_ERR; |
| 10092 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10093 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10094 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10095 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10096 | |
| 10097 | /* TODO: later accept arguments. */ |
| 10098 | rule->arg.hlua_rule->args = NULL; |
| 10099 | |
| 10100 | /* Add applet pointer in the rule. */ |
| 10101 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10102 | rule->applet.name = fcn->name; |
| 10103 | rule->applet.init = hlua_applet_tcp_init; |
| 10104 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10105 | rule->applet.release = hlua_applet_tcp_release; |
| 10106 | rule->applet.timeout = hlua_timeout_applet; |
| 10107 | |
| 10108 | return 0; |
| 10109 | } |
| 10110 | |
| 10111 | /* This function is an LUA binding used for registering |
| 10112 | * "sample-conv" functions. It expects a converter name used |
| 10113 | * in the haproxy configuration file, and an LUA function. |
| 10114 | */ |
| 10115 | __LJMP static int hlua_register_service(lua_State *L) |
| 10116 | { |
| 10117 | struct action_kw_list *akl; |
| 10118 | const char *name; |
| 10119 | const char *env; |
| 10120 | int ref; |
| 10121 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10122 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10123 | struct buffer *trash; |
| 10124 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10125 | |
| 10126 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10127 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10128 | if (hlua_gethlua(L)) { |
| 10129 | /* runtime processing */ |
| 10130 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 10131 | } |
| 10132 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10133 | /* First argument : converter name. */ |
| 10134 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10135 | |
| 10136 | /* Second argument : environment. */ |
| 10137 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10138 | |
| 10139 | /* Third argument : lua function. */ |
| 10140 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10141 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10142 | /* Check for service already registered */ |
| 10143 | trash = get_trash_chunk(); |
| 10144 | chunk_printf(trash, "lua.%s", name); |
| 10145 | akw = service_find(trash->area); |
| 10146 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10147 | fcn = akw->private; |
| 10148 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10149 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10150 | "This will become a hard error in version 2.5.\n", name); |
| 10151 | } |
| 10152 | fcn->function_ref[hlua_state_id] = ref; |
| 10153 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10154 | } |
| 10155 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10156 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10157 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10158 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10159 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10160 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10161 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10162 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10163 | |
| 10164 | /* Fill fcn. */ |
| 10165 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10166 | fcn->name = calloc(1, len); |
| 10167 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10168 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10169 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10170 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10171 | |
| 10172 | /* List head */ |
| 10173 | akl->list.n = akl->list.p = NULL; |
| 10174 | |
| 10175 | /* converter keyword. */ |
| 10176 | len = strlen("lua.") + strlen(name) + 1; |
| 10177 | akl->kw[0].kw = calloc(1, len); |
| 10178 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10179 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10180 | |
| 10181 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10182 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10183 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10184 | if (strcmp(env, "tcp") == 0) |
| 10185 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10186 | else if (strcmp(env, "http") == 0) |
| 10187 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10188 | else { |
| 10189 | release_hlua_function(fcn); |
| 10190 | if (akl) |
| 10191 | ha_free((char **)&(akl->kw[0].kw)); |
| 10192 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10193 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10194 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10195 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10196 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10197 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10198 | akl->kw[0].private = fcn; |
| 10199 | |
| 10200 | /* End of array. */ |
| 10201 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10202 | |
| 10203 | /* Register this new converter */ |
| 10204 | service_keywords_register(akl); |
| 10205 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10206 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10207 | |
| 10208 | alloc_error: |
| 10209 | release_hlua_function(fcn); |
| 10210 | ha_free(&akl); |
| 10211 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10212 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10213 | } |
| 10214 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10215 | /* This function initialises Lua cli handler. It copies the |
| 10216 | * arguments in the Lua stack and create channel IO objects. |
| 10217 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10218 | 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] | 10219 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10220 | 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] | 10221 | struct hlua *hlua; |
| 10222 | struct hlua_function *fcn; |
| 10223 | int i; |
| 10224 | const char *error; |
| 10225 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10226 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10227 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10228 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10229 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10230 | if (!hlua) { |
| 10231 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10232 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10233 | } |
| 10234 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10235 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10236 | |
| 10237 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10238 | * 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] | 10239 | * applet_http. It is absolutely compatible. |
| 10240 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10241 | ctx->task = task_new_here(); |
| 10242 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10243 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10244 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10245 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10246 | ctx->task->nice = 0; |
| 10247 | ctx->task->context = appctx; |
| 10248 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10249 | |
| 10250 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10251 | 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] | 10252 | 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] | 10253 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10254 | } |
| 10255 | |
| 10256 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10257 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10258 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10259 | error = lua_tostring(hlua->T, -1); |
| 10260 | else |
| 10261 | error = "critical error"; |
| 10262 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10263 | goto error; |
| 10264 | } |
| 10265 | |
| 10266 | /* Check stack available size. */ |
| 10267 | if (!lua_checkstack(hlua->T, 2)) { |
| 10268 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10269 | goto error; |
| 10270 | } |
| 10271 | |
| 10272 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10273 | 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] | 10274 | |
| 10275 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10276 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10277 | */ |
| 10278 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10279 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10280 | goto error; |
| 10281 | } |
| 10282 | hlua->nargs = 1; |
| 10283 | |
| 10284 | /* push keywords in the stack. */ |
| 10285 | for (i = 0; *args[i]; i++) { |
| 10286 | /* Check stack available size. */ |
| 10287 | if (!lua_checkstack(hlua->T, 1)) { |
| 10288 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10289 | goto error; |
| 10290 | } |
| 10291 | lua_pushstring(hlua->T, args[i]); |
| 10292 | hlua->nargs++; |
| 10293 | } |
| 10294 | |
| 10295 | /* We must initialize the execution timeouts. */ |
| 10296 | hlua->max_time = hlua_timeout_session; |
| 10297 | |
| 10298 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10299 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10300 | |
| 10301 | /* It's ok */ |
| 10302 | return 0; |
| 10303 | |
| 10304 | /* It's not ok. */ |
| 10305 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10306 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10307 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10308 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10309 | return 1; |
| 10310 | } |
| 10311 | |
| 10312 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10313 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10314 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10315 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10316 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10317 | struct hlua_function *fcn; |
| 10318 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10319 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10320 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10321 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10322 | |
| 10323 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10324 | 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] | 10325 | return 1; |
| 10326 | |
| 10327 | /* Execute the function. */ |
| 10328 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10329 | |
| 10330 | /* finished. */ |
| 10331 | case HLUA_E_OK: |
| 10332 | return 1; |
| 10333 | |
| 10334 | /* yield. */ |
| 10335 | case HLUA_E_AGAIN: |
| 10336 | /* We want write. */ |
| 10337 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10338 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10339 | /* Set the timeout. */ |
| 10340 | if (hlua->wake_time != TICK_ETERNITY) |
| 10341 | task_schedule(hlua->task, hlua->wake_time); |
| 10342 | return 0; |
| 10343 | |
| 10344 | /* finished with error. */ |
| 10345 | case HLUA_E_ERRMSG: |
| 10346 | /* Display log. */ |
| 10347 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10348 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10349 | lua_pop(hlua->T, 1); |
| 10350 | return 1; |
| 10351 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10352 | case HLUA_E_ETMOUT: |
| 10353 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10354 | fcn->name); |
| 10355 | return 1; |
| 10356 | |
| 10357 | case HLUA_E_NOMEM: |
| 10358 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10359 | fcn->name); |
| 10360 | return 1; |
| 10361 | |
| 10362 | case HLUA_E_YIELD: /* unexpected */ |
| 10363 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10364 | fcn->name); |
| 10365 | return 1; |
| 10366 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10367 | case HLUA_E_ERR: |
| 10368 | /* Display log. */ |
| 10369 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10370 | fcn->name); |
| 10371 | return 1; |
| 10372 | |
| 10373 | default: |
| 10374 | return 1; |
| 10375 | } |
| 10376 | |
| 10377 | return 1; |
| 10378 | } |
| 10379 | |
| 10380 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10381 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10382 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10383 | |
| 10384 | hlua_ctx_destroy(ctx->hlua); |
| 10385 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10386 | } |
| 10387 | |
| 10388 | /* This function is an LUA binding used for registering |
| 10389 | * new keywords in the cli. It expects a list of keywords |
| 10390 | * which are the "path". It is limited to 5 keywords. A |
| 10391 | * description of the command, a function to be executed |
| 10392 | * for the parsing and a function for io handlers. |
| 10393 | */ |
| 10394 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10395 | { |
| 10396 | struct cli_kw_list *cli_kws; |
| 10397 | const char *message; |
| 10398 | int ref_io; |
| 10399 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10400 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10401 | int index; |
| 10402 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10403 | struct buffer *trash; |
| 10404 | const char *kw[5]; |
| 10405 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10406 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10407 | |
| 10408 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10409 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10410 | if (hlua_gethlua(L)) { |
| 10411 | /* runtime processing */ |
| 10412 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 10413 | } |
| 10414 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10415 | /* First argument : an array of maximum 5 keywords. */ |
| 10416 | if (!lua_istable(L, 1)) |
| 10417 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10418 | |
| 10419 | /* Second argument : string with contextual message. */ |
| 10420 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10421 | |
| 10422 | /* Third and fourth argument : lua function. */ |
| 10423 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10424 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10425 | /* Check for CLI service already registered */ |
| 10426 | trash = get_trash_chunk(); |
| 10427 | index = 0; |
| 10428 | lua_pushnil(L); |
| 10429 | memset(kw, 0, sizeof(kw)); |
| 10430 | while (lua_next(L, 1) != 0) { |
| 10431 | if (index >= CLI_PREFIX_KW_NB) |
| 10432 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10433 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10434 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10435 | kw[index] = lua_tostring(L, -1); |
| 10436 | if (index == 0) |
| 10437 | chunk_printf(trash, "%s", kw[index]); |
| 10438 | else |
| 10439 | chunk_appendf(trash, " %s", kw[index]); |
| 10440 | index++; |
| 10441 | lua_pop(L, 1); |
| 10442 | } |
| 10443 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10444 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10445 | fcn = cli_kw->private; |
| 10446 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10447 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10448 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10449 | } |
| 10450 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10451 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10452 | } |
| 10453 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10454 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10455 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10456 | if (!cli_kws) { |
| 10457 | errmsg = "Lua out of memory error."; |
| 10458 | goto error; |
| 10459 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10460 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10461 | if (!fcn) { |
| 10462 | errmsg = "Lua out of memory error."; |
| 10463 | goto error; |
| 10464 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10465 | |
| 10466 | /* Fill path. */ |
| 10467 | index = 0; |
| 10468 | lua_pushnil(L); |
| 10469 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10470 | if (index >= 5) { |
| 10471 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10472 | goto error; |
| 10473 | } |
| 10474 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10475 | errmsg = "1st argument must be a table filled with strings"; |
| 10476 | goto error; |
| 10477 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10478 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10479 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10480 | errmsg = "Lua out of memory error."; |
| 10481 | goto error; |
| 10482 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10483 | index++; |
| 10484 | lua_pop(L, 1); |
| 10485 | } |
| 10486 | |
| 10487 | /* Copy help message. */ |
| 10488 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10489 | if (!cli_kws->kw[0].usage) { |
| 10490 | errmsg = "Lua out of memory error."; |
| 10491 | goto error; |
| 10492 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10493 | |
| 10494 | /* Fill fcn io handler. */ |
| 10495 | len = strlen("<lua.cli>") + 1; |
| 10496 | for (i = 0; i < index; i++) |
| 10497 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10498 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10499 | if (!fcn->name) { |
| 10500 | errmsg = "Lua out of memory error."; |
| 10501 | goto error; |
| 10502 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10503 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10504 | for (i = 0; i < index; i++) { |
| 10505 | strncat((char *)fcn->name, ".", len); |
| 10506 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10507 | } |
| 10508 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10509 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10510 | |
| 10511 | /* Fill last entries. */ |
| 10512 | cli_kws->kw[0].private = fcn; |
| 10513 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10514 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10515 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10516 | |
| 10517 | /* Register this new converter */ |
| 10518 | cli_register_kw(cli_kws); |
| 10519 | |
| 10520 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10521 | |
| 10522 | error: |
| 10523 | release_hlua_function(fcn); |
| 10524 | if (cli_kws) { |
| 10525 | for (i = 0; i < index; i++) |
| 10526 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10527 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10528 | } |
| 10529 | ha_free(&cli_kws); |
| 10530 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10531 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10532 | } |
| 10533 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10534 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10535 | { |
| 10536 | struct hlua_flt_config *conf = fconf->conf; |
| 10537 | lua_State *L; |
| 10538 | int error, pos, state_id, flt_ref; |
| 10539 | |
| 10540 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10541 | L = hlua_states[state_id]; |
| 10542 | pos = lua_gettop(L); |
| 10543 | |
| 10544 | /* The filter parsing function */ |
| 10545 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10546 | |
| 10547 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10548 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10549 | |
| 10550 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10551 | lua_newtable(L); |
| 10552 | lua_pushnil(L); |
| 10553 | |
| 10554 | while (lua_next(L, pos+2)) { |
| 10555 | lua_pushvalue(L, -2); |
| 10556 | lua_insert(L, -2); |
| 10557 | lua_settable(L, -4); |
| 10558 | } |
| 10559 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10560 | |
| 10561 | /* Remove the original lua filter class from the stack */ |
| 10562 | lua_pop(L, 1); |
| 10563 | |
| 10564 | /* Push the copy on the stack */ |
| 10565 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10566 | |
| 10567 | /* extra args are pushed in a table */ |
| 10568 | lua_newtable(L); |
| 10569 | for (pos = 0; conf->args[pos]; pos++) { |
| 10570 | /* Check stack available size. */ |
| 10571 | if (!lua_checkstack(L, 1)) { |
| 10572 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10573 | goto error; |
| 10574 | } |
| 10575 | lua_pushstring(L, conf->args[pos]); |
| 10576 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10577 | } |
| 10578 | |
| 10579 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10580 | switch (error) { |
| 10581 | case LUA_OK: |
| 10582 | /* replace the filter ref */ |
| 10583 | conf->ref[state_id] = flt_ref; |
| 10584 | break; |
| 10585 | case LUA_ERRRUN: |
| 10586 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10587 | goto error; |
| 10588 | case LUA_ERRMEM: |
| 10589 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10590 | goto error; |
| 10591 | case LUA_ERRERR: |
| 10592 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10593 | goto error; |
| 10594 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10595 | case LUA_ERRGCMM: |
| 10596 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10597 | goto error; |
| 10598 | #endif |
| 10599 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10600 | 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] | 10601 | goto error; |
| 10602 | } |
| 10603 | |
| 10604 | lua_settop(L, 0); |
| 10605 | return 0; |
| 10606 | |
| 10607 | error: |
| 10608 | lua_settop(L, 0); |
| 10609 | return -1; |
| 10610 | } |
| 10611 | |
| 10612 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10613 | { |
| 10614 | struct hlua_flt_config *conf = fconf->conf; |
| 10615 | lua_State *L; |
| 10616 | int state_id; |
| 10617 | |
| 10618 | if (!conf) |
| 10619 | return; |
| 10620 | |
| 10621 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10622 | L = hlua_states[state_id]; |
| 10623 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10624 | } |
| 10625 | |
| 10626 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10627 | { |
| 10628 | struct hlua_flt_config *conf = fconf->conf; |
| 10629 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10630 | |
| 10631 | /* Rely on per-thread init for global scripts */ |
| 10632 | if (!state_id) |
| 10633 | return hlua_filter_init_per_thread(px, fconf); |
| 10634 | return 0; |
| 10635 | } |
| 10636 | |
| 10637 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10638 | { |
| 10639 | |
| 10640 | if (fconf->conf) { |
| 10641 | struct hlua_flt_config *conf = fconf->conf; |
| 10642 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10643 | int pos; |
| 10644 | |
| 10645 | /* Rely on per-thread deinit for global scripts */ |
| 10646 | if (!state_id) |
| 10647 | hlua_filter_deinit_per_thread(px, fconf); |
| 10648 | |
| 10649 | for (pos = 0; conf->args[pos]; pos++) |
| 10650 | free(conf->args[pos]); |
| 10651 | free(conf->args); |
| 10652 | } |
| 10653 | ha_free(&fconf->conf); |
| 10654 | ha_free((char **)&fconf->id); |
| 10655 | ha_free(&fconf->ops); |
| 10656 | } |
| 10657 | |
| 10658 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10659 | { |
| 10660 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10661 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10662 | int ret = 1; |
| 10663 | |
| 10664 | /* In the execution wrappers linked with a stream, the |
| 10665 | * Lua context can be not initialized. This behavior |
| 10666 | * permits to save performances because a systematic |
| 10667 | * Lua initialization cause 5% performances loss. |
| 10668 | */ |
| 10669 | if (!s->hlua) { |
| 10670 | struct hlua *hlua; |
| 10671 | |
| 10672 | hlua = pool_alloc(pool_head_hlua); |
| 10673 | if (!hlua) { |
| 10674 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10675 | conf->reg->name); |
| 10676 | ret = 0; |
| 10677 | goto end; |
| 10678 | } |
| 10679 | HLUA_INIT(hlua); |
| 10680 | s->hlua = hlua; |
| 10681 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10682 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10683 | conf->reg->name); |
| 10684 | ret = 0; |
| 10685 | goto end; |
| 10686 | } |
| 10687 | } |
| 10688 | |
| 10689 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10690 | if (!flt_ctx) { |
| 10691 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10692 | conf->reg->name); |
| 10693 | ret = 0; |
| 10694 | goto end; |
| 10695 | } |
| 10696 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10697 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10698 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10699 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10700 | conf->reg->name); |
| 10701 | ret = 0; |
| 10702 | goto end; |
| 10703 | } |
| 10704 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10705 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10706 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10707 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10708 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10709 | conf->reg->name); |
| 10710 | ret = 0; |
| 10711 | goto end; |
| 10712 | } |
| 10713 | |
| 10714 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10715 | /* The following Lua calls can fail. */ |
| 10716 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10717 | const char *error; |
| 10718 | |
| 10719 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10720 | error = lua_tostring(s->hlua->T, -1); |
| 10721 | else |
| 10722 | error = "critical error"; |
| 10723 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10724 | ret = 0; |
| 10725 | goto end; |
| 10726 | } |
| 10727 | |
| 10728 | /* Check stack size. */ |
| 10729 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10730 | 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] | 10731 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10732 | ret = 0; |
| 10733 | goto end; |
| 10734 | } |
| 10735 | |
| 10736 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10737 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10738 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10739 | conf->reg->name); |
| 10740 | RESET_SAFE_LJMP(s->hlua); |
| 10741 | ret = 0; |
| 10742 | goto end; |
| 10743 | } |
| 10744 | lua_insert(s->hlua->T, -2); |
| 10745 | |
| 10746 | /* Push the copy on the stack */ |
| 10747 | s->hlua->nargs = 1; |
| 10748 | |
| 10749 | /* We must initialize the execution timeouts. */ |
| 10750 | s->hlua->max_time = hlua_timeout_session; |
| 10751 | |
| 10752 | /* At this point the execution is safe. */ |
| 10753 | RESET_SAFE_LJMP(s->hlua); |
| 10754 | } |
| 10755 | |
| 10756 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10757 | case HLUA_E_OK: |
| 10758 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10759 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10760 | ret = 0; |
| 10761 | goto end; |
| 10762 | } |
| 10763 | |
| 10764 | /* Attached the filter pointer to the ctx */ |
| 10765 | lua_pushstring(s->hlua->T, "__filter"); |
| 10766 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10767 | lua_settable(s->hlua->T, -3); |
| 10768 | |
| 10769 | /* Save a ref on the filter ctx */ |
| 10770 | lua_pushvalue(s->hlua->T, 1); |
| 10771 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10772 | filter->ctx = flt_ctx; |
| 10773 | break; |
| 10774 | case HLUA_E_ERRMSG: |
| 10775 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10776 | ret = -1; |
| 10777 | goto end; |
| 10778 | case HLUA_E_ETMOUT: |
| 10779 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10780 | ret = 0; |
| 10781 | goto end; |
| 10782 | case HLUA_E_NOMEM: |
| 10783 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10784 | ret = 0; |
| 10785 | goto end; |
| 10786 | case HLUA_E_AGAIN: |
| 10787 | case HLUA_E_YIELD: |
| 10788 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10789 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10790 | ret = 0; |
| 10791 | goto end; |
| 10792 | case HLUA_E_ERR: |
| 10793 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10794 | ret = 0; |
| 10795 | goto end; |
| 10796 | default: |
| 10797 | ret = 0; |
| 10798 | goto end; |
| 10799 | } |
| 10800 | |
| 10801 | end: |
| 10802 | if (s->hlua) |
| 10803 | lua_settop(s->hlua->T, 0); |
| 10804 | if (ret <= 0) { |
| 10805 | if (flt_ctx) { |
| 10806 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10807 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10808 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10809 | } |
| 10810 | } |
| 10811 | return ret; |
| 10812 | } |
| 10813 | |
| 10814 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10815 | { |
| 10816 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10817 | |
| 10818 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10819 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10820 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10821 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10822 | filter->ctx = NULL; |
| 10823 | } |
| 10824 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10825 | static int hlua_filter_from_payload(struct filter *filter) |
| 10826 | { |
| 10827 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10828 | |
| 10829 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10830 | } |
| 10831 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10832 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10833 | int dir, unsigned int flags) |
| 10834 | { |
| 10835 | struct hlua *flt_hlua; |
| 10836 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10837 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10838 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10839 | int ret = 1; |
| 10840 | |
| 10841 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10842 | if (!flt_hlua) |
| 10843 | goto end; |
| 10844 | |
| 10845 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10846 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10847 | |
| 10848 | /* The following Lua calls can fail. */ |
| 10849 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10850 | const char *error; |
| 10851 | |
| 10852 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10853 | error = lua_tostring(flt_hlua->T, -1); |
| 10854 | else |
| 10855 | error = "critical error"; |
| 10856 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10857 | goto end; |
| 10858 | } |
| 10859 | |
| 10860 | /* Check stack size. */ |
| 10861 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10862 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10863 | RESET_SAFE_LJMP(flt_hlua); |
| 10864 | goto end; |
| 10865 | } |
| 10866 | |
| 10867 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10868 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10869 | RESET_SAFE_LJMP(flt_hlua); |
| 10870 | goto end; |
| 10871 | } |
| 10872 | lua_insert(flt_hlua->T, -2); |
| 10873 | |
| 10874 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10875 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10876 | RESET_SAFE_LJMP(flt_hlua); |
| 10877 | goto end; |
| 10878 | } |
| 10879 | flt_hlua->nargs = 2; |
| 10880 | |
| 10881 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10882 | if (dir == SMP_OPT_DIR_REQ) |
| 10883 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10884 | else |
| 10885 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10886 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10887 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10888 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10889 | lua_settable(flt_hlua->T, -3); |
| 10890 | } |
| 10891 | flt_hlua->nargs++; |
| 10892 | } |
| 10893 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10894 | if (dir == SMP_OPT_DIR_REQ) |
| 10895 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10896 | else |
| 10897 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10898 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10899 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10900 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10901 | lua_settable(flt_hlua->T, -3); |
| 10902 | } |
| 10903 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10904 | } |
| 10905 | |
| 10906 | /* Check stack size. */ |
| 10907 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10908 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10909 | RESET_SAFE_LJMP(flt_hlua); |
| 10910 | goto end; |
| 10911 | } |
| 10912 | |
| 10913 | while (extra_idx--) { |
| 10914 | lua_pushvalue(flt_hlua->T, 1); |
| 10915 | lua_remove(flt_hlua->T, 1); |
| 10916 | flt_hlua->nargs++; |
| 10917 | } |
| 10918 | |
| 10919 | /* We must initialize the execution timeouts. */ |
| 10920 | flt_hlua->max_time = hlua_timeout_session; |
| 10921 | |
| 10922 | /* At this point the execution is safe. */ |
| 10923 | RESET_SAFE_LJMP(flt_hlua); |
| 10924 | } |
| 10925 | |
| 10926 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10927 | case HLUA_E_OK: |
| 10928 | /* Catch the return value if it required */ |
| 10929 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10930 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10931 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10932 | } |
| 10933 | |
| 10934 | /* Set timeout in the required channel. */ |
| 10935 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10936 | if (dir == SMP_OPT_DIR_REQ) |
| 10937 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10938 | else |
| 10939 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10940 | } |
| 10941 | break; |
| 10942 | case HLUA_E_AGAIN: |
| 10943 | /* Set timeout in the required channel. */ |
| 10944 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10945 | if (dir == SMP_OPT_DIR_REQ) |
| 10946 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10947 | else |
| 10948 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10949 | } |
| 10950 | /* Some actions can be wake up when a "write" event |
| 10951 | * is detected on a response channel. This is useful |
| 10952 | * only for actions targeted on the requests. |
| 10953 | */ |
| 10954 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10955 | s->res.flags |= CF_WAKE_WRITE; |
| 10956 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10957 | s->req.flags |= CF_WAKE_WRITE; |
| 10958 | ret = 0; |
| 10959 | goto end; |
| 10960 | case HLUA_E_ERRMSG: |
| 10961 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10962 | ret = -1; |
| 10963 | goto end; |
| 10964 | case HLUA_E_ETMOUT: |
| 10965 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10966 | goto end; |
| 10967 | case HLUA_E_NOMEM: |
| 10968 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10969 | goto end; |
| 10970 | case HLUA_E_YIELD: |
| 10971 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10972 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10973 | goto end; |
| 10974 | case HLUA_E_ERR: |
| 10975 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10976 | goto end; |
| 10977 | default: |
| 10978 | goto end; |
| 10979 | } |
| 10980 | |
| 10981 | |
| 10982 | end: |
| 10983 | return ret; |
| 10984 | } |
| 10985 | |
| 10986 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10987 | { |
| 10988 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10989 | |
| 10990 | flt_ctx->flags = 0; |
| 10991 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10992 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10993 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10994 | } |
| 10995 | |
| 10996 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10997 | { |
| 10998 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10999 | |
| 11000 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11001 | return hlua_filter_callback(s, filter, "end_analyze", |
| 11002 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11003 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11004 | } |
| 11005 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11006 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11007 | { |
| 11008 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11009 | |
| 11010 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11011 | return hlua_filter_callback(s, filter, "http_headers", |
| 11012 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11013 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11014 | } |
| 11015 | |
| 11016 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 11017 | unsigned int offset, unsigned int len) |
| 11018 | { |
| 11019 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11020 | struct hlua *flt_hlua; |
| 11021 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11022 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11023 | int ret; |
| 11024 | |
| 11025 | flt_hlua = flt_ctx->hlua[idx]; |
| 11026 | flt_ctx->cur_off[idx] = offset; |
| 11027 | flt_ctx->cur_len[idx] = len; |
| 11028 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11029 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11030 | if (ret != -1) { |
| 11031 | ret = flt_ctx->cur_len[idx]; |
| 11032 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11033 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11034 | if (ret > flt_ctx->cur_len[idx]) |
| 11035 | ret = flt_ctx->cur_len[idx]; |
| 11036 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11037 | } |
| 11038 | } |
| 11039 | return ret; |
| 11040 | } |
| 11041 | |
| 11042 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11043 | { |
| 11044 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11045 | |
| 11046 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11047 | return hlua_filter_callback(s, filter, "http_end", |
| 11048 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11049 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11050 | } |
| 11051 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11052 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 11053 | unsigned int offset, unsigned int len) |
| 11054 | { |
| 11055 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11056 | struct hlua *flt_hlua; |
| 11057 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11058 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11059 | int ret; |
| 11060 | |
| 11061 | flt_hlua = flt_ctx->hlua[idx]; |
| 11062 | flt_ctx->cur_off[idx] = offset; |
| 11063 | flt_ctx->cur_len[idx] = len; |
| 11064 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11065 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 11066 | if (ret != -1) { |
| 11067 | ret = flt_ctx->cur_len[idx]; |
| 11068 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11069 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11070 | if (ret > flt_ctx->cur_len[idx]) |
| 11071 | ret = flt_ctx->cur_len[idx]; |
| 11072 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11073 | } |
| 11074 | } |
| 11075 | return ret; |
| 11076 | } |
| 11077 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11078 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11079 | struct flt_conf *fconf, char **err, void *private) |
| 11080 | { |
| 11081 | struct hlua_reg_filter *reg_flt = private; |
| 11082 | lua_State *L; |
| 11083 | struct hlua_flt_config *conf = NULL; |
| 11084 | const char *flt_id = NULL; |
| 11085 | int state_id, pos, flt_flags = 0; |
| 11086 | struct flt_ops *hlua_flt_ops = NULL; |
| 11087 | |
| 11088 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11089 | L = hlua_states[state_id]; |
| 11090 | |
| 11091 | /* Initialize the filter ops with default callbacks */ |
| 11092 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11093 | if (!hlua_flt_ops) |
| 11094 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11095 | hlua_flt_ops->init = hlua_filter_init; |
| 11096 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11097 | if (state_id) { |
| 11098 | /* Set per-thread callback if script is loaded per-thread */ |
| 11099 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11100 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11101 | } |
| 11102 | hlua_flt_ops->attach = hlua_filter_new; |
| 11103 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11104 | |
| 11105 | /* Push the filter class on the stack and resolve all callbacks */ |
| 11106 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 11107 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11108 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11109 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11110 | lua_pop(L, 1); |
| 11111 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11112 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11113 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11114 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11115 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11116 | lua_pop(L, 1); |
| 11117 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11118 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11119 | lua_pop(L, 1); |
| 11120 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11121 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11122 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11123 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11124 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11125 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11126 | |
| 11127 | /* Get id and flags of the filter class */ |
| 11128 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11129 | flt_id = lua_tostring(L, -1); |
| 11130 | lua_pop(L, 1); |
| 11131 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11132 | flt_flags = lua_tointeger(L, -1); |
| 11133 | lua_pop(L, 1); |
| 11134 | |
| 11135 | /* Create the filter config */ |
| 11136 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11137 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11138 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11139 | conf->reg = reg_flt; |
| 11140 | |
| 11141 | /* duplicate args */ |
| 11142 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11143 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11144 | if (!conf->args) |
| 11145 | goto error; |
| 11146 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11147 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11148 | if (!conf->args[pos]) |
| 11149 | goto error; |
| 11150 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11151 | conf->args[pos] = NULL; |
| 11152 | *cur_arg += pos + 1; |
| 11153 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11154 | if (flt_id) { |
| 11155 | fconf->id = strdup(flt_id); |
| 11156 | if (!fconf->id) |
| 11157 | goto error; |
| 11158 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11159 | fconf->flags = flt_flags; |
| 11160 | fconf->conf = conf; |
| 11161 | fconf->ops = hlua_flt_ops; |
| 11162 | |
| 11163 | lua_settop(L, 0); |
| 11164 | return 0; |
| 11165 | |
| 11166 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11167 | 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] | 11168 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11169 | if (conf && conf->args) { |
| 11170 | for (pos = 0; conf->args[pos]; pos++) |
| 11171 | free(conf->args[pos]); |
| 11172 | free(conf->args); |
| 11173 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11174 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11175 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11176 | lua_settop(L, 0); |
| 11177 | return -1; |
| 11178 | } |
| 11179 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11180 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11181 | { |
| 11182 | struct filter *filter; |
| 11183 | struct channel *chn; |
| 11184 | |
| 11185 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11186 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11187 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11188 | |
| 11189 | lua_getfield(L, 1, "__filter"); |
| 11190 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11191 | filter = lua_touserdata (L, -1); |
| 11192 | lua_pop(L, 1); |
| 11193 | |
| 11194 | register_data_filter(chn_strm(chn), chn, filter); |
| 11195 | return 1; |
| 11196 | } |
| 11197 | |
| 11198 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11199 | { |
| 11200 | struct filter *filter; |
| 11201 | struct channel *chn; |
| 11202 | |
| 11203 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11204 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11205 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11206 | |
| 11207 | lua_getfield(L, 1, "__filter"); |
| 11208 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11209 | filter = lua_touserdata (L, -1); |
| 11210 | lua_pop(L, 1); |
| 11211 | |
| 11212 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11213 | return 1; |
| 11214 | } |
| 11215 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11216 | /* 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] | 11217 | * 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] | 11218 | * parse configuration arguments. |
| 11219 | */ |
| 11220 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11221 | { |
| 11222 | struct buffer *trash; |
| 11223 | struct flt_kw_list *fkl; |
| 11224 | struct flt_kw *fkw; |
| 11225 | const char *name; |
| 11226 | struct hlua_reg_filter *reg_flt= NULL; |
| 11227 | int flt_ref, fun_ref; |
| 11228 | int len; |
| 11229 | |
| 11230 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11231 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11232 | if (hlua_gethlua(L)) { |
| 11233 | /* runtime processing */ |
| 11234 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 11235 | } |
| 11236 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11237 | /* First argument : filter name. */ |
| 11238 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11239 | |
| 11240 | /* Second argument : The filter class */ |
| 11241 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11242 | |
| 11243 | /* Third argument : lua function. */ |
| 11244 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11245 | |
| 11246 | trash = get_trash_chunk(); |
| 11247 | chunk_printf(trash, "lua.%s", name); |
| 11248 | fkw = flt_find_kw(trash->area); |
| 11249 | if (fkw != NULL) { |
| 11250 | reg_flt = fkw->private; |
| 11251 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11252 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11253 | "This will become a hard error in version 2.5.\n", name); |
| 11254 | } |
| 11255 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11256 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11257 | return 0; |
| 11258 | } |
| 11259 | |
| 11260 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11261 | if (!fkl) |
| 11262 | goto alloc_error; |
| 11263 | fkl->scope = "HLUA"; |
| 11264 | |
| 11265 | reg_flt = new_hlua_reg_filter(name); |
| 11266 | if (!reg_flt) |
| 11267 | goto alloc_error; |
| 11268 | |
| 11269 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11270 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11271 | |
| 11272 | /* The filter keyword */ |
| 11273 | len = strlen("lua.") + strlen(name) + 1; |
| 11274 | fkl->kw[0].kw = calloc(1, len); |
| 11275 | if (!fkl->kw[0].kw) |
| 11276 | goto alloc_error; |
| 11277 | |
| 11278 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11279 | |
| 11280 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11281 | fkl->kw[0].private = reg_flt; |
| 11282 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11283 | |
| 11284 | /* Register this new filter */ |
| 11285 | flt_register_keywords(fkl); |
| 11286 | |
| 11287 | return 0; |
| 11288 | |
| 11289 | alloc_error: |
| 11290 | release_hlua_reg_filter(reg_flt); |
| 11291 | ha_free(&fkl); |
| 11292 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11293 | return 0; /* Never reached */ |
| 11294 | } |
| 11295 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11296 | 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] | 11297 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11298 | char **err, unsigned int *timeout) |
| 11299 | { |
| 11300 | const char *error; |
| 11301 | |
| 11302 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11303 | if (error == PARSE_TIME_OVER) { |
| 11304 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11305 | args[1], args[0]); |
| 11306 | return -1; |
| 11307 | } |
| 11308 | else if (error == PARSE_TIME_UNDER) { |
| 11309 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11310 | args[1], args[0]); |
| 11311 | return -1; |
| 11312 | } |
| 11313 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11314 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11315 | return -1; |
| 11316 | } |
| 11317 | return 0; |
| 11318 | } |
| 11319 | |
| 11320 | 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] | 11321 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11322 | char **err) |
| 11323 | { |
| 11324 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11325 | file, line, err, &hlua_timeout_session); |
| 11326 | } |
| 11327 | |
| 11328 | 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] | 11329 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11330 | char **err) |
| 11331 | { |
| 11332 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11333 | file, line, err, &hlua_timeout_task); |
| 11334 | } |
| 11335 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11336 | 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] | 11337 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11338 | char **err) |
| 11339 | { |
| 11340 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11341 | file, line, err, &hlua_timeout_applet); |
| 11342 | } |
| 11343 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11344 | 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] | 11345 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11346 | char **err) |
| 11347 | { |
| 11348 | char *error; |
| 11349 | |
| 11350 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11351 | if (*error != '\0') { |
| 11352 | memprintf(err, "%s: invalid number", args[0]); |
| 11353 | return -1; |
| 11354 | } |
| 11355 | return 0; |
| 11356 | } |
| 11357 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11358 | 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] | 11359 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11360 | char **err) |
| 11361 | { |
| 11362 | char *error; |
| 11363 | |
| 11364 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11365 | 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] | 11366 | return -1; |
| 11367 | } |
| 11368 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11369 | if (*error != '\0') { |
| 11370 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11371 | return -1; |
| 11372 | } |
| 11373 | return 0; |
| 11374 | } |
| 11375 | |
| 11376 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11377 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11378 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11379 | * the main lua entry point. |
| 11380 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11381 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11382 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11383 | * |
| 11384 | * In some error case, LUA set an error message in top of the stack. This function |
| 11385 | * 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] | 11386 | * |
| 11387 | * This function can fail with an abort() due to an Lua critical error. |
| 11388 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11389 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11390 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11391 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11392 | { |
| 11393 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11394 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11395 | |
| 11396 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11397 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11398 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11399 | 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] | 11400 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11401 | return -1; |
| 11402 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11403 | |
| 11404 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11405 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11406 | /* Check stack size. */ |
| 11407 | if (!lua_checkstack(L, 1)) { |
| 11408 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11409 | return -1; |
| 11410 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11411 | lua_pushstring(L, args[nargs]); |
| 11412 | } |
| 11413 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11414 | |
| 11415 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11416 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11417 | switch (error) { |
| 11418 | case LUA_OK: |
| 11419 | break; |
| 11420 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11421 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11422 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11423 | return -1; |
| 11424 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11425 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11426 | return -1; |
| 11427 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11428 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11429 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11430 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11431 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11432 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11433 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11434 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11435 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11436 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11437 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11438 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11439 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11440 | return -1; |
| 11441 | } |
| 11442 | |
| 11443 | return 0; |
| 11444 | } |
| 11445 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11446 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11447 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11448 | char **err) |
| 11449 | { |
| 11450 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11451 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11452 | return -1; |
| 11453 | } |
| 11454 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11455 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11456 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11457 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11458 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11459 | } |
| 11460 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11461 | 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] | 11462 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11463 | char **err) |
| 11464 | { |
| 11465 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11466 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11467 | |
| 11468 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11469 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11470 | return -1; |
| 11471 | } |
| 11472 | |
| 11473 | if (per_thread_load == NULL) { |
| 11474 | /* allocate the first entry large enough to store the final NULL */ |
| 11475 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11476 | if (per_thread_load == NULL) { |
| 11477 | memprintf(err, "out of memory error"); |
| 11478 | return -1; |
| 11479 | } |
| 11480 | } |
| 11481 | |
| 11482 | /* count used entries */ |
| 11483 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11484 | ; |
| 11485 | |
| 11486 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11487 | if (per_thread_load == NULL) { |
| 11488 | memprintf(err, "out of memory error"); |
| 11489 | return -1; |
| 11490 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11491 | per_thread_load[len + 1] = NULL; |
| 11492 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11493 | /* count args excepting the first, allocate array and copy args */ |
| 11494 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11495 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11496 | if (per_thread_load[len] == NULL) { |
| 11497 | memprintf(err, "out of memory error"); |
| 11498 | return -1; |
| 11499 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11500 | for (i = 1; *(args[i]) != 0; i++) { |
| 11501 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11502 | if (per_thread_load[len][i - 1] == NULL) { |
| 11503 | memprintf(err, "out of memory error"); |
| 11504 | return -1; |
| 11505 | } |
| 11506 | } |
| 11507 | per_thread_load[len][i - 1] = strdup(""); |
| 11508 | if (per_thread_load[len][i - 1] == NULL) { |
| 11509 | memprintf(err, "out of memory error"); |
| 11510 | return -1; |
| 11511 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11512 | |
| 11513 | /* loading for thread 1 only */ |
| 11514 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11515 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11516 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11517 | } |
| 11518 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11519 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11520 | * in the given <ctx>. |
| 11521 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11522 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11523 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11524 | lua_getglobal(L, "package"); /* push package variable */ |
| 11525 | lua_pushstring(L, path); /* push given path */ |
| 11526 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11527 | lua_getfield(L, -3, type); /* push old path */ |
| 11528 | lua_concat(L, 3); /* concatenate to new path */ |
| 11529 | lua_setfield(L, -2, type); /* store new path */ |
| 11530 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11531 | |
| 11532 | return 0; |
| 11533 | } |
| 11534 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11535 | 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] | 11536 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11537 | char **err) |
| 11538 | { |
| 11539 | char *path; |
| 11540 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11541 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11542 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11543 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11544 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11545 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11546 | } |
| 11547 | |
| 11548 | if (!(*args[1])) { |
| 11549 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11550 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11551 | } |
| 11552 | path = args[1]; |
| 11553 | |
| 11554 | if (*args[2]) { |
| 11555 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11556 | 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] | 11557 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11558 | } |
| 11559 | type = args[2]; |
| 11560 | } |
| 11561 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11562 | p = calloc(1, sizeof(*p)); |
| 11563 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11564 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11565 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11566 | } |
| 11567 | p->path = strdup(path); |
| 11568 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11569 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11570 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11571 | } |
| 11572 | p->type = strdup(type); |
| 11573 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11574 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11575 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11576 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11577 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11578 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11579 | /* Handle the global state and the per-thread state for the first |
| 11580 | * thread. The remaining threads will be initialized based on |
| 11581 | * prepend_path_list. |
| 11582 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11583 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11584 | lua_State *L = hlua_states[i]; |
| 11585 | const char *error; |
| 11586 | |
| 11587 | if (setjmp(safe_ljmp_env) != 0) { |
| 11588 | lua_atpanic(L, hlua_panic_safe); |
| 11589 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11590 | error = lua_tostring(L, -1); |
| 11591 | else |
| 11592 | error = "critical error"; |
| 11593 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11594 | exit(1); |
| 11595 | } else { |
| 11596 | lua_atpanic(L, hlua_panic_ljmp); |
| 11597 | } |
| 11598 | |
| 11599 | hlua_prepend_path(L, type, path); |
| 11600 | |
| 11601 | lua_atpanic(L, hlua_panic_safe); |
| 11602 | } |
| 11603 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11604 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11605 | |
| 11606 | err2: |
| 11607 | free(p->type); |
| 11608 | free(p->path); |
| 11609 | err: |
| 11610 | free(p); |
| 11611 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11612 | } |
| 11613 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11614 | /* configuration keywords declaration */ |
| 11615 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11616 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11617 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11618 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11619 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11620 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11621 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11622 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11623 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11624 | { 0, NULL, NULL }, |
| 11625 | }}; |
| 11626 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11627 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11628 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11629 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11630 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11631 | /* |
| 11632 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11633 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11634 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11635 | * way. |
| 11636 | */ |
| 11637 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11638 | { |
| 11639 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11640 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11641 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11642 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11643 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11644 | struct hlua *hlua; |
| 11645 | char *err = NULL; |
| 11646 | int y = 1; |
| 11647 | |
| 11648 | hlua = hlua_gethlua(L); |
| 11649 | |
| 11650 | /* get the first ckchi to copy */ |
| 11651 | if (ckchi == NULL) |
| 11652 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11653 | |
| 11654 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11655 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11656 | struct ckch_inst *new_inst; |
| 11657 | |
| 11658 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11659 | if (y % 10 == 0) { |
| 11660 | |
| 11661 | *lua_ckchi = ckchi; |
| 11662 | |
| 11663 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11664 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11665 | } |
| 11666 | |
| 11667 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11668 | goto error; |
| 11669 | |
| 11670 | /* link the new ckch_inst to the duplicate */ |
| 11671 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11672 | y++; |
| 11673 | } |
| 11674 | |
| 11675 | /* The generation is finished, we can insert everything */ |
| 11676 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11677 | |
| 11678 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11679 | |
| 11680 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11681 | |
| 11682 | return 0; |
| 11683 | |
| 11684 | error: |
| 11685 | ckch_store_free(new_ckchs); |
| 11686 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11687 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11688 | free(err); |
| 11689 | |
| 11690 | return 0; |
| 11691 | } |
| 11692 | |
| 11693 | /* |
| 11694 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11695 | * from the table in parameter. |
| 11696 | * |
| 11697 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11698 | * means it does not need to have a transaction since everything is done in the |
| 11699 | * same function. |
| 11700 | * |
| 11701 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11702 | * |
| 11703 | */ |
| 11704 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11705 | { |
| 11706 | struct hlua *hlua; |
| 11707 | struct ckch_inst **lua_ckchi; |
| 11708 | struct ckch_store **lua_ckchs; |
| 11709 | struct ckch_store *old_ckchs = NULL; |
| 11710 | struct ckch_store *new_ckchs = NULL; |
| 11711 | int errcode = 0; |
| 11712 | char *err = NULL; |
| 11713 | struct cert_exts *cert_ext = NULL; |
| 11714 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11715 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11716 | int ret; |
| 11717 | |
| 11718 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11719 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11720 | |
| 11721 | hlua = hlua_gethlua(L); |
| 11722 | |
| 11723 | /* FIXME: this should not return an error but should come back later */ |
| 11724 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11725 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11726 | |
| 11727 | ret = lua_getfield(L, -1, "filename"); |
| 11728 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11729 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11730 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11731 | goto end; |
| 11732 | } |
| 11733 | filename = (char *)lua_tostring(L, -1); |
| 11734 | |
| 11735 | |
| 11736 | /* look for the filename in the tree */ |
| 11737 | old_ckchs = ckchs_lookup(filename); |
| 11738 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11739 | 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] | 11740 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11741 | goto end; |
| 11742 | } |
| 11743 | /* TODO: handle extra_files_noext */ |
| 11744 | |
| 11745 | new_ckchs = ckchs_dup(old_ckchs); |
| 11746 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11747 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11748 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11749 | goto end; |
| 11750 | } |
| 11751 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11752 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11753 | |
| 11754 | /* loop on the field in the table, which have the same name as the |
| 11755 | * possible extensions of files */ |
| 11756 | lua_pushnil(L); |
| 11757 | while (lua_next(L, 1)) { |
| 11758 | int i; |
| 11759 | const char *field = lua_tostring(L, -2); |
| 11760 | char *payload = (char *)lua_tostring(L, -1); |
| 11761 | |
| 11762 | if (!field || strcmp(field, "filename") == 0) { |
| 11763 | lua_pop(L, 1); |
| 11764 | continue; |
| 11765 | } |
| 11766 | |
| 11767 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11768 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11769 | cert_ext = &cert_exts[i]; |
| 11770 | break; |
| 11771 | } |
| 11772 | } |
| 11773 | |
| 11774 | /* this is the default type, the field is not supported */ |
| 11775 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11776 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11777 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11778 | goto end; |
| 11779 | } |
| 11780 | |
| 11781 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11782 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11783 | 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] | 11784 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11785 | goto end; |
| 11786 | } |
| 11787 | lua_pop(L, 1); |
| 11788 | } |
| 11789 | |
| 11790 | /* store the pointers on the lua stack */ |
| 11791 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11792 | lua_ckchs[0] = old_ckchs; |
| 11793 | lua_ckchs[1] = new_ckchs; |
| 11794 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11795 | *lua_ckchi = NULL; |
| 11796 | |
| 11797 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11798 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11799 | |
| 11800 | end: |
| 11801 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11802 | |
| 11803 | if (errcode & ERR_CODE) { |
| 11804 | ckch_store_free(new_ckchs); |
| 11805 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11806 | } |
| 11807 | free(err); |
| 11808 | |
| 11809 | return 0; |
| 11810 | } |
| 11811 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11812 | #else |
| 11813 | |
| 11814 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11815 | { |
| 11816 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11817 | |
| 11818 | return 0; |
| 11819 | } |
| 11820 | #endif /* ! USE_OPENSSL */ |
| 11821 | |
| 11822 | |
| 11823 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11824 | /* This function can fail with an abort() due to an Lua critical error. |
| 11825 | * We are in the initialisation process of HAProxy, this abort() is |
| 11826 | * tolerated. |
| 11827 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11828 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11829 | { |
| 11830 | struct hlua_init_function *init; |
| 11831 | const char *msg; |
| 11832 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11833 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11834 | const char *kind; |
| 11835 | const char *trace; |
| 11836 | int return_status = 1; |
| 11837 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11838 | int nres; |
| 11839 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11840 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11841 | /* disable memory limit checks if limit is not set */ |
| 11842 | if (!hlua_global_allocator.limit) |
| 11843 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11844 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11845 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11846 | if (setjmp(safe_ljmp_env) != 0) { |
| 11847 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11848 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11849 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11850 | else |
| 11851 | error = "critical error"; |
| 11852 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11853 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11854 | } else { |
| 11855 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11856 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11857 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11858 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11859 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame^] | 11860 | /* function ref should be released right away since it was pushed |
| 11861 | * on the stack and will not be used anymore |
| 11862 | */ |
| 11863 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11864 | |
| 11865 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11866 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11867 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11868 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11869 | #endif |
| 11870 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11871 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11872 | |
| 11873 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11874 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11875 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11876 | |
| 11877 | case LUA_ERRERR: |
| 11878 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11879 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11880 | case LUA_ERRRUN: |
| 11881 | if (!kind) |
| 11882 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11883 | msg = lua_tostring(L, -1); |
| 11884 | lua_settop(L, 0); /* Empty the stack. */ |
| 11885 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11886 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11887 | if (msg) |
| 11888 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11889 | else |
| 11890 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11891 | return_status = 0; |
| 11892 | break; |
| 11893 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11894 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11895 | /* Unknown error */ |
| 11896 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11897 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11898 | case LUA_YIELD: |
| 11899 | /* yield is not configured at this step, this state doesn't happen */ |
| 11900 | if (!kind) |
| 11901 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11902 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11903 | case LUA_ERRMEM: |
| 11904 | if (!kind) |
| 11905 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11906 | lua_settop(L, 0); |
| 11907 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11908 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11909 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11910 | return_status = 0; |
| 11911 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11912 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11913 | if (!return_status) |
| 11914 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11915 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11916 | |
| 11917 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11918 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11919 | } |
| 11920 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11921 | int hlua_post_init() |
| 11922 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11923 | int ret; |
| 11924 | int i; |
| 11925 | int errors; |
| 11926 | char *err = NULL; |
| 11927 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11928 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11929 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11930 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11931 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11932 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11933 | int saved_used_backed = global.ssl_used_backend; |
| 11934 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11935 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11936 | global.ssl_used_backend = saved_used_backed; |
| 11937 | } |
| 11938 | #endif |
| 11939 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11940 | /* Perform post init of common thread */ |
| 11941 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11942 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11943 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11944 | if (ret == 0) |
| 11945 | return 0; |
| 11946 | |
| 11947 | /* init remaining lua states and load files */ |
| 11948 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11949 | |
| 11950 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11951 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11952 | |
| 11953 | /* Init lua state */ |
| 11954 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11955 | |
| 11956 | /* Load lua files */ |
| 11957 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11958 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11959 | if (ret != 0) { |
| 11960 | ha_alert("Lua init: %s\n", err); |
| 11961 | return 0; |
| 11962 | } |
| 11963 | } |
| 11964 | } |
| 11965 | |
| 11966 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11967 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11968 | |
| 11969 | /* Execute post init for all states */ |
| 11970 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11971 | |
| 11972 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11973 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11974 | |
| 11975 | /* run post init */ |
| 11976 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11977 | if (ret == 0) |
| 11978 | return 0; |
| 11979 | } |
| 11980 | |
| 11981 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11982 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11983 | |
| 11984 | /* control functions registering. Each function must have: |
| 11985 | * - only the function_ref[0] set positive and all other to -1 |
| 11986 | * - only the function_ref[0] set to -1 and all other positive |
| 11987 | * This ensure a same reference is not used both in shared |
| 11988 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11989 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11990 | * complicated to found for the end user. |
| 11991 | */ |
| 11992 | errors = 0; |
| 11993 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11994 | ret = 0; |
| 11995 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11996 | if (fcn->function_ref[i] == -1) |
| 11997 | ret--; |
| 11998 | else |
| 11999 | ret++; |
| 12000 | } |
| 12001 | if (abs(ret) != global.nbthread) { |
| 12002 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 12003 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 12004 | errors++; |
| 12005 | continue; |
| 12006 | } |
| 12007 | |
| 12008 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12009 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12010 | "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] | 12011 | "exclusive.\n", fcn->name); |
| 12012 | errors++; |
| 12013 | } |
| 12014 | } |
| 12015 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12016 | /* Do the same with registered filters */ |
| 12017 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 12018 | ret = 0; |
| 12019 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12020 | if (reg_flt->flt_ref[i] == -1) |
| 12021 | ret--; |
| 12022 | else |
| 12023 | ret++; |
| 12024 | } |
| 12025 | if (abs(ret) != global.nbthread) { |
| 12026 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 12027 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 12028 | errors++; |
| 12029 | continue; |
| 12030 | } |
| 12031 | |
| 12032 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 12033 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12034 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 12035 | "exclusive.\n", fcn->name); |
| 12036 | errors++; |
| 12037 | } |
| 12038 | } |
| 12039 | |
| 12040 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12041 | if (errors > 0) |
| 12042 | return 0; |
| 12043 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12044 | /* 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] | 12045 | * -1 in order to have probably a segfault if someone use it |
| 12046 | */ |
| 12047 | hlua_state_id = -1; |
| 12048 | |
| 12049 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12050 | } |
| 12051 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12052 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 12053 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 12054 | * 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] | 12055 | * allocation. <nsize> is the requested new size. A new allocation is |
| 12056 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12057 | * zero. This one verifies that the limits are respected but is optimized |
| 12058 | * 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] | 12059 | * |
| 12060 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 12061 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 12062 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 12063 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 12064 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12065 | */ |
| 12066 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 12067 | { |
| 12068 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12069 | size_t limit, old, new; |
| 12070 | |
| 12071 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 12072 | * for accounting anymore. |
| 12073 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12074 | if (likely(~zone->limit == 0)) { |
| 12075 | if (!nsize) |
| 12076 | ha_free(&ptr); |
| 12077 | else |
| 12078 | ptr = realloc(ptr, nsize); |
| 12079 | return ptr; |
| 12080 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12081 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12082 | if (!ptr) |
| 12083 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12084 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12085 | /* enforce strict limits across all threads */ |
| 12086 | limit = zone->limit; |
| 12087 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12088 | do { |
| 12089 | new = old + nsize - osize; |
| 12090 | if (unlikely(nsize && limit && new > limit)) |
| 12091 | return NULL; |
| 12092 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12093 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12094 | if (!nsize) |
| 12095 | ha_free(&ptr); |
| 12096 | else |
| 12097 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12098 | |
| 12099 | if (unlikely(!ptr && nsize)) // failed |
| 12100 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12101 | |
| 12102 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12103 | return ptr; |
| 12104 | } |
| 12105 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12106 | /* 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] | 12107 | * We are in the initialisation process of HAProxy, this abort() is |
| 12108 | * tolerated. |
| 12109 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12110 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12111 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12112 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12113 | int idx; |
| 12114 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12115 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12116 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12117 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12118 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12119 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12120 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12121 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12122 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12123 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12124 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12125 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12126 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12127 | *context = NULL; |
| 12128 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12129 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12130 | * the Lua function can fail with an abort. We are in the initialisation |
| 12131 | * process of HAProxy, this abort() is tolerated. |
| 12132 | */ |
| 12133 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12134 | /* Call post initialisation function in safe environment. */ |
| 12135 | if (setjmp(safe_ljmp_env) != 0) { |
| 12136 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12137 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12138 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12139 | else |
| 12140 | error_msg = "critical error"; |
| 12141 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12142 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12143 | } else { |
| 12144 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12145 | } |
| 12146 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12147 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12148 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12149 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12150 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12151 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12152 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12153 | #endif |
| 12154 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12155 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12156 | #endif |
| 12157 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12158 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12159 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12160 | /* Apply configured prepend path */ |
| 12161 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12162 | hlua_prepend_path(L, pp->type, pp->path); |
| 12163 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12164 | /* |
| 12165 | * |
| 12166 | * Create "core" object. |
| 12167 | * |
| 12168 | */ |
| 12169 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12170 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12171 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12172 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12173 | /* set the thread id */ |
| 12174 | hlua_class_const_int(L, "thread", thread_num); |
| 12175 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12176 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12177 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12178 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12179 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12180 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12181 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12182 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12183 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12184 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12185 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12186 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12187 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12188 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12189 | hlua_class_function(L, "yield", hlua_yield); |
| 12190 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12191 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12192 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12193 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12194 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12195 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12196 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12197 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12198 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12199 | hlua_class_function(L, "log", hlua_log); |
| 12200 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12201 | hlua_class_function(L, "Info", hlua_log_info); |
| 12202 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12203 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12204 | hlua_class_function(L, "done", hlua_done); |
| 12205 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12206 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12207 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12208 | |
| 12209 | /* |
| 12210 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12211 | * Create "act" object. |
| 12212 | * |
| 12213 | */ |
| 12214 | |
| 12215 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12216 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12217 | |
| 12218 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12219 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12220 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12221 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12222 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12223 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12224 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12225 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12226 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12227 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12228 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12229 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12230 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12231 | |
| 12232 | /* |
| 12233 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12234 | * Create "Filter" object. |
| 12235 | * |
| 12236 | */ |
| 12237 | |
| 12238 | /* This table entry is the object "filter" base. */ |
| 12239 | lua_newtable(L); |
| 12240 | |
| 12241 | /* push flags and constants */ |
| 12242 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12243 | hlua_class_const_int(L, "WAIT", 0); |
| 12244 | hlua_class_const_int(L, "ERROR", -1); |
| 12245 | |
| 12246 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12247 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12248 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12249 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12250 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12251 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12252 | lua_setglobal(L, "filter"); |
| 12253 | |
| 12254 | /* |
| 12255 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12256 | * Register class Map |
| 12257 | * |
| 12258 | */ |
| 12259 | |
| 12260 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12261 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12262 | |
| 12263 | /* register pattern types. */ |
| 12264 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12265 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12266 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12267 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12268 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12269 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12270 | |
| 12271 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12272 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 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 | 3def393 | 2015-04-07 11:27:54 +0200 | [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 | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12280 | |
| 12281 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12282 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12283 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12284 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12285 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12286 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12287 | /* Register previous table in the registry with reference and named entry. |
| 12288 | * The function hlua_register_metatable() pops the stack, so we |
| 12289 | * previously create a copy of the table. |
| 12290 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12291 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12292 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12293 | |
| 12294 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12295 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12296 | |
| 12297 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12298 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12299 | |
| 12300 | /* |
| 12301 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12302 | * Register "CertCache" class |
| 12303 | * |
| 12304 | */ |
| 12305 | |
| 12306 | /* Create and fill the metatable. */ |
| 12307 | lua_newtable(L); |
| 12308 | /* Register */ |
| 12309 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12310 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12311 | |
| 12312 | /* |
| 12313 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12314 | * Register class Channel |
| 12315 | * |
| 12316 | */ |
| 12317 | |
| 12318 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12319 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12320 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12321 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12322 | lua_pushstring(L, "__index"); |
| 12323 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12324 | |
| 12325 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12326 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12327 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12328 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12329 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12330 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12331 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12332 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12333 | hlua_class_function(L, "send", hlua_channel_send); |
| 12334 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12335 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12336 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12337 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12338 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12339 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12340 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12341 | /* Deprecated API */ |
| 12342 | hlua_class_function(L, "get", hlua_channel_get); |
| 12343 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12344 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12345 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12346 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12347 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12348 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12349 | |
| 12350 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12351 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12352 | |
| 12353 | /* |
| 12354 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12355 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12356 | * |
| 12357 | */ |
| 12358 | |
| 12359 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12360 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12361 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12362 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12363 | lua_pushstring(L, "__index"); |
| 12364 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12365 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12366 | /* Browse existing fetches and create the associated |
| 12367 | * object method. |
| 12368 | */ |
| 12369 | sf = NULL; |
| 12370 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12371 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12372 | * by an underscore. |
| 12373 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12374 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12375 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12376 | if (*p == '.' || *p == '-' || *p == '+') |
| 12377 | *p = '_'; |
| 12378 | |
| 12379 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12380 | lua_pushstring(L, trash.area); |
| 12381 | lua_pushlightuserdata(L, sf); |
| 12382 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12383 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12384 | } |
| 12385 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12386 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12387 | |
| 12388 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12389 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12390 | |
| 12391 | /* |
| 12392 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12393 | * Register class Converters |
| 12394 | * |
| 12395 | */ |
| 12396 | |
| 12397 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12398 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12399 | |
| 12400 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12401 | lua_pushstring(L, "__index"); |
| 12402 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12403 | |
| 12404 | /* Browse existing converters and create the associated |
| 12405 | * object method. |
| 12406 | */ |
| 12407 | sc = NULL; |
| 12408 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12409 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12410 | * by an underscore. |
| 12411 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12412 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12413 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12414 | if (*p == '.' || *p == '-' || *p == '+') |
| 12415 | *p = '_'; |
| 12416 | |
| 12417 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12418 | lua_pushstring(L, trash.area); |
| 12419 | lua_pushlightuserdata(L, sc); |
| 12420 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12421 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12422 | } |
| 12423 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12424 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12425 | |
| 12426 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12427 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12428 | |
| 12429 | /* |
| 12430 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12431 | * Register class HTTP |
| 12432 | * |
| 12433 | */ |
| 12434 | |
| 12435 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12436 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12437 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12438 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12439 | lua_pushstring(L, "__index"); |
| 12440 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12441 | |
| 12442 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12443 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12444 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12445 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12446 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12447 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12448 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12449 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12450 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12451 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12452 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12453 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12454 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12455 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12456 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12457 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12458 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12459 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12460 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12461 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12462 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12463 | |
| 12464 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12465 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12466 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12467 | /* |
| 12468 | * |
| 12469 | * Register class HTTPMessage |
| 12470 | * |
| 12471 | */ |
| 12472 | |
| 12473 | /* Create and fill the metatable. */ |
| 12474 | lua_newtable(L); |
| 12475 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12476 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12477 | lua_pushstring(L, "__index"); |
| 12478 | lua_newtable(L); |
| 12479 | |
| 12480 | /* Register Lua functions. */ |
| 12481 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12482 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12483 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12484 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12485 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12486 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12487 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12488 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12489 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12490 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12491 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12492 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12493 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12494 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12495 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12496 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12497 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12498 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12499 | |
| 12500 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12501 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12502 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12503 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12504 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12505 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12506 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12507 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12508 | |
| 12509 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12510 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12511 | |
| 12512 | lua_rawset(L, -3); |
| 12513 | |
| 12514 | /* Register previous table in the registry with reference and named entry. */ |
| 12515 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12516 | |
| 12517 | /* |
| 12518 | * |
| 12519 | * Register class HTTPClient |
| 12520 | * |
| 12521 | */ |
| 12522 | |
| 12523 | /* Create and fill the metatable. */ |
| 12524 | lua_newtable(L); |
| 12525 | lua_pushstring(L, "__index"); |
| 12526 | lua_newtable(L); |
| 12527 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12528 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12529 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12530 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12531 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12532 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12533 | /* Register the garbage collector entry. */ |
| 12534 | lua_pushstring(L, "__gc"); |
| 12535 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12536 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12537 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12538 | |
| 12539 | |
| 12540 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12541 | /* |
| 12542 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12543 | * Register class AppletTCP |
| 12544 | * |
| 12545 | */ |
| 12546 | |
| 12547 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12548 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12549 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12550 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12551 | lua_pushstring(L, "__index"); |
| 12552 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12553 | |
| 12554 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12555 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12556 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12557 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12558 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12559 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12560 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12561 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12562 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12563 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12564 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12565 | |
| 12566 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12567 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12568 | |
| 12569 | /* |
| 12570 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12571 | * Register class AppletHTTP |
| 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 | a30b5db | 2015-09-18 09:04:27 +0200 | [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 | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12581 | |
| 12582 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12583 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12584 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12585 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12586 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12587 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12588 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12589 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12590 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12591 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12592 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12593 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12594 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12595 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12596 | |
| 12597 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12598 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12599 | |
| 12600 | /* |
| 12601 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12602 | * Register class TXN |
| 12603 | * |
| 12604 | */ |
| 12605 | |
| 12606 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12607 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12608 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12609 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12610 | lua_pushstring(L, "__index"); |
| 12611 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12612 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12613 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12614 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12615 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12616 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12617 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12618 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12619 | hlua_class_function(L, "done", hlua_txn_done); |
| 12620 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12621 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12622 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12623 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12624 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12625 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12626 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12627 | hlua_class_function(L, "log", hlua_txn_log); |
| 12628 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12629 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12630 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12631 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12632 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12633 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12634 | |
| 12635 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12636 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12637 | |
| 12638 | /* |
| 12639 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12640 | * Register class reply |
| 12641 | * |
| 12642 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12643 | lua_newtable(L); |
| 12644 | lua_pushstring(L, "__index"); |
| 12645 | lua_newtable(L); |
| 12646 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12647 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12648 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12649 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12650 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12651 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12652 | |
| 12653 | |
| 12654 | /* |
| 12655 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12656 | * Register class Socket |
| 12657 | * |
| 12658 | */ |
| 12659 | |
| 12660 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12661 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12662 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12663 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12664 | lua_pushstring(L, "__index"); |
| 12665 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12666 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12667 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12668 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12669 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12670 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12671 | hlua_class_function(L, "send", hlua_socket_send); |
| 12672 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12673 | hlua_class_function(L, "close", hlua_socket_close); |
| 12674 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12675 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12676 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12677 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12678 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12679 | 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] | 12680 | |
| 12681 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12682 | lua_pushstring(L, "__gc"); |
| 12683 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12684 | 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] | 12685 | |
| 12686 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12687 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12688 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12689 | lua_atpanic(L, hlua_panic_safe); |
| 12690 | |
| 12691 | return L; |
| 12692 | } |
| 12693 | |
| 12694 | void hlua_init(void) { |
| 12695 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12696 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12697 | #ifdef USE_OPENSSL |
| 12698 | struct srv_kw *kw; |
| 12699 | int tmp_error; |
| 12700 | char *error; |
| 12701 | char *args[] = { /* SSL client configuration. */ |
| 12702 | "ssl", |
| 12703 | "verify", |
| 12704 | "none", |
| 12705 | NULL |
| 12706 | }; |
| 12707 | #endif |
| 12708 | |
| 12709 | /* Init post init function list head */ |
| 12710 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12711 | LIST_INIT(&hlua_init_functions[i]); |
| 12712 | |
| 12713 | /* Init state for common/shared lua parts */ |
| 12714 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12715 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12716 | hlua_states[0] = hlua_init_state(0); |
| 12717 | |
| 12718 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12719 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12720 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12721 | hlua_states[1] = hlua_init_state(1); |
| 12722 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12723 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12724 | 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] | 12725 | if (!socket_proxy) { |
| 12726 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12727 | exit(1); |
| 12728 | } |
| 12729 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12730 | |
| 12731 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12732 | socket_tcp = new_server(socket_proxy); |
| 12733 | if (!socket_tcp) { |
| 12734 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12735 | exit(1); |
| 12736 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12737 | |
| 12738 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12739 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12740 | socket_ssl = new_server(socket_proxy); |
| 12741 | if (!socket_ssl) { |
| 12742 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12743 | exit(1); |
| 12744 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12745 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12746 | socket_ssl->use_ssl = 1; |
| 12747 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12748 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12749 | for (i = 0; args[i] != NULL; i++) { |
| 12750 | 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] | 12751 | /* |
| 12752 | * |
| 12753 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12754 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12755 | * features like client certificates and ssl_verify. |
| 12756 | * |
| 12757 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12758 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12759 | if (tmp_error != 0) { |
| 12760 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12761 | abort(); /* This must be never arrives because the command line |
| 12762 | not editable by the user. */ |
| 12763 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12764 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12765 | } |
| 12766 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12767 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12768 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12769 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12770 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12771 | static void hlua_deinit() |
| 12772 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12773 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12774 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12775 | |
| 12776 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12777 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12778 | |
| 12779 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12780 | if (hlua_states[thr]) |
| 12781 | lua_close(hlua_states[thr]); |
| 12782 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12783 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12784 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12785 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12786 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12787 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12788 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12789 | |
| 12790 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12791 | } |
| 12792 | |
| 12793 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12794 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12795 | static void hlua_register_build_options(void) |
| 12796 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12797 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12798 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12799 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12800 | hap_register_build_opts(ptr, 1); |
| 12801 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12802 | |
| 12803 | INITCALL0(STG_REGISTER, hlua_register_build_options); |