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 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 155 | static inline void lua_take_global_lock() |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 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 | |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 165 | /* lua lock helpers: only lock when required */ |
| 166 | static inline void hlua_lock(struct hlua *hlua) |
| 167 | { |
| 168 | if (hlua->state_id == 0) |
| 169 | lua_take_global_lock(); |
| 170 | } |
| 171 | static inline void hlua_unlock(struct hlua *hlua) |
| 172 | { |
| 173 | if (hlua->state_id == 0) |
| 174 | lua_drop_global_lock(); |
| 175 | } |
| 176 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 177 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 178 | ({ \ |
| 179 | int ret; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 180 | hlua_lock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 181 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 182 | lua_atpanic(__L, hlua_panic_safe); \ |
| 183 | ret = 0; \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 184 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 185 | } else { \ |
| 186 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 187 | ret = 1; \ |
| 188 | } \ |
| 189 | ret; \ |
| 190 | }) |
| 191 | |
| 192 | /* If we are the last function catching Lua errors, we |
| 193 | * must reset the panic function. |
| 194 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 195 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 196 | do { \ |
| 197 | lua_atpanic(__L, hlua_panic_safe); \ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 198 | hlua_unlock(__HLUA); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 199 | } while(0) |
| 200 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 201 | #define SET_SAFE_LJMP(__HLUA) \ |
| 202 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 203 | |
| 204 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 205 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 206 | |
| 207 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 208 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 209 | |
| 210 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 211 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 212 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 213 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 214 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 215 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 216 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 217 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 218 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 219 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 220 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 221 | /* The main Lua execution context. The 0 index is the |
| 222 | * common state shared by all threads. |
| 223 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 224 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 225 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 226 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 227 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 228 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 229 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 230 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 231 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 232 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 233 | struct hlua_reg_filter { |
| 234 | char *name; |
| 235 | int flt_ref[MAX_THREADS + 1]; |
| 236 | int fun_ref[MAX_THREADS + 1]; |
| 237 | struct list l; |
| 238 | }; |
| 239 | |
| 240 | struct hlua_flt_config { |
| 241 | struct hlua_reg_filter *reg; |
| 242 | int ref[MAX_THREADS + 1]; |
| 243 | char **args; |
| 244 | }; |
| 245 | |
| 246 | struct hlua_flt_ctx { |
| 247 | int ref; /* ref to the filter lua object */ |
| 248 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 249 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 250 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 251 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 252 | }; |
| 253 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 254 | /* appctx context used by the cosockets */ |
| 255 | struct hlua_csk_ctx { |
| 256 | int connected; |
| 257 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 258 | struct list wake_on_read; |
| 259 | struct list wake_on_write; |
| 260 | struct appctx *appctx; |
| 261 | int die; |
| 262 | }; |
| 263 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 264 | /* appctx context used by TCP services */ |
| 265 | struct hlua_tcp_ctx { |
| 266 | struct hlua *hlua; |
| 267 | int flags; |
| 268 | struct task *task; |
| 269 | }; |
| 270 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 271 | /* appctx context used by HTTP services */ |
| 272 | struct hlua_http_ctx { |
| 273 | struct hlua *hlua; |
| 274 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 275 | int flags; |
| 276 | int status; |
| 277 | const char *reason; |
| 278 | struct task *task; |
| 279 | }; |
| 280 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 281 | /* used by registered CLI keywords */ |
| 282 | struct hlua_cli_ctx { |
| 283 | struct hlua *hlua; |
| 284 | struct task *task; |
| 285 | struct hlua_function *fcn; |
| 286 | }; |
| 287 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 288 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 289 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 290 | static int hlua_filter_from_payload(struct filter *filter); |
| 291 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 292 | /* This is the chained list of struct hlua_flt referenced |
| 293 | * for haproxy filters. It is used for a post-initialisation control. |
| 294 | */ |
| 295 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 296 | |
| 297 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 298 | /* This is the memory pool containing struct lua for applets |
| 299 | * (including cli). |
| 300 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 301 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 302 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 303 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 304 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 305 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 306 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 307 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 308 | #endif |
| 309 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 310 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 311 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 312 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 313 | /* The following variables contains the reference of the different |
| 314 | * Lua classes. These references are useful for identify metadata |
| 315 | * associated with an object. |
| 316 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 317 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 318 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 319 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 320 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 321 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 322 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 323 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 324 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 325 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 326 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 327 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 328 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 329 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 330 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 331 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 332 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 333 | * because a task may remain alive during all the haproxy execution. |
| 334 | */ |
| 335 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 336 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 337 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 338 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 339 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 340 | * it is used for preventing infinite loops. |
| 341 | * |
| 342 | * I test the scheer with an infinite loop containing one incrementation |
| 343 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 344 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 345 | * to one interrupt each 10 000 instructions. |
| 346 | * |
| 347 | * configured | Number of |
| 348 | * instructions | loops executed |
| 349 | * between two | in milions |
| 350 | * forced yields | |
| 351 | * ---------------+--------------- |
| 352 | * 10 | 160 |
| 353 | * 500 | 670 |
| 354 | * 1000 | 680 |
| 355 | * 5000 | 700 |
| 356 | * 7000 | 700 |
| 357 | * 8000 | 700 |
| 358 | * 9000 | 710 <- ceil |
| 359 | * 10000 | 710 |
| 360 | * 100000 | 710 |
| 361 | * 1000000 | 710 |
| 362 | * |
| 363 | */ |
| 364 | static unsigned int hlua_nb_instruction = 10000; |
| 365 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 366 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 367 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 368 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 369 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 370 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 371 | */ |
| 372 | struct hlua_mem_allocator { |
| 373 | size_t allocated; |
| 374 | size_t limit; |
| 375 | }; |
| 376 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 377 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 378 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | /* These functions converts types between HAProxy internal args or |
| 380 | * sample and LUA types. Another function permits to check if the |
| 381 | * LUA stack contains arguments according with an required ARG_T |
| 382 | * format. |
| 383 | */ |
| 384 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 385 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 386 | __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] | 387 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 388 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 389 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 390 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 391 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 392 | __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] | 393 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 394 | struct prepend_path { |
| 395 | struct list l; |
| 396 | char *type; |
| 397 | char *path; |
| 398 | }; |
| 399 | |
| 400 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 401 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 402 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 403 | do { \ |
| 404 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 405 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 406 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 407 | } while (0) |
| 408 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 409 | static inline struct hlua_function *new_hlua_function() |
| 410 | { |
| 411 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 412 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 413 | |
| 414 | fcn = calloc(1, sizeof(*fcn)); |
| 415 | if (!fcn) |
| 416 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 417 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 418 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 419 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 420 | return fcn; |
| 421 | } |
| 422 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 423 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 424 | { |
| 425 | if (!fcn) |
| 426 | return; |
| 427 | if (fcn->name) |
| 428 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 429 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 430 | ha_free(&fcn); |
| 431 | } |
| 432 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 433 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 434 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 435 | { |
| 436 | if (fcn->function_ref[0] == -1) |
| 437 | return tid + 1; |
| 438 | return 0; |
| 439 | } |
| 440 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 441 | /* Create a new registered filter. Only its name is filled */ |
| 442 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 443 | { |
| 444 | struct hlua_reg_filter *reg_flt; |
| 445 | int i; |
| 446 | |
| 447 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 448 | if (!reg_flt) |
| 449 | return NULL; |
| 450 | reg_flt->name = strdup(name); |
| 451 | if (!reg_flt->name) { |
| 452 | free(reg_flt); |
| 453 | return NULL; |
| 454 | } |
| 455 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 456 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 457 | reg_flt->flt_ref[i] = -1; |
| 458 | reg_flt->fun_ref[i] = -1; |
| 459 | } |
| 460 | return reg_flt; |
| 461 | } |
| 462 | |
| 463 | /* Release a registered filter */ |
| 464 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 465 | { |
| 466 | if (!reg_flt) |
| 467 | return; |
| 468 | if (reg_flt->name) |
| 469 | ha_free(®_flt->name); |
| 470 | LIST_DELETE(®_flt->l); |
| 471 | ha_free(®_flt); |
| 472 | } |
| 473 | |
| 474 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 475 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 476 | { |
| 477 | if (reg_flt->fun_ref[0] == -1) |
| 478 | return tid + 1; |
| 479 | return 0; |
| 480 | } |
| 481 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 482 | /* Used to check an Lua function type in the stack. It creates and |
| 483 | * returns a reference of the function. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 484 | * error if the argument is not a "function". |
| 485 | * When no longer used, the ref must be released with hlua_unref() |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 486 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 487 | __LJMP int hlua_checkfunction(lua_State *L, int argno) |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 488 | { |
| 489 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 490 | 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] | 491 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 492 | } |
| 493 | lua_pushvalue(L, argno); |
| 494 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 495 | } |
| 496 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 497 | /* Used to check an Lua table type in the stack. It creates and |
| 498 | * returns a reference of the table. This function throws an |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 499 | * error if the argument is not a "table". |
| 500 | * When no longer used, the ref must be released with hlua_unref() |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 501 | */ |
Aurelien DARRAGON | 9ee0d04 | 2023-03-20 18:36:08 +0100 | [diff] [blame] | 502 | __LJMP int hlua_checktable(lua_State *L, int argno) |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 503 | { |
| 504 | if (!lua_istable(L, argno)) { |
| 505 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 506 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 507 | } |
| 508 | lua_pushvalue(L, argno); |
| 509 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 510 | } |
| 511 | |
Aurelien DARRAGON | f8f8a2b | 2023-03-02 17:50:49 +0100 | [diff] [blame] | 512 | /* Get a reference to the object that is at the top of the stack |
| 513 | * The referenced object will be popped from the stack |
| 514 | * |
| 515 | * The function returns the reference to the object which must |
| 516 | * be cleared using hlua_unref() when no longer used |
| 517 | */ |
| 518 | __LJMP int hlua_ref(lua_State *L) |
| 519 | { |
| 520 | return MAY_LJMP(luaL_ref(L, LUA_REGISTRYINDEX)); |
| 521 | } |
| 522 | |
| 523 | /* Pushes a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 524 | * on <L> stack |
| 525 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 526 | * |
| 527 | * When the reference is no longer used, it should be released by calling |
| 528 | * hlua_unref() |
| 529 | * |
| 530 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 531 | * parent state that the one used to get the reference. |
| 532 | */ |
| 533 | void hlua_pushref(lua_State *L, int ref) |
| 534 | { |
| 535 | lua_rawgeti(L, LUA_REGISTRYINDEX, ref); |
| 536 | } |
| 537 | |
| 538 | /* Releases a reference previously created using luaL_ref(L, LUA_REGISTRYINDEX) |
| 539 | * (ie: hlua_checkfunction(), hlua_checktable() or hlua_ref()) |
| 540 | * |
| 541 | * This will allow the reference to be reused and the referred object |
| 542 | * to be garbage collected. |
| 543 | * |
| 544 | * <L> can be from any co-routine as long as it belongs to the same lua |
| 545 | * parent state that the one used to get the reference. |
| 546 | */ |
| 547 | void hlua_unref(lua_State *L, int ref) |
| 548 | { |
| 549 | luaL_unref(L, LUA_REGISTRYINDEX, ref); |
| 550 | } |
| 551 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 552 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 553 | { |
| 554 | lua_Debug ar; |
| 555 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 556 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 557 | |
| 558 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 559 | /* Fill fields: |
| 560 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 561 | * 'l': fills in the field currentline; |
| 562 | * 'n': fills in the field name and namewhat; |
| 563 | * 't': fills in the field istailcall; |
| 564 | */ |
| 565 | lua_getinfo(L, "Slnt", &ar); |
| 566 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 567 | /* skip these empty entries, usually they come from deep C functions */ |
| 568 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 569 | continue; |
| 570 | |
| 571 | /* Add separator */ |
| 572 | if (b_data(msg)) |
| 573 | chunk_appendf(msg, "%s", sep); |
| 574 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 575 | /* Append code localisation */ |
| 576 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 577 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 578 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 579 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 580 | |
| 581 | /* |
| 582 | * Get function name |
| 583 | * |
| 584 | * if namewhat is no empty, name is defined. |
| 585 | * what contains "Lua" for Lua function, "C" for C function, |
| 586 | * or "main" for main code. |
| 587 | */ |
| 588 | 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] | 589 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 590 | |
| 591 | 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] | 592 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 593 | |
| 594 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 595 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 596 | |
| 597 | else /* nothing left... */ |
| 598 | chunk_appendf(msg, "?"); |
| 599 | |
| 600 | |
| 601 | /* Display tailed call */ |
| 602 | if (ar.istailcall) |
| 603 | chunk_appendf(msg, " ..."); |
| 604 | } |
| 605 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 606 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 610 | /* This function check the number of arguments available in the |
| 611 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 612 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 613 | */ |
| 614 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 615 | { |
| 616 | if (lua_gettop(L) == nb) |
| 617 | return; |
| 618 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 619 | } |
| 620 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 621 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 622 | * and the line number where the error is encountered. |
| 623 | */ |
| 624 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 625 | { |
| 626 | va_list argp; |
| 627 | va_start(argp, fmt); |
| 628 | luaL_where(L, 1); |
| 629 | lua_pushvfstring(L, fmt, argp); |
| 630 | va_end(argp); |
| 631 | lua_concat(L, 2); |
| 632 | return 1; |
| 633 | } |
| 634 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 635 | /* This functions is used with sample fetch and converters. It |
| 636 | * converts the HAProxy configuration argument in a lua stack |
| 637 | * values. |
| 638 | * |
| 639 | * It takes an array of "arg", and each entry of the array is |
| 640 | * converted and pushed in the LUA stack. |
| 641 | */ |
| 642 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 643 | { |
| 644 | switch (arg->type) { |
| 645 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 646 | case ARGT_TIME: |
| 647 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 648 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 649 | break; |
| 650 | |
| 651 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 652 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | break; |
| 654 | |
| 655 | case ARGT_IPV4: |
| 656 | case ARGT_IPV6: |
| 657 | case ARGT_MSK4: |
| 658 | case ARGT_MSK6: |
| 659 | case ARGT_FE: |
| 660 | case ARGT_BE: |
| 661 | case ARGT_TAB: |
| 662 | case ARGT_SRV: |
| 663 | case ARGT_USR: |
| 664 | case ARGT_MAP: |
| 665 | default: |
| 666 | lua_pushnil(L); |
| 667 | break; |
| 668 | } |
| 669 | return 1; |
| 670 | } |
| 671 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 672 | /* 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] | 673 | * 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] | 674 | * with sample fetch wrappers. The input arguments are given to the |
| 675 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | */ |
| 677 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 678 | { |
| 679 | switch (lua_type(L, ud)) { |
| 680 | |
| 681 | case LUA_TNUMBER: |
| 682 | case LUA_TBOOLEAN: |
| 683 | arg->type = ARGT_SINT; |
| 684 | arg->data.sint = lua_tointeger(L, ud); |
| 685 | break; |
| 686 | |
| 687 | case LUA_TSTRING: |
| 688 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 689 | 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] | 690 | /* 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] | 691 | 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] | 692 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 693 | break; |
| 694 | |
| 695 | case LUA_TUSERDATA: |
| 696 | case LUA_TNIL: |
| 697 | case LUA_TTABLE: |
| 698 | case LUA_TFUNCTION: |
| 699 | case LUA_TTHREAD: |
| 700 | case LUA_TLIGHTUSERDATA: |
| 701 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 702 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 703 | break; |
| 704 | } |
| 705 | return 1; |
| 706 | } |
| 707 | |
| 708 | /* the following functions are used to convert a struct sample |
| 709 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 710 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 711 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 712 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 713 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 714 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 715 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 716 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 717 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 718 | break; |
| 719 | |
| 720 | case SMP_T_BIN: |
| 721 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 722 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 723 | break; |
| 724 | |
| 725 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 726 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 727 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 728 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 729 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 730 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 731 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 732 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 733 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 734 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 735 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 736 | 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] | 737 | break; |
| 738 | default: |
| 739 | lua_pushnil(L); |
| 740 | break; |
| 741 | } |
| 742 | break; |
| 743 | |
| 744 | case SMP_T_IPV4: |
| 745 | case SMP_T_IPV6: |
| 746 | 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] | 747 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 748 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 749 | 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] | 750 | else |
| 751 | lua_pushnil(L); |
| 752 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 753 | default: |
| 754 | lua_pushnil(L); |
| 755 | break; |
| 756 | } |
| 757 | return 1; |
| 758 | } |
| 759 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 760 | /* the following functions are used to convert a struct sample |
| 761 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 762 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 763 | */ |
| 764 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 765 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 766 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 767 | |
| 768 | case SMP_T_BIN: |
| 769 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 770 | 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] | 771 | break; |
| 772 | |
| 773 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 774 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 775 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 776 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 777 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 778 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 779 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 780 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 781 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 782 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 783 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 784 | 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] | 785 | break; |
| 786 | default: |
| 787 | lua_pushstring(L, ""); |
| 788 | break; |
| 789 | } |
| 790 | break; |
| 791 | |
| 792 | case SMP_T_SINT: |
| 793 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 794 | case SMP_T_IPV4: |
| 795 | case SMP_T_IPV6: |
| 796 | 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] | 797 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 798 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 799 | 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] | 800 | else |
| 801 | lua_pushstring(L, ""); |
| 802 | break; |
| 803 | default: |
| 804 | lua_pushstring(L, ""); |
| 805 | break; |
| 806 | } |
| 807 | return 1; |
| 808 | } |
| 809 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 810 | /* the following functions are used to convert an Lua type in a |
| 811 | * struct sample. This is useful to provide data from a converter |
| 812 | * to the LUA code. |
| 813 | */ |
| 814 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 815 | { |
| 816 | switch (lua_type(L, ud)) { |
| 817 | |
| 818 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 819 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 820 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 821 | break; |
| 822 | |
| 823 | |
| 824 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 825 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 826 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | break; |
| 828 | |
| 829 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 830 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 831 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 832 | 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] | 833 | /* 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] | 834 | 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] | 835 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 836 | break; |
| 837 | |
| 838 | case LUA_TUSERDATA: |
| 839 | case LUA_TNIL: |
| 840 | case LUA_TTABLE: |
| 841 | case LUA_TFUNCTION: |
| 842 | case LUA_TTHREAD: |
| 843 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 844 | case LUA_TNONE: |
| 845 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 846 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 847 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 848 | break; |
| 849 | } |
| 850 | return 1; |
| 851 | } |
| 852 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 853 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 854 | * 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] | 855 | * 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] | 856 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 857 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 858 | * 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] | 859 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 860 | __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] | 861 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 862 | { |
| 863 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 864 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 865 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 866 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 867 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 868 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 869 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 870 | |
| 871 | idx = 0; |
| 872 | min_arg = ARGM(mask); |
| 873 | mask >>= ARGM_BITS; |
| 874 | |
| 875 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 876 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 877 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 878 | /* Check for mandatory arguments. */ |
| 879 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 880 | if (idx < min_arg) { |
| 881 | |
| 882 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 883 | if (idx > 0) { |
| 884 | msg = "Mandatory argument expected"; |
| 885 | goto error; |
| 886 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 887 | |
| 888 | /* If first argument have a certain type, some default values |
| 889 | * may be used. See the function smp_resolve_args(). |
| 890 | */ |
| 891 | switch (mask & ARGT_MASK) { |
| 892 | |
| 893 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | if (!(p->cap & PR_CAP_FE)) { |
| 895 | msg = "Mandatory argument expected"; |
| 896 | goto error; |
| 897 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 898 | argp[idx].data.prx = p; |
| 899 | argp[idx].type = ARGT_FE; |
| 900 | argp[idx+1].type = ARGT_STOP; |
| 901 | break; |
| 902 | |
| 903 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 904 | if (!(p->cap & PR_CAP_BE)) { |
| 905 | msg = "Mandatory argument expected"; |
| 906 | goto error; |
| 907 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 908 | argp[idx].data.prx = p; |
| 909 | argp[idx].type = ARGT_BE; |
| 910 | argp[idx+1].type = ARGT_STOP; |
| 911 | break; |
| 912 | |
| 913 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 914 | if (!p->table) { |
| 915 | msg = "Mandatory argument expected"; |
| 916 | goto error; |
| 917 | } |
| 918 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 919 | argp[idx].type = ARGT_TAB; |
| 920 | argp[idx+1].type = ARGT_STOP; |
| 921 | break; |
| 922 | |
| 923 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 924 | msg = "Mandatory argument expected"; |
| 925 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 926 | break; |
| 927 | } |
| 928 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 929 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 930 | } |
| 931 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 932 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 933 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 934 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | msg = "Last argument expected"; |
| 936 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 937 | } |
| 938 | |
| 939 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 940 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 941 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 942 | } |
| 943 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 944 | /* Convert some argument types. All string in argp[] are for not |
| 945 | * duplicated yet. |
| 946 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 947 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 948 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 949 | if (argp[idx].type != ARGT_SINT) { |
| 950 | msg = "integer expected"; |
| 951 | goto error; |
| 952 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | argp[idx].type = ARGT_SINT; |
| 954 | break; |
| 955 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 956 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 957 | if (argp[idx].type != ARGT_SINT) { |
| 958 | msg = "integer expected"; |
| 959 | goto error; |
| 960 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 961 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 962 | break; |
| 963 | |
| 964 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 965 | if (argp[idx].type != ARGT_SINT) { |
| 966 | msg = "integer expected"; |
| 967 | goto error; |
| 968 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 969 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 970 | break; |
| 971 | |
| 972 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 973 | if (argp[idx].type != ARGT_STR) { |
| 974 | msg = "string expected"; |
| 975 | goto error; |
| 976 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 977 | 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] | 978 | if (!argp[idx].data.prx) { |
| 979 | msg = "frontend doesn't exist"; |
| 980 | goto error; |
| 981 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 982 | argp[idx].type = ARGT_FE; |
| 983 | break; |
| 984 | |
| 985 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 986 | if (argp[idx].type != ARGT_STR) { |
| 987 | msg = "string expected"; |
| 988 | goto error; |
| 989 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 990 | 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] | 991 | if (!argp[idx].data.prx) { |
| 992 | msg = "backend doesn't exist"; |
| 993 | goto error; |
| 994 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 995 | argp[idx].type = ARGT_BE; |
| 996 | break; |
| 997 | |
| 998 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 999 | if (argp[idx].type != ARGT_STR) { |
| 1000 | msg = "string expected"; |
| 1001 | goto error; |
| 1002 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1003 | 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] | 1004 | if (!argp[idx].data.t) { |
| 1005 | msg = "table doesn't exist"; |
| 1006 | goto error; |
| 1007 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1008 | argp[idx].type = ARGT_TAB; |
| 1009 | break; |
| 1010 | |
| 1011 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1012 | if (argp[idx].type != ARGT_STR) { |
| 1013 | msg = "string expected"; |
| 1014 | goto error; |
| 1015 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1016 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1017 | if (sname) { |
| 1018 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1019 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 1020 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1021 | if (!px) { |
| 1022 | msg = "backend doesn't exist"; |
| 1023 | goto error; |
| 1024 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1025 | } |
| 1026 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 1027 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1028 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1029 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1030 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1031 | if (!argp[idx].data.srv) { |
| 1032 | msg = "server doesn't exist"; |
| 1033 | goto error; |
| 1034 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1035 | argp[idx].type = ARGT_SRV; |
| 1036 | break; |
| 1037 | |
| 1038 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1039 | if (argp[idx].type != ARGT_STR) { |
| 1040 | msg = "string expected"; |
| 1041 | goto error; |
| 1042 | } |
| 1043 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1044 | msg = "invalid IPv4 address"; |
| 1045 | goto error; |
| 1046 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1047 | argp[idx].type = ARGT_IPV4; |
| 1048 | break; |
| 1049 | |
| 1050 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1051 | if (argp[idx].type == ARGT_SINT) |
| 1052 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1053 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1054 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1055 | msg = "invalid IPv4 mask"; |
| 1056 | goto error; |
| 1057 | } |
| 1058 | } |
| 1059 | else { |
| 1060 | msg = "integer or string expected"; |
| 1061 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1062 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1063 | argp[idx].type = ARGT_MSK4; |
| 1064 | break; |
| 1065 | |
| 1066 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1067 | if (argp[idx].type != ARGT_STR) { |
| 1068 | msg = "string expected"; |
| 1069 | goto error; |
| 1070 | } |
| 1071 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1072 | msg = "invalid IPv6 address"; |
| 1073 | goto error; |
| 1074 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1075 | argp[idx].type = ARGT_IPV6; |
| 1076 | break; |
| 1077 | |
| 1078 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1079 | if (argp[idx].type == ARGT_SINT) |
| 1080 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1081 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1082 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1083 | msg = "invalid IPv6 mask"; |
| 1084 | goto error; |
| 1085 | } |
| 1086 | } |
| 1087 | else { |
| 1088 | msg = "integer or string expected"; |
| 1089 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1090 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1091 | argp[idx].type = ARGT_MSK6; |
| 1092 | break; |
| 1093 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1094 | case ARGT_REG: |
| 1095 | if (argp[idx].type != ARGT_STR) { |
| 1096 | msg = "string expected"; |
| 1097 | goto error; |
| 1098 | } |
| 1099 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1100 | if (!reg) { |
| 1101 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1102 | argp[idx].data.str.area, err); |
| 1103 | free(err); |
| 1104 | goto error; |
| 1105 | } |
| 1106 | argp[idx].type = ARGT_REG; |
| 1107 | argp[idx].data.reg = reg; |
| 1108 | break; |
| 1109 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1110 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1111 | if (argp[idx].type != ARGT_STR) { |
| 1112 | msg = "string expected"; |
| 1113 | goto error; |
| 1114 | } |
| 1115 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1116 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1117 | ul = p->uri_auth->userlist; |
| 1118 | else |
| 1119 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1120 | |
| 1121 | if (!ul) { |
| 1122 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1123 | goto error; |
| 1124 | } |
| 1125 | argp[idx].type = ARGT_USR; |
| 1126 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1127 | break; |
| 1128 | |
| 1129 | case ARGT_STR: |
| 1130 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1131 | msg = "unable to duplicate string arg"; |
| 1132 | goto error; |
| 1133 | } |
| 1134 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1135 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1136 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1137 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1138 | msg = "type not yet supported"; |
| 1139 | goto error; |
| 1140 | break; |
| 1141 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | /* Check for type of argument. */ |
| 1145 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1146 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1147 | arg_type_names[(mask & ARGT_MASK)], |
| 1148 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1149 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1150 | } |
| 1151 | |
| 1152 | /* Next argument. */ |
| 1153 | mask >>= ARGT_BITS; |
| 1154 | idx++; |
| 1155 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1156 | return 0; |
| 1157 | |
| 1158 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1159 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1160 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1161 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1162 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1163 | } |
| 1164 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1166 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1168 | * |
| 1169 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1171 | */ |
| 1172 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1173 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1174 | struct hlua **hlua = lua_getextraspace(L); |
| 1175 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | } |
| 1177 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1178 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1179 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1180 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1181 | } |
| 1182 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1183 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1184 | * currently is executing from within a Lua function. One line per entry will |
| 1185 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1186 | * Lua function is not detected, NULL is returned. |
| 1187 | */ |
| 1188 | const char *hlua_show_current_location(const char *pfx) |
| 1189 | { |
| 1190 | lua_State *L; |
| 1191 | lua_Debug ar; |
| 1192 | |
| 1193 | /* global or per-thread stack initializing ? */ |
| 1194 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1195 | return hlua_traceback(L, pfx); |
| 1196 | |
| 1197 | /* per-thread stack running ? */ |
| 1198 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1199 | return hlua_traceback(L, pfx); |
| 1200 | |
| 1201 | /* global stack running ? */ |
| 1202 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1203 | return hlua_traceback(L, pfx); |
| 1204 | |
| 1205 | return NULL; |
| 1206 | } |
| 1207 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1208 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1209 | * and on the default syslog server. |
| 1210 | */ |
| 1211 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1212 | { |
| 1213 | struct tm tm; |
| 1214 | char *p; |
| 1215 | |
| 1216 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1217 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1218 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1219 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1220 | /* Break the message if exceed the buffer size. */ |
| 1221 | *(p-4) = ' '; |
| 1222 | *(p-3) = '.'; |
| 1223 | *(p-2) = '.'; |
| 1224 | *(p-1) = '.'; |
| 1225 | break; |
| 1226 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1227 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1228 | *p = *msg; |
| 1229 | else |
| 1230 | *p = '.'; |
| 1231 | } |
| 1232 | *p = '\0'; |
| 1233 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1234 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1235 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1236 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1237 | return; |
| 1238 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1239 | get_localtime(date.tv_sec, &tm); |
| 1240 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1241 | 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] | 1242 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1243 | fflush(stderr); |
| 1244 | } |
| 1245 | } |
| 1246 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1247 | /* This function just ensure that the yield will be always |
| 1248 | * returned with a timeout and permit to set some flags |
| 1249 | */ |
| 1250 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1251 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1252 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1253 | struct hlua *hlua; |
| 1254 | |
| 1255 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1256 | hlua = hlua_gethlua(L); |
| 1257 | if (!hlua) { |
| 1258 | return; |
| 1259 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1260 | |
| 1261 | /* Set the wake timeout. If timeout is required, we set |
| 1262 | * the expiration time. |
| 1263 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1264 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1265 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1266 | hlua->flags |= flags; |
| 1267 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1268 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1269 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1270 | } |
| 1271 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1272 | /* This function initialises the Lua environment stored in the stream. |
| 1273 | * 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] | 1274 | * 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] | 1275 | * |
| 1276 | * This function is particular. it initialises a new Lua thread. If the |
| 1277 | * initialisation fails (example: out of memory error), the lua function |
| 1278 | * throws an error (longjmp). |
| 1279 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1280 | * 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] | 1281 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1282 | * threads appear, the safe environment set a lock to ensure only one |
| 1283 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1284 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1285 | * |
| 1286 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1287 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1288 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1289 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1290 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1291 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1292 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1293 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1294 | 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] | 1295 | { |
| 1296 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1297 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1298 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1299 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1300 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1301 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1302 | MT_LIST_INIT(&lua->hc_list); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1303 | if (!already_safe) { |
| 1304 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1305 | lua->Tref = LUA_REFNIL; |
| 1306 | return 0; |
| 1307 | } |
| 1308 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1309 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1310 | if (!lua->T) { |
| 1311 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1312 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1313 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1314 | return 0; |
| 1315 | } |
| 1316 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1317 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1318 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1319 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1320 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1321 | return 1; |
| 1322 | } |
| 1323 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1324 | /* kill all associated httpclient to this hlua task |
| 1325 | * We must take extra precautions as we're manipulating lua-exposed |
| 1326 | * objects without the main lua lock. |
| 1327 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1328 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1329 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1330 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1331 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1332 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1333 | * another thread sharing the same main lua stack (lua coroutine) |
| 1334 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1335 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1336 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1337 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1338 | * be thread safe in Lua. (From lua co-author: |
| 1339 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1340 | * |
| 1341 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1342 | * since in this case coroutines exclusively run on the same thread |
| 1343 | * (main stack is not shared between OS threads). |
| 1344 | */ |
| 1345 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1346 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1347 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1348 | } |
| 1349 | } |
| 1350 | |
| 1351 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1352 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1353 | * is destroyed. The destroy also the memory context. The struct "lua" |
Aurelien DARRAGON | 60ab0f7 | 2023-03-01 16:45:50 +0100 | [diff] [blame] | 1354 | * will be freed. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1355 | */ |
| 1356 | void hlua_ctx_destroy(struct hlua *lua) |
| 1357 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1358 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1359 | return; |
| 1360 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1361 | if (!lua->T) |
| 1362 | goto end; |
| 1363 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1364 | /* clean all running httpclient */ |
| 1365 | hlua_httpclient_destroy_all(lua); |
| 1366 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1367 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1368 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1369 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1370 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1371 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1372 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1373 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1374 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1375 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1376 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1377 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1378 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1379 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1380 | * without error, we run the GC on the thread pointer. Its freed all |
| 1381 | * the unused memory. |
| 1382 | * If the thread is finnish with an error or is currently yielded, |
| 1383 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1384 | * so e run the GC on the main thread. |
| 1385 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1386 | * the garbage collection. |
| 1387 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1388 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1389 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1390 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1391 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1392 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1393 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1394 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1395 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1396 | |
| 1397 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1398 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1399 | } |
| 1400 | |
| 1401 | /* This function is used to restore the Lua context when a coroutine |
| 1402 | * fails. This function copy the common memory between old coroutine |
| 1403 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1404 | * replaced by the new coroutine. |
| 1405 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1406 | * as string error message and it is copied in the new stack. |
| 1407 | */ |
| 1408 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1409 | { |
| 1410 | lua_State *T; |
| 1411 | int new_ref; |
| 1412 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1413 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1414 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1415 | if (!T) |
| 1416 | return 0; |
| 1417 | |
| 1418 | /* Copy last error message. */ |
| 1419 | if (keep_msg) |
| 1420 | lua_xmove(lua->T, T, 1); |
| 1421 | |
| 1422 | /* Copy data between the coroutines. */ |
| 1423 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1424 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1425 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1426 | |
| 1427 | /* Destroy old data. */ |
| 1428 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1429 | |
| 1430 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1431 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | |
| 1433 | /* Fill the struct with the new coroutine values. */ |
| 1434 | lua->Mref = new_ref; |
| 1435 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1436 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1437 | |
| 1438 | /* Set context. */ |
| 1439 | hlua_sethlua(lua); |
| 1440 | |
| 1441 | return 1; |
| 1442 | } |
| 1443 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1444 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1445 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1446 | struct hlua *hlua; |
| 1447 | |
| 1448 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1449 | hlua = hlua_gethlua(L); |
| 1450 | if (!hlua) |
| 1451 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1452 | |
| 1453 | /* Lua cannot yield when its returning from a function, |
| 1454 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1455 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1456 | */ |
| 1457 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1458 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1459 | return; |
| 1460 | } |
| 1461 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1462 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1463 | * If the state is not yieldable, trying yield causes an error. |
| 1464 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1465 | if (lua_isyieldable(L)) { |
| 1466 | /* note: for converters/fetches.. where yielding is not allowed |
| 1467 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1468 | * instead of rescheduling hlua->task. |
| 1469 | * also: hlua_ctx_resume() will take care of checking execution |
| 1470 | * timeout and re-applying the hook as needed. |
| 1471 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1472 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1473 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1474 | * |
| 1475 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1476 | * |
| 1477 | * Moreover, it seems that we don't want to continue after the yield |
| 1478 | * because the end of the function is about handling unyieldable function, |
| 1479 | * which is not the case here. |
| 1480 | * |
| 1481 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1482 | * in the function. |
| 1483 | */ |
| 1484 | return; |
| 1485 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1486 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1487 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1488 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1489 | hlua->run_time += now_ms - hlua->start_time; |
| 1490 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1491 | lua_pushfstring(L, "execution timeout"); |
| 1492 | WILL_LJMP(lua_error(L)); |
| 1493 | } |
| 1494 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1495 | /* Update the start time. */ |
| 1496 | hlua->start_time = now_ms; |
| 1497 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1498 | /* Try to interrupt the process at the end of the current |
| 1499 | * unyieldable function. |
| 1500 | */ |
| 1501 | 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] | 1502 | } |
| 1503 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1504 | /* This function start or resumes the Lua stack execution. If the flag |
| 1505 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1506 | * The function return an error. |
| 1507 | * |
| 1508 | * The function can returns 4 values: |
| 1509 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1510 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1511 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1512 | * - 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] | 1513 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1514 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1515 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1516 | * 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] | 1517 | * LUA code. |
| 1518 | */ |
| 1519 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1520 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1521 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1522 | int nres; |
| 1523 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1524 | int ret; |
| 1525 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1526 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1527 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1528 | /* Initialise run time counter. */ |
| 1529 | if (!HLUA_IS_RUNNING(lua)) |
| 1530 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1531 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1532 | /* Lock the whole Lua execution. This lock must be before the |
| 1533 | * label "resume_execution". |
| 1534 | */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 1535 | hlua_lock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1536 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1537 | resume_execution: |
| 1538 | |
| 1539 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1540 | * instructions. it is used for preventing infinite loops. |
| 1541 | */ |
| 1542 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1543 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1544 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1545 | HLUA_SET_RUN(lua); |
| 1546 | HLUA_CLR_CTRLYIELD(lua); |
| 1547 | HLUA_CLR_WAKERESWR(lua); |
| 1548 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1549 | HLUA_CLR_NOYIELD(lua); |
| 1550 | if (!yield_allowed) |
| 1551 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1552 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1553 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1554 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1555 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1556 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1557 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1558 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1559 | 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] | 1560 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1561 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1562 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1563 | switch (ret) { |
| 1564 | |
| 1565 | case LUA_OK: |
| 1566 | ret = HLUA_E_OK; |
| 1567 | break; |
| 1568 | |
| 1569 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1570 | /* Check if the execution timeout is expired. It it is the case, we |
| 1571 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1572 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1573 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1574 | lua->run_time += now_ms - lua->start_time; |
| 1575 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1576 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1577 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1578 | break; |
| 1579 | } |
| 1580 | /* Process the forced yield. if the general yield is not allowed or |
| 1581 | * if no task were associated this the current Lua execution |
| 1582 | * coroutine, we resume the execution. Else we want to return in the |
| 1583 | * scheduler and we want to be waked up again, to continue the |
| 1584 | * current Lua execution. So we schedule our own task. |
| 1585 | */ |
| 1586 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1587 | if (!yield_allowed || !lua->task) |
| 1588 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1589 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1590 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1591 | if (!yield_allowed) { |
| 1592 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1593 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1594 | break; |
| 1595 | } |
| 1596 | ret = HLUA_E_AGAIN; |
| 1597 | break; |
| 1598 | |
| 1599 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1600 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1601 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1602 | * because the errors ares the only one mean to return immediately |
| 1603 | * from and lua execution. |
| 1604 | */ |
| 1605 | if (lua->flags & HLUA_EXIT) { |
| 1606 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1607 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1608 | break; |
| 1609 | } |
| 1610 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1611 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1612 | if (!lua_checkstack(lua->T, 1)) { |
| 1613 | ret = HLUA_E_ERR; |
| 1614 | break; |
| 1615 | } |
| 1616 | msg = lua_tostring(lua->T, -1); |
| 1617 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1618 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1619 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1620 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1621 | 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] | 1622 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1623 | 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] | 1624 | ret = HLUA_E_ERRMSG; |
| 1625 | break; |
| 1626 | |
| 1627 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1628 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1629 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1630 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1631 | break; |
| 1632 | |
| 1633 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1634 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1635 | if (!lua_checkstack(lua->T, 1)) { |
| 1636 | ret = HLUA_E_ERR; |
| 1637 | break; |
| 1638 | } |
| 1639 | msg = lua_tostring(lua->T, -1); |
| 1640 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1641 | lua_pop(lua->T, 1); |
| 1642 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1643 | 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] | 1644 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1645 | 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] | 1646 | ret = HLUA_E_ERRMSG; |
| 1647 | break; |
| 1648 | |
| 1649 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1650 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1651 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1652 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | switch (ret) { |
| 1657 | case HLUA_E_AGAIN: |
| 1658 | break; |
| 1659 | |
| 1660 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1661 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1662 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1663 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1664 | break; |
| 1665 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1666 | case HLUA_E_ETMOUT: |
| 1667 | case HLUA_E_NOMEM: |
| 1668 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1669 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1670 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1671 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1672 | hlua_ctx_renew(lua, 0); |
| 1673 | break; |
| 1674 | |
| 1675 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1676 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1677 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1678 | break; |
| 1679 | } |
| 1680 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1681 | /* This is the main exit point, remove the Lua lock. */ |
Aurelien DARRAGON | e36f803 | 2023-03-21 13:22:33 +0100 | [diff] [blame^] | 1682 | hlua_unlock(lua); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1683 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1684 | return ret; |
| 1685 | } |
| 1686 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1687 | /* This function exit the current code. */ |
| 1688 | __LJMP static int hlua_done(lua_State *L) |
| 1689 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1690 | struct hlua *hlua; |
| 1691 | |
| 1692 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1693 | hlua = hlua_gethlua(L); |
| 1694 | if (!hlua) |
| 1695 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1696 | |
| 1697 | hlua->flags |= HLUA_EXIT; |
| 1698 | WILL_LJMP(lua_error(L)); |
| 1699 | |
| 1700 | return 0; |
| 1701 | } |
| 1702 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1703 | /* This function is an LUA binding. It provides a function |
| 1704 | * for deleting ACL from a referenced ACL file. |
| 1705 | */ |
| 1706 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1707 | { |
| 1708 | const char *name; |
| 1709 | const char *key; |
| 1710 | struct pat_ref *ref; |
| 1711 | |
| 1712 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1713 | |
| 1714 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1715 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1716 | |
| 1717 | ref = pat_ref_lookup(name); |
| 1718 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1719 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1720 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1721 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1722 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1723 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | /* This function is an LUA binding. It provides a function |
| 1728 | * for deleting map entry from a referenced map file. |
| 1729 | */ |
| 1730 | static int hlua_del_map(lua_State *L) |
| 1731 | { |
| 1732 | const char *name; |
| 1733 | const char *key; |
| 1734 | struct pat_ref *ref; |
| 1735 | |
| 1736 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1737 | |
| 1738 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1739 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1740 | |
| 1741 | ref = pat_ref_lookup(name); |
| 1742 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1743 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1744 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1745 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1746 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1747 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1748 | return 0; |
| 1749 | } |
| 1750 | |
| 1751 | /* This function is an LUA binding. It provides a function |
| 1752 | * for adding ACL pattern from a referenced ACL file. |
| 1753 | */ |
| 1754 | static int hlua_add_acl(lua_State *L) |
| 1755 | { |
| 1756 | const char *name; |
| 1757 | const char *key; |
| 1758 | struct pat_ref *ref; |
| 1759 | |
| 1760 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1761 | |
| 1762 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1763 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1764 | |
| 1765 | ref = pat_ref_lookup(name); |
| 1766 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1767 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1768 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1769 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1770 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1771 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1772 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1773 | return 0; |
| 1774 | } |
| 1775 | |
| 1776 | /* This function is an LUA binding. It provides a function |
| 1777 | * for setting map pattern and sample from a referenced map |
| 1778 | * file. |
| 1779 | */ |
| 1780 | static int hlua_set_map(lua_State *L) |
| 1781 | { |
| 1782 | const char *name; |
| 1783 | const char *key; |
| 1784 | const char *value; |
| 1785 | struct pat_ref *ref; |
| 1786 | |
| 1787 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1788 | |
| 1789 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1790 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1791 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1792 | |
| 1793 | ref = pat_ref_lookup(name); |
| 1794 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1795 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1796 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1797 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1798 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1799 | pat_ref_set(ref, key, value, NULL); |
| 1800 | else |
| 1801 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1802 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1803 | return 0; |
| 1804 | } |
| 1805 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1806 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1807 | * 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] | 1808 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1809 | * the name of the object (_G[<name>] = <metable> ). |
| 1810 | * |
| 1811 | * A metable is a table that modify the standard behavior of a standard |
| 1812 | * access to the associated data. The entries of this new metatable are |
| 1813 | * defined as is: |
| 1814 | * |
| 1815 | * http://lua-users.org/wiki/MetatableEvents |
| 1816 | * |
| 1817 | * __index |
| 1818 | * |
| 1819 | * we access an absent field in a table, the result is nil. This is |
| 1820 | * true, but it is not the whole truth. Actually, such access triggers |
| 1821 | * the interpreter to look for an __index metamethod: If there is no |
| 1822 | * such method, as usually happens, then the access results in nil; |
| 1823 | * otherwise, the metamethod will provide the result. |
| 1824 | * |
| 1825 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1826 | * the key does not appear in the table, but the metatable has an __index |
| 1827 | * property: |
| 1828 | * |
| 1829 | * - if the value is a function, the function is called, passing in the |
| 1830 | * table and the key; the return value of that function is returned as |
| 1831 | * the result. |
| 1832 | * |
| 1833 | * - if the value is another table, the value of the key in that table is |
| 1834 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1835 | * table's metatable has an __index property, then it continues on up) |
| 1836 | * |
| 1837 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1838 | * |
| 1839 | * http://www.lua.org/pil/13.4.1.html |
| 1840 | * |
| 1841 | * __newindex |
| 1842 | * |
| 1843 | * Like __index, but control property assignment. |
| 1844 | * |
| 1845 | * __mode - Control weak references. A string value with one or both |
| 1846 | * of the characters 'k' and 'v' which specifies that the the |
| 1847 | * keys and/or values in the table are weak references. |
| 1848 | * |
| 1849 | * __call - Treat a table like a function. When a table is followed by |
| 1850 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1851 | * a __call key pointing to a function, that function is invoked |
| 1852 | * (passing any specified arguments) and the return value is |
| 1853 | * returned. |
| 1854 | * |
| 1855 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1856 | * called, if the metatable for myTable has a __metatable |
| 1857 | * key, the value of that key is returned instead of the |
| 1858 | * actual metatable. |
| 1859 | * |
| 1860 | * __tostring - Control string representation. When the builtin |
| 1861 | * "tostring( myTable )" function is called, if the metatable |
| 1862 | * for myTable has a __tostring property set to a function, |
| 1863 | * that function is invoked (passing myTable to it) and the |
| 1864 | * return value is used as the string representation. |
| 1865 | * |
| 1866 | * __len - Control table length. When the table length is requested using |
| 1867 | * the length operator ( '#' ), if the metatable for myTable has |
| 1868 | * a __len key pointing to a function, that function is invoked |
| 1869 | * (passing myTable to it) and the return value used as the value |
| 1870 | * of "#myTable". |
| 1871 | * |
| 1872 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1873 | * collected, if the metatable has a __gc field pointing to a |
| 1874 | * function, that function is first invoked, passing the userdata |
| 1875 | * to it. The __gc metamethod is not called for tables. |
| 1876 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1877 | * |
| 1878 | * Special metamethods for redefining standard operators: |
| 1879 | * http://www.lua.org/pil/13.1.html |
| 1880 | * |
| 1881 | * __add "+" |
| 1882 | * __sub "-" |
| 1883 | * __mul "*" |
| 1884 | * __div "/" |
| 1885 | * __unm "!" |
| 1886 | * __pow "^" |
| 1887 | * __concat ".." |
| 1888 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1889 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1890 | * http://www.lua.org/pil/13.2.html |
| 1891 | * |
| 1892 | * __eq "==" |
| 1893 | * __lt "<" |
| 1894 | * __le "<=" |
| 1895 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1896 | |
| 1897 | /* |
| 1898 | * |
| 1899 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1900 | * Class Map |
| 1901 | * |
| 1902 | * |
| 1903 | */ |
| 1904 | |
| 1905 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1906 | * a class session, otherwise it throws an error. |
| 1907 | */ |
| 1908 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1909 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1910 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1911 | } |
| 1912 | |
| 1913 | /* This function is the map constructor. It don't need |
| 1914 | * the class Map object. It creates and return a new Map |
| 1915 | * object. It must be called only during "body" or "init" |
| 1916 | * context because it process some filesystem accesses. |
| 1917 | */ |
| 1918 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1919 | { |
| 1920 | const char *fn; |
| 1921 | int match = PAT_MATCH_STR; |
| 1922 | struct sample_conv conv; |
| 1923 | const char *file = ""; |
| 1924 | int line = 0; |
| 1925 | lua_Debug ar; |
| 1926 | char *err = NULL; |
| 1927 | struct arg args[2]; |
| 1928 | |
| 1929 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1930 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1931 | |
| 1932 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1933 | |
| 1934 | if (lua_gettop(L) >= 2) { |
| 1935 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1936 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1937 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1938 | } |
| 1939 | |
| 1940 | /* Get Lua filename and line number. */ |
| 1941 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1942 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1943 | if (ar.currentline > 0) { /* is there info? */ |
| 1944 | file = ar.short_src; |
| 1945 | line = ar.currentline; |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /* fill fake sample_conv struct. */ |
| 1950 | conv.kw = ""; /* unused. */ |
| 1951 | conv.process = NULL; /* unused. */ |
| 1952 | conv.arg_mask = 0; /* unused. */ |
| 1953 | conv.val_args = NULL; /* unused. */ |
| 1954 | conv.out_type = SMP_T_STR; |
| 1955 | conv.private = (void *)(long)match; |
| 1956 | switch (match) { |
| 1957 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1958 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1959 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1960 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1961 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1962 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1963 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1964 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1965 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1966 | default: |
| 1967 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1968 | } |
| 1969 | |
| 1970 | /* fill fake args. */ |
| 1971 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1972 | args[0].data.str.area = strdup(fn); |
| 1973 | args[0].data.str.data = strlen(fn); |
| 1974 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1975 | args[1].type = ARGT_STOP; |
| 1976 | |
| 1977 | /* load the map. */ |
| 1978 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1979 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1980 | * free the err variable. |
| 1981 | */ |
| 1982 | luaL_where(L, 1); |
| 1983 | lua_pushfstring(L, "'new': %s.", err); |
| 1984 | lua_concat(L, 2); |
| 1985 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1986 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1987 | WILL_LJMP(lua_error(L)); |
| 1988 | } |
| 1989 | |
| 1990 | /* create the lua object. */ |
| 1991 | lua_newtable(L); |
| 1992 | lua_pushlightuserdata(L, args[0].data.map); |
| 1993 | lua_rawseti(L, -2, 0); |
| 1994 | |
| 1995 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1996 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1997 | lua_setmetatable(L, -2); |
| 1998 | |
| 1999 | |
| 2000 | return 1; |
| 2001 | } |
| 2002 | |
| 2003 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 2004 | { |
| 2005 | struct map_descriptor *desc; |
| 2006 | struct pattern *pat; |
| 2007 | struct sample smp; |
| 2008 | |
| 2009 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 2010 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 2011 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2012 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 2013 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2014 | } |
| 2015 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 2016 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2017 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2018 | 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] | 2019 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2020 | } |
| 2021 | |
| 2022 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 2023 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 2024 | if (str) |
| 2025 | lua_pushstring(L, ""); |
| 2026 | else |
| 2027 | lua_pushnil(L); |
| 2028 | return 1; |
| 2029 | } |
| 2030 | |
| 2031 | /* 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] | 2032 | 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] | 2033 | return 1; |
| 2034 | } |
| 2035 | |
| 2036 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 2037 | { |
| 2038 | return _hlua_map_lookup(L, 0); |
| 2039 | } |
| 2040 | |
| 2041 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 2042 | { |
| 2043 | return _hlua_map_lookup(L, 1); |
| 2044 | } |
| 2045 | |
| 2046 | /* |
| 2047 | * |
| 2048 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | * Class Socket |
| 2050 | * |
| 2051 | * |
| 2052 | */ |
| 2053 | |
| 2054 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2055 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2056 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | } |
| 2058 | |
| 2059 | /* 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] | 2060 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | * received. |
| 2062 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2063 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2065 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2066 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2067 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2068 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2069 | notification_wake(&ctx->wake_on_read); |
| 2070 | notification_wake(&ctx->wake_on_write); |
| 2071 | return; |
| 2072 | } |
| 2073 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2074 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2075 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2076 | notification_wake(&ctx->wake_on_read); |
| 2077 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2078 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2079 | } |
| 2080 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2081 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2082 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2083 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2084 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2085 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2086 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2087 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2088 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2089 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2090 | * to be notified whenever the connection completes. |
| 2091 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2092 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2093 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2094 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2095 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2096 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2097 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2098 | |
| 2099 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2100 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2101 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2102 | /* 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] | 2103 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2104 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2105 | |
| 2106 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2107 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2108 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2109 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2110 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2111 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2112 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2113 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2114 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2115 | } |
| 2116 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2117 | static int hlua_socket_init(struct appctx *appctx) |
| 2118 | { |
| 2119 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2120 | struct stream *s; |
| 2121 | |
| 2122 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2123 | goto error; |
| 2124 | |
| 2125 | s = appctx_strm(appctx); |
| 2126 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2127 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2128 | * and retrieve data from the server. The connection is initialized |
| 2129 | * with the "struct server". |
| 2130 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2131 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2132 | |
| 2133 | /* Force destination server. */ |
| 2134 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2135 | s->target = &socket_tcp->obj_type; |
| 2136 | |
| 2137 | ctx->appctx = appctx; |
| 2138 | return 0; |
| 2139 | |
| 2140 | error: |
| 2141 | return -1; |
| 2142 | } |
| 2143 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2144 | /* This function is called when the "struct stream" is destroyed. |
| 2145 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | * Wake all the pending signals. |
| 2147 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2148 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2150 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2151 | struct xref *peer; |
| 2152 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2153 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2154 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2155 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2156 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | |
| 2158 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2159 | notification_wake(&ctx->wake_on_read); |
| 2160 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2161 | } |
| 2162 | |
| 2163 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2164 | * uses this object. If the stream does not exists, just quit. |
| 2165 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | * pending signal can rest in the read and write lists. destroy |
| 2167 | * it. |
| 2168 | */ |
| 2169 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2170 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2171 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2172 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2173 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2175 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2176 | |
| 2177 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2178 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2179 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2181 | |
| 2182 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2183 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2184 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2185 | ctx->die = 1; |
| 2186 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2187 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2188 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2189 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2190 | return 0; |
| 2191 | } |
| 2192 | |
| 2193 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2194 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2196 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2198 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2199 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2200 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2201 | struct hlua *hlua; |
| 2202 | |
| 2203 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2204 | hlua = hlua_gethlua(L); |
| 2205 | if (!hlua) |
| 2206 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2207 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2208 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2209 | |
| 2210 | /* Check if we run on the same thread than the xreator thread. |
| 2211 | * We cannot access to the socket if the thread is different. |
| 2212 | */ |
| 2213 | if (socket->tid != tid) |
| 2214 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2215 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2216 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2217 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2219 | |
| 2220 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2221 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2222 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2223 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2224 | ctx->die = 1; |
| 2225 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2227 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2228 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | return 0; |
| 2230 | } |
| 2231 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2232 | /* The close function calls close_helper. |
| 2233 | */ |
| 2234 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2235 | { |
| 2236 | MAY_LJMP(check_args(L, 1, "close")); |
| 2237 | return hlua_socket_close_helper(L); |
| 2238 | } |
| 2239 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | /* This Lua function assumes that the stack contain three parameters. |
| 2241 | * 1 - USERDATA containing a struct socket |
| 2242 | * 2 - INTEGER with values of the macro defined below |
| 2243 | * If the integer is -1, we must read at most one line. |
| 2244 | * If the integer is -2, we ust read all the data until the |
| 2245 | * end of the stream. |
| 2246 | * If the integer is positive value, we must read a number of |
| 2247 | * bytes corresponding to this value. |
| 2248 | */ |
| 2249 | #define HLSR_READ_LINE (-1) |
| 2250 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2251 | __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] | 2252 | { |
| 2253 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2254 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2255 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2256 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2258 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2259 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2260 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2261 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2262 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2263 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2264 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2265 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2266 | struct stream *s; |
| 2267 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2268 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2269 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2270 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2271 | hlua = hlua_gethlua(L); |
| 2272 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | /* Check if this lua stack is schedulable. */ |
| 2274 | if (!hlua || !hlua->task) |
| 2275 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2276 | "'frontend', 'backend' or 'task'")); |
| 2277 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2278 | /* Check if we run on the same thread than the xreator thread. |
| 2279 | * We cannot access to the socket if the thread is different. |
| 2280 | */ |
| 2281 | if (socket->tid != tid) |
| 2282 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2283 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2284 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2285 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2286 | if (!peer) |
| 2287 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2288 | |
| 2289 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2290 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2291 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2292 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2294 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2296 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | if (nblk < 0) /* Connection close. */ |
| 2298 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2299 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2301 | |
| 2302 | /* remove final \r\n. */ |
| 2303 | if (nblk == 1) { |
| 2304 | if (blk1[len1-1] == '\n') { |
| 2305 | len1--; |
| 2306 | skip_at_end++; |
| 2307 | if (blk1[len1-1] == '\r') { |
| 2308 | len1--; |
| 2309 | skip_at_end++; |
| 2310 | } |
| 2311 | } |
| 2312 | } |
| 2313 | else { |
| 2314 | if (blk2[len2-1] == '\n') { |
| 2315 | len2--; |
| 2316 | skip_at_end++; |
| 2317 | if (blk2[len2-1] == '\r') { |
| 2318 | len2--; |
| 2319 | skip_at_end++; |
| 2320 | } |
| 2321 | } |
| 2322 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | } |
| 2324 | |
| 2325 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2327 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | if (nblk < 0) /* Connection close. */ |
| 2329 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2330 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | goto connection_empty; |
| 2332 | } |
| 2333 | |
| 2334 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2336 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | if (nblk < 0) /* Connection close. */ |
| 2338 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2339 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2340 | goto connection_empty; |
| 2341 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2342 | missing_bytes = wanted - socket->b.n; |
| 2343 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2345 | len1 = missing_bytes; |
| 2346 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2347 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2348 | } |
| 2349 | |
| 2350 | len = len1; |
| 2351 | |
| 2352 | luaL_addlstring(&socket->b, blk1, len1); |
| 2353 | if (nblk == 2) { |
| 2354 | len += len2; |
| 2355 | luaL_addlstring(&socket->b, blk2, len2); |
| 2356 | } |
| 2357 | |
| 2358 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2359 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | |
| 2361 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2362 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | |
| 2364 | /* If the pattern reclaim to read all the data |
| 2365 | * in the connection, got out. |
| 2366 | */ |
| 2367 | if (wanted == HLSR_READ_ALL) |
| 2368 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2369 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2370 | goto connection_empty; |
| 2371 | |
| 2372 | /* Return result. */ |
| 2373 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2374 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | return 1; |
| 2376 | |
| 2377 | connection_closed: |
| 2378 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2379 | xref_unlock(&socket->xref, peer); |
| 2380 | |
| 2381 | no_peer: |
| 2382 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | /* If the buffer containds data. */ |
| 2384 | if (socket->b.n > 0) { |
| 2385 | luaL_pushresult(&socket->b); |
| 2386 | return 1; |
| 2387 | } |
| 2388 | lua_pushnil(L); |
| 2389 | lua_pushstring(L, "connection closed."); |
| 2390 | return 2; |
| 2391 | |
| 2392 | connection_empty: |
| 2393 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2394 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2395 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2397 | } |
| 2398 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2399 | 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] | 2400 | return 0; |
| 2401 | } |
| 2402 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2403 | /* This Lua function gets two parameters. The first one can be string |
| 2404 | * or a number. If the string is "*l", the user requires one line. If |
| 2405 | * 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] | 2406 | * If the value is a number, the user require a number of bytes equal |
| 2407 | * to the value. The default value is "*l" (a line). |
| 2408 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2409 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2410 | * integer takes this values: |
| 2411 | * -1 : read a line |
| 2412 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2413 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2414 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2415 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | * concatenated with the read data. |
| 2417 | */ |
| 2418 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2419 | { |
| 2420 | int wanted = HLSR_READ_LINE; |
| 2421 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2422 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2423 | char *error; |
| 2424 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2425 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2426 | |
| 2427 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2428 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2429 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2430 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2432 | /* Check if we run on the same thread than the xreator thread. |
| 2433 | * We cannot access to the socket if the thread is different. |
| 2434 | */ |
| 2435 | if (socket->tid != tid) |
| 2436 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2437 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | /* check for pattern. */ |
| 2439 | if (lua_gettop(L) >= 2) { |
| 2440 | type = lua_type(L, 2); |
| 2441 | if (type == LUA_TSTRING) { |
| 2442 | pattern = lua_tostring(L, 2); |
| 2443 | if (strcmp(pattern, "*a") == 0) |
| 2444 | wanted = HLSR_READ_ALL; |
| 2445 | else if (strcmp(pattern, "*l") == 0) |
| 2446 | wanted = HLSR_READ_LINE; |
| 2447 | else { |
| 2448 | wanted = strtoll(pattern, &error, 10); |
| 2449 | if (*error != '\0') |
| 2450 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2451 | } |
| 2452 | } |
| 2453 | else if (type == LUA_TNUMBER) { |
| 2454 | wanted = lua_tointeger(L, 2); |
| 2455 | if (wanted < 0) |
| 2456 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2457 | } |
| 2458 | } |
| 2459 | |
| 2460 | /* Set pattern. */ |
| 2461 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2462 | |
| 2463 | /* Check if we would replace the top by itself. */ |
| 2464 | if (lua_gettop(L) != 2) |
| 2465 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2467 | /* Save index of the top of the stack because since buffers are used, it |
| 2468 | * may change |
| 2469 | */ |
| 2470 | lastarg = lua_gettop(L); |
| 2471 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2472 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2473 | luaL_buffinit(L, &socket->b); |
| 2474 | |
| 2475 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2476 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2477 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2478 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2479 | pattern = lua_tolstring(L, 3, &len); |
| 2480 | luaL_addlstring(&socket->b, pattern, len); |
| 2481 | } |
| 2482 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2483 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2484 | } |
| 2485 | |
| 2486 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2487 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2489 | 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] | 2490 | { |
| 2491 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2492 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2493 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2494 | struct appctx *appctx; |
| 2495 | size_t buf_len; |
| 2496 | const char *buf; |
| 2497 | int len; |
| 2498 | int send_len; |
| 2499 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2500 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2502 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2503 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2504 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2505 | hlua = hlua_gethlua(L); |
| 2506 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | /* Check if this lua stack is schedulable. */ |
| 2508 | if (!hlua || !hlua->task) |
| 2509 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2510 | "'frontend', 'backend' or 'task'")); |
| 2511 | |
| 2512 | /* Get object */ |
| 2513 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2514 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2515 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2517 | /* Check if we run on the same thread than the xreator thread. |
| 2518 | * We cannot access to the socket if the thread is different. |
| 2519 | */ |
| 2520 | if (socket->tid != tid) |
| 2521 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2522 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2523 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2524 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2525 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2526 | lua_pushinteger(L, -1); |
| 2527 | return 1; |
| 2528 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2529 | |
| 2530 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2531 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2532 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2533 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2534 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2536 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2537 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | lua_pushinteger(L, -1); |
| 2539 | return 1; |
| 2540 | } |
| 2541 | |
| 2542 | /* Update the input buffer data. */ |
| 2543 | buf += sent; |
| 2544 | send_len = buf_len - sent; |
| 2545 | |
| 2546 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2547 | if (sent >= buf_len) { |
| 2548 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2549 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2552 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2553 | * the request buffer if its not required. |
| 2554 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2555 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2556 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2557 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2558 | } |
| 2559 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2560 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2561 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2562 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2564 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2565 | |
| 2566 | /* send data */ |
| 2567 | if (len < send_len) |
| 2568 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2569 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | |
| 2571 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2572 | * the data" (-2) are not expected because the available length |
| 2573 | * is tested. |
| 2574 | * Other unknown error are also not expected. |
| 2575 | */ |
| 2576 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2577 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2578 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2579 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2580 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2581 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2582 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | return 1; |
| 2585 | } |
| 2586 | |
| 2587 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2588 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2589 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | /* Update length sent. */ |
| 2591 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2592 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | |
| 2594 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2595 | if (sent + len >= buf_len) { |
| 2596 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2598 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2599 | |
| 2600 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2601 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2602 | xref_unlock(&socket->xref, peer); |
| 2603 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2604 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2605 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2606 | 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] | 2607 | return 0; |
| 2608 | } |
| 2609 | |
| 2610 | /* This function initiate the send of data. It just check the input |
| 2611 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2612 | * 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] | 2613 | * "hlua_socket_write_yield" that can yield. |
| 2614 | * |
| 2615 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2616 | * the associated object. The second is a string buffer. The third is |
| 2617 | * a facultative integer that represents where is the buffer position |
| 2618 | * of the start of the data that can send. The first byte is the |
| 2619 | * position "1". The default value is "1". The fourth argument is a |
| 2620 | * facultative integer that represents where is the buffer position |
| 2621 | * of the end of the data that can send. The default is the last byte. |
| 2622 | */ |
| 2623 | static int hlua_socket_send(struct lua_State *L) |
| 2624 | { |
| 2625 | int i; |
| 2626 | int j; |
| 2627 | const char *buf; |
| 2628 | size_t buf_len; |
| 2629 | |
| 2630 | /* Check number of arguments. */ |
| 2631 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2632 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2633 | |
| 2634 | /* Get the string. */ |
| 2635 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2636 | |
| 2637 | /* Get and check j. */ |
| 2638 | if (lua_gettop(L) == 4) { |
| 2639 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2640 | if (j < 0) |
| 2641 | j = buf_len + j + 1; |
| 2642 | if (j > buf_len) |
| 2643 | j = buf_len + 1; |
| 2644 | lua_pop(L, 1); |
| 2645 | } |
| 2646 | else |
| 2647 | j = buf_len; |
| 2648 | |
| 2649 | /* Get and check i. */ |
| 2650 | if (lua_gettop(L) == 3) { |
| 2651 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2652 | if (i < 0) |
| 2653 | i = buf_len + i + 1; |
| 2654 | if (i > buf_len) |
| 2655 | i = buf_len + 1; |
| 2656 | lua_pop(L, 1); |
| 2657 | } else |
| 2658 | i = 1; |
| 2659 | |
| 2660 | /* Check bth i and j. */ |
| 2661 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2662 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | return 1; |
| 2664 | } |
| 2665 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2666 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2667 | return 1; |
| 2668 | } |
| 2669 | if (i == 0) |
| 2670 | i = 1; |
| 2671 | if (j == 0) |
| 2672 | j = 1; |
| 2673 | |
| 2674 | /* Pop the string. */ |
| 2675 | lua_pop(L, 1); |
| 2676 | |
| 2677 | /* Update the buffer length. */ |
| 2678 | buf += i - 1; |
| 2679 | buf_len = j - i + 1; |
| 2680 | lua_pushlstring(L, buf, buf_len); |
| 2681 | |
| 2682 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2683 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2685 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | } |
| 2687 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2688 | #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] | 2689 | __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] | 2690 | { |
| 2691 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2692 | int ret; |
| 2693 | int len; |
| 2694 | char *p; |
| 2695 | |
| 2696 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2697 | if (ret <= 0) { |
| 2698 | lua_pushnil(L); |
| 2699 | return 1; |
| 2700 | } |
| 2701 | |
| 2702 | if (ret == AF_UNIX) { |
| 2703 | lua_pushstring(L, buffer+1); |
| 2704 | return 1; |
| 2705 | } |
| 2706 | else if (ret == AF_INET6) { |
| 2707 | buffer[0] = '['; |
| 2708 | len = strlen(buffer); |
| 2709 | buffer[len] = ']'; |
| 2710 | len++; |
| 2711 | buffer[len] = ':'; |
| 2712 | len++; |
| 2713 | p = buffer; |
| 2714 | } |
| 2715 | else if (ret == AF_INET) { |
| 2716 | p = buffer + 1; |
| 2717 | len = strlen(p); |
| 2718 | p[len] = ':'; |
| 2719 | len++; |
| 2720 | } |
| 2721 | else { |
| 2722 | lua_pushnil(L); |
| 2723 | return 1; |
| 2724 | } |
| 2725 | |
| 2726 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2727 | lua_pushnil(L); |
| 2728 | return 1; |
| 2729 | } |
| 2730 | |
| 2731 | lua_pushstring(L, p); |
| 2732 | return 1; |
| 2733 | } |
| 2734 | |
| 2735 | /* Returns information about the peer of the connection. */ |
| 2736 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2737 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2738 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2739 | struct xref *peer; |
| 2740 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2741 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2742 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2743 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2744 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2745 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2746 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2747 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2748 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2749 | /* Check if we run on the same thread than the xreator thread. |
| 2750 | * We cannot access to the socket if the thread is different. |
| 2751 | */ |
| 2752 | if (socket->tid != tid) |
| 2753 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2754 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2755 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2756 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2757 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2758 | lua_pushnil(L); |
| 2759 | return 1; |
| 2760 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2761 | |
| 2762 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2763 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2764 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2765 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2766 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2767 | lua_pushnil(L); |
| 2768 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2769 | } |
| 2770 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2771 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | xref_unlock(&socket->xref, peer); |
| 2773 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2774 | } |
| 2775 | |
| 2776 | /* Returns information about my connection side. */ |
| 2777 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2778 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2779 | struct hlua_socket *socket; |
| 2780 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2781 | struct appctx *appctx; |
| 2782 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2783 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2784 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2785 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2786 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2787 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2788 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2790 | /* Check if we run on the same thread than the xreator thread. |
| 2791 | * We cannot access to the socket if the thread is different. |
| 2792 | */ |
| 2793 | if (socket->tid != tid) |
| 2794 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2795 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2796 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2797 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2798 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2799 | lua_pushnil(L); |
| 2800 | return 1; |
| 2801 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2802 | |
| 2803 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2804 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2805 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2806 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2807 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2808 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | lua_pushnil(L); |
| 2810 | return 1; |
| 2811 | } |
| 2812 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2813 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2814 | xref_unlock(&socket->xref, peer); |
| 2815 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | } |
| 2817 | |
| 2818 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2819 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2820 | .obj_type = OBJ_TYPE_APPLET, |
| 2821 | .name = "<LUA_TCP>", |
| 2822 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2823 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2824 | .release = hlua_socket_release, |
| 2825 | }; |
| 2826 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2827 | __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] | 2828 | { |
| 2829 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2830 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2831 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2832 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2833 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2834 | struct stream *s; |
| 2835 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2836 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2837 | hlua = hlua_gethlua(L); |
| 2838 | if (!hlua) |
| 2839 | return 0; |
| 2840 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2841 | /* Check if we run on the same thread than the xreator thread. |
| 2842 | * We cannot access to the socket if the thread is different. |
| 2843 | */ |
| 2844 | if (socket->tid != tid) |
| 2845 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2846 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2847 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2848 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2849 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2850 | lua_pushnil(L); |
| 2851 | lua_pushstring(L, "Can't connect"); |
| 2852 | return 2; |
| 2853 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2854 | |
| 2855 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2856 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2857 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2858 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2859 | /* Check if we run on the same thread than the xreator thread. |
| 2860 | * We cannot access to the socket if the thread is different. |
| 2861 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2862 | if (socket->tid != tid) { |
| 2863 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2864 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2865 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2866 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2867 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2868 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2869 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2870 | lua_pushnil(L); |
| 2871 | lua_pushstring(L, "Can't connect"); |
| 2872 | return 2; |
| 2873 | } |
| 2874 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2875 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2876 | |
| 2877 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2878 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2879 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2880 | lua_pushinteger(L, 1); |
| 2881 | return 1; |
| 2882 | } |
| 2883 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2884 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2885 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2886 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2887 | } |
| 2888 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2889 | 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] | 2890 | return 0; |
| 2891 | } |
| 2892 | |
| 2893 | /* This function fail or initite the connection. */ |
| 2894 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2895 | { |
| 2896 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2897 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2898 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2899 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2900 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2901 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2902 | int low, high; |
| 2903 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2904 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2905 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2906 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2907 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2908 | if (lua_gettop(L) < 2) |
| 2909 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | |
| 2911 | /* Get args. */ |
| 2912 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2913 | |
| 2914 | /* Check if we run on the same thread than the xreator thread. |
| 2915 | * We cannot access to the socket if the thread is different. |
| 2916 | */ |
| 2917 | if (socket->tid != tid) |
| 2918 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2919 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2921 | if (lua_gettop(L) >= 3) { |
| 2922 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2923 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2924 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2925 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2926 | * that are not enclosed within square brackets. |
| 2927 | */ |
| 2928 | if (port > 0) { |
| 2929 | luaL_buffinit(L, &b); |
| 2930 | luaL_addstring(&b, ip); |
| 2931 | luaL_addchar(&b, ':'); |
| 2932 | luaL_pushresult(&b); |
| 2933 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2934 | } |
| 2935 | } |
| 2936 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2937 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2938 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2939 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2940 | lua_pushnil(L); |
| 2941 | return 1; |
| 2942 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2943 | |
| 2944 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2945 | 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] | 2946 | if (!addr) { |
| 2947 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2948 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2949 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2950 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2951 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2952 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2953 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2954 | if (port == -1) { |
| 2955 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2956 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2957 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2958 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2959 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2960 | if (port == -1) { |
| 2961 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2962 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2963 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2964 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2965 | } |
| 2966 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2967 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2968 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2969 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2970 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2971 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2972 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2973 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2974 | xref_unlock(&socket->xref, peer); |
| 2975 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2976 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2977 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2978 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2979 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2980 | if (!hlua) |
| 2981 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2982 | |
| 2983 | /* inform the stream that we want to be notified whenever the |
| 2984 | * connection completes. |
| 2985 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2986 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2987 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2988 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2989 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2990 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2991 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2992 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2993 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2994 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2995 | } |
| 2996 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2997 | |
| 2998 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2999 | /* Return yield waiting for connection. */ |
| 3000 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3001 | 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] | 3002 | |
| 3003 | return 0; |
| 3004 | } |
| 3005 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3006 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3007 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 3008 | { |
| 3009 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3010 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3011 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3012 | |
| 3013 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 3014 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3015 | |
| 3016 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3017 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3018 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3019 | lua_pushnil(L); |
| 3020 | return 1; |
| 3021 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3022 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3023 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3024 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 3025 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3026 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3027 | return MAY_LJMP(hlua_socket_connect(L)); |
| 3028 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 3029 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3030 | |
| 3031 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 3032 | { |
| 3033 | return 0; |
| 3034 | } |
| 3035 | |
| 3036 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 3037 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3038 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3039 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3040 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3041 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3042 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3043 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3044 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 3045 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3046 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3047 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3048 | /* convert the timeout to millis */ |
| 3049 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3050 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3051 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3052 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3053 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3054 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3055 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3056 | 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] | 3057 | |
| 3058 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3059 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3060 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3061 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3062 | /* Check if we run on the same thread than the xreator thread. |
| 3063 | * We cannot access to the socket if the thread is different. |
| 3064 | */ |
| 3065 | if (socket->tid != tid) |
| 3066 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3067 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3068 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3069 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3070 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3071 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3072 | WILL_LJMP(lua_error(L)); |
| 3073 | return 0; |
| 3074 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3075 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3076 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3077 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3078 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3079 | s->scf->ioto = tmout; |
| 3080 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3081 | |
| 3082 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3083 | task_queue(s->task); |
| 3084 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3085 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3086 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3087 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3088 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3089 | } |
| 3090 | |
| 3091 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3092 | { |
| 3093 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3094 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3095 | struct appctx *appctx; |
| 3096 | |
| 3097 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3098 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3099 | hlua_pusherror(L, "socket: full stack"); |
| 3100 | goto out_fail_conf; |
| 3101 | } |
| 3102 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3103 | /* Create the object: obj[0] = userdata. */ |
| 3104 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3105 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3106 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3107 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3108 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3109 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3110 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3111 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3112 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3113 | goto out_fail_conf; |
| 3114 | } |
| 3115 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3116 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3117 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3118 | lua_setmetatable(L, -2); |
| 3119 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3120 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3121 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3122 | if (!appctx) { |
| 3123 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3124 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3125 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3126 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3127 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3128 | ctx->die = 0; |
| 3129 | LIST_INIT(&ctx->wake_on_write); |
| 3130 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3131 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3132 | if (appctx_init(appctx) == -1) { |
| 3133 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3134 | goto out_fail_appctx; |
| 3135 | } |
| 3136 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3137 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3138 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3139 | return 1; |
| 3140 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3141 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3142 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3143 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3144 | WILL_LJMP(lua_error(L)); |
| 3145 | return 0; |
| 3146 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3147 | |
| 3148 | /* |
| 3149 | * |
| 3150 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | * Class Channel |
| 3152 | * |
| 3153 | * |
| 3154 | */ |
| 3155 | |
| 3156 | /* Returns the struct hlua_channel join to the class channel in the |
| 3157 | * stack entry "ud" or throws an argument error. |
| 3158 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3159 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3161 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | } |
| 3163 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3164 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3165 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3166 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3167 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3168 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3169 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3170 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3171 | return 0; |
| 3172 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3173 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3174 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3175 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3176 | |
| 3177 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3178 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3179 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | return 1; |
| 3181 | } |
| 3182 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3183 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3184 | * 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] | 3185 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3186 | * initialized. |
| 3187 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3188 | 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] | 3189 | { |
| 3190 | struct filter *filter = NULL; |
| 3191 | |
| 3192 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3193 | struct hlua_flt_ctx *flt_ctx; |
| 3194 | |
| 3195 | filter = lua_touserdata (L, -1); |
| 3196 | flt_ctx = filter->ctx; |
| 3197 | if (hlua_filter_from_payload(filter)) { |
| 3198 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3199 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3200 | } |
| 3201 | } |
| 3202 | |
| 3203 | lua_pop(L, 1); |
| 3204 | return filter; |
| 3205 | } |
| 3206 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3207 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3208 | * 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] | 3209 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3210 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3211 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3212 | */ |
| 3213 | 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] | 3214 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3215 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3216 | luaL_Buffer b; |
| 3217 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3218 | block1 = len; |
| 3219 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3220 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3221 | block2 = len - block1; |
| 3222 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3223 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3224 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3225 | if (block2) |
| 3226 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3227 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3228 | return len; |
| 3229 | } |
| 3230 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3231 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3232 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3233 | * number of bytes copied. |
| 3234 | */ |
| 3235 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3236 | { |
| 3237 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3238 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3239 | /* Nothing to do, just return */ |
| 3240 | if (unlikely(istlen(str) == 0)) |
| 3241 | goto end; |
| 3242 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3243 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3244 | ret = -1; |
| 3245 | goto end; |
| 3246 | } |
| 3247 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3248 | |
| 3249 | end: |
| 3250 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3251 | } |
| 3252 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3253 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3254 | */ |
| 3255 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3256 | { |
| 3257 | size_t end = offset + len; |
| 3258 | |
| 3259 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3260 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3261 | b_data(&chn->buf) - end, -len); |
| 3262 | b_sub(&chn->buf, len); |
| 3263 | } |
| 3264 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3265 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3266 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3267 | * offset by default). If there is not enough input data and more data can be |
| 3268 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3269 | * |
| 3270 | * From an action, All input data are considered. For a filter, the offset and |
| 3271 | * 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] | 3272 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3273 | __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] | 3274 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3275 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3276 | struct filter *filter; |
| 3277 | size_t input, output; |
| 3278 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3279 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3280 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3281 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3282 | output = co_data(chn); |
| 3283 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3284 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3285 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3286 | if (filter && !hlua_filter_from_payload(filter)) |
| 3287 | WILL_LJMP(lua_error(L)); |
| 3288 | |
| 3289 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3290 | if (lua_gettop(L) > 1) { |
| 3291 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3292 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3293 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3294 | offset += output; |
| 3295 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | lua_pushfstring(L, "offset out of range."); |
| 3297 | WILL_LJMP(lua_error(L)); |
| 3298 | } |
| 3299 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3301 | if (lua_gettop(L) == 3) { |
| 3302 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3303 | if (!len) |
| 3304 | goto dup; |
| 3305 | if (len == -1) |
| 3306 | len = global.tune.bufsize; |
| 3307 | if (len < 0) { |
| 3308 | lua_pushfstring(L, "length out of range."); |
| 3309 | WILL_LJMP(lua_error(L)); |
| 3310 | } |
| 3311 | } |
| 3312 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3313 | /* Wait for more data if possible if no length was specified and there |
| 3314 | * is no data or not enough data was received. |
| 3315 | */ |
| 3316 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3317 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3318 | /* Yield waiting for more data, as requested */ |
| 3319 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3320 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3321 | |
| 3322 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3323 | if (!len) { |
| 3324 | lua_pushnil(L); |
| 3325 | return -1; |
| 3326 | } |
| 3327 | |
| 3328 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3329 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3330 | } |
| 3331 | |
| 3332 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3333 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3334 | return 1; |
| 3335 | } |
| 3336 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3337 | /* Copies the first line (including the trailing LF) of input data in the |
| 3338 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3339 | * a length (all remaining input data starting for the offset by default). If |
| 3340 | * there is not enough input data and more data can be received, the function |
| 3341 | * yields. If a length is explicitly specified, no more data are |
| 3342 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3343 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3344 | * |
| 3345 | * From an action, All input data are considered. For a filter, the offset and |
| 3346 | * 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] | 3347 | */ |
| 3348 | __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] | 3349 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3350 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3351 | struct filter *filter; |
| 3352 | size_t l, input, output; |
| 3353 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3354 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3355 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3356 | output = co_data(chn); |
| 3357 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3358 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3359 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3360 | if (filter && !hlua_filter_from_payload(filter)) |
| 3361 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3362 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3363 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3364 | if (lua_gettop(L) > 1) { |
| 3365 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3366 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3367 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3368 | offset += output; |
| 3369 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3370 | lua_pushfstring(L, "offset out of range."); |
| 3371 | WILL_LJMP(lua_error(L)); |
| 3372 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3373 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3374 | |
| 3375 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3376 | if (lua_gettop(L) == 3) { |
| 3377 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3378 | if (!len) |
| 3379 | goto dup; |
| 3380 | if (len == -1) |
| 3381 | len = global.tune.bufsize; |
| 3382 | if (len < 0) { |
| 3383 | lua_pushfstring(L, "length out of range."); |
| 3384 | WILL_LJMP(lua_error(L)); |
| 3385 | } |
| 3386 | } |
| 3387 | |
| 3388 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3389 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3390 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3391 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3392 | len = l+1; |
| 3393 | goto dup; |
| 3394 | } |
| 3395 | } |
| 3396 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3397 | /* Wait for more data if possible if no line is found and no length was |
| 3398 | * specified or not enough data was received. |
| 3399 | */ |
| 3400 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3401 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3402 | /* Yield waiting for more data */ |
| 3403 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3404 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3405 | |
| 3406 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3407 | if (!len) { |
| 3408 | lua_pushnil(L); |
| 3409 | return -1; |
| 3410 | } |
| 3411 | |
| 3412 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3413 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3414 | } |
| 3415 | |
| 3416 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3417 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3418 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3419 | } |
| 3420 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3421 | /* [ DEPRECATED ] |
| 3422 | * |
| 3423 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3424 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3425 | * |
| 3426 | * From an action, All input data are considered. For a filter, the offset and |
| 3427 | * 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] | 3428 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3429 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3431 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3432 | struct filter *filter; |
| 3433 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3434 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3435 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3436 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3437 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3438 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3439 | WILL_LJMP(lua_error(L)); |
| 3440 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3441 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3442 | offset = co_data(chn); |
| 3443 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3444 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3445 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3446 | if (filter && !hlua_filter_from_payload(filter)) |
| 3447 | WILL_LJMP(lua_error(L)); |
| 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3450 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3451 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3452 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3453 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3454 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3455 | return 1; |
| 3456 | } |
| 3457 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3458 | /* [ DEPRECATED ] |
| 3459 | * |
| 3460 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3461 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3462 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3463 | * |
| 3464 | * From an action, All input data are considered. For a filter, the offset and |
| 3465 | * 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] | 3466 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3467 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3468 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3469 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3470 | struct filter *filter; |
| 3471 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3472 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3473 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3474 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3475 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3476 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3477 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3478 | WILL_LJMP(lua_error(L)); |
| 3479 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3480 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3481 | offset = co_data(chn); |
| 3482 | len = ci_data(chn); |
| 3483 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3484 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3485 | if (filter && !hlua_filter_from_payload(filter)) |
| 3486 | WILL_LJMP(lua_error(L)); |
| 3487 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3488 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3489 | lua_pushnil(L); |
| 3490 | return 1; |
| 3491 | } |
| 3492 | |
| 3493 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3494 | _hlua_channel_delete(chn, offset, ret); |
| 3495 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3496 | } |
| 3497 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3498 | /* This functions consumes and returns one line. If the channel is closed, |
| 3499 | * 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] | 3500 | * 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] | 3501 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3502 | * |
| 3503 | * From an action, All input data are considered. For a filter, the offset and |
| 3504 | * 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] | 3505 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3506 | __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] | 3507 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3508 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3509 | struct filter *filter; |
| 3510 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3511 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3512 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3513 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3514 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3515 | offset = co_data(chn); |
| 3516 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3517 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3518 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3519 | if (filter && !hlua_filter_from_payload(filter)) |
| 3520 | WILL_LJMP(lua_error(L)); |
| 3521 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3522 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3523 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3524 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3525 | } |
| 3526 | |
| 3527 | for (l = 0; l < len; l++) { |
| 3528 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3529 | len = l+1; |
| 3530 | goto dup; |
| 3531 | } |
| 3532 | } |
| 3533 | |
| 3534 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3535 | /* Yield waiting for more data */ |
| 3536 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3537 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3538 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3539 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3540 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3541 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3542 | return 1; |
| 3543 | } |
| 3544 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3545 | /* [ DEPRECATED ] |
| 3546 | * |
| 3547 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3548 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3549 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3550 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3551 | struct channel *chn; |
| 3552 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3553 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3554 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3555 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3556 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3557 | WILL_LJMP(lua_error(L)); |
| 3558 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3559 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3560 | } |
| 3561 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3562 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3563 | * available input data are returned. The offset may be negactive to start from |
| 3564 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3565 | * size. |
| 3566 | */ |
| 3567 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3568 | { |
| 3569 | struct channel *chn; |
| 3570 | |
| 3571 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3572 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3573 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3574 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3575 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3576 | WILL_LJMP(lua_error(L)); |
| 3577 | } |
| 3578 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3579 | } |
| 3580 | |
| 3581 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3582 | * available input data are returned. The offset may be negactive to start from |
| 3583 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3584 | * size. |
| 3585 | */ |
| 3586 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3587 | { |
| 3588 | struct channel *chn; |
| 3589 | |
| 3590 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3591 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3592 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3593 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3594 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3595 | WILL_LJMP(lua_error(L)); |
| 3596 | } |
| 3597 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3598 | } |
| 3599 | |
| 3600 | /* Appends a string into the input side of channel. It returns the length of the |
| 3601 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3602 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3603 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3604 | * |
| 3605 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3606 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3607 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3608 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3609 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3610 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3611 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3612 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3613 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3614 | |
| 3615 | MAY_LJMP(check_args(L, 2, "append")); |
| 3616 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3617 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3618 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3619 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3620 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3621 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3622 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3623 | offset = co_data(chn); |
| 3624 | len = ci_data(chn); |
| 3625 | |
| 3626 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3627 | if (filter && !hlua_filter_from_payload(filter)) |
| 3628 | WILL_LJMP(lua_error(L)); |
| 3629 | |
| 3630 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3631 | if (ret > 0 && filter) { |
| 3632 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3633 | |
| 3634 | flt_update_offsets(filter, chn, ret); |
| 3635 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3636 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3637 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3638 | return 1; |
| 3639 | } |
| 3640 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3641 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3642 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3643 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3644 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3645 | * |
| 3646 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3647 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3648 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3649 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3650 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3651 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3652 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3653 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3654 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3655 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3656 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3657 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3658 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3659 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3660 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3661 | WILL_LJMP(lua_error(L)); |
| 3662 | } |
| 3663 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3664 | offset = co_data(chn); |
| 3665 | len = ci_data(chn); |
| 3666 | |
| 3667 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3668 | if (filter && !hlua_filter_from_payload(filter)) |
| 3669 | WILL_LJMP(lua_error(L)); |
| 3670 | |
| 3671 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3672 | if (ret > 0 && filter) { |
| 3673 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3674 | |
| 3675 | flt_update_offsets(filter, chn, ret); |
| 3676 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3677 | } |
| 3678 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3679 | lua_pushinteger(L, ret); |
| 3680 | return 1; |
| 3681 | } |
| 3682 | |
| 3683 | /* 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] | 3684 | * 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] | 3685 | * returns the length of the written string, or -1 if the channel is closed or |
| 3686 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3687 | * |
| 3688 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3689 | */ |
| 3690 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3691 | { |
| 3692 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3693 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3694 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3695 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3696 | int ret, offset; |
| 3697 | |
| 3698 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3699 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3700 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3701 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3702 | |
| 3703 | output = co_data(chn); |
| 3704 | input = ci_data(chn); |
| 3705 | |
| 3706 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3707 | if (filter && !hlua_filter_from_payload(filter)) |
| 3708 | WILL_LJMP(lua_error(L)); |
| 3709 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3710 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3711 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3712 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3713 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3714 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3715 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3716 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3717 | lua_pushfstring(L, "offset out of range."); |
| 3718 | WILL_LJMP(lua_error(L)); |
| 3719 | } |
| 3720 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3721 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3722 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3723 | WILL_LJMP(lua_error(L)); |
| 3724 | } |
| 3725 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3726 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3727 | if (ret > 0 && filter) { |
| 3728 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3729 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3730 | flt_update_offsets(filter, chn, ret); |
| 3731 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3732 | } |
| 3733 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3734 | lua_pushinteger(L, ret); |
| 3735 | return 1; |
| 3736 | } |
| 3737 | /* Replaces a given amount of input data at the given offset by a string |
| 3738 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3739 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3740 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3741 | * |
| 3742 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3743 | */ |
| 3744 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3745 | { |
| 3746 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3747 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3748 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | size_t sz, input, output; |
| 3750 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3751 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3752 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3753 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3754 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3755 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3756 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3757 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3758 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3759 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3760 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3761 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3762 | output = co_data(chn); |
| 3763 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3764 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3765 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3766 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3767 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3768 | |
| 3769 | offset = output; |
| 3770 | if (lua_gettop(L) > 2) { |
| 3771 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3772 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3773 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3774 | offset += output; |
| 3775 | if (offset < output || offset > input + output) { |
| 3776 | lua_pushfstring(L, "offset out of range."); |
| 3777 | WILL_LJMP(lua_error(L)); |
| 3778 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3779 | } |
| 3780 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3781 | len = output + input - offset; |
| 3782 | if (lua_gettop(L) == 4) { |
| 3783 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3784 | if (!len) |
| 3785 | goto set; |
| 3786 | if (len == -1) |
| 3787 | len = output + input - offset; |
| 3788 | if (len < 0 || offset + len > output + input) { |
| 3789 | lua_pushfstring(L, "length out of range."); |
| 3790 | WILL_LJMP(lua_error(L)); |
| 3791 | } |
| 3792 | } |
| 3793 | |
| 3794 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3795 | /* 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] | 3796 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3797 | lua_pushinteger(L, -1); |
| 3798 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3799 | _hlua_channel_delete(chn, offset, len); |
| 3800 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3801 | if (filter) { |
| 3802 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3803 | |
| 3804 | len -= (ret > 0 ? ret : 0); |
| 3805 | flt_update_offsets(filter, chn, -len); |
| 3806 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3807 | } |
| 3808 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3809 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3810 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3811 | return 1; |
| 3812 | } |
| 3813 | |
| 3814 | /* 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] | 3815 | * 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] | 3816 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3817 | * |
| 3818 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3819 | */ |
| 3820 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3821 | { |
| 3822 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3823 | struct filter *filter; |
| 3824 | size_t input, output; |
| 3825 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3826 | |
| 3827 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3828 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3829 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3830 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3831 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3832 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3833 | WILL_LJMP(lua_error(L)); |
| 3834 | } |
| 3835 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3836 | output = co_data(chn); |
| 3837 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3838 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3839 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3840 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3841 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | |
| 3843 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3844 | if (lua_gettop(L) > 1) { |
| 3845 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3846 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3847 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3848 | offset += output; |
| 3849 | if (offset < output || offset > input + output) { |
| 3850 | lua_pushfstring(L, "offset out of range."); |
| 3851 | WILL_LJMP(lua_error(L)); |
| 3852 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3853 | } |
| 3854 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3855 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3856 | if (lua_gettop(L) == 3) { |
| 3857 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3858 | if (!len) |
| 3859 | goto end; |
| 3860 | if (len == -1) |
| 3861 | len = output + input - offset; |
| 3862 | if (len < 0 || offset + len > output + input) { |
| 3863 | lua_pushfstring(L, "length out of range."); |
| 3864 | WILL_LJMP(lua_error(L)); |
| 3865 | } |
| 3866 | } |
| 3867 | |
| 3868 | _hlua_channel_delete(chn, offset, len); |
| 3869 | if (filter) { |
| 3870 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3871 | |
| 3872 | flt_update_offsets(filter, chn, -len); |
| 3873 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3874 | } |
| 3875 | |
| 3876 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3877 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3878 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3879 | } |
| 3880 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3881 | /* Append data in the output side of the buffer. This data is immediately |
| 3882 | * sent. The function returns the amount of data written. If the buffer |
| 3883 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | * if the channel is closed. |
| 3885 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3886 | __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] | 3887 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3888 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3889 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3890 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3891 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3892 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3893 | struct hlua *hlua; |
| 3894 | |
| 3895 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3896 | hlua = hlua_gethlua(L); |
| 3897 | if (!hlua) { |
| 3898 | lua_pushnil(L); |
| 3899 | return 1; |
| 3900 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3901 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3902 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3903 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3904 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3905 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3906 | offset = co_data(chn); |
| 3907 | len = ci_data(chn); |
| 3908 | |
| 3909 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3910 | if (filter && !hlua_filter_from_payload(filter)) |
| 3911 | WILL_LJMP(lua_error(L)); |
| 3912 | |
| 3913 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3914 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3915 | lua_pushinteger(L, -1); |
| 3916 | return 1; |
| 3917 | } |
| 3918 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3919 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3920 | if (len > sz -l) { |
| 3921 | if (filter) { |
| 3922 | lua_pushinteger(L, -1); |
| 3923 | return 1; |
| 3924 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3925 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3926 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3927 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3928 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3929 | if (ret == -1) { |
| 3930 | lua_pop(L, 1); |
| 3931 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3932 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3933 | } |
| 3934 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3935 | if (filter) { |
| 3936 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3937 | |
| 3938 | |
| 3939 | flt_update_offsets(filter, chn, ret); |
| 3940 | FLT_OFF(filter, chn) += ret; |
| 3941 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3942 | } |
| 3943 | else |
| 3944 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3945 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3946 | l += ret; |
| 3947 | lua_pop(L, 1); |
| 3948 | lua_pushinteger(L, l); |
| 3949 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3950 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3951 | if (l < sz) { |
| 3952 | /* Yield only if the channel's output is not empty. |
| 3953 | * Otherwise it means we cannot add more data. */ |
| 3954 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3955 | return 1; |
| 3956 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3957 | /* If we are waiting for space in the response buffer, we |
| 3958 | * must set the flag WAKERESWR. This flag required the task |
| 3959 | * wake up if any activity is detected on the response buffer. |
| 3960 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3961 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3962 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3963 | else |
| 3964 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3965 | 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] | 3966 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3967 | |
| 3968 | return 1; |
| 3969 | } |
| 3970 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3971 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3972 | * yield the LUA process, and resume it without checking the |
| 3973 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3974 | * |
| 3975 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3976 | */ |
| 3977 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3978 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3979 | struct channel *chn; |
| 3980 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3981 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3982 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3983 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3984 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3985 | WILL_LJMP(lua_error(L)); |
| 3986 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3987 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3988 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3989 | } |
| 3990 | |
| 3991 | /* This function forward and amount of butes. The data pass from |
| 3992 | * the input side of the buffer to the output side, and can be |
| 3993 | * forwarded. This function never fails. |
| 3994 | * |
| 3995 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3996 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3997 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3998 | __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] | 3999 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4000 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4001 | struct filter *filter; |
| 4002 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4003 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4004 | struct hlua *hlua; |
| 4005 | |
| 4006 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 4007 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4008 | if (!hlua) { |
| 4009 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 4010 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 4011 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 4012 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4013 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4014 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4015 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 4016 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4017 | offset = co_data(chn); |
| 4018 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4019 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4020 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 4021 | if (filter && !hlua_filter_from_payload(filter)) |
| 4022 | WILL_LJMP(lua_error(L)); |
| 4023 | |
| 4024 | max = fwd - l; |
| 4025 | if (max > len) |
| 4026 | max = len; |
| 4027 | |
| 4028 | if (filter) { |
| 4029 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 4030 | |
| 4031 | FLT_OFF(filter, chn) += max; |
| 4032 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 4033 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 4034 | } |
| 4035 | else |
| 4036 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4037 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4038 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4039 | lua_pop(L, 1); |
| 4040 | lua_pushinteger(L, l); |
| 4041 | |
| 4042 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4043 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4044 | /* The the input channel or the output channel are closed, we |
| 4045 | * must return the amount of data forwarded. |
| 4046 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 4047 | 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] | 4048 | return 1; |
| 4049 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4050 | /* If we are waiting for space data in the response buffer, we |
| 4051 | * must set the flag WAKERESWR. This flag required the task |
| 4052 | * wake up if any activity is detected on the response buffer. |
| 4053 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4054 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4055 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4056 | else |
| 4057 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4058 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4059 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4060 | 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] | 4061 | } |
| 4062 | |
| 4063 | return 1; |
| 4064 | } |
| 4065 | |
| 4066 | /* Just check the input and prepare the stack for the previous |
| 4067 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4068 | * |
| 4069 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4070 | */ |
| 4071 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4072 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4073 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4074 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4075 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4076 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4077 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4078 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4079 | WILL_LJMP(lua_error(L)); |
| 4080 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4081 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4082 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4083 | } |
| 4084 | |
| 4085 | /* Just returns the number of bytes available in the input |
| 4086 | * side of the buffer. This function never fails. |
| 4087 | */ |
| 4088 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4089 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4090 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4091 | struct filter *filter; |
| 4092 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4093 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4094 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4095 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4096 | |
| 4097 | output = co_data(chn); |
| 4098 | input = ci_data(chn); |
| 4099 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4100 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4101 | lua_pushinteger(L, input); |
| 4102 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4103 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4104 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4105 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4106 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4107 | return 1; |
| 4108 | } |
| 4109 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4110 | /* Returns true if the channel is full. */ |
| 4111 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4112 | { |
| 4113 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4114 | |
| 4115 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4116 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4117 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4118 | * applet). |
| 4119 | */ |
| 4120 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4121 | return 1; |
| 4122 | } |
| 4123 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4124 | /* Returns true if the channel may still receive data. */ |
| 4125 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4126 | { |
| 4127 | struct channel *chn; |
| 4128 | |
| 4129 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4130 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4131 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4132 | return 1; |
| 4133 | } |
| 4134 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4135 | /* Returns true if the channel is the response channel. */ |
| 4136 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4137 | { |
| 4138 | struct channel *chn; |
| 4139 | |
| 4140 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4141 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4142 | |
| 4143 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4144 | return 1; |
| 4145 | } |
| 4146 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4147 | /* Just returns the number of bytes available in the output |
| 4148 | * side of the buffer. This function never fails. |
| 4149 | */ |
| 4150 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4151 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4152 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4153 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4154 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4155 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4156 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4157 | |
| 4158 | output = co_data(chn); |
| 4159 | input = ci_data(chn); |
| 4160 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4161 | |
| 4162 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4163 | return 1; |
| 4164 | } |
| 4165 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4166 | /* |
| 4167 | * |
| 4168 | * |
| 4169 | * Class Fetches |
| 4170 | * |
| 4171 | * |
| 4172 | */ |
| 4173 | |
| 4174 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4175 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4176 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4177 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4178 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4179 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | /* This function creates and push in the stack a fetch object according |
| 4183 | * with a current TXN. |
| 4184 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4185 | 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] | 4186 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4187 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4188 | |
| 4189 | /* Check stack size. */ |
| 4190 | if (!lua_checkstack(L, 3)) |
| 4191 | return 0; |
| 4192 | |
| 4193 | /* Create the object: obj[0] = userdata. |
| 4194 | * Note that the base of the Fetches object is the |
| 4195 | * transaction object. |
| 4196 | */ |
| 4197 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4198 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4199 | lua_rawseti(L, -2, 0); |
| 4200 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4201 | hsmp->s = txn->s; |
| 4202 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4203 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4204 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4205 | |
| 4206 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4207 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4208 | lua_setmetatable(L, -2); |
| 4209 | |
| 4210 | return 1; |
| 4211 | } |
| 4212 | |
| 4213 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4214 | * It uses closure argument to store the associated sample-fetch. It |
| 4215 | * returns only one argument or throws an error. An error is thrown |
| 4216 | * only if an error is encountered during the argument parsing. If |
| 4217 | * the "sample-fetch" function fails, nil is returned. |
| 4218 | */ |
| 4219 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4220 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4221 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4222 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4223 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4224 | int i; |
| 4225 | struct sample smp; |
| 4226 | |
| 4227 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4228 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4229 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4230 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4231 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4232 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4233 | /* Check execution authorization. */ |
| 4234 | if (f->use & SMP_USE_HTTP_ANY && |
| 4235 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4236 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4237 | "is not available in Lua services", f->kw); |
| 4238 | WILL_LJMP(lua_error(L)); |
| 4239 | } |
| 4240 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4241 | /* Get extra arguments. */ |
| 4242 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4243 | if (i >= ARGM_NBARGS) |
| 4244 | break; |
| 4245 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4246 | } |
| 4247 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4248 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4249 | |
| 4250 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4251 | 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] | 4252 | |
| 4253 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4254 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4255 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4256 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4257 | } |
| 4258 | |
| 4259 | /* Initialise the sample. */ |
| 4260 | memset(&smp, 0, sizeof(smp)); |
| 4261 | |
| 4262 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4263 | 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] | 4264 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4265 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4266 | lua_pushstring(L, ""); |
| 4267 | else |
| 4268 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4269 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
| 4272 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4273 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4274 | hlua_smp2lua_str(L, &smp); |
| 4275 | else |
| 4276 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4277 | |
| 4278 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4279 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4280 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4281 | |
| 4282 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4283 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4284 | WILL_LJMP(lua_error(L)); |
| 4285 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4286 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4287 | |
| 4288 | /* |
| 4289 | * |
| 4290 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4291 | * Class Converters |
| 4292 | * |
| 4293 | * |
| 4294 | */ |
| 4295 | |
| 4296 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4297 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4298 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4299 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4300 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4301 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | /* This function creates and push in the stack a Converters object |
| 4305 | * according with a current TXN. |
| 4306 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4307 | 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] | 4308 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4309 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4310 | |
| 4311 | /* Check stack size. */ |
| 4312 | if (!lua_checkstack(L, 3)) |
| 4313 | return 0; |
| 4314 | |
| 4315 | /* Create the object: obj[0] = userdata. |
| 4316 | * Note that the base of the Converters object is the |
| 4317 | * same than the TXN object. |
| 4318 | */ |
| 4319 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4320 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4321 | lua_rawseti(L, -2, 0); |
| 4322 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4323 | hsmp->s = txn->s; |
| 4324 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4325 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4326 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4327 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4328 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4329 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4330 | lua_setmetatable(L, -2); |
| 4331 | |
| 4332 | return 1; |
| 4333 | } |
| 4334 | |
| 4335 | /* This function is an LUA binding. It is called with each converter. |
| 4336 | * It uses closure argument to store the associated converter. It |
| 4337 | * returns only one argument or throws an error. An error is thrown |
| 4338 | * only if an error is encountered during the argument parsing. If |
| 4339 | * the converter function function fails, nil is returned. |
| 4340 | */ |
| 4341 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4342 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4343 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4344 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4345 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4346 | int i; |
| 4347 | struct sample smp; |
| 4348 | |
| 4349 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4350 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4351 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4352 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4353 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4354 | |
| 4355 | /* Get extra arguments. */ |
| 4356 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4357 | if (i >= ARGM_NBARGS) |
| 4358 | break; |
| 4359 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4360 | } |
| 4361 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4362 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4365 | 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] | 4366 | |
| 4367 | /* Run the special args checker. */ |
| 4368 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4369 | hlua_pusherror(L, "error in arguments"); |
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 | |
| 4373 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4374 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4375 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4376 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4377 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4378 | } |
| 4379 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4380 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4381 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4382 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4383 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4384 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4385 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4386 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4387 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4388 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4389 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4390 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4391 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4392 | } |
| 4393 | |
| 4394 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4395 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4396 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4397 | lua_pushstring(L, ""); |
| 4398 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4399 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4400 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4404 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4405 | hlua_smp2lua_str(L, &smp); |
| 4406 | else |
| 4407 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4408 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4409 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4410 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4411 | |
| 4412 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4413 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4414 | WILL_LJMP(lua_error(L)); |
| 4415 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4416 | } |
| 4417 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4418 | /* |
| 4419 | * |
| 4420 | * |
| 4421 | * Class AppletTCP |
| 4422 | * |
| 4423 | * |
| 4424 | */ |
| 4425 | |
| 4426 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4427 | * a class stream, otherwise it throws an error. |
| 4428 | */ |
| 4429 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4430 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4431 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4432 | } |
| 4433 | |
| 4434 | /* This function creates and push in the stack an Applet object |
| 4435 | * according with a current TXN. |
| 4436 | */ |
| 4437 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4438 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4439 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4440 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4441 | struct proxy *p; |
| 4442 | |
| 4443 | ALREADY_CHECKED(s); |
| 4444 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4445 | |
| 4446 | /* Check stack size. */ |
| 4447 | if (!lua_checkstack(L, 3)) |
| 4448 | return 0; |
| 4449 | |
| 4450 | /* Create the object: obj[0] = userdata. |
| 4451 | * Note that the base of the Converters object is the |
| 4452 | * same than the TXN object. |
| 4453 | */ |
| 4454 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4455 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4456 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4457 | luactx->appctx = ctx; |
| 4458 | luactx->htxn.s = s; |
| 4459 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4460 | |
| 4461 | /* Create the "f" field that contains a list of fetches. */ |
| 4462 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4463 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4464 | return 0; |
| 4465 | lua_settable(L, -3); |
| 4466 | |
| 4467 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4468 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4469 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4470 | return 0; |
| 4471 | lua_settable(L, -3); |
| 4472 | |
| 4473 | /* Create the "c" field that contains a list of converters. */ |
| 4474 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4475 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4476 | return 0; |
| 4477 | lua_settable(L, -3); |
| 4478 | |
| 4479 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4480 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4481 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4482 | return 0; |
| 4483 | lua_settable(L, -3); |
| 4484 | |
| 4485 | /* Pop a class stream metatable and affect it to the table. */ |
| 4486 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4487 | lua_setmetatable(L, -2); |
| 4488 | |
| 4489 | return 1; |
| 4490 | } |
| 4491 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4492 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4493 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4494 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4495 | struct stream *s; |
| 4496 | const char *name; |
| 4497 | size_t len; |
| 4498 | struct sample smp; |
| 4499 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4500 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4501 | 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] | 4502 | |
| 4503 | /* It is useles to retrieve the stream, but this function |
| 4504 | * runs only in a stream context. |
| 4505 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4506 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4507 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4508 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4509 | |
| 4510 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4511 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4512 | hlua_lua2smp(L, 3, &smp); |
| 4513 | |
| 4514 | /* Store the sample in a variable. */ |
| 4515 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4516 | |
| 4517 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4518 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4519 | else |
| 4520 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4521 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4522 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4523 | } |
| 4524 | |
| 4525 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4526 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4527 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4528 | struct stream *s; |
| 4529 | const char *name; |
| 4530 | size_t len; |
| 4531 | struct sample smp; |
| 4532 | |
| 4533 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4534 | |
| 4535 | /* It is useles to retrieve the stream, but this function |
| 4536 | * runs only in a stream context. |
| 4537 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4538 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4539 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4540 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4541 | |
| 4542 | /* Unset the variable. */ |
| 4543 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4544 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4545 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4546 | } |
| 4547 | |
| 4548 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4549 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4550 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4551 | struct stream *s; |
| 4552 | const char *name; |
| 4553 | size_t len; |
| 4554 | struct sample smp; |
| 4555 | |
| 4556 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4557 | |
| 4558 | /* It is useles to retrieve the stream, but this function |
| 4559 | * runs only in a stream context. |
| 4560 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4561 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4562 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4564 | |
| 4565 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4566 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4567 | lua_pushnil(L); |
| 4568 | return 1; |
| 4569 | } |
| 4570 | |
| 4571 | return hlua_smp2lua(L, &smp); |
| 4572 | } |
| 4573 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4574 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4575 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4576 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4577 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4578 | struct hlua *hlua; |
| 4579 | |
| 4580 | /* 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] | 4581 | if (!s->hlua) |
| 4582 | return 0; |
| 4583 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4584 | |
| 4585 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4586 | |
| 4587 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4588 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4589 | |
| 4590 | /* Get and store new value. */ |
| 4591 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4592 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4593 | |
| 4594 | return 0; |
| 4595 | } |
| 4596 | |
| 4597 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4598 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4599 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4600 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4601 | struct hlua *hlua; |
| 4602 | |
| 4603 | /* 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] | 4604 | if (!s->hlua) { |
| 4605 | lua_pushnil(L); |
| 4606 | return 1; |
| 4607 | } |
| 4608 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4609 | |
| 4610 | /* Push configuration index in the stack. */ |
| 4611 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4612 | |
| 4613 | return 1; |
| 4614 | } |
| 4615 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4616 | /* If expected data not yet available, it returns a yield. This function |
| 4617 | * consumes the data in the buffer. It returns a string containing the |
| 4618 | * data. This string can be empty. |
| 4619 | */ |
| 4620 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4621 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4622 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4623 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4624 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4625 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4626 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4627 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4628 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4629 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4630 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4631 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4633 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4634 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4635 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4636 | 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] | 4637 | } |
| 4638 | |
| 4639 | /* End of data: commit the total strings and return. */ |
| 4640 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4641 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4642 | return 1; |
| 4643 | } |
| 4644 | |
| 4645 | /* Ensure that the block 2 length is usable. */ |
| 4646 | if (ret == 1) |
| 4647 | len2 = 0; |
| 4648 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4649 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4650 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4651 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4652 | |
| 4653 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4654 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4655 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4656 | return 1; |
| 4657 | } |
| 4658 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4659 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4660 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4661 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4662 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4663 | |
| 4664 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4665 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4666 | |
| 4667 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4668 | } |
| 4669 | |
| 4670 | /* If expected data not yet available, it returns a yield. This function |
| 4671 | * consumes the data in the buffer. It returns a string containing the |
| 4672 | * data. This string can be empty. |
| 4673 | */ |
| 4674 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4675 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4676 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4677 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4678 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4679 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4680 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4681 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4682 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4683 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4684 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4685 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4686 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4687 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4688 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4689 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4690 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4691 | 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] | 4692 | } |
| 4693 | |
| 4694 | /* End of data: commit the total strings and return. */ |
| 4695 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4696 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4697 | return 1; |
| 4698 | } |
| 4699 | |
| 4700 | /* Ensure that the block 2 length is usable. */ |
| 4701 | if (ret == 1) |
| 4702 | len2 = 0; |
| 4703 | |
| 4704 | if (len == -1) { |
| 4705 | |
| 4706 | /* If len == -1, catenate all the data avalaile and |
| 4707 | * yield because we want to get all the data until |
| 4708 | * the end of data stream. |
| 4709 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4710 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4711 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4712 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4713 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4714 | 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] | 4715 | |
| 4716 | } else { |
| 4717 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4718 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4719 | if (len1 > len) |
| 4720 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4721 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4722 | len -= len1; |
| 4723 | |
| 4724 | /* Copy the second block. */ |
| 4725 | if (len2 > len) |
| 4726 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4727 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4728 | len -= len2; |
| 4729 | |
| 4730 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4731 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4732 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4733 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4734 | if (len > 0) { |
| 4735 | lua_pushinteger(L, len); |
| 4736 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4737 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4738 | 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] | 4739 | } |
| 4740 | |
| 4741 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4742 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4743 | return 1; |
| 4744 | } |
| 4745 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4746 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4747 | hlua_pusherror(L, "Lua: internal error"); |
| 4748 | WILL_LJMP(lua_error(L)); |
| 4749 | return 0; |
| 4750 | } |
| 4751 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4752 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4753 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4754 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4755 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4756 | int len = -1; |
| 4757 | |
| 4758 | if (lua_gettop(L) > 2) |
| 4759 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4760 | if (lua_gettop(L) >= 2) { |
| 4761 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4762 | lua_pop(L, 1); |
| 4763 | } |
| 4764 | |
| 4765 | /* Confirm or set the required length */ |
| 4766 | lua_pushinteger(L, len); |
| 4767 | |
| 4768 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4769 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4770 | |
| 4771 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4772 | } |
| 4773 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4774 | /* Append data in the output side of the buffer. This data is immediately |
| 4775 | * sent. The function returns the amount of data written. If the buffer |
| 4776 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4777 | * if the channel is closed. |
| 4778 | */ |
| 4779 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4780 | { |
| 4781 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4782 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4783 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4784 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4785 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4786 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4787 | int max; |
| 4788 | |
| 4789 | /* Get the max amount of data which can write as input in the channel. */ |
| 4790 | max = channel_recv_max(chn); |
| 4791 | if (max > (len - l)) |
| 4792 | max = len - l; |
| 4793 | |
| 4794 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4795 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4796 | |
| 4797 | /* update counters. */ |
| 4798 | l += max; |
| 4799 | lua_pop(L, 1); |
| 4800 | lua_pushinteger(L, l); |
| 4801 | |
| 4802 | /* If some data is not send, declares the situation to the |
| 4803 | * applet, and returns a yield. |
| 4804 | */ |
| 4805 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4806 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4807 | 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] | 4808 | } |
| 4809 | |
| 4810 | return 1; |
| 4811 | } |
| 4812 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4813 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4814 | * yield the LUA process, and resume it without checking the |
| 4815 | * input arguments. |
| 4816 | */ |
| 4817 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4818 | { |
| 4819 | MAY_LJMP(check_args(L, 2, "send")); |
| 4820 | lua_pushinteger(L, 0); |
| 4821 | |
| 4822 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4823 | } |
| 4824 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4825 | /* |
| 4826 | * |
| 4827 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4828 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4829 | * |
| 4830 | * |
| 4831 | */ |
| 4832 | |
| 4833 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4834 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4835 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4836 | __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] | 4837 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4838 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4839 | } |
| 4840 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4841 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4842 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4843 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4844 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4845 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4846 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4847 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4848 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4849 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4850 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4851 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4852 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4853 | struct htx *htx; |
| 4854 | struct htx_blk *blk; |
| 4855 | struct htx_sl *sl; |
| 4856 | struct ist path; |
| 4857 | unsigned long long len = 0; |
| 4858 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4859 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4860 | |
| 4861 | /* Check stack size. */ |
| 4862 | if (!lua_checkstack(L, 3)) |
| 4863 | return 0; |
| 4864 | |
| 4865 | /* Create the object: obj[0] = userdata. |
| 4866 | * Note that the base of the Converters object is the |
| 4867 | * same than the TXN object. |
| 4868 | */ |
| 4869 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4870 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4871 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4872 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4873 | http_ctx->status = 200; /* Default status code returned. */ |
| 4874 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4875 | luactx->htxn.s = s; |
| 4876 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4877 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4878 | /* Create the "f" field that contains a list of fetches. */ |
| 4879 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4880 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4881 | return 0; |
| 4882 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4884 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4885 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4886 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4887 | return 0; |
| 4888 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4889 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4890 | /* Create the "c" field that contains a list of converters. */ |
| 4891 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4892 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4893 | return 0; |
| 4894 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4895 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4896 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4897 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4898 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4899 | return 0; |
| 4900 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4901 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4902 | htx = htxbuf(&s->req.buf); |
| 4903 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4904 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4905 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4906 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4907 | /* Stores the request method. */ |
| 4908 | lua_pushstring(L, "method"); |
| 4909 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4910 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4912 | /* Stores the http version. */ |
| 4913 | lua_pushstring(L, "version"); |
| 4914 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4915 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4916 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4917 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4918 | * the array on the top of the stack. |
| 4919 | */ |
| 4920 | lua_pushstring(L, "headers"); |
| 4921 | htxn.s = s; |
| 4922 | htxn.p = px; |
| 4923 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4924 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4925 | return 0; |
| 4926 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4927 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4928 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4929 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4930 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4931 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4932 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4933 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4934 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4935 | q = p; |
| 4936 | while (q < end && *q != '?') |
| 4937 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4938 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4939 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4940 | lua_pushstring(L, "path"); |
| 4941 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4942 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4943 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4944 | /* Stores the query string. */ |
| 4945 | lua_pushstring(L, "qs"); |
| 4946 | if (*q == '?') |
| 4947 | q++; |
| 4948 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4949 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4950 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4951 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4952 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4953 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4954 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4955 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4956 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4957 | break; |
| 4958 | if (type == HTX_BLK_DATA) |
| 4959 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4960 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 4961 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4962 | len += htx->extra; |
| 4963 | |
| 4964 | /* Stores the request path. */ |
| 4965 | lua_pushstring(L, "length"); |
| 4966 | lua_pushinteger(L, len); |
| 4967 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4968 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4969 | /* Create an empty array of HTTP request headers. */ |
| 4970 | lua_pushstring(L, "response"); |
| 4971 | lua_newtable(L); |
| 4972 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4973 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4974 | /* Pop a class stream metatable and affect it to the table. */ |
| 4975 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4976 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4977 | |
| 4978 | return 1; |
| 4979 | } |
| 4980 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4981 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4982 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4983 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4984 | struct stream *s; |
| 4985 | const char *name; |
| 4986 | size_t len; |
| 4987 | struct sample smp; |
| 4988 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4989 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4990 | 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] | 4991 | |
| 4992 | /* It is useles to retrieve the stream, but this function |
| 4993 | * runs only in a stream context. |
| 4994 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4995 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4996 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4997 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4998 | |
| 4999 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 5000 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5001 | hlua_lua2smp(L, 3, &smp); |
| 5002 | |
| 5003 | /* Store the sample in a variable. */ |
| 5004 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5005 | |
| 5006 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5007 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5008 | else |
| 5009 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5010 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5011 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5012 | } |
| 5013 | |
| 5014 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 5015 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5016 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5017 | struct stream *s; |
| 5018 | const char *name; |
| 5019 | size_t len; |
| 5020 | struct sample smp; |
| 5021 | |
| 5022 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5023 | |
| 5024 | /* It is useles to retrieve the stream, but this function |
| 5025 | * runs only in a stream context. |
| 5026 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5027 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5028 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5029 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5030 | |
| 5031 | /* Unset the variable. */ |
| 5032 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5033 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5034 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5035 | } |
| 5036 | |
| 5037 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 5038 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5039 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5040 | struct stream *s; |
| 5041 | const char *name; |
| 5042 | size_t len; |
| 5043 | struct sample smp; |
| 5044 | |
| 5045 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5046 | |
| 5047 | /* It is useles to retrieve the stream, but this function |
| 5048 | * runs only in a stream context. |
| 5049 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5050 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5051 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5052 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5053 | |
| 5054 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5055 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5056 | lua_pushnil(L); |
| 5057 | return 1; |
| 5058 | } |
| 5059 | |
| 5060 | return hlua_smp2lua(L, &smp); |
| 5061 | } |
| 5062 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5063 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5064 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5065 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5066 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5067 | struct hlua *hlua; |
| 5068 | |
| 5069 | /* 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] | 5070 | if (!s->hlua) |
| 5071 | return 0; |
| 5072 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5073 | |
| 5074 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5075 | |
| 5076 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5077 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5078 | |
| 5079 | /* Get and store new value. */ |
| 5080 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5081 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5082 | |
| 5083 | return 0; |
| 5084 | } |
| 5085 | |
| 5086 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5087 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5088 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5089 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5090 | struct hlua *hlua; |
| 5091 | |
| 5092 | /* 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] | 5093 | if (!s->hlua) { |
| 5094 | lua_pushnil(L); |
| 5095 | return 1; |
| 5096 | } |
| 5097 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5098 | |
| 5099 | /* Push configuration index in the stack. */ |
| 5100 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5101 | |
| 5102 | return 1; |
| 5103 | } |
| 5104 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5105 | /* If expected data not yet available, it returns a yield. This function |
| 5106 | * consumes the data in the buffer. It returns a string containing the |
| 5107 | * data. This string can be empty. |
| 5108 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5109 | __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] | 5110 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5111 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5112 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5113 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5114 | struct htx *htx; |
| 5115 | struct htx_blk *blk; |
| 5116 | size_t count; |
| 5117 | int stop = 0; |
| 5118 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5119 | htx = htx_from_buf(&req->buf); |
| 5120 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5121 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5122 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5123 | while (count && !stop && blk) { |
| 5124 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5125 | uint32_t sz = htx_get_blksz(blk); |
| 5126 | struct ist v; |
| 5127 | uint32_t vlen; |
| 5128 | char *nl; |
| 5129 | |
| 5130 | vlen = sz; |
| 5131 | if (vlen > count) { |
| 5132 | if (type != HTX_BLK_DATA) |
| 5133 | break; |
| 5134 | vlen = count; |
| 5135 | } |
| 5136 | |
| 5137 | switch (type) { |
| 5138 | case HTX_BLK_UNUSED: |
| 5139 | break; |
| 5140 | |
| 5141 | case HTX_BLK_DATA: |
| 5142 | v = htx_get_blk_value(htx, blk); |
| 5143 | v.len = vlen; |
| 5144 | nl = istchr(v, '\n'); |
| 5145 | if (nl != NULL) { |
| 5146 | stop = 1; |
| 5147 | vlen = nl - v.ptr + 1; |
| 5148 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5149 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5150 | break; |
| 5151 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5152 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5153 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5154 | stop = 1; |
| 5155 | break; |
| 5156 | |
| 5157 | default: |
| 5158 | break; |
| 5159 | } |
| 5160 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5161 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5162 | count -= vlen; |
| 5163 | if (sz == vlen) |
| 5164 | blk = htx_remove_blk(htx, blk); |
| 5165 | else { |
| 5166 | htx_cut_data_blk(htx, blk, vlen); |
| 5167 | break; |
| 5168 | } |
| 5169 | } |
| 5170 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5171 | /* The message was fully consumed and no more data are expected |
| 5172 | * (EOM flag set). |
| 5173 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5174 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5175 | stop = 1; |
| 5176 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5177 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5178 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5179 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5180 | 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] | 5181 | } |
| 5182 | |
| 5183 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5184 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5185 | return 1; |
| 5186 | } |
| 5187 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5188 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5189 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5190 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5191 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5192 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5193 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5194 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5195 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5196 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5197 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5198 | } |
| 5199 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5200 | /* If expected data not yet available, it returns a yield. This function |
| 5201 | * consumes the data in the buffer. It returns a string containing the |
| 5202 | * data. This string can be empty. |
| 5203 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5204 | __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] | 5205 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5206 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5207 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5208 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5209 | struct htx *htx; |
| 5210 | struct htx_blk *blk; |
| 5211 | size_t count; |
| 5212 | int len; |
| 5213 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5214 | htx = htx_from_buf(&req->buf); |
| 5215 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5216 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5217 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5218 | while (count && len && blk) { |
| 5219 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5220 | uint32_t sz = htx_get_blksz(blk); |
| 5221 | struct ist v; |
| 5222 | uint32_t vlen; |
| 5223 | |
| 5224 | vlen = sz; |
| 5225 | if (len > 0 && vlen > len) |
| 5226 | vlen = len; |
| 5227 | if (vlen > count) { |
| 5228 | if (type != HTX_BLK_DATA) |
| 5229 | break; |
| 5230 | vlen = count; |
| 5231 | } |
| 5232 | |
| 5233 | switch (type) { |
| 5234 | case HTX_BLK_UNUSED: |
| 5235 | break; |
| 5236 | |
| 5237 | case HTX_BLK_DATA: |
| 5238 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5239 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5240 | break; |
| 5241 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5242 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5243 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5244 | len = 0; |
| 5245 | break; |
| 5246 | |
| 5247 | default: |
| 5248 | break; |
| 5249 | } |
| 5250 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5251 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5252 | count -= vlen; |
| 5253 | if (len > 0) |
| 5254 | len -= vlen; |
| 5255 | if (sz == vlen) |
| 5256 | blk = htx_remove_blk(htx, blk); |
| 5257 | else { |
| 5258 | htx_cut_data_blk(htx, blk, vlen); |
| 5259 | break; |
| 5260 | } |
| 5261 | } |
| 5262 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5263 | /* The message was fully consumed and no more data are expected |
| 5264 | * (EOM flag set). |
| 5265 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5266 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5267 | len = 0; |
| 5268 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5269 | htx_to_buf(htx, &req->buf); |
| 5270 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5271 | /* If we are no other data available, yield waiting for new data. */ |
| 5272 | if (len) { |
| 5273 | if (len > 0) { |
| 5274 | lua_pushinteger(L, len); |
| 5275 | lua_replace(L, 2); |
| 5276 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5277 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5278 | 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] | 5279 | } |
| 5280 | |
| 5281 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5282 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5283 | return 1; |
| 5284 | } |
| 5285 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5286 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5287 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5288 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5289 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5290 | int len = -1; |
| 5291 | |
| 5292 | /* Check arguments. */ |
| 5293 | if (lua_gettop(L) > 2) |
| 5294 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5295 | if (lua_gettop(L) >= 2) { |
| 5296 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5297 | lua_pop(L, 1); |
| 5298 | } |
| 5299 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5300 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5301 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5302 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5303 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5304 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5305 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5306 | } |
| 5307 | |
| 5308 | /* Append data in the output side of the buffer. This data is immediately |
| 5309 | * sent. The function returns the amount of data written. If the buffer |
| 5310 | * cannot contain the data, the function yields. The function returns -1 |
| 5311 | * if the channel is closed. |
| 5312 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5313 | __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] | 5314 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5315 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5316 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5317 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5318 | struct htx *htx = htx_from_buf(&res->buf); |
| 5319 | const char *data; |
| 5320 | size_t len; |
| 5321 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5322 | int max; |
| 5323 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5324 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5325 | if (!max) |
| 5326 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5327 | |
| 5328 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5329 | |
| 5330 | /* Get the max amount of data which can write as input in the channel. */ |
| 5331 | if (max > (len - l)) |
| 5332 | max = len - l; |
| 5333 | |
| 5334 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5335 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5336 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5337 | |
| 5338 | /* update counters. */ |
| 5339 | l += max; |
| 5340 | lua_pop(L, 1); |
| 5341 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5342 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5343 | /* If some data is not send, declares the situation to the |
| 5344 | * applet, and returns a yield. |
| 5345 | */ |
| 5346 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5347 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5348 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5349 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5350 | 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] | 5351 | } |
| 5352 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5353 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5354 | return 1; |
| 5355 | } |
| 5356 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5357 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5358 | * yield the LUA process, and resume it without checking the |
| 5359 | * input arguments. |
| 5360 | */ |
| 5361 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5362 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5363 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5364 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5365 | |
| 5366 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5367 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5368 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5369 | WILL_LJMP(lua_error(L)); |
| 5370 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5371 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5372 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5373 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5374 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5375 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5376 | } |
| 5377 | |
| 5378 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5379 | { |
| 5380 | const char *name; |
| 5381 | int ret; |
| 5382 | |
| 5383 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5384 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5385 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5386 | |
| 5387 | /* Push in the stack the "response" entry. */ |
| 5388 | ret = lua_getfield(L, 1, "response"); |
| 5389 | if (ret != LUA_TTABLE) { |
| 5390 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5391 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5392 | WILL_LJMP(lua_error(L)); |
| 5393 | } |
| 5394 | |
| 5395 | /* check if the header is already registered if it is not |
| 5396 | * the case, register it. |
| 5397 | */ |
| 5398 | ret = lua_getfield(L, -1, name); |
| 5399 | if (ret == LUA_TNIL) { |
| 5400 | |
| 5401 | /* Entry not found. */ |
| 5402 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5403 | |
| 5404 | /* Insert the new header name in the array in the top of the stack. |
| 5405 | * It left the new array in the top of the stack. |
| 5406 | */ |
| 5407 | lua_newtable(L); |
| 5408 | lua_pushvalue(L, 2); |
| 5409 | lua_pushvalue(L, -2); |
| 5410 | lua_settable(L, -4); |
| 5411 | |
| 5412 | } else if (ret != LUA_TTABLE) { |
| 5413 | |
| 5414 | /* corruption error. */ |
| 5415 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5416 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5417 | WILL_LJMP(lua_error(L)); |
| 5418 | } |
| 5419 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5420 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5421 | * the header value as new entry. |
| 5422 | */ |
| 5423 | lua_pushvalue(L, 3); |
| 5424 | ret = lua_rawlen(L, -2); |
| 5425 | lua_rawseti(L, -2, ret + 1); |
| 5426 | lua_pushboolean(L, 1); |
| 5427 | return 1; |
| 5428 | } |
| 5429 | |
| 5430 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5431 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5432 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5433 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5434 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5435 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5436 | |
| 5437 | if (status < 100 || status > 599) { |
| 5438 | lua_pushboolean(L, 0); |
| 5439 | return 1; |
| 5440 | } |
| 5441 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5442 | http_ctx->status = status; |
| 5443 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5444 | lua_pushboolean(L, 1); |
| 5445 | return 1; |
| 5446 | } |
| 5447 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5448 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5449 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5450 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5451 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5452 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5453 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5454 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5455 | struct htx *htx; |
| 5456 | struct htx_sl *sl; |
| 5457 | struct h1m h1m; |
| 5458 | const char *status, *reason; |
| 5459 | const char *name, *value; |
| 5460 | size_t nlen, vlen; |
| 5461 | unsigned int flags; |
| 5462 | |
| 5463 | /* Send the message at once. */ |
| 5464 | htx = htx_from_buf(&res->buf); |
| 5465 | h1m_init_res(&h1m); |
| 5466 | |
| 5467 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5468 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5469 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5470 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5471 | reason = http_get_reason(http_ctx->status); |
| 5472 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5473 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5474 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5475 | } |
| 5476 | else { |
| 5477 | flags = HTX_SL_F_IS_RESP; |
| 5478 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5479 | } |
| 5480 | if (!sl) { |
| 5481 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5482 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5483 | WILL_LJMP(lua_error(L)); |
| 5484 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5485 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5486 | |
| 5487 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5488 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5489 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5490 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5491 | WILL_LJMP(lua_error(L)); |
| 5492 | } |
| 5493 | |
| 5494 | /* Browse the list of headers. */ |
| 5495 | lua_pushnil(L); |
| 5496 | while(lua_next(L, -2) != 0) { |
| 5497 | /* We expect a string as -2. */ |
| 5498 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5499 | 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] | 5500 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5501 | lua_typename(L, lua_type(L, -2))); |
| 5502 | WILL_LJMP(lua_error(L)); |
| 5503 | } |
| 5504 | name = lua_tolstring(L, -2, &nlen); |
| 5505 | |
| 5506 | /* We expect an array as -1. */ |
| 5507 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5508 | 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] | 5509 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5510 | name, |
| 5511 | lua_typename(L, lua_type(L, -1))); |
| 5512 | WILL_LJMP(lua_error(L)); |
| 5513 | } |
| 5514 | |
| 5515 | /* Browse the table who is on the top of the stack. */ |
| 5516 | lua_pushnil(L); |
| 5517 | while(lua_next(L, -2) != 0) { |
| 5518 | int id; |
| 5519 | |
| 5520 | /* We expect a number as -2. */ |
| 5521 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5522 | 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] | 5523 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5524 | name, |
| 5525 | lua_typename(L, lua_type(L, -2))); |
| 5526 | WILL_LJMP(lua_error(L)); |
| 5527 | } |
| 5528 | id = lua_tointeger(L, -2); |
| 5529 | |
| 5530 | /* We expect a string as -2. */ |
| 5531 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5532 | 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] | 5533 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5534 | name, id, |
| 5535 | lua_typename(L, lua_type(L, -1))); |
| 5536 | WILL_LJMP(lua_error(L)); |
| 5537 | } |
| 5538 | value = lua_tolstring(L, -1, &vlen); |
| 5539 | |
| 5540 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5541 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5542 | int ret; |
| 5543 | |
| 5544 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5545 | if (ret < 0) { |
| 5546 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5547 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5548 | name); |
| 5549 | WILL_LJMP(lua_error(L)); |
| 5550 | } |
| 5551 | else if (ret == 0) |
| 5552 | goto next; /* Skip it */ |
| 5553 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5554 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5555 | struct ist v = ist2(value, vlen); |
| 5556 | int ret; |
| 5557 | |
| 5558 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5559 | if (ret < 0) { |
| 5560 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5561 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5562 | name); |
| 5563 | WILL_LJMP(lua_error(L)); |
| 5564 | } |
| 5565 | else if (ret == 0) |
| 5566 | goto next; /* Skip it */ |
| 5567 | } |
| 5568 | |
| 5569 | /* Add a new header */ |
| 5570 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5571 | 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] | 5572 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5573 | name); |
| 5574 | WILL_LJMP(lua_error(L)); |
| 5575 | } |
| 5576 | next: |
| 5577 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5578 | lua_pop(L, 1); |
| 5579 | } |
| 5580 | |
| 5581 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5582 | lua_pop(L, 1); |
| 5583 | } |
| 5584 | |
| 5585 | if (h1m.flags & H1_MF_CHNK) |
| 5586 | h1m.flags &= ~H1_MF_CLEN; |
| 5587 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5588 | h1m.flags |= H1_MF_XFER_LEN; |
| 5589 | |
| 5590 | /* Uset HTX start-line flags */ |
| 5591 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5592 | flags |= HTX_SL_F_XFER_ENC; |
| 5593 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5594 | flags |= HTX_SL_F_XFER_LEN; |
| 5595 | if (h1m.flags & H1_MF_CHNK) |
| 5596 | flags |= HTX_SL_F_CHNK; |
| 5597 | else if (h1m.flags & H1_MF_CLEN) |
| 5598 | flags |= HTX_SL_F_CLEN; |
| 5599 | if (h1m.body_len == 0) |
| 5600 | flags |= HTX_SL_F_BODYLESS; |
| 5601 | } |
| 5602 | sl->flags |= flags; |
| 5603 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5604 | /* 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] | 5605 | * and the status code implies the presence of a message body, we must |
| 5606 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5607 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5608 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5609 | */ |
| 5610 | 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] | 5611 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5612 | /* Add a new header */ |
| 5613 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5614 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5615 | 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] | 5616 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5617 | WILL_LJMP(lua_error(L)); |
| 5618 | } |
| 5619 | } |
| 5620 | |
| 5621 | /* Finalize headers. */ |
| 5622 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5623 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5624 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5625 | WILL_LJMP(lua_error(L)); |
| 5626 | } |
| 5627 | |
| 5628 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5629 | b_reset(&res->buf); |
| 5630 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5631 | WILL_LJMP(lua_error(L)); |
| 5632 | } |
| 5633 | |
| 5634 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5635 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5636 | |
| 5637 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5638 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5639 | return 0; |
| 5640 | |
| 5641 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5642 | /* We will build the status line and the headers of the HTTP response. |
| 5643 | * We will try send at once if its not possible, we give back the hand |
| 5644 | * waiting for more room. |
| 5645 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5646 | __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] | 5647 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5648 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5649 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5650 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5651 | |
| 5652 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5653 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5654 | 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] | 5655 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5656 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5657 | } |
| 5658 | |
| 5659 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5660 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5661 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5662 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5663 | } |
| 5664 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5665 | /* |
| 5666 | * |
| 5667 | * |
| 5668 | * Class HTTP |
| 5669 | * |
| 5670 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5671 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5672 | |
| 5673 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5674 | * a class stream, otherwise it throws an error. |
| 5675 | */ |
| 5676 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5677 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5678 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5679 | } |
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 | /* This function creates and push in the stack a HTTP object |
| 5682 | * according with a current TXN. |
| 5683 | */ |
| 5684 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5685 | { |
| 5686 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5687 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5688 | /* Check stack size. */ |
| 5689 | if (!lua_checkstack(L, 3)) |
| 5690 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5691 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5692 | /* Create the object: obj[0] = userdata. |
| 5693 | * Note that the base of the Converters object is the |
| 5694 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5695 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5696 | lua_newtable(L); |
| 5697 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5698 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5699 | |
| 5700 | htxn->s = txn->s; |
| 5701 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5702 | htxn->dir = txn->dir; |
| 5703 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5704 | |
| 5705 | /* Pop a class stream metatable and affect it to the table. */ |
| 5706 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5707 | lua_setmetatable(L, -2); |
| 5708 | |
| 5709 | return 1; |
| 5710 | } |
| 5711 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5712 | /* This function creates and returns an array containing the status-line |
| 5713 | * elements. This function does not fails. |
| 5714 | */ |
| 5715 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5716 | { |
| 5717 | /* Create the table. */ |
| 5718 | lua_newtable(L); |
| 5719 | |
| 5720 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5721 | lua_pushstring(L, "version"); |
| 5722 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5723 | lua_settable(L, -3); |
| 5724 | lua_pushstring(L, "code"); |
| 5725 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5726 | lua_settable(L, -3); |
| 5727 | lua_pushstring(L, "reason"); |
| 5728 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5729 | lua_settable(L, -3); |
| 5730 | } |
| 5731 | else { |
| 5732 | lua_pushstring(L, "method"); |
| 5733 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5734 | lua_settable(L, -3); |
| 5735 | lua_pushstring(L, "uri"); |
| 5736 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5737 | lua_settable(L, -3); |
| 5738 | lua_pushstring(L, "version"); |
| 5739 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5740 | lua_settable(L, -3); |
| 5741 | } |
| 5742 | return 1; |
| 5743 | } |
| 5744 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5745 | /* This function creates ans returns an array of HTTP headers. |
| 5746 | * This function does not fails. It is used as wrapper with the |
| 5747 | * 2 following functions. |
| 5748 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5749 | __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] | 5750 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5751 | struct htx *htx; |
| 5752 | int32_t pos; |
| 5753 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5754 | /* Create the table. */ |
| 5755 | lua_newtable(L); |
| 5756 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5757 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5758 | htx = htxbuf(&msg->chn->buf); |
| 5759 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5760 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5761 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5762 | struct ist n, v; |
| 5763 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5764 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5765 | if (type == HTX_BLK_HDR) { |
| 5766 | n = htx_get_blk_name(htx,blk); |
| 5767 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5768 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5769 | else if (type == HTX_BLK_EOH) |
| 5770 | break; |
| 5771 | else |
| 5772 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5773 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5774 | /* Check for existing entry: |
| 5775 | * assume that the table is on the top of the stack, and |
| 5776 | * push the key in the stack, the function lua_gettable() |
| 5777 | * perform the lookup. |
| 5778 | */ |
| 5779 | lua_pushlstring(L, n.ptr, n.len); |
| 5780 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5781 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5782 | switch (lua_type(L, -1)) { |
| 5783 | case LUA_TNIL: |
| 5784 | /* Table not found, create it. */ |
| 5785 | lua_pop(L, 1); /* remove the nil value. */ |
| 5786 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5787 | lua_newtable(L); /* create and push empty table. */ |
| 5788 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5789 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5790 | 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] | 5791 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5792 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5793 | case LUA_TTABLE: |
| 5794 | /* Entry found: push the value in the table. */ |
| 5795 | len = lua_rawlen(L, -1); |
| 5796 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5797 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5798 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5799 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5800 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5801 | default: |
| 5802 | /* Other cases are errors. */ |
| 5803 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5804 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5805 | } |
| 5806 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5807 | return 1; |
| 5808 | } |
| 5809 | |
| 5810 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5811 | { |
| 5812 | struct hlua_txn *htxn; |
| 5813 | |
| 5814 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5815 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5816 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5817 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5818 | WILL_LJMP(lua_error(L)); |
| 5819 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5820 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5821 | } |
| 5822 | |
| 5823 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5824 | { |
| 5825 | struct hlua_txn *htxn; |
| 5826 | |
| 5827 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5828 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5829 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5830 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5831 | WILL_LJMP(lua_error(L)); |
| 5832 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5833 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5834 | } |
| 5835 | |
| 5836 | /* This function replace full header, or just a value in |
| 5837 | * the request or in the response. It is a wrapper fir the |
| 5838 | * 4 following functions. |
| 5839 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5840 | __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] | 5841 | { |
| 5842 | size_t name_len; |
| 5843 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5844 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5845 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5846 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5847 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5848 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5849 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5850 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5851 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5852 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5853 | 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] | 5854 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5855 | return 0; |
| 5856 | } |
| 5857 | |
| 5858 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5859 | { |
| 5860 | struct hlua_txn *htxn; |
| 5861 | |
| 5862 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5863 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5864 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5865 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5866 | WILL_LJMP(lua_error(L)); |
| 5867 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5868 | 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] | 5869 | } |
| 5870 | |
| 5871 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5872 | { |
| 5873 | struct hlua_txn *htxn; |
| 5874 | |
| 5875 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5876 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5877 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5878 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5879 | WILL_LJMP(lua_error(L)); |
| 5880 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5881 | 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] | 5882 | } |
| 5883 | |
| 5884 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5885 | { |
| 5886 | struct hlua_txn *htxn; |
| 5887 | |
| 5888 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5889 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5890 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5891 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5892 | WILL_LJMP(lua_error(L)); |
| 5893 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5894 | 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] | 5895 | } |
| 5896 | |
| 5897 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5898 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5899 | struct hlua_txn *htxn; |
| 5900 | |
| 5901 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5902 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5903 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5904 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5905 | WILL_LJMP(lua_error(L)); |
| 5906 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5907 | 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] | 5908 | } |
| 5909 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5910 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5911 | * It is a wrapper for the 2 following functions. |
| 5912 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5913 | __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] | 5914 | { |
| 5915 | size_t len; |
| 5916 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5917 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5918 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5919 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5920 | ctx.blk = NULL; |
| 5921 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5922 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5927 | { |
| 5928 | struct hlua_txn *htxn; |
| 5929 | |
| 5930 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5931 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5932 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5933 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5934 | WILL_LJMP(lua_error(L)); |
| 5935 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5936 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5937 | } |
| 5938 | |
| 5939 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5940 | { |
| 5941 | struct hlua_txn *htxn; |
| 5942 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5943 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5944 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5945 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5946 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5947 | WILL_LJMP(lua_error(L)); |
| 5948 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5949 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5950 | } |
| 5951 | |
| 5952 | /* This function adds an header. It is a wrapper used by |
| 5953 | * the 2 following functions. |
| 5954 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5955 | __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] | 5956 | { |
| 5957 | size_t name_len; |
| 5958 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5959 | size_t value_len; |
| 5960 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5961 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5962 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5963 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5964 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5965 | return 0; |
| 5966 | } |
| 5967 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5968 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5969 | { |
| 5970 | struct hlua_txn *htxn; |
| 5971 | |
| 5972 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5973 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5974 | |
| 5975 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5976 | WILL_LJMP(lua_error(L)); |
| 5977 | |
| 5978 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5979 | } |
| 5980 | |
| 5981 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5982 | { |
| 5983 | struct hlua_txn *htxn; |
| 5984 | |
| 5985 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5986 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5987 | |
| 5988 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5989 | WILL_LJMP(lua_error(L)); |
| 5990 | |
| 5991 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5992 | } |
| 5993 | |
| 5994 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5995 | { |
| 5996 | struct hlua_txn *htxn; |
| 5997 | |
| 5998 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5999 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6000 | |
| 6001 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6002 | WILL_LJMP(lua_error(L)); |
| 6003 | |
| 6004 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 6005 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 6006 | } |
| 6007 | |
| 6008 | static int hlua_http_res_set_hdr(lua_State *L) |
| 6009 | { |
| 6010 | struct hlua_txn *htxn; |
| 6011 | |
| 6012 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 6013 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6014 | |
| 6015 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6016 | WILL_LJMP(lua_error(L)); |
| 6017 | |
| 6018 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 6019 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 6020 | } |
| 6021 | |
| 6022 | /* This function set the method. */ |
| 6023 | static int hlua_http_req_set_meth(lua_State *L) |
| 6024 | { |
| 6025 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6026 | size_t name_len; |
| 6027 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6028 | |
| 6029 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6030 | WILL_LJMP(lua_error(L)); |
| 6031 | |
| 6032 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 6033 | return 1; |
| 6034 | } |
| 6035 | |
| 6036 | /* This function set the method. */ |
| 6037 | static int hlua_http_req_set_path(lua_State *L) |
| 6038 | { |
| 6039 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6040 | size_t name_len; |
| 6041 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6042 | |
| 6043 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6044 | WILL_LJMP(lua_error(L)); |
| 6045 | |
| 6046 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 6047 | return 1; |
| 6048 | } |
| 6049 | |
| 6050 | /* This function set the query-string. */ |
| 6051 | static int hlua_http_req_set_query(lua_State *L) |
| 6052 | { |
| 6053 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6054 | size_t name_len; |
| 6055 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6056 | |
| 6057 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6058 | WILL_LJMP(lua_error(L)); |
| 6059 | |
| 6060 | /* Check length. */ |
| 6061 | if (name_len > trash.size - 1) { |
| 6062 | lua_pushboolean(L, 0); |
| 6063 | return 1; |
| 6064 | } |
| 6065 | |
| 6066 | /* Add the mark question as prefix. */ |
| 6067 | chunk_reset(&trash); |
| 6068 | trash.area[trash.data++] = '?'; |
| 6069 | memcpy(trash.area + trash.data, name, name_len); |
| 6070 | trash.data += name_len; |
| 6071 | |
| 6072 | lua_pushboolean(L, |
| 6073 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6074 | return 1; |
| 6075 | } |
| 6076 | |
| 6077 | /* This function set the uri. */ |
| 6078 | static int hlua_http_req_set_uri(lua_State *L) |
| 6079 | { |
| 6080 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6081 | size_t name_len; |
| 6082 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6083 | |
| 6084 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6085 | WILL_LJMP(lua_error(L)); |
| 6086 | |
| 6087 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6088 | return 1; |
| 6089 | } |
| 6090 | |
| 6091 | /* This function set the response code & optionally reason. */ |
| 6092 | static int hlua_http_res_set_status(lua_State *L) |
| 6093 | { |
| 6094 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6095 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6096 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6097 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6098 | |
| 6099 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6100 | WILL_LJMP(lua_error(L)); |
| 6101 | |
| 6102 | http_res_set_status(code, reason, htxn->s); |
| 6103 | return 0; |
| 6104 | } |
| 6105 | |
| 6106 | /* |
| 6107 | * |
| 6108 | * |
| 6109 | * Class HTTPMessage |
| 6110 | * |
| 6111 | * |
| 6112 | */ |
| 6113 | |
| 6114 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6115 | * otherwise it throws an error. |
| 6116 | */ |
| 6117 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6118 | { |
| 6119 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6120 | } |
| 6121 | |
| 6122 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6123 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6124 | 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] | 6125 | { |
| 6126 | /* Check stack size. */ |
| 6127 | if (!lua_checkstack(L, 3)) |
| 6128 | return 0; |
| 6129 | |
| 6130 | lua_newtable(L); |
| 6131 | lua_pushlightuserdata(L, msg); |
| 6132 | lua_rawseti(L, -2, 0); |
| 6133 | |
| 6134 | /* Create the "channel" field that contains the request channel object. */ |
| 6135 | lua_pushstring(L, "channel"); |
| 6136 | if (!hlua_channel_new(L, msg->chn)) |
| 6137 | return 0; |
| 6138 | lua_rawset(L, -3); |
| 6139 | |
| 6140 | /* Pop a class stream metatable and affect it to the table. */ |
| 6141 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6142 | lua_setmetatable(L, -2); |
| 6143 | |
| 6144 | return 1; |
| 6145 | } |
| 6146 | |
| 6147 | /* Helper function returning a filter attached to the HTTP message at the |
| 6148 | * 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] | 6149 | * 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] | 6150 | * filled with output and input length respectively. |
| 6151 | */ |
| 6152 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6153 | { |
| 6154 | struct channel *chn = msg->chn; |
| 6155 | struct htx *htx = htxbuf(&chn->buf); |
| 6156 | struct filter *filter = NULL; |
| 6157 | |
| 6158 | *offset = co_data(msg->chn); |
| 6159 | *len = htx->data - co_data(msg->chn); |
| 6160 | |
| 6161 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6162 | filter = lua_touserdata (L, -1); |
| 6163 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6164 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6165 | |
| 6166 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6167 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6168 | } |
| 6169 | } |
| 6170 | |
| 6171 | lua_pop(L, 1); |
| 6172 | return filter; |
| 6173 | } |
| 6174 | |
| 6175 | /* Returns true if the channel attached to the HTTP message is the response |
| 6176 | * channel. |
| 6177 | */ |
| 6178 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6179 | { |
| 6180 | struct http_msg *msg; |
| 6181 | |
| 6182 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6183 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6184 | |
| 6185 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6186 | return 1; |
| 6187 | } |
| 6188 | |
| 6189 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6190 | * on hlua_http_get_stline(). |
| 6191 | */ |
| 6192 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6193 | { |
| 6194 | struct http_msg *msg; |
| 6195 | struct htx *htx; |
| 6196 | struct htx_sl *sl; |
| 6197 | |
| 6198 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6199 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6200 | |
| 6201 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6202 | WILL_LJMP(lua_error(L)); |
| 6203 | |
| 6204 | htx = htxbuf(&msg->chn->buf); |
| 6205 | sl = http_get_stline(htx); |
| 6206 | if (!sl) |
| 6207 | return 0; |
| 6208 | return hlua_http_get_stline(L, sl); |
| 6209 | } |
| 6210 | |
| 6211 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6212 | * hlua_http_get_headers(). |
| 6213 | */ |
| 6214 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6215 | { |
| 6216 | struct http_msg *msg; |
| 6217 | |
| 6218 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6219 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6220 | |
| 6221 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6222 | WILL_LJMP(lua_error(L)); |
| 6223 | |
| 6224 | return hlua_http_get_headers(L, msg); |
| 6225 | } |
| 6226 | |
| 6227 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6228 | * name. It relies on hlua_http_del_hdr(). |
| 6229 | */ |
| 6230 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6231 | { |
| 6232 | struct http_msg *msg; |
| 6233 | |
| 6234 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6235 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6236 | |
| 6237 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6238 | WILL_LJMP(lua_error(L)); |
| 6239 | |
| 6240 | return hlua_http_del_hdr(L, msg); |
| 6241 | } |
| 6242 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6243 | /* 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] | 6244 | * message given its name against a regex and replaces it if it matches. It |
| 6245 | * relies on hlua_http_rep_hdr(). |
| 6246 | */ |
| 6247 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6248 | { |
| 6249 | struct http_msg *msg; |
| 6250 | |
| 6251 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6252 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6253 | |
| 6254 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6255 | WILL_LJMP(lua_error(L)); |
| 6256 | |
| 6257 | return hlua_http_rep_hdr(L, msg, 1); |
| 6258 | } |
| 6259 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6260 | /* 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] | 6261 | * message given its name against a regex and replaces it if it matches. It |
| 6262 | * relies on hlua_http_rep_hdr(). |
| 6263 | */ |
| 6264 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6265 | { |
| 6266 | struct http_msg *msg; |
| 6267 | |
| 6268 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6269 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6270 | |
| 6271 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6272 | WILL_LJMP(lua_error(L)); |
| 6273 | |
| 6274 | return hlua_http_rep_hdr(L, msg, 0); |
| 6275 | } |
| 6276 | |
| 6277 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6278 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6279 | { |
| 6280 | struct http_msg *msg; |
| 6281 | |
| 6282 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6283 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6284 | |
| 6285 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6286 | WILL_LJMP(lua_error(L)); |
| 6287 | |
| 6288 | return hlua_http_add_hdr(L, msg); |
| 6289 | } |
| 6290 | |
| 6291 | /* Add an header in the HTTP message removing existing headers with the same |
| 6292 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6293 | */ |
| 6294 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6295 | { |
| 6296 | struct http_msg *msg; |
| 6297 | |
| 6298 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6299 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6300 | |
| 6301 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6302 | WILL_LJMP(lua_error(L)); |
| 6303 | |
| 6304 | hlua_http_del_hdr(L, msg); |
| 6305 | return hlua_http_add_hdr(L, msg); |
| 6306 | } |
| 6307 | |
| 6308 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6309 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6310 | { |
| 6311 | struct stream *s; |
| 6312 | struct http_msg *msg; |
| 6313 | const char *name; |
| 6314 | size_t name_len; |
| 6315 | |
| 6316 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6317 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6318 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6319 | |
| 6320 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6321 | WILL_LJMP(lua_error(L)); |
| 6322 | |
| 6323 | s = chn_strm(msg->chn); |
| 6324 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6325 | return 1; |
| 6326 | } |
| 6327 | |
| 6328 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6329 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6330 | { |
| 6331 | struct stream *s; |
| 6332 | struct http_msg *msg; |
| 6333 | const char *name; |
| 6334 | size_t name_len; |
| 6335 | |
| 6336 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6337 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6338 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6339 | |
| 6340 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6341 | WILL_LJMP(lua_error(L)); |
| 6342 | |
| 6343 | s = chn_strm(msg->chn); |
| 6344 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6345 | return 1; |
| 6346 | } |
| 6347 | |
| 6348 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6349 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6350 | { |
| 6351 | struct stream *s; |
| 6352 | struct http_msg *msg; |
| 6353 | const char *name; |
| 6354 | size_t name_len; |
| 6355 | |
| 6356 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6357 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6358 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6359 | |
| 6360 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6361 | WILL_LJMP(lua_error(L)); |
| 6362 | |
| 6363 | /* Check length. */ |
| 6364 | if (name_len > trash.size - 1) { |
| 6365 | lua_pushboolean(L, 0); |
| 6366 | return 1; |
| 6367 | } |
| 6368 | |
| 6369 | /* Add the mark question as prefix. */ |
| 6370 | chunk_reset(&trash); |
| 6371 | trash.area[trash.data++] = '?'; |
| 6372 | memcpy(trash.area + trash.data, name, name_len); |
| 6373 | trash.data += name_len; |
| 6374 | |
| 6375 | s = chn_strm(msg->chn); |
| 6376 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6377 | return 1; |
| 6378 | } |
| 6379 | |
| 6380 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6381 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6382 | { |
| 6383 | struct stream *s; |
| 6384 | struct http_msg *msg; |
| 6385 | const char *name; |
| 6386 | size_t name_len; |
| 6387 | |
| 6388 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6389 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6390 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6391 | |
| 6392 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6393 | WILL_LJMP(lua_error(L)); |
| 6394 | |
| 6395 | s = chn_strm(msg->chn); |
| 6396 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6397 | return 1; |
| 6398 | } |
| 6399 | |
| 6400 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6401 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6402 | { |
| 6403 | struct http_msg *msg; |
| 6404 | unsigned int code; |
| 6405 | const char *reason; |
| 6406 | size_t reason_len; |
| 6407 | |
| 6408 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6409 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6410 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6411 | |
| 6412 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6413 | WILL_LJMP(lua_error(L)); |
| 6414 | |
| 6415 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6416 | return 1; |
| 6417 | } |
| 6418 | |
| 6419 | /* Returns true if the HTTP message is full. */ |
| 6420 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6421 | { |
| 6422 | struct http_msg *msg; |
| 6423 | |
| 6424 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6425 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6426 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6427 | return 1; |
| 6428 | } |
| 6429 | |
| 6430 | /* Returns true if the HTTP message may still receive data. */ |
| 6431 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6432 | { |
| 6433 | struct http_msg *msg; |
| 6434 | struct htx *htx; |
| 6435 | |
| 6436 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6437 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6438 | htx = htxbuf(&msg->chn->buf); |
| 6439 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6440 | return 1; |
| 6441 | } |
| 6442 | |
| 6443 | /* Returns true if the HTTP message EOM was received */ |
| 6444 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6445 | { |
| 6446 | struct http_msg *msg; |
| 6447 | struct htx *htx; |
| 6448 | |
| 6449 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6450 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6451 | htx = htxbuf(&msg->chn->buf); |
| 6452 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6453 | return 1; |
| 6454 | } |
| 6455 | |
| 6456 | /* Returns the number of bytes available in the input side of the HTTP |
| 6457 | * message. This function never fails. |
| 6458 | */ |
| 6459 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6460 | { |
| 6461 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6462 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6463 | |
| 6464 | MAY_LJMP(check_args(L, 1, "input")); |
| 6465 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6466 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6467 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6468 | return 1; |
| 6469 | } |
| 6470 | |
| 6471 | /* Returns the number of bytes available in the output side of the HTTP |
| 6472 | * message. This function never fails. |
| 6473 | */ |
| 6474 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6475 | { |
| 6476 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6477 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6478 | |
| 6479 | MAY_LJMP(check_args(L, 1, "output")); |
| 6480 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6481 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6482 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6483 | return 1; |
| 6484 | } |
| 6485 | |
| 6486 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6487 | * 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] | 6488 | * 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] | 6489 | * block. This function is called during the payload filtering, so the headers |
| 6490 | * are already scheduled for output (from the filter point of view). |
| 6491 | */ |
| 6492 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6493 | { |
| 6494 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6495 | struct htx_blk *blk; |
| 6496 | struct htx_ret htxret; |
| 6497 | luaL_Buffer b; |
| 6498 | int ret = 0; |
| 6499 | |
| 6500 | luaL_buffinit(L, &b); |
| 6501 | htxret = htx_find_offset(htx, offset); |
| 6502 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6503 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6504 | struct ist v; |
| 6505 | |
| 6506 | switch (type) { |
| 6507 | case HTX_BLK_UNUSED: |
| 6508 | break; |
| 6509 | |
| 6510 | case HTX_BLK_DATA: |
| 6511 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6512 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6513 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6514 | |
| 6515 | luaL_addlstring(&b, v.ptr, v.len); |
| 6516 | ret += v.len; |
| 6517 | break; |
| 6518 | |
| 6519 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6520 | if (!ret) |
| 6521 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6522 | goto end; |
| 6523 | } |
| 6524 | offset = 0; |
| 6525 | } |
| 6526 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6527 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6528 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6529 | goto no_data; |
| 6530 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6531 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6532 | |
| 6533 | no_data: |
| 6534 | /* Remove the empty string and push nil on the stack */ |
| 6535 | lua_pop(L, 1); |
| 6536 | lua_pushnil(L); |
| 6537 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6538 | } |
| 6539 | |
| 6540 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6541 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6542 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6543 | * the filter context. |
| 6544 | */ |
| 6545 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6546 | { |
| 6547 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6548 | struct htx_ret htxret; |
| 6549 | int /*max, */ret = 0; |
| 6550 | |
| 6551 | /* Nothing to do, just return */ |
| 6552 | if (unlikely(istlen(str) == 0)) |
| 6553 | goto end; |
| 6554 | |
| 6555 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6556 | ret = -1; |
| 6557 | goto end; |
| 6558 | } |
| 6559 | |
| 6560 | htxret = htx_find_offset(htx, offset); |
| 6561 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6562 | if (!htx_add_last_data(htx, str)) |
| 6563 | goto end; |
| 6564 | } |
| 6565 | else { |
| 6566 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6567 | v.ptr += htxret.ret; |
| 6568 | v.len = 0; |
| 6569 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6570 | goto end; |
| 6571 | } |
| 6572 | ret = str.len; |
| 6573 | if (ret) { |
| 6574 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6575 | flt_update_offsets(filter, msg->chn, ret); |
| 6576 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6577 | } |
| 6578 | |
| 6579 | end: |
| 6580 | htx_to_buf(htx, &msg->chn->buf); |
| 6581 | return ret; |
| 6582 | } |
| 6583 | |
| 6584 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6585 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6586 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6587 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6588 | * update the filter context. |
| 6589 | */ |
| 6590 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6591 | { |
| 6592 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6593 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6594 | struct htx_blk *blk; |
| 6595 | struct htx_ret htxret; |
| 6596 | size_t ret = 0; |
| 6597 | |
| 6598 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6599 | 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] | 6600 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6601 | * of special blocks like HTX_BLK_EOT. |
| 6602 | * We simply truncate after offset |
| 6603 | * (truncate targeted blk and discard the following ones) |
| 6604 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6605 | htx_truncate(htx, offset); |
| 6606 | ret = len; |
| 6607 | goto end; |
| 6608 | } |
| 6609 | |
| 6610 | htxret = htx_find_offset(htx, offset); |
| 6611 | blk = htxret.blk; |
| 6612 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6613 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6614 | struct ist v; |
| 6615 | |
| 6616 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6617 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6618 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6619 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6620 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6621 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6622 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6623 | /* trimming data in blk: discard everything after the offset |
| 6624 | * (replace 'v' with 'IST_NULL') |
| 6625 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6626 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6627 | if (blk && v.len < len) { |
| 6628 | /* In this case, caller wants to keep removing data, |
| 6629 | * but we need to spare current blk |
| 6630 | * because it was already trimmed |
| 6631 | */ |
| 6632 | blk = htx_get_next_blk(htx, blk); |
| 6633 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6634 | len -= v.len; |
| 6635 | ret += v.len; |
| 6636 | } |
| 6637 | |
| 6638 | |
| 6639 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6640 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6641 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6642 | uint32_t sz = htx_get_blksz(blk); |
| 6643 | |
| 6644 | switch (type) { |
| 6645 | case HTX_BLK_UNUSED: |
| 6646 | break; |
| 6647 | |
| 6648 | case HTX_BLK_DATA: |
| 6649 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6650 | /* don't discard whole blk, only part of it |
| 6651 | * (from the beginning) |
| 6652 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6653 | htx_cut_data_blk(htx, blk, len); |
| 6654 | ret += len; |
| 6655 | goto end; |
| 6656 | } |
| 6657 | break; |
| 6658 | |
| 6659 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6660 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6661 | goto end; |
| 6662 | } |
| 6663 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6664 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6665 | len -= sz; |
| 6666 | ret += sz; |
| 6667 | blk = htx_remove_blk(htx, blk); |
| 6668 | } |
| 6669 | |
| 6670 | end: |
| 6671 | flt_update_offsets(filter, msg->chn, -ret); |
| 6672 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6673 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6674 | * to loose the EOM flag if the message is empty. |
| 6675 | */ |
| 6676 | } |
| 6677 | |
| 6678 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6679 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6680 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6681 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6682 | * the stack. |
| 6683 | */ |
| 6684 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6685 | { |
| 6686 | struct http_msg *msg; |
| 6687 | struct filter *filter; |
| 6688 | size_t output, input; |
| 6689 | int offset, len; |
| 6690 | |
| 6691 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6692 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6693 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6694 | |
| 6695 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6696 | WILL_LJMP(lua_error(L)); |
| 6697 | |
| 6698 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6699 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6700 | WILL_LJMP(lua_error(L)); |
| 6701 | |
| 6702 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6703 | lua_pushnil(L); |
| 6704 | return 1; |
| 6705 | } |
| 6706 | |
| 6707 | offset = output; |
| 6708 | if (lua_gettop(L) > 1) { |
| 6709 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6710 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6711 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6712 | offset += output; |
| 6713 | if (offset < output || offset > input + output) { |
| 6714 | lua_pushfstring(L, "offset out of range."); |
| 6715 | WILL_LJMP(lua_error(L)); |
| 6716 | } |
| 6717 | } |
| 6718 | len = output + input - offset; |
| 6719 | if (lua_gettop(L) == 3) { |
| 6720 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6721 | if (!len) |
| 6722 | goto dup; |
| 6723 | if (len == -1) |
| 6724 | len = global.tune.bufsize; |
| 6725 | if (len < 0) { |
| 6726 | lua_pushfstring(L, "length out of range."); |
| 6727 | WILL_LJMP(lua_error(L)); |
| 6728 | } |
| 6729 | } |
| 6730 | |
| 6731 | dup: |
| 6732 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6733 | return 1; |
| 6734 | } |
| 6735 | |
| 6736 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6737 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6738 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6739 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6740 | * yield. An exception is returned if it is called from another callback. |
| 6741 | */ |
| 6742 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6743 | { |
| 6744 | struct http_msg *msg; |
| 6745 | struct filter *filter; |
| 6746 | const char *str; |
| 6747 | size_t offset, len, sz; |
| 6748 | int ret; |
| 6749 | |
| 6750 | MAY_LJMP(check_args(L, 2, "append")); |
| 6751 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6752 | |
| 6753 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6754 | WILL_LJMP(lua_error(L)); |
| 6755 | |
| 6756 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6757 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6758 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6759 | WILL_LJMP(lua_error(L)); |
| 6760 | |
| 6761 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6762 | lua_pushinteger(L, ret); |
| 6763 | return 1; |
| 6764 | } |
| 6765 | |
| 6766 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6767 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6768 | * channel function used to prepend data, this one can only be called inside a |
| 6769 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6770 | * returned if it is called from another callback. |
| 6771 | */ |
| 6772 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6773 | { |
| 6774 | struct http_msg *msg; |
| 6775 | struct filter *filter; |
| 6776 | const char *str; |
| 6777 | size_t offset, len, sz; |
| 6778 | int ret; |
| 6779 | |
| 6780 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6781 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6782 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6783 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6784 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6785 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6786 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6787 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6788 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6789 | WILL_LJMP(lua_error(L)); |
| 6790 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6791 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6792 | lua_pushinteger(L, ret); |
| 6793 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6794 | } |
| 6795 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6796 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6797 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6798 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6799 | * this one can only be called inside a filter, from http_payload callback. So |
| 6800 | * it cannot yield. An exception is returned if it is called from another |
| 6801 | * callback. |
| 6802 | */ |
| 6803 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
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 | struct http_msg *msg; |
| 6806 | struct filter *filter; |
| 6807 | const char *str; |
| 6808 | size_t input, output, sz; |
| 6809 | int offset; |
| 6810 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6811 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6812 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6813 | 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] | 6814 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6815 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6816 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6817 | WILL_LJMP(lua_error(L)); |
| 6818 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6819 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6820 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6821 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6822 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6823 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6824 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6825 | if (lua_gettop(L) > 2) { |
| 6826 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6827 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6828 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6829 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6830 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6831 | lua_pushfstring(L, "offset out of range."); |
| 6832 | WILL_LJMP(lua_error(L)); |
| 6833 | } |
| 6834 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6835 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6836 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6837 | lua_pushinteger(L, ret); |
| 6838 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6839 | } |
| 6840 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6841 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6842 | * default all DATA blocks are removed. It returns the amount of data |
| 6843 | * removed. Unlike the channel function used to remove data, this one can only |
| 6844 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6845 | * exception is returned if it is called from another callback. |
| 6846 | */ |
| 6847 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6848 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6849 | struct http_msg *msg; |
| 6850 | struct filter *filter; |
| 6851 | size_t input, output; |
| 6852 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6853 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6854 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6855 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6856 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6857 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6858 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6859 | WILL_LJMP(lua_error(L)); |
| 6860 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6861 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6862 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6863 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6864 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6865 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6866 | if (lua_gettop(L) > 1) { |
| 6867 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6868 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6869 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6870 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6871 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6872 | lua_pushfstring(L, "offset out of range."); |
| 6873 | WILL_LJMP(lua_error(L)); |
| 6874 | } |
| 6875 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6876 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6877 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6878 | if (lua_gettop(L) == 3) { |
| 6879 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6880 | if (!len) |
| 6881 | goto end; |
| 6882 | if (len == -1) |
| 6883 | len = output + input - offset; |
| 6884 | if (len < 0 || offset + len > output + input) { |
| 6885 | lua_pushfstring(L, "length out of range."); |
| 6886 | WILL_LJMP(lua_error(L)); |
| 6887 | } |
| 6888 | } |
| 6889 | |
| 6890 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6891 | |
| 6892 | end: |
| 6893 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6894 | return 1; |
| 6895 | } |
| 6896 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6897 | /* 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] | 6898 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6899 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6900 | * function used to replace data, this one can only be called inside a filter, |
| 6901 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6902 | * it is called from another callback. |
| 6903 | */ |
| 6904 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6905 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6906 | struct http_msg *msg; |
| 6907 | struct filter *filter; |
| 6908 | struct htx *htx; |
| 6909 | const char *str; |
| 6910 | size_t input, output, sz; |
| 6911 | int offset, len; |
| 6912 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6913 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6914 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6915 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6916 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6917 | |
| 6918 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6919 | WILL_LJMP(lua_error(L)); |
| 6920 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6921 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6922 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6923 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6924 | WILL_LJMP(lua_error(L)); |
| 6925 | |
| 6926 | offset = output; |
| 6927 | if (lua_gettop(L) > 2) { |
| 6928 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6929 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6930 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6931 | offset += output; |
| 6932 | if (offset < output || offset > input + output) { |
| 6933 | lua_pushfstring(L, "offset out of range."); |
| 6934 | WILL_LJMP(lua_error(L)); |
| 6935 | } |
| 6936 | } |
| 6937 | |
| 6938 | len = output + input - offset; |
| 6939 | if (lua_gettop(L) == 4) { |
| 6940 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6941 | if (!len) |
| 6942 | goto set; |
| 6943 | if (len == -1) |
| 6944 | len = output + input - offset; |
| 6945 | if (len < 0 || offset + len > output + input) { |
| 6946 | lua_pushfstring(L, "length out of range."); |
| 6947 | WILL_LJMP(lua_error(L)); |
| 6948 | } |
| 6949 | } |
| 6950 | |
| 6951 | set: |
| 6952 | /* Be sure we can copied the string once input data will be removed. */ |
| 6953 | htx = htx_from_buf(&msg->chn->buf); |
| 6954 | if (sz > htx_free_data_space(htx) + len) |
| 6955 | lua_pushinteger(L, -1); |
| 6956 | else { |
| 6957 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6958 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6959 | lua_pushinteger(L, ret); |
| 6960 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6961 | return 1; |
| 6962 | } |
| 6963 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6964 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6965 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6966 | * the channel function used to send data, this one can only be called inside a |
| 6967 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6968 | * returned if it is called from another callback. |
| 6969 | */ |
| 6970 | __LJMP static int hlua_http_msg_send(lua_State *L) |
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 | struct http_msg *msg; |
| 6973 | struct filter *filter; |
| 6974 | struct htx *htx; |
| 6975 | const char *str; |
| 6976 | size_t offset, len, sz; |
| 6977 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6978 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6979 | MAY_LJMP(check_args(L, 2, "send")); |
| 6980 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6981 | |
| 6982 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6983 | WILL_LJMP(lua_error(L)); |
| 6984 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6985 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6986 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6987 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6988 | WILL_LJMP(lua_error(L)); |
| 6989 | |
| 6990 | /* Return an error if the channel's output is closed */ |
| 6991 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6992 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6993 | return 1; |
| 6994 | } |
| 6995 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6996 | htx = htx_from_buf(&msg->chn->buf); |
| 6997 | if (sz > htx_free_data_space(htx)) { |
| 6998 | lua_pushinteger(L, -1); |
| 6999 | return 1; |
| 7000 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7001 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7002 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 7003 | if (ret > 0) { |
| 7004 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7005 | |
| 7006 | FLT_OFF(filter, msg->chn) += ret; |
| 7007 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7008 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7009 | } |
| 7010 | |
| 7011 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7012 | return 1; |
| 7013 | } |
| 7014 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7015 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 7016 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 7017 | * channel function used to forward data, this one can only be called inside a |
| 7018 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 7019 | * returned if it is called from another callback. All other functions deal with |
| 7020 | * DATA block, this one not. |
| 7021 | */ |
| 7022 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7023 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7024 | struct http_msg *msg; |
| 7025 | struct filter *filter; |
| 7026 | size_t offset, len; |
| 7027 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7028 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7029 | MAY_LJMP(check_args(L, 2, "forward")); |
| 7030 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7031 | |
| 7032 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7033 | WILL_LJMP(lua_error(L)); |
| 7034 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7035 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7036 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 7037 | if (!filter || !hlua_filter_from_payload(filter)) |
| 7038 | WILL_LJMP(lua_error(L)); |
| 7039 | |
| 7040 | /* Nothing to do, just return */ |
| 7041 | if (!fwd) |
| 7042 | goto end; |
| 7043 | |
| 7044 | /* Return an error if the channel's output is closed */ |
| 7045 | if (unlikely(channel_output_closed(msg->chn))) { |
| 7046 | ret = -1; |
| 7047 | goto end; |
| 7048 | } |
| 7049 | |
| 7050 | ret = fwd; |
| 7051 | if (ret > len) |
| 7052 | ret = len; |
| 7053 | |
| 7054 | if (ret) { |
| 7055 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7056 | |
| 7057 | FLT_OFF(filter, msg->chn) += ret; |
| 7058 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7059 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7060 | } |
| 7061 | |
| 7062 | end: |
| 7063 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7064 | return 1; |
| 7065 | } |
| 7066 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7067 | /* Set EOM flag on the HTX message. |
| 7068 | * |
| 7069 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7070 | * really know how to do without this feature. |
| 7071 | */ |
| 7072 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7073 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7074 | struct http_msg *msg; |
| 7075 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7076 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7077 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7078 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7079 | htx = htxbuf(&msg->chn->buf); |
| 7080 | htx->flags |= HTX_FL_EOM; |
| 7081 | return 0; |
| 7082 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7083 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7084 | /* Unset EOM flag on the HTX message. |
| 7085 | * |
| 7086 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7087 | * really know how to do without this feature. |
| 7088 | */ |
| 7089 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7090 | { |
| 7091 | struct http_msg *msg; |
| 7092 | struct htx *htx; |
| 7093 | |
| 7094 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7095 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7096 | htx = htxbuf(&msg->chn->buf); |
| 7097 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7098 | return 0; |
| 7099 | } |
| 7100 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7101 | /* |
| 7102 | * |
| 7103 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7104 | * Class HTTPClient |
| 7105 | * |
| 7106 | * |
| 7107 | */ |
| 7108 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7109 | { |
| 7110 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7111 | } |
| 7112 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7113 | |
| 7114 | /* stops the httpclient and ask it to kill itself */ |
| 7115 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7116 | { |
| 7117 | struct hlua_httpclient *hlua_hc; |
| 7118 | |
| 7119 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7120 | |
| 7121 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7122 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7123 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7124 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7125 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7126 | hlua_hc->hc = NULL; |
| 7127 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7128 | |
| 7129 | return 0; |
| 7130 | } |
| 7131 | |
| 7132 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7133 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7134 | { |
| 7135 | struct hlua_httpclient *hlua_hc; |
| 7136 | struct hlua *hlua; |
| 7137 | |
| 7138 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7139 | hlua = hlua_gethlua(L); |
| 7140 | if (!hlua) |
| 7141 | return 0; |
| 7142 | |
| 7143 | /* Check stack size. */ |
| 7144 | if (!lua_checkstack(L, 3)) { |
| 7145 | hlua_pusherror(L, "httpclient: full stack"); |
| 7146 | goto err; |
| 7147 | } |
| 7148 | /* Create the object: obj[0] = userdata. */ |
| 7149 | lua_newtable(L); |
| 7150 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7151 | lua_rawseti(L, -2, 0); |
| 7152 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7153 | |
| 7154 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7155 | if (!hlua_hc->hc) |
| 7156 | goto err; |
| 7157 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7158 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7159 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7160 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7161 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7162 | lua_setmetatable(L, -2); |
| 7163 | |
| 7164 | return 1; |
| 7165 | |
| 7166 | err: |
| 7167 | WILL_LJMP(lua_error(L)); |
| 7168 | return 0; |
| 7169 | } |
| 7170 | |
| 7171 | |
| 7172 | /* |
| 7173 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7174 | * httpclient receives some data |
| 7175 | * |
| 7176 | */ |
| 7177 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7178 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7179 | { |
| 7180 | struct hlua *hlua = hc->caller; |
| 7181 | |
| 7182 | if (!hlua || !hlua->task) |
| 7183 | return; |
| 7184 | |
| 7185 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7186 | } |
| 7187 | |
| 7188 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7189 | * Fill the lua stack with headers from the httpclient response |
| 7190 | * This works the same way as the hlua_http_get_headers() function |
| 7191 | */ |
| 7192 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7193 | { |
| 7194 | struct http_hdr *hdr; |
| 7195 | |
| 7196 | lua_newtable(L); |
| 7197 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7198 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7199 | struct ist n, v; |
| 7200 | int len; |
| 7201 | |
| 7202 | n = hdr->n; |
| 7203 | v = hdr->v; |
| 7204 | |
| 7205 | /* Check for existing entry: |
| 7206 | * assume that the table is on the top of the stack, and |
| 7207 | * push the key in the stack, the function lua_gettable() |
| 7208 | * perform the lookup. |
| 7209 | */ |
| 7210 | |
| 7211 | lua_pushlstring(L, n.ptr, n.len); |
| 7212 | lua_gettable(L, -2); |
| 7213 | |
| 7214 | switch (lua_type(L, -1)) { |
| 7215 | case LUA_TNIL: |
| 7216 | /* Table not found, create it. */ |
| 7217 | lua_pop(L, 1); /* remove the nil value. */ |
| 7218 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7219 | lua_newtable(L); /* create and push empty table. */ |
| 7220 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7221 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7222 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7223 | break; |
| 7224 | |
| 7225 | case LUA_TTABLE: |
| 7226 | /* Entry found: push the value in the table. */ |
| 7227 | len = lua_rawlen(L, -1); |
| 7228 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7229 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7230 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7231 | break; |
| 7232 | |
| 7233 | default: |
| 7234 | /* Other cases are errors. */ |
| 7235 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7236 | WILL_LJMP(lua_error(L)); |
| 7237 | } |
| 7238 | } |
| 7239 | return 1; |
| 7240 | } |
| 7241 | |
| 7242 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7243 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7244 | * |
| 7245 | * Caller must free the result |
| 7246 | */ |
| 7247 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7248 | { |
| 7249 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7250 | struct http_hdr *result = NULL; |
| 7251 | uint32_t hdr_num = 0; |
| 7252 | |
| 7253 | lua_pushnil(L); |
| 7254 | while (lua_next(L, -2) != 0) { |
| 7255 | struct ist name, value; |
| 7256 | const char *n, *v; |
| 7257 | size_t nlen, vlen; |
| 7258 | |
| 7259 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7260 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7261 | goto next_hdr; |
| 7262 | } |
| 7263 | |
| 7264 | n = lua_tolstring(L, -2, &nlen); |
| 7265 | name = ist2(n, nlen); |
| 7266 | |
| 7267 | /* Loop on header's values */ |
| 7268 | lua_pushnil(L); |
| 7269 | while (lua_next(L, -2)) { |
| 7270 | if (!lua_isstring(L, -1)) { |
| 7271 | /* Skip the value if it is not a string */ |
| 7272 | goto next_value; |
| 7273 | } |
| 7274 | |
| 7275 | v = lua_tolstring(L, -1, &vlen); |
| 7276 | value = ist2(v, vlen); |
| 7277 | name = ist2(n, nlen); |
| 7278 | |
| 7279 | hdrs[hdr_num].n = istdup(name); |
| 7280 | hdrs[hdr_num].v = istdup(value); |
| 7281 | |
| 7282 | hdr_num++; |
| 7283 | |
| 7284 | next_value: |
| 7285 | lua_pop(L, 1); |
| 7286 | } |
| 7287 | |
| 7288 | next_hdr: |
| 7289 | lua_pop(L, 1); |
| 7290 | |
| 7291 | } |
| 7292 | |
| 7293 | if (hdr_num) { |
| 7294 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7295 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7296 | if (!result) |
| 7297 | goto skip_headers; |
| 7298 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7299 | |
| 7300 | result[hdr_num].n = IST_NULL; |
| 7301 | result[hdr_num].v = IST_NULL; |
| 7302 | } |
| 7303 | |
| 7304 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7305 | |
| 7306 | return result; |
| 7307 | } |
| 7308 | |
| 7309 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7310 | /* |
| 7311 | * For each yield, checks if there is some data in the httpclient and push them |
| 7312 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7313 | * the stack |
| 7314 | */ |
| 7315 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7316 | { |
| 7317 | struct buffer *tr; |
| 7318 | int res; |
| 7319 | struct hlua *hlua = hlua_gethlua(L); |
| 7320 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7321 | |
| 7322 | |
| 7323 | tr = get_trash_chunk(); |
| 7324 | |
| 7325 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7326 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7327 | |
| 7328 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7329 | |
| 7330 | luaL_pushresult(&hlua_hc->b); |
| 7331 | lua_settable(L, -3); |
| 7332 | |
| 7333 | lua_pushstring(L, "status"); |
| 7334 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7335 | lua_settable(L, -3); |
| 7336 | |
| 7337 | |
| 7338 | lua_pushstring(L, "reason"); |
| 7339 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7340 | lua_settable(L, -3); |
| 7341 | |
| 7342 | lua_pushstring(L, "headers"); |
| 7343 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7344 | lua_settable(L, -3); |
| 7345 | |
| 7346 | return 1; |
| 7347 | } |
| 7348 | |
| 7349 | if (httpclient_data(hlua_hc->hc)) |
| 7350 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7351 | |
| 7352 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7353 | |
| 7354 | return 0; |
| 7355 | } |
| 7356 | |
| 7357 | /* |
| 7358 | * Call this when trying to stream a body during a request |
| 7359 | */ |
| 7360 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7361 | { |
| 7362 | struct hlua *hlua; |
| 7363 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7364 | const char *body_str = NULL; |
| 7365 | int ret; |
| 7366 | int end = 0; |
| 7367 | size_t buf_len; |
| 7368 | size_t to_send = 0; |
| 7369 | |
| 7370 | hlua = hlua_gethlua(L); |
| 7371 | |
| 7372 | if (!hlua || !hlua->task) |
| 7373 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7374 | "'frontend', 'backend' or 'task'")); |
| 7375 | |
| 7376 | ret = lua_getfield(L, -1, "body"); |
| 7377 | if (ret != LUA_TSTRING) |
| 7378 | goto rcv; |
| 7379 | |
| 7380 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7381 | lua_pop(L, 1); |
| 7382 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7383 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7384 | |
| 7385 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7386 | end = 1; |
| 7387 | |
| 7388 | /* the end flag is always set since we are using the whole remaining size */ |
| 7389 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7390 | |
| 7391 | if (buf_len > hlua_hc->sent) { |
| 7392 | /* still need to process the buffer */ |
| 7393 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7394 | } else { |
| 7395 | goto rcv; |
| 7396 | /* we sent the whole request buffer we can recv */ |
| 7397 | } |
| 7398 | return 0; |
| 7399 | |
| 7400 | rcv: |
| 7401 | |
| 7402 | /* we return a "res" object */ |
| 7403 | lua_newtable(L); |
| 7404 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7405 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7406 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7407 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7408 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7409 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7410 | |
| 7411 | return 1; |
| 7412 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7413 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7414 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7415 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7416 | */ |
| 7417 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7418 | __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] | 7419 | { |
| 7420 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7421 | struct http_hdr *hdrs = NULL; |
| 7422 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7423 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7424 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7425 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7426 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7427 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7428 | |
| 7429 | hlua = hlua_gethlua(L); |
| 7430 | |
| 7431 | if (!hlua || !hlua->task) |
| 7432 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7433 | "'frontend', 'backend' or 'task'")); |
| 7434 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7435 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7436 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7437 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7438 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7439 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7440 | lua_pushnil(L); /* first key */ |
| 7441 | while (lua_next(L, 2)) { |
| 7442 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7443 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7444 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7445 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7446 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7447 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7448 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7449 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7450 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7451 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7452 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7453 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7454 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7455 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7456 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7457 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7458 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7459 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7460 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7461 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7462 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7463 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7464 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7465 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7466 | } else { |
| 7467 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7468 | } |
| 7469 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7470 | lua_pop(L, 1); |
| 7471 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7472 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7473 | if (!url_str) { |
| 7474 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7475 | return 0; |
| 7476 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7477 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7478 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7479 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7480 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7481 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7482 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7483 | |
| 7484 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7485 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7486 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7487 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7488 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7489 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7490 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7491 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7492 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7493 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7494 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7495 | 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] | 7496 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7497 | /* free the temporary headers array */ |
| 7498 | hdrs_i = hdrs; |
| 7499 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7500 | istfree(&hdrs_i->n); |
| 7501 | istfree(&hdrs_i->v); |
| 7502 | hdrs_i++; |
| 7503 | } |
| 7504 | ha_free(&hdrs); |
| 7505 | |
| 7506 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7507 | if (ret != ERR_NONE) { |
| 7508 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7509 | return 0; |
| 7510 | } |
| 7511 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7512 | if (!httpclient_start(hlua_hc->hc)) |
| 7513 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7514 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7515 | 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] | 7516 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7517 | return 0; |
| 7518 | } |
| 7519 | |
| 7520 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7521 | * Sends an HTTP HEAD request and wait for a response |
| 7522 | * |
| 7523 | * httpclient:head(url, headers, payload) |
| 7524 | */ |
| 7525 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7526 | { |
| 7527 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7528 | } |
| 7529 | |
| 7530 | /* |
| 7531 | * Send an HTTP GET request and wait for a response |
| 7532 | * |
| 7533 | * httpclient:get(url, headers, payload) |
| 7534 | */ |
| 7535 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7536 | { |
| 7537 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7538 | |
| 7539 | } |
| 7540 | |
| 7541 | /* |
| 7542 | * Sends an HTTP PUT request and wait for a response |
| 7543 | * |
| 7544 | * httpclient:put(url, headers, payload) |
| 7545 | */ |
| 7546 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7547 | { |
| 7548 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7549 | } |
| 7550 | |
| 7551 | /* |
| 7552 | * Send an HTTP POST request and wait for a response |
| 7553 | * |
| 7554 | * httpclient:post(url, headers, payload) |
| 7555 | */ |
| 7556 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7557 | { |
| 7558 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7559 | } |
| 7560 | |
| 7561 | |
| 7562 | /* |
| 7563 | * Sends an HTTP DELETE request and wait for a response |
| 7564 | * |
| 7565 | * httpclient:delete(url, headers, payload) |
| 7566 | */ |
| 7567 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7568 | { |
| 7569 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7570 | } |
| 7571 | |
| 7572 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7573 | * |
| 7574 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7575 | * Class TXN |
| 7576 | * |
| 7577 | * |
| 7578 | */ |
| 7579 | |
| 7580 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7581 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7582 | */ |
| 7583 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7584 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7585 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7586 | } |
| 7587 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7588 | __LJMP static int hlua_set_var(lua_State *L) |
| 7589 | { |
| 7590 | struct hlua_txn *htxn; |
| 7591 | const char *name; |
| 7592 | size_t len; |
| 7593 | struct sample smp; |
| 7594 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7595 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7596 | 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] | 7597 | |
| 7598 | /* It is useles to retrieve the stream, but this function |
| 7599 | * runs only in a stream context. |
| 7600 | */ |
| 7601 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7602 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7603 | |
| 7604 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7605 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7606 | hlua_lua2smp(L, 3, &smp); |
| 7607 | |
| 7608 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7609 | 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] | 7610 | |
| 7611 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7612 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7613 | else |
| 7614 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7615 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7616 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7617 | } |
| 7618 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7619 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7620 | { |
| 7621 | struct hlua_txn *htxn; |
| 7622 | const char *name; |
| 7623 | size_t len; |
| 7624 | struct sample smp; |
| 7625 | |
| 7626 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7627 | |
| 7628 | /* It is useles to retrieve the stream, but this function |
| 7629 | * runs only in a stream context. |
| 7630 | */ |
| 7631 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7632 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7633 | |
| 7634 | /* Unset the variable. */ |
| 7635 | 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] | 7636 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7637 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7638 | } |
| 7639 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7640 | __LJMP static int hlua_get_var(lua_State *L) |
| 7641 | { |
| 7642 | struct hlua_txn *htxn; |
| 7643 | const char *name; |
| 7644 | size_t len; |
| 7645 | struct sample smp; |
| 7646 | |
| 7647 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7648 | |
| 7649 | /* It is useles to retrieve the stream, but this function |
| 7650 | * runs only in a stream context. |
| 7651 | */ |
| 7652 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7653 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7654 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7655 | 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] | 7656 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7657 | lua_pushnil(L); |
| 7658 | return 1; |
| 7659 | } |
| 7660 | |
| 7661 | return hlua_smp2lua(L, &smp); |
| 7662 | } |
| 7663 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7664 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7665 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7666 | struct hlua *hlua; |
| 7667 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7668 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7669 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7670 | /* It is useles to retrieve the stream, but this function |
| 7671 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7672 | */ |
| 7673 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7674 | |
| 7675 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7676 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7677 | if (!hlua) |
| 7678 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7679 | |
| 7680 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7681 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7682 | |
| 7683 | /* Get and store new value. */ |
| 7684 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7685 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7686 | |
| 7687 | return 0; |
| 7688 | } |
| 7689 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7690 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7691 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7692 | struct hlua *hlua; |
| 7693 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7694 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7695 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7696 | /* It is useles to retrieve the stream, but this function |
| 7697 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7698 | */ |
| 7699 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7700 | |
| 7701 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7702 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7703 | if (!hlua) { |
| 7704 | lua_pushnil(L); |
| 7705 | return 1; |
| 7706 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7707 | |
| 7708 | /* Push configuration index in the stack. */ |
| 7709 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7710 | |
| 7711 | return 1; |
| 7712 | } |
| 7713 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7714 | /* Create stack entry containing a class TXN. This function |
| 7715 | * return 0 if the stack does not contains free slots, |
| 7716 | * otherwise it returns 1. |
| 7717 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7718 | 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] | 7719 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7720 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7721 | |
| 7722 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7723 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7724 | return 0; |
| 7725 | |
| 7726 | /* NOTE: The allocation never fails. The failure |
| 7727 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7728 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7729 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7730 | /* Create the object: obj[0] = userdata. */ |
| 7731 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7732 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7733 | lua_rawseti(L, -2, 0); |
| 7734 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7735 | htxn->s = s; |
| 7736 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7737 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7738 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7739 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7740 | /* Create the "f" field that contains a list of fetches. */ |
| 7741 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7742 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7743 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7744 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7745 | |
| 7746 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7747 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7748 | 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] | 7749 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7750 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7751 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7752 | /* Create the "c" field that contains a list of converters. */ |
| 7753 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7754 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7755 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7756 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7757 | |
| 7758 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7759 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7760 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7761 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7762 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7763 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7764 | /* Create the "req" field that contains the request channel object. */ |
| 7765 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7766 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7767 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7768 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7769 | |
| 7770 | /* Create the "res" field that contains the response channel object. */ |
| 7771 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7772 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7773 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7774 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7775 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7776 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7777 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7778 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7779 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7780 | return 0; |
| 7781 | } |
| 7782 | else |
| 7783 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7784 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7785 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7786 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7787 | /* HTTPMessage object are created when a lua TXN is created from |
| 7788 | * a filter context only |
| 7789 | */ |
| 7790 | |
| 7791 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7792 | lua_pushstring(L, "http_req"); |
| 7793 | if (p->mode == PR_MODE_HTTP) { |
| 7794 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7795 | return 0; |
| 7796 | } |
| 7797 | else |
| 7798 | lua_pushnil(L); |
| 7799 | lua_rawset(L, -3); |
| 7800 | |
| 7801 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7802 | lua_pushstring(L, "http_res"); |
| 7803 | if (p->mode == PR_MODE_HTTP) { |
| 7804 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7805 | return 0; |
| 7806 | } |
| 7807 | else |
| 7808 | lua_pushnil(L); |
| 7809 | lua_rawset(L, -3); |
| 7810 | } |
| 7811 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7812 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7813 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7814 | lua_setmetatable(L, -2); |
| 7815 | |
| 7816 | return 1; |
| 7817 | } |
| 7818 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7819 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7820 | { |
| 7821 | const char *msg; |
| 7822 | struct hlua_txn *htxn; |
| 7823 | |
| 7824 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7825 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7826 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7827 | |
| 7828 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7829 | return 0; |
| 7830 | } |
| 7831 | |
| 7832 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7833 | { |
| 7834 | int level; |
| 7835 | const char *msg; |
| 7836 | struct hlua_txn *htxn; |
| 7837 | |
| 7838 | MAY_LJMP(check_args(L, 3, "log")); |
| 7839 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7840 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7841 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7842 | |
| 7843 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7844 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7845 | |
| 7846 | hlua_sendlog(htxn->s->be, level, msg); |
| 7847 | return 0; |
| 7848 | } |
| 7849 | |
| 7850 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7851 | { |
| 7852 | const char *msg; |
| 7853 | struct hlua_txn *htxn; |
| 7854 | |
| 7855 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7856 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7857 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7858 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7859 | return 0; |
| 7860 | } |
| 7861 | |
| 7862 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7863 | { |
| 7864 | const char *msg; |
| 7865 | struct hlua_txn *htxn; |
| 7866 | |
| 7867 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7868 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7869 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7870 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7871 | return 0; |
| 7872 | } |
| 7873 | |
| 7874 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7875 | { |
| 7876 | const char *msg; |
| 7877 | struct hlua_txn *htxn; |
| 7878 | |
| 7879 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7880 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7881 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7882 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7883 | return 0; |
| 7884 | } |
| 7885 | |
| 7886 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7887 | { |
| 7888 | const char *msg; |
| 7889 | struct hlua_txn *htxn; |
| 7890 | |
| 7891 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7892 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7893 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7894 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7895 | return 0; |
| 7896 | } |
| 7897 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7898 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7899 | { |
| 7900 | struct hlua_txn *htxn; |
| 7901 | int ll; |
| 7902 | |
| 7903 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7904 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7905 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7906 | |
| 7907 | if (ll < 0 || ll > 7) |
| 7908 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7909 | |
| 7910 | htxn->s->logs.level = ll; |
| 7911 | return 0; |
| 7912 | } |
| 7913 | |
| 7914 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7915 | { |
| 7916 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7917 | int tos; |
| 7918 | |
| 7919 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7920 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7921 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7922 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7923 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7924 | return 0; |
| 7925 | } |
| 7926 | |
| 7927 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7928 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7929 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7930 | int mark; |
| 7931 | |
| 7932 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7933 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7934 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7935 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7936 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7937 | return 0; |
| 7938 | } |
| 7939 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7940 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7941 | { |
| 7942 | struct hlua_txn *htxn; |
| 7943 | |
| 7944 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7945 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7946 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7947 | return 0; |
| 7948 | } |
| 7949 | |
| 7950 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7951 | { |
| 7952 | struct hlua_txn *htxn; |
| 7953 | |
| 7954 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7955 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7956 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7957 | return 0; |
| 7958 | } |
| 7959 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7960 | /* 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] | 7961 | * 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] | 7962 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7963 | * error. The Reply must be on top of the stack. |
| 7964 | */ |
| 7965 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7966 | { |
| 7967 | struct htx *htx; |
| 7968 | struct htx_sl *sl; |
| 7969 | struct h1m h1m; |
| 7970 | const char *status, *reason, *body; |
| 7971 | size_t status_len, reason_len, body_len; |
| 7972 | int ret, code, flags; |
| 7973 | |
| 7974 | code = 200; |
| 7975 | status = "200"; |
| 7976 | status_len = 3; |
| 7977 | ret = lua_getfield(L, -1, "status"); |
| 7978 | if (ret == LUA_TNUMBER) { |
| 7979 | code = lua_tointeger(L, -1); |
| 7980 | status = lua_tolstring(L, -1, &status_len); |
| 7981 | } |
| 7982 | lua_pop(L, 1); |
| 7983 | |
| 7984 | reason = http_get_reason(code); |
| 7985 | reason_len = strlen(reason); |
| 7986 | ret = lua_getfield(L, -1, "reason"); |
| 7987 | if (ret == LUA_TSTRING) |
| 7988 | reason = lua_tolstring(L, -1, &reason_len); |
| 7989 | lua_pop(L, 1); |
| 7990 | |
| 7991 | body = NULL; |
| 7992 | body_len = 0; |
| 7993 | ret = lua_getfield(L, -1, "body"); |
| 7994 | if (ret == LUA_TSTRING) |
| 7995 | body = lua_tolstring(L, -1, &body_len); |
| 7996 | lua_pop(L, 1); |
| 7997 | |
| 7998 | /* Prepare the response before inserting the headers */ |
| 7999 | h1m_init_res(&h1m); |
| 8000 | htx = htx_from_buf(&s->res.buf); |
| 8001 | channel_htx_truncate(&s->res, htx); |
| 8002 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 8003 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 8004 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 8005 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8006 | } |
| 8007 | else { |
| 8008 | flags = HTX_SL_F_IS_RESP; |
| 8009 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 8010 | ist2(status, status_len), ist2(reason, reason_len)); |
| 8011 | } |
| 8012 | if (!sl) |
| 8013 | goto fail; |
| 8014 | sl->info.res.status = code; |
| 8015 | |
| 8016 | /* Push in the stack the "headers" entry. */ |
| 8017 | ret = lua_getfield(L, -1, "headers"); |
| 8018 | if (ret != LUA_TTABLE) |
| 8019 | goto skip_headers; |
| 8020 | |
| 8021 | lua_pushnil(L); |
| 8022 | while (lua_next(L, -2) != 0) { |
| 8023 | struct ist name, value; |
| 8024 | const char *n, *v; |
| 8025 | size_t nlen, vlen; |
| 8026 | |
| 8027 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 8028 | /* Skip element if the key is not a string or if the value is not a table */ |
| 8029 | goto next_hdr; |
| 8030 | } |
| 8031 | |
| 8032 | n = lua_tolstring(L, -2, &nlen); |
| 8033 | name = ist2(n, nlen); |
| 8034 | if (isteqi(name, ist("content-length"))) { |
| 8035 | /* Always skip content-length header. It will be added |
| 8036 | * later with the correct len |
| 8037 | */ |
| 8038 | goto next_hdr; |
| 8039 | } |
| 8040 | |
| 8041 | /* Loop on header's values */ |
| 8042 | lua_pushnil(L); |
| 8043 | while (lua_next(L, -2)) { |
| 8044 | if (!lua_isstring(L, -1)) { |
| 8045 | /* Skip the value if it is not a string */ |
| 8046 | goto next_value; |
| 8047 | } |
| 8048 | |
| 8049 | v = lua_tolstring(L, -1, &vlen); |
| 8050 | value = ist2(v, vlen); |
| 8051 | |
| 8052 | if (isteqi(name, ist("transfer-encoding"))) |
| 8053 | h1_parse_xfer_enc_header(&h1m, value); |
| 8054 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8055 | goto fail; |
| 8056 | |
| 8057 | next_value: |
| 8058 | lua_pop(L, 1); |
| 8059 | } |
| 8060 | |
| 8061 | next_hdr: |
| 8062 | lua_pop(L, 1); |
| 8063 | } |
| 8064 | skip_headers: |
| 8065 | lua_pop(L, 1); |
| 8066 | |
| 8067 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8068 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8069 | const char *clen = ultoa(body_len); |
| 8070 | |
| 8071 | h1m.flags |= H1_MF_CLEN; |
| 8072 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8073 | goto fail; |
| 8074 | } |
| 8075 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8076 | h1m.flags |= H1_MF_XFER_LEN; |
| 8077 | |
| 8078 | /* Update HTX start-line flags */ |
| 8079 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8080 | flags |= HTX_SL_F_XFER_ENC; |
| 8081 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8082 | flags |= HTX_SL_F_XFER_LEN; |
| 8083 | if (h1m.flags & H1_MF_CHNK) |
| 8084 | flags |= HTX_SL_F_CHNK; |
| 8085 | else if (h1m.flags & H1_MF_CLEN) |
| 8086 | flags |= HTX_SL_F_CLEN; |
| 8087 | if (h1m.body_len == 0) |
| 8088 | flags |= HTX_SL_F_BODYLESS; |
| 8089 | } |
| 8090 | sl->flags |= flags; |
| 8091 | |
| 8092 | |
| 8093 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8094 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8095 | goto fail; |
| 8096 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8097 | htx->flags |= HTX_FL_EOM; |
| 8098 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8099 | /* Now, forward the response and terminate the transaction */ |
| 8100 | s->txn->status = code; |
| 8101 | htx_to_buf(htx, &s->res.buf); |
| 8102 | if (!http_forward_proxy_resp(s, 1)) |
| 8103 | goto fail; |
| 8104 | |
| 8105 | return 1; |
| 8106 | |
| 8107 | fail: |
| 8108 | channel_htx_truncate(&s->res, htx); |
| 8109 | return 0; |
| 8110 | } |
| 8111 | |
| 8112 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8113 | * processing is just aborted. Nothing is returned to the client and all |
| 8114 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8115 | * is forwarded to the client before terminating the transaction. On success, |
| 8116 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8117 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8118 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8119 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8120 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8121 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8122 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8123 | struct stream *s; |
| 8124 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8125 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8126 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8127 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8128 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8129 | * just end the execution of the current lua code. */ |
| 8130 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8131 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8132 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8133 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8134 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8135 | struct channel *req = &s->req; |
| 8136 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8137 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8138 | channel_auto_read(req); |
| 8139 | channel_abort(req); |
| 8140 | channel_auto_close(req); |
| 8141 | channel_erase(req); |
| 8142 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8143 | channel_auto_read(res); |
| 8144 | channel_auto_close(res); |
| 8145 | channel_shutr_now(res); |
| 8146 | |
| 8147 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8148 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8149 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8150 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8151 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8152 | /* No reply or invalid reply */ |
| 8153 | s->txn->status = 0; |
| 8154 | http_reply_and_close(s, 0, NULL); |
| 8155 | } |
| 8156 | else { |
| 8157 | /* Remove extra args to have the reply on top of the stack */ |
| 8158 | if (lua_gettop(L) > 2) |
| 8159 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8160 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8161 | if (!hlua_txn_forward_reply(L, s)) { |
| 8162 | if (!(s->flags & SF_ERR_MASK)) |
| 8163 | s->flags |= SF_ERR_PRXCOND; |
| 8164 | lua_pushinteger(L, ACT_RET_ERR); |
| 8165 | WILL_LJMP(hlua_done(L)); |
| 8166 | return 0; /* Never reached */ |
| 8167 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8168 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8169 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8170 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8171 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8172 | /* let's log the request time */ |
| 8173 | s->logs.tv_request = now; |
| 8174 | 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] | 8175 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8176 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8177 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8178 | done: |
| 8179 | if (!(s->flags & SF_ERR_MASK)) |
| 8180 | s->flags |= SF_ERR_LOCAL; |
| 8181 | if (!(s->flags & SF_FINST_MASK)) |
| 8182 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8183 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8184 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8185 | lua_pushinteger(L, -1); |
| 8186 | else |
| 8187 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8188 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8189 | return 0; |
| 8190 | } |
| 8191 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8192 | /* |
| 8193 | * |
| 8194 | * |
| 8195 | * Class REPLY |
| 8196 | * |
| 8197 | * |
| 8198 | */ |
| 8199 | |
| 8200 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8201 | * free slots, the function fails and returns 0; |
| 8202 | */ |
| 8203 | static int hlua_txn_reply_new(lua_State *L) |
| 8204 | { |
| 8205 | struct hlua_txn *htxn; |
| 8206 | const char *reason, *body = NULL; |
| 8207 | int ret, status; |
| 8208 | |
| 8209 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8210 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8211 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8212 | WILL_LJMP(lua_error(L)); |
| 8213 | } |
| 8214 | |
| 8215 | /* Default value */ |
| 8216 | status = 200; |
| 8217 | reason = http_get_reason(status); |
| 8218 | |
| 8219 | if (lua_istable(L, 2)) { |
| 8220 | /* load status and reason from the table argument at index 2 */ |
| 8221 | ret = lua_getfield(L, 2, "status"); |
| 8222 | if (ret == LUA_TNIL) |
| 8223 | goto reason; |
| 8224 | else if (ret != LUA_TNUMBER) { |
| 8225 | /* invalid status: ignore the reason */ |
| 8226 | goto body; |
| 8227 | } |
| 8228 | status = lua_tointeger(L, -1); |
| 8229 | |
| 8230 | reason: |
| 8231 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8232 | ret = lua_getfield(L, 2, "reason"); |
| 8233 | if (ret == LUA_TSTRING) |
| 8234 | reason = lua_tostring(L, -1); |
| 8235 | |
| 8236 | body: |
| 8237 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8238 | ret = lua_getfield(L, 2, "body"); |
| 8239 | if (ret == LUA_TSTRING) |
| 8240 | body = lua_tostring(L, -1); |
| 8241 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8242 | } |
| 8243 | |
| 8244 | /* Create the Reply table */ |
| 8245 | lua_newtable(L); |
| 8246 | |
| 8247 | /* Add status element */ |
| 8248 | lua_pushstring(L, "status"); |
| 8249 | lua_pushinteger(L, status); |
| 8250 | lua_settable(L, -3); |
| 8251 | |
| 8252 | /* Add reason element */ |
| 8253 | reason = http_get_reason(status); |
| 8254 | lua_pushstring(L, "reason"); |
| 8255 | lua_pushstring(L, reason); |
| 8256 | lua_settable(L, -3); |
| 8257 | |
| 8258 | /* Add body element, nil if undefined */ |
| 8259 | lua_pushstring(L, "body"); |
| 8260 | if (body) |
| 8261 | lua_pushstring(L, body); |
| 8262 | else |
| 8263 | lua_pushnil(L); |
| 8264 | lua_settable(L, -3); |
| 8265 | |
| 8266 | /* Add headers element */ |
| 8267 | lua_pushstring(L, "headers"); |
| 8268 | lua_newtable(L); |
| 8269 | |
| 8270 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8271 | if (lua_istable(L, 2)) { |
| 8272 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8273 | ret = lua_getfield(L, 2, "headers"); |
| 8274 | if (ret == LUA_TTABLE) { |
| 8275 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8276 | lua_pushnil(L); |
| 8277 | while (lua_next(L, -2) != 0) { |
| 8278 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8279 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8280 | /* invalid value type, skip it */ |
| 8281 | lua_pop(L, 1); |
| 8282 | continue; |
| 8283 | } |
| 8284 | |
| 8285 | |
| 8286 | /* Duplicate the key and swap it with the value. */ |
| 8287 | lua_pushvalue(L, -2); |
| 8288 | lua_insert(L, -2); |
| 8289 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8290 | |
| 8291 | lua_newtable(L); |
| 8292 | lua_insert(L, -2); |
| 8293 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8294 | |
| 8295 | if (lua_isstring(L, -1)) { |
| 8296 | /* push the value in the inner table */ |
| 8297 | lua_rawseti(L, -2, 1); |
| 8298 | } |
| 8299 | else { /* table */ |
| 8300 | lua_pushnil(L); |
| 8301 | while (lua_next(L, -2) != 0) { |
| 8302 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8303 | if (!lua_isstring(L, -1)) { |
| 8304 | /* invalid value type, skip it*/ |
| 8305 | lua_pop(L, 1); |
| 8306 | continue; |
| 8307 | } |
| 8308 | /* push the value in the inner table */ |
| 8309 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8310 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8311 | } |
| 8312 | lua_pop(L, 1); |
| 8313 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8314 | } |
| 8315 | |
| 8316 | /* push (k,v) on the stack in the headers table: |
| 8317 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8318 | */ |
| 8319 | lua_settable(L, -5); |
| 8320 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8321 | } |
| 8322 | } |
| 8323 | lua_pop(L, 1); |
| 8324 | } |
| 8325 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8326 | lua_settable(L, -3); |
| 8327 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8328 | |
| 8329 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8330 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8331 | lua_setmetatable(L, -2); |
| 8332 | return 1; |
| 8333 | } |
| 8334 | |
| 8335 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8336 | * provided, the default one corresponding to the status code is used. |
| 8337 | */ |
| 8338 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8339 | { |
| 8340 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8341 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8342 | |
| 8343 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8344 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8345 | |
| 8346 | if (status < 100 || status > 599) { |
| 8347 | lua_pushboolean(L, 0); |
| 8348 | return 1; |
| 8349 | } |
| 8350 | if (!reason) |
| 8351 | reason = http_get_reason(status); |
| 8352 | |
| 8353 | lua_pushinteger(L, status); |
| 8354 | lua_setfield(L, 1, "status"); |
| 8355 | |
| 8356 | lua_pushstring(L, reason); |
| 8357 | lua_setfield(L, 1, "reason"); |
| 8358 | |
| 8359 | lua_pushboolean(L, 1); |
| 8360 | return 1; |
| 8361 | } |
| 8362 | |
| 8363 | /* Add a header into the reply object. Each header name is associated to an |
| 8364 | * array of values in the "headers" table. If the header name is not found, a |
| 8365 | * new entry is created. |
| 8366 | */ |
| 8367 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8368 | { |
| 8369 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8370 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8371 | int ret; |
| 8372 | |
| 8373 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8374 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8375 | |
| 8376 | /* Push in the stack the "headers" entry. */ |
| 8377 | ret = lua_getfield(L, 1, "headers"); |
| 8378 | if (ret != LUA_TTABLE) { |
| 8379 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8380 | WILL_LJMP(lua_error(L)); |
| 8381 | } |
| 8382 | |
| 8383 | /* check if the header is already registered. If not, register it. */ |
| 8384 | ret = lua_getfield(L, -1, name); |
| 8385 | if (ret == LUA_TNIL) { |
| 8386 | /* Entry not found. */ |
| 8387 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8388 | |
| 8389 | /* Insert the new header name in the array in the top of the stack. |
| 8390 | * It left the new array in the top of the stack. |
| 8391 | */ |
| 8392 | lua_newtable(L); |
| 8393 | lua_pushstring(L, name); |
| 8394 | lua_pushvalue(L, -2); |
| 8395 | lua_settable(L, -4); |
| 8396 | } |
| 8397 | else if (ret != LUA_TTABLE) { |
| 8398 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8399 | WILL_LJMP(lua_error(L)); |
| 8400 | } |
| 8401 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8402 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8403 | * the header value as new entry. |
| 8404 | */ |
| 8405 | lua_pushstring(L, value); |
| 8406 | ret = lua_rawlen(L, -2); |
| 8407 | lua_rawseti(L, -2, ret + 1); |
| 8408 | |
| 8409 | lua_pushboolean(L, 1); |
| 8410 | return 1; |
| 8411 | } |
| 8412 | |
| 8413 | /* Remove all occurrences of a given header name. */ |
| 8414 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8415 | { |
| 8416 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8417 | int ret; |
| 8418 | |
| 8419 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8420 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8421 | |
| 8422 | /* Push in the stack the "headers" entry. */ |
| 8423 | ret = lua_getfield(L, 1, "headers"); |
| 8424 | if (ret != LUA_TTABLE) { |
| 8425 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8426 | WILL_LJMP(lua_error(L)); |
| 8427 | } |
| 8428 | |
| 8429 | lua_pushstring(L, name); |
| 8430 | lua_pushnil(L); |
| 8431 | lua_settable(L, -3); |
| 8432 | |
| 8433 | lua_pushboolean(L, 1); |
| 8434 | return 1; |
| 8435 | } |
| 8436 | |
| 8437 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8438 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8439 | { |
| 8440 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8441 | |
| 8442 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8443 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8444 | |
| 8445 | lua_pushstring(L, payload); |
| 8446 | lua_setfield(L, 1, "body"); |
| 8447 | |
| 8448 | lua_pushboolean(L, 1); |
| 8449 | return 1; |
| 8450 | } |
| 8451 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8452 | __LJMP static int hlua_log(lua_State *L) |
| 8453 | { |
| 8454 | int level; |
| 8455 | const char *msg; |
| 8456 | |
| 8457 | MAY_LJMP(check_args(L, 2, "log")); |
| 8458 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8459 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8460 | |
| 8461 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8462 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8463 | |
| 8464 | hlua_sendlog(NULL, level, msg); |
| 8465 | return 0; |
| 8466 | } |
| 8467 | |
| 8468 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8469 | { |
| 8470 | const char *msg; |
| 8471 | |
| 8472 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8473 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8474 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8475 | return 0; |
| 8476 | } |
| 8477 | |
| 8478 | __LJMP static int hlua_log_info(lua_State *L) |
| 8479 | { |
| 8480 | const char *msg; |
| 8481 | |
| 8482 | MAY_LJMP(check_args(L, 1, "info")); |
| 8483 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8484 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8485 | return 0; |
| 8486 | } |
| 8487 | |
| 8488 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8489 | { |
| 8490 | const char *msg; |
| 8491 | |
| 8492 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8493 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8494 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8495 | return 0; |
| 8496 | } |
| 8497 | |
| 8498 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8499 | { |
| 8500 | const char *msg; |
| 8501 | |
| 8502 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8503 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8504 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8505 | return 0; |
| 8506 | } |
| 8507 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8508 | __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] | 8509 | { |
| 8510 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8511 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8512 | 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] | 8513 | return 0; |
| 8514 | } |
| 8515 | |
| 8516 | __LJMP static int hlua_sleep(lua_State *L) |
| 8517 | { |
| 8518 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8519 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8520 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8521 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8522 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8523 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8524 | wakeup_ms = tick_add(now_ms, delay); |
| 8525 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8526 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8527 | 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] | 8528 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8529 | } |
| 8530 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8531 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8532 | { |
| 8533 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8534 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8535 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8536 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8537 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8538 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8539 | wakeup_ms = tick_add(now_ms, delay); |
| 8540 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8541 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8542 | 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] | 8543 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8544 | } |
| 8545 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8546 | /* This functionis an LUA binding. it permits to give back |
| 8547 | * the hand at the HAProxy scheduler. It is used when the |
| 8548 | * LUA processing consumes a lot of time. |
| 8549 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8550 | __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] | 8551 | { |
| 8552 | return 0; |
| 8553 | } |
| 8554 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8555 | __LJMP static int hlua_yield(lua_State *L) |
| 8556 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8557 | 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] | 8558 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8559 | } |
| 8560 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8561 | /* This function change the nice of the currently executed |
| 8562 | * task. It is used set low or high priority at the current |
| 8563 | * task. |
| 8564 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8565 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8566 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8567 | struct hlua *hlua; |
| 8568 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8569 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8570 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8571 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8572 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8573 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8574 | hlua = hlua_gethlua(L); |
| 8575 | |
| 8576 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8577 | if (!hlua || !hlua->task) |
| 8578 | return 0; |
| 8579 | |
| 8580 | if (nice < -1024) |
| 8581 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8582 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8583 | nice = 1024; |
| 8584 | |
| 8585 | hlua->task->nice = nice; |
| 8586 | return 0; |
| 8587 | } |
| 8588 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8589 | /* 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] | 8590 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8591 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8592 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8593 | * |
| 8594 | * Task wrapper are longjmp safe because the only one Lua code |
| 8595 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8596 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8597 | 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] | 8598 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8599 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8600 | enum hlua_exec status; |
| 8601 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8602 | if (task->tid < 0) |
| 8603 | task->tid = tid; |
| 8604 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8605 | /* If it is the first call to the task, we must initialize the |
| 8606 | * execution timeouts. |
| 8607 | */ |
| 8608 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8609 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8610 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8611 | /* Execute the Lua code. */ |
| 8612 | status = hlua_ctx_resume(hlua, 1); |
| 8613 | |
| 8614 | switch (status) { |
| 8615 | /* finished or yield */ |
| 8616 | case HLUA_E_OK: |
| 8617 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8618 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8619 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8620 | break; |
| 8621 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8622 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8623 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8624 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8625 | break; |
| 8626 | |
| 8627 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8628 | case HLUA_E_ETMOUT: |
| 8629 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8630 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8631 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8632 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8633 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8634 | case HLUA_E_ERR: |
| 8635 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8636 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame] | 8637 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8638 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8639 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8640 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8641 | break; |
| 8642 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8643 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8644 | } |
| 8645 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8646 | /* This function is an LUA binding that register LUA function to be |
| 8647 | * executed after the HAProxy configuration parsing and before the |
| 8648 | * HAProxy scheduler starts. This function expect only one LUA |
| 8649 | * argument that is a function. This function returns nothing, but |
| 8650 | * throws if an error is encountered. |
| 8651 | */ |
| 8652 | __LJMP static int hlua_register_init(lua_State *L) |
| 8653 | { |
| 8654 | struct hlua_init_function *init; |
| 8655 | int ref; |
| 8656 | |
| 8657 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8658 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 8659 | if (hlua_gethlua(L)) { |
| 8660 | /* runtime processing */ |
| 8661 | WILL_LJMP(luaL_error(L, "register_init: not available outside of body context")); |
| 8662 | } |
| 8663 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8664 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8665 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8666 | init = calloc(1, sizeof(*init)); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8667 | if (!init) { |
| 8668 | hlua_unref(L, ref); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8669 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8670 | } |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8671 | |
| 8672 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8673 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8674 | return 0; |
| 8675 | } |
| 8676 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8677 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8678 | * executed in parallel of the main HAroxy activity. The task is |
| 8679 | * created and it is set in the HAProxy scheduler. It can be called |
| 8680 | * from the "init" section, "post init" or during the runtime. |
| 8681 | * |
| 8682 | * Lua prototype: |
| 8683 | * |
| 8684 | * <none> core.register_task(<function>) |
| 8685 | */ |
| 8686 | static int hlua_register_task(lua_State *L) |
| 8687 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8688 | struct hlua *hlua = NULL; |
| 8689 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8690 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8691 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8692 | |
| 8693 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8694 | |
| 8695 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8696 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8697 | /* Get the reference state. If the reference is NULL, L is the master |
| 8698 | * state, otherwise hlua->T is. |
| 8699 | */ |
| 8700 | hlua = hlua_gethlua(L); |
| 8701 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8702 | /* we are in runtime processing */ |
| 8703 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8704 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8705 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8706 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8707 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8708 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8709 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8710 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8711 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8712 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8713 | /* We are in the common lua state, execute the task anywhere, |
| 8714 | * otherwise, inherit the current thread identifier |
| 8715 | */ |
| 8716 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8717 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8718 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8719 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8720 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8721 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8722 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8723 | task->context = hlua; |
| 8724 | task->process = hlua_process_task; |
| 8725 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8726 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8727 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8728 | |
| 8729 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8730 | hlua_pushref(hlua->T, ref); |
Aurelien DARRAGON | be58d66 | 2023-03-13 14:09:21 +0100 | [diff] [blame] | 8731 | /* function ref not needed anymore since it was pushed to the substack */ |
| 8732 | hlua_unref(L, ref); |
| 8733 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8734 | hlua->nargs = 0; |
| 8735 | |
| 8736 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8737 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8738 | |
| 8739 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8740 | |
| 8741 | alloc_error: |
| 8742 | task_destroy(task); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 8743 | hlua_unref(L, ref); |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8744 | hlua_ctx_destroy(hlua); |
| 8745 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8746 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8747 | } |
| 8748 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8749 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8750 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8751 | * resume converters. |
| 8752 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8753 | 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] | 8754 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8755 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8756 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8757 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8758 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8759 | if (!stream) |
| 8760 | return 0; |
| 8761 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8762 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8763 | * Lua context can be not initialized. This behavior |
| 8764 | * permits to save performances because a systematic |
| 8765 | * Lua initialization cause 5% performances loss. |
| 8766 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8767 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8768 | struct hlua *hlua; |
| 8769 | |
| 8770 | hlua = pool_alloc(pool_head_hlua); |
| 8771 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8772 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8773 | return 0; |
| 8774 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8775 | HLUA_INIT(hlua); |
| 8776 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8777 | 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] | 8778 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8779 | return 0; |
| 8780 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8781 | } |
| 8782 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8783 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8784 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8785 | |
| 8786 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8787 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8788 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8789 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8790 | else |
| 8791 | error = "critical error"; |
| 8792 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8793 | return 0; |
| 8794 | } |
| 8795 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8796 | /* Check stack available size. */ |
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 | } |
| 8802 | |
| 8803 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8804 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8805 | |
| 8806 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8807 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8808 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8809 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8810 | return 0; |
| 8811 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8812 | hlua_smp2lua(stream->hlua->T, smp); |
| 8813 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8814 | |
| 8815 | /* push keywords in the stack. */ |
| 8816 | if (arg_p) { |
| 8817 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8818 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8819 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8820 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8821 | return 0; |
| 8822 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8823 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8824 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8825 | } |
| 8826 | } |
| 8827 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8828 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8829 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8830 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8831 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8832 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8833 | } |
| 8834 | |
| 8835 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8836 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8837 | /* finished. */ |
| 8838 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8839 | /* If the stack is empty, the function fails. */ |
| 8840 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8841 | return 0; |
| 8842 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8843 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8844 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8845 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8846 | return 1; |
| 8847 | |
| 8848 | /* yield. */ |
| 8849 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8850 | 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] | 8851 | return 0; |
| 8852 | |
| 8853 | /* finished with error. */ |
| 8854 | case HLUA_E_ERRMSG: |
| 8855 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8856 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8857 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8858 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8859 | return 0; |
| 8860 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8861 | case HLUA_E_ETMOUT: |
| 8862 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8863 | return 0; |
| 8864 | |
| 8865 | case HLUA_E_NOMEM: |
| 8866 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8867 | return 0; |
| 8868 | |
| 8869 | case HLUA_E_YIELD: |
| 8870 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8871 | return 0; |
| 8872 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8873 | case HLUA_E_ERR: |
| 8874 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8875 | 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] | 8876 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8877 | |
| 8878 | default: |
| 8879 | return 0; |
| 8880 | } |
| 8881 | } |
| 8882 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8883 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8884 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8885 | * resume sample-fetches. This function will be called by the sample |
| 8886 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8887 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8888 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8889 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8890 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8891 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8892 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8893 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8894 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8895 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8896 | if (!stream) |
| 8897 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8898 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8899 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8900 | * Lua context can be not initialized. This behavior |
| 8901 | * permits to save performances because a systematic |
| 8902 | * Lua initialization cause 5% performances loss. |
| 8903 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8904 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8905 | struct hlua *hlua; |
| 8906 | |
| 8907 | hlua = pool_alloc(pool_head_hlua); |
| 8908 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8909 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8910 | return 0; |
| 8911 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8912 | hlua->T = NULL; |
| 8913 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8914 | 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] | 8915 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8916 | return 0; |
| 8917 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8918 | } |
| 8919 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8920 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8921 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8922 | |
| 8923 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8924 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8925 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8926 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8927 | else |
| 8928 | error = "critical error"; |
| 8929 | 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] | 8930 | return 0; |
| 8931 | } |
| 8932 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8933 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8934 | if (!lua_checkstack(stream->hlua->T, 2)) { |
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 | } |
| 8939 | |
| 8940 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 8941 | hlua_pushref(stream->hlua->T, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8942 | |
| 8943 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8944 | 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] | 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 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8950 | |
| 8951 | /* push keywords in the stack. */ |
| 8952 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8953 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8954 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8955 | 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] | 8956 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8957 | return 0; |
| 8958 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8959 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8960 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8961 | } |
| 8962 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8963 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8964 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8965 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8966 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8967 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8968 | } |
| 8969 | |
| 8970 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8971 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8972 | /* finished. */ |
| 8973 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8974 | /* If the stack is empty, the function fails. */ |
| 8975 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8976 | return 0; |
| 8977 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8978 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8979 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8980 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8981 | |
| 8982 | /* Set the end of execution flag. */ |
| 8983 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8984 | return 1; |
| 8985 | |
| 8986 | /* yield. */ |
| 8987 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8988 | 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] | 8989 | return 0; |
| 8990 | |
| 8991 | /* finished with error. */ |
| 8992 | case HLUA_E_ERRMSG: |
| 8993 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8994 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8995 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8996 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8997 | return 0; |
| 8998 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8999 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9000 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 9001 | return 0; |
| 9002 | |
| 9003 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9004 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 9005 | return 0; |
| 9006 | |
| 9007 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9008 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 9009 | return 0; |
| 9010 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9011 | case HLUA_E_ERR: |
| 9012 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9013 | 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] | 9014 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9015 | |
| 9016 | default: |
| 9017 | return 0; |
| 9018 | } |
| 9019 | } |
| 9020 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9021 | /* This function is an LUA binding used for registering |
| 9022 | * "sample-conv" functions. It expects a converter name used |
| 9023 | * in the haproxy configuration file, and an LUA function. |
| 9024 | */ |
| 9025 | __LJMP static int hlua_register_converters(lua_State *L) |
| 9026 | { |
| 9027 | struct sample_conv_kw_list *sck; |
| 9028 | const char *name; |
| 9029 | int ref; |
| 9030 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9031 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9032 | struct sample_conv *sc; |
| 9033 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9034 | |
| 9035 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 9036 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9037 | if (hlua_gethlua(L)) { |
| 9038 | /* runtime processing */ |
| 9039 | WILL_LJMP(luaL_error(L, "register_converters: not available outside of body context")); |
| 9040 | } |
| 9041 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9042 | /* First argument : converter name. */ |
| 9043 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9044 | |
| 9045 | /* Second argument : lua function. */ |
| 9046 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9047 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9048 | /* Check if the converter is already registered */ |
| 9049 | trash = get_trash_chunk(); |
| 9050 | chunk_printf(trash, "lua.%s", name); |
| 9051 | sc = find_sample_conv(trash->area, trash->data); |
| 9052 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9053 | fcn = sc->private; |
| 9054 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9055 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 9056 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9057 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9058 | } |
| 9059 | fcn->function_ref[hlua_state_id] = ref; |
| 9060 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9061 | } |
| 9062 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9063 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9064 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9065 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9066 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9067 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9068 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9069 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9070 | |
| 9071 | /* Fill fcn. */ |
| 9072 | fcn->name = strdup(name); |
| 9073 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9074 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9075 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9076 | |
| 9077 | /* List head */ |
| 9078 | sck->list.n = sck->list.p = NULL; |
| 9079 | |
| 9080 | /* converter keyword. */ |
| 9081 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9082 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9083 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9084 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9085 | |
| 9086 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9087 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9088 | 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] | 9089 | sck->kw[0].val_args = NULL; |
| 9090 | sck->kw[0].in_type = SMP_T_STR; |
| 9091 | sck->kw[0].out_type = SMP_T_STR; |
| 9092 | sck->kw[0].private = fcn; |
| 9093 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9094 | /* Register this new converter */ |
| 9095 | sample_register_convs(sck); |
| 9096 | |
| 9097 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9098 | |
| 9099 | alloc_error: |
| 9100 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9101 | hlua_unref(L, ref); |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9102 | ha_free(&sck); |
| 9103 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9104 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9105 | } |
| 9106 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9107 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9108 | * "sample-fetch" functions. It expects a converter name used |
| 9109 | * in the haproxy configuration file, and an LUA function. |
| 9110 | */ |
| 9111 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9112 | { |
| 9113 | const char *name; |
| 9114 | int ref; |
| 9115 | int len; |
| 9116 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9117 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9118 | struct sample_fetch *sf; |
| 9119 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9120 | |
| 9121 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9122 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9123 | if (hlua_gethlua(L)) { |
| 9124 | /* runtime processing */ |
| 9125 | WILL_LJMP(luaL_error(L, "register_fetches: not available outside of body context")); |
| 9126 | } |
| 9127 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9128 | /* First argument : sample-fetch name. */ |
| 9129 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9130 | |
| 9131 | /* Second argument : lua function. */ |
| 9132 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9133 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9134 | /* Check if the sample-fetch is already registered */ |
| 9135 | trash = get_trash_chunk(); |
| 9136 | chunk_printf(trash, "lua.%s", name); |
| 9137 | sf = find_sample_fetch(trash->area, trash->data); |
| 9138 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9139 | fcn = sf->private; |
| 9140 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9141 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9142 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9143 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9144 | } |
| 9145 | fcn->function_ref[hlua_state_id] = ref; |
| 9146 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9147 | } |
| 9148 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9149 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9150 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9151 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9152 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9153 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9154 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9155 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9156 | |
| 9157 | /* Fill fcn. */ |
| 9158 | fcn->name = strdup(name); |
| 9159 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9160 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9161 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9162 | |
| 9163 | /* List head */ |
| 9164 | sfk->list.n = sfk->list.p = NULL; |
| 9165 | |
| 9166 | /* sample-fetch keyword. */ |
| 9167 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9168 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9169 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9170 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9171 | |
| 9172 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9173 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9174 | 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] | 9175 | sfk->kw[0].val_args = NULL; |
| 9176 | sfk->kw[0].out_type = SMP_T_STR; |
| 9177 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9178 | sfk->kw[0].val = 0; |
| 9179 | sfk->kw[0].private = fcn; |
| 9180 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9181 | /* Register this new fetch. */ |
| 9182 | sample_register_fetches(sfk); |
| 9183 | |
| 9184 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9185 | |
| 9186 | alloc_error: |
| 9187 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9188 | hlua_unref(L, ref); |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9189 | ha_free(&sfk); |
| 9190 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9191 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9192 | } |
| 9193 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9194 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9195 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9196 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9197 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9198 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9199 | unsigned int delay; |
| 9200 | unsigned int wakeup_ms; |
| 9201 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9202 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9203 | hlua = hlua_gethlua(L); |
| 9204 | if (!hlua) { |
| 9205 | return 0; |
| 9206 | } |
| 9207 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9208 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9209 | |
| 9210 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9211 | wakeup_ms = tick_add(now_ms, delay); |
| 9212 | hlua->wake_time = wakeup_ms; |
| 9213 | return 0; |
| 9214 | } |
| 9215 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9216 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9217 | * wrapper during the initialisation period. This function may return any |
| 9218 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9219 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9220 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9221 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9222 | 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] | 9223 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9224 | { |
| 9225 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9226 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9227 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9228 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9229 | |
| 9230 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9231 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9232 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9233 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9234 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9235 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9236 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9237 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9238 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9239 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9240 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9241 | * Lua context can be not initialized. This behavior |
| 9242 | * permits to save performances because a systematic |
| 9243 | * Lua initialization cause 5% performances loss. |
| 9244 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9245 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9246 | struct hlua *hlua; |
| 9247 | |
| 9248 | hlua = pool_alloc(pool_head_hlua); |
| 9249 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9250 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9251 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9252 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9253 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9254 | HLUA_INIT(hlua); |
| 9255 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9256 | 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] | 9257 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9258 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9259 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9260 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9261 | } |
| 9262 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9263 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9264 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9265 | |
| 9266 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9267 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9268 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9269 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9270 | else |
| 9271 | error = "critical error"; |
| 9272 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9273 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9274 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9275 | } |
| 9276 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9277 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9278 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9279 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9280 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9281 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9282 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9283 | } |
| 9284 | |
| 9285 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9286 | hlua_pushref(s->hlua->T, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9287 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9288 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9289 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9290 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9291 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9292 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9293 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9294 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9295 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9296 | |
| 9297 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9298 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9299 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9300 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9301 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9302 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9303 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9304 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9305 | lua_pushstring(s->hlua->T, *arg); |
| 9306 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9307 | } |
| 9308 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9309 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9310 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9311 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9312 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9313 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9314 | } |
| 9315 | |
| 9316 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9317 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9318 | /* finished. */ |
| 9319 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9320 | /* Catch the return value */ |
| 9321 | if (lua_gettop(s->hlua->T) > 0) |
| 9322 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9323 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9324 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9325 | if (act_ret == ACT_RET_YIELD) { |
| 9326 | if (flags & ACT_OPT_FINAL) |
| 9327 | goto err_yield; |
| 9328 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9329 | if (dir == SMP_OPT_DIR_REQ) |
| 9330 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9331 | s->hlua->wake_time); |
| 9332 | else |
| 9333 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9334 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9335 | } |
| 9336 | goto end; |
| 9337 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9338 | /* yield. */ |
| 9339 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9340 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9341 | if (dir == SMP_OPT_DIR_REQ) |
| 9342 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9343 | s->hlua->wake_time); |
| 9344 | else |
| 9345 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9346 | s->hlua->wake_time); |
| 9347 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9348 | /* Some actions can be wake up when a "write" event |
| 9349 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9350 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9351 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9352 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9353 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9354 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9355 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9356 | act_ret = ACT_RET_YIELD; |
| 9357 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9358 | |
| 9359 | /* finished with error. */ |
| 9360 | case HLUA_E_ERRMSG: |
| 9361 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9362 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9363 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9364 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9365 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9366 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9367 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9368 | 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] | 9369 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9370 | |
| 9371 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9372 | 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] | 9373 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9374 | |
| 9375 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9376 | err_yield: |
| 9377 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9378 | 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] | 9379 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9380 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9381 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9382 | case HLUA_E_ERR: |
| 9383 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9384 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9385 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9386 | |
| 9387 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9388 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9389 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9390 | |
| 9391 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9392 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9393 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9394 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9395 | } |
| 9396 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9397 | 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] | 9398 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9399 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9400 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9401 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9402 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9403 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9404 | } |
| 9405 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9406 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9407 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9408 | 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] | 9409 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9410 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9411 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9412 | struct task *task; |
| 9413 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9414 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9415 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9416 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9417 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9418 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9419 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9420 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9421 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9422 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9423 | tcp_ctx->hlua = hlua; |
| 9424 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9425 | |
| 9426 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9427 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9428 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9429 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9430 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9431 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9432 | } |
| 9433 | task->nice = 0; |
| 9434 | task->context = ctx; |
| 9435 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9436 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9437 | |
| 9438 | /* In the execution wrappers linked with a stream, the |
| 9439 | * Lua context can be not initialized. This behavior |
| 9440 | * permits to save performances because a systematic |
| 9441 | * Lua initialization cause 5% performances loss. |
| 9442 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9443 | 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] | 9444 | 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] | 9445 | ctx->rule->arg.hlua_rule->fcn->name); |
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 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9450 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9451 | |
| 9452 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9453 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9454 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9455 | error = lua_tostring(hlua->T, -1); |
| 9456 | else |
| 9457 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9458 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9459 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9460 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9461 | } |
| 9462 | |
| 9463 | /* Check stack available size. */ |
| 9464 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9465 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9466 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9467 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9468 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9469 | } |
| 9470 | |
| 9471 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9472 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9473 | |
| 9474 | /* Create and and push object stream in the stack. */ |
| 9475 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9476 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9477 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9478 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9479 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9480 | } |
| 9481 | hlua->nargs = 1; |
| 9482 | |
| 9483 | /* push keywords in the stack. */ |
| 9484 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9485 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9486 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9487 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9488 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9489 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9490 | } |
| 9491 | lua_pushstring(hlua->T, *arg); |
| 9492 | hlua->nargs++; |
| 9493 | } |
| 9494 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9495 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9496 | |
| 9497 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9498 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9499 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9500 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9501 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9502 | } |
| 9503 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9504 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9505 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9506 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9507 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9508 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9509 | struct act_rule *rule = ctx->rule; |
| 9510 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9511 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9512 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9513 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9514 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9515 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9516 | /* The applet execution is already done. */ |
| 9517 | if (tcp_ctx->flags & APPLET_DONE) |
| 9518 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9519 | |
| 9520 | /* Execute the function. */ |
| 9521 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9522 | /* finished. */ |
| 9523 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9524 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9525 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 9526 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9527 | |
| 9528 | /* yield. */ |
| 9529 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9530 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9531 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9532 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9533 | |
| 9534 | /* finished with error. */ |
| 9535 | case HLUA_E_ERRMSG: |
| 9536 | /* Display log. */ |
| 9537 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9538 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9539 | lua_pop(hlua->T, 1); |
| 9540 | goto error; |
| 9541 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9542 | case HLUA_E_ETMOUT: |
| 9543 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9544 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9545 | goto error; |
| 9546 | |
| 9547 | case HLUA_E_NOMEM: |
| 9548 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9549 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9550 | goto error; |
| 9551 | |
| 9552 | case HLUA_E_YIELD: /* unexpected */ |
| 9553 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9554 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9555 | goto error; |
| 9556 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9557 | case HLUA_E_ERR: |
| 9558 | /* Display log. */ |
| 9559 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9560 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9561 | goto error; |
| 9562 | |
| 9563 | default: |
| 9564 | goto error; |
| 9565 | } |
| 9566 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9567 | out: |
| 9568 | /* eat the whole request */ |
| 9569 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 9570 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9571 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9572 | error: |
| 9573 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9574 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9575 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9576 | } |
| 9577 | |
| 9578 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9579 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9580 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9581 | |
| 9582 | task_destroy(tcp_ctx->task); |
| 9583 | tcp_ctx->task = NULL; |
| 9584 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9585 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9586 | } |
| 9587 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9588 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9589 | * 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] | 9590 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9591 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9592 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9593 | 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] | 9594 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9595 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9596 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9597 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9598 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9599 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9600 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9601 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9602 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9603 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9604 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9605 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9606 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9607 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9608 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9609 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9610 | http_ctx->hlua = hlua; |
| 9611 | http_ctx->left_bytes = -1; |
| 9612 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9613 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9614 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9615 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9616 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9617 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9618 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9619 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9620 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9621 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9622 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9623 | } |
| 9624 | task->nice = 0; |
| 9625 | task->context = ctx; |
| 9626 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9627 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9628 | |
| 9629 | /* In the execution wrappers linked with a stream, the |
| 9630 | * Lua context can be not initialized. This behavior |
| 9631 | * permits to save performances because a systematic |
| 9632 | * Lua initialization cause 5% performances loss. |
| 9633 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9634 | 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] | 9635 | 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] | 9636 | ctx->rule->arg.hlua_rule->fcn->name); |
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 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9641 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9642 | |
| 9643 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9644 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9645 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9646 | error = lua_tostring(hlua->T, -1); |
| 9647 | else |
| 9648 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9649 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9650 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9651 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9652 | } |
| 9653 | |
| 9654 | /* Check stack available size. */ |
| 9655 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9656 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9657 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9658 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9659 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9660 | } |
| 9661 | |
| 9662 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 9663 | hlua_pushref(hlua->T, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9664 | |
| 9665 | /* Create and and push object stream in the stack. */ |
| 9666 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9667 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9668 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9669 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9670 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9671 | } |
| 9672 | hlua->nargs = 1; |
| 9673 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9674 | /* push keywords in the stack. */ |
| 9675 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9676 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9677 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9678 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9679 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9680 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9681 | } |
| 9682 | lua_pushstring(hlua->T, *arg); |
| 9683 | hlua->nargs++; |
| 9684 | } |
| 9685 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9686 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9687 | |
| 9688 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9689 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9690 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9691 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9692 | } |
| 9693 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9694 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9695 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9696 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9697 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9698 | struct stream *strm = __sc_strm(sc); |
| 9699 | struct channel *req = sc_oc(sc); |
| 9700 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9701 | struct act_rule *rule = ctx->rule; |
| 9702 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9703 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9704 | struct htx *req_htx, *res_htx; |
| 9705 | |
| 9706 | res_htx = htx_from_buf(&res->buf); |
| 9707 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9708 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9709 | goto out; |
| 9710 | |
| 9711 | /* The applet execution is already done. */ |
| 9712 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9713 | goto out; |
| 9714 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9715 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9716 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9717 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9718 | goto out; |
| 9719 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9720 | |
| 9721 | /* Set the currently running flag. */ |
| 9722 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9723 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9724 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9725 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9726 | goto out; |
| 9727 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9728 | } |
| 9729 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9730 | /* Execute the function. */ |
| 9731 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9732 | /* finished. */ |
| 9733 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9734 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9735 | break; |
| 9736 | |
| 9737 | /* yield. */ |
| 9738 | case HLUA_E_AGAIN: |
| 9739 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9740 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9741 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9742 | |
| 9743 | /* finished with error. */ |
| 9744 | case HLUA_E_ERRMSG: |
| 9745 | /* Display log. */ |
| 9746 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9747 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9748 | lua_pop(hlua->T, 1); |
| 9749 | goto error; |
| 9750 | |
| 9751 | case HLUA_E_ETMOUT: |
| 9752 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9753 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9754 | goto error; |
| 9755 | |
| 9756 | case HLUA_E_NOMEM: |
| 9757 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9758 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9759 | goto error; |
| 9760 | |
| 9761 | case HLUA_E_YIELD: /* unexpected */ |
| 9762 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9763 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9764 | goto error; |
| 9765 | |
| 9766 | case HLUA_E_ERR: |
| 9767 | /* Display log. */ |
| 9768 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9769 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9770 | goto error; |
| 9771 | |
| 9772 | default: |
| 9773 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9774 | } |
| 9775 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9776 | if (http_ctx->flags & APPLET_DONE) { |
| 9777 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9778 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9779 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9780 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9781 | goto error; |
| 9782 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9783 | /* no more data are expected. If the response buffer is empty |
| 9784 | * for a chunked message, be sure to add something (EOT block in |
| 9785 | * this case) to have something to send. It is important to be |
| 9786 | * sure the EOM flags will be handled by the endpoint. |
| 9787 | */ |
| 9788 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9789 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9790 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9791 | goto out; |
| 9792 | } |
| 9793 | channel_add_input(res, 1); |
| 9794 | } |
| 9795 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9796 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9797 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9798 | strm->txn->status = http_ctx->status; |
| 9799 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9800 | } |
| 9801 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9802 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9803 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9804 | /* eat the whole request */ |
| 9805 | if (co_data(req)) { |
| 9806 | req_htx = htx_from_buf(&req->buf); |
| 9807 | co_htx_skip(req, req_htx, co_data(req)); |
| 9808 | htx_to_buf(req_htx, &req->buf); |
| 9809 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9810 | return; |
| 9811 | |
| 9812 | error: |
| 9813 | |
| 9814 | /* If we are in HTTP mode, and we are not send any |
| 9815 | * data, return a 500 server error in best effort: |
| 9816 | * if there is no room available in the buffer, |
| 9817 | * just close the connection. |
| 9818 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9819 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9820 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9821 | |
| 9822 | channel_erase(res); |
| 9823 | res->buf.data = b_data(err); |
| 9824 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9825 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9826 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9827 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9828 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9829 | else |
| 9830 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 9831 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9832 | if (!(strm->flags & SF_ERR_MASK)) |
| 9833 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9834 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9835 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9836 | } |
| 9837 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9838 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9839 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9840 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9841 | |
| 9842 | task_destroy(http_ctx->task); |
| 9843 | http_ctx->task = NULL; |
| 9844 | hlua_ctx_destroy(http_ctx->hlua); |
| 9845 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9846 | } |
| 9847 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9848 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9849 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9850 | * |
| 9851 | * This function can fail with an abort() due to an Lua critical error. |
| 9852 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9853 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9854 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9855 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9856 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9857 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9858 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9859 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9860 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9861 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9862 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9863 | if (!rule->arg.hlua_rule) { |
| 9864 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9865 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9866 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9867 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9868 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9869 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9870 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9871 | if (!rule->arg.hlua_rule->args) { |
| 9872 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9873 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9874 | } |
| 9875 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9876 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9877 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9878 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9879 | /* Expect some arguments */ |
| 9880 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9881 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9882 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9883 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9884 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9885 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9886 | if (!rule->arg.hlua_rule->args[i]) { |
| 9887 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9888 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9889 | } |
| 9890 | (*cur_arg)++; |
| 9891 | } |
| 9892 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9893 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9894 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9895 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9896 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9897 | |
| 9898 | error: |
| 9899 | if (rule->arg.hlua_rule) { |
| 9900 | if (rule->arg.hlua_rule->args) { |
| 9901 | for (i = 0; i < fcn->nargs; i++) |
| 9902 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9903 | ha_free(&rule->arg.hlua_rule->args); |
| 9904 | } |
| 9905 | ha_free(&rule->arg.hlua_rule); |
| 9906 | } |
| 9907 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9908 | } |
| 9909 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9910 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9911 | struct act_rule *rule, char **err) |
| 9912 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9913 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9914 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9915 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9916 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9917 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9918 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9919 | * the call of this analyzer. |
| 9920 | */ |
| 9921 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9922 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9923 | return ACT_RET_PRS_ERR; |
| 9924 | } |
| 9925 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9926 | /* Memory for the rule. */ |
| 9927 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9928 | if (!rule->arg.hlua_rule) { |
| 9929 | memprintf(err, "out of memory error"); |
| 9930 | return ACT_RET_PRS_ERR; |
| 9931 | } |
| 9932 | |
| 9933 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9934 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9935 | |
| 9936 | /* TODO: later accept arguments. */ |
| 9937 | rule->arg.hlua_rule->args = NULL; |
| 9938 | |
| 9939 | /* Add applet pointer in the rule. */ |
| 9940 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9941 | rule->applet.name = fcn->name; |
| 9942 | rule->applet.init = hlua_applet_http_init; |
| 9943 | rule->applet.fct = hlua_applet_http_fct; |
| 9944 | rule->applet.release = hlua_applet_http_release; |
| 9945 | rule->applet.timeout = hlua_timeout_applet; |
| 9946 | |
| 9947 | return ACT_RET_PRS_OK; |
| 9948 | } |
| 9949 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9950 | /* This function is an LUA binding used for registering |
| 9951 | * "sample-conv" functions. It expects a converter name used |
| 9952 | * in the haproxy configuration file, and an LUA function. |
| 9953 | */ |
| 9954 | __LJMP static int hlua_register_action(lua_State *L) |
| 9955 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9956 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9957 | const char *name; |
| 9958 | int ref; |
| 9959 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9960 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9961 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9962 | struct buffer *trash; |
| 9963 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9964 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9965 | /* Initialise the number of expected arguments at 0. */ |
| 9966 | nargs = 0; |
| 9967 | |
| 9968 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9969 | 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] | 9970 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 9971 | if (hlua_gethlua(L)) { |
| 9972 | /* runtime processing */ |
| 9973 | WILL_LJMP(luaL_error(L, "register_action: not available outside of body context")); |
| 9974 | } |
| 9975 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9976 | /* First argument : converter name. */ |
| 9977 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9978 | |
| 9979 | /* Second argument : environment. */ |
| 9980 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9981 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9982 | |
| 9983 | /* Third argument : lua function. */ |
| 9984 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9985 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9986 | /* 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] | 9987 | if (lua_gettop(L) >= 4) |
| 9988 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9989 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9990 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9991 | lua_pushnil(L); |
| 9992 | while (lua_next(L, 2) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9993 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 9994 | hlua_unref(L, ref); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9995 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 9996 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9997 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9998 | /* Check if action exists */ |
| 9999 | trash = get_trash_chunk(); |
| 10000 | chunk_printf(trash, "lua.%s", name); |
| 10001 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 10002 | akw = tcp_req_cont_action(trash->area); |
| 10003 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 10004 | akw = tcp_res_cont_action(trash->area); |
| 10005 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 10006 | akw = action_http_req_custom(trash->area); |
| 10007 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 10008 | akw = action_http_res_custom(trash->area); |
| 10009 | } else { |
| 10010 | akw = NULL; |
| 10011 | } |
| 10012 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10013 | fcn = akw->private; |
| 10014 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10015 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 10016 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10017 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10018 | } |
| 10019 | fcn->function_ref[hlua_state_id] = ref; |
| 10020 | |
| 10021 | /* pop the environment string. */ |
| 10022 | lua_pop(L, 1); |
| 10023 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10024 | } |
| 10025 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10026 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 10027 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10028 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10029 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10030 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10031 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10032 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10033 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10034 | |
| 10035 | /* Fill fcn. */ |
| 10036 | fcn->name = strdup(name); |
| 10037 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10038 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10039 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10040 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 10041 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 10042 | fcn->nargs = nargs; |
| 10043 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10044 | /* List head */ |
| 10045 | akl->list.n = akl->list.p = NULL; |
| 10046 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10047 | /* action keyword. */ |
| 10048 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 10049 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10050 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10051 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10052 | |
| 10053 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10054 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10055 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10056 | akl->kw[0].private = fcn; |
| 10057 | akl->kw[0].parse = action_register_lua; |
| 10058 | |
| 10059 | /* select the action registering point. */ |
| 10060 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 10061 | tcp_req_cont_keywords_register(akl); |
| 10062 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 10063 | tcp_res_cont_keywords_register(akl); |
| 10064 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 10065 | http_req_keywords_register(akl); |
| 10066 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 10067 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10068 | else { |
| 10069 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10070 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10071 | if (akl) |
| 10072 | ha_free((char **)&(akl->kw[0].kw)); |
| 10073 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10074 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10075 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 10076 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10077 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10078 | |
| 10079 | /* pop the environment string. */ |
| 10080 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10081 | |
| 10082 | /* reset for next loop */ |
| 10083 | akl = NULL; |
| 10084 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10085 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10086 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10087 | |
| 10088 | alloc_error: |
| 10089 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10090 | hlua_unref(L, ref); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10091 | ha_free(&akl); |
| 10092 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10093 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10094 | } |
| 10095 | |
| 10096 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10097 | struct act_rule *rule, char **err) |
| 10098 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10099 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10100 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10101 | if (px->mode == PR_MODE_HTTP) { |
| 10102 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10103 | return ACT_RET_PRS_ERR; |
| 10104 | } |
| 10105 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10106 | /* Memory for the rule. */ |
| 10107 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10108 | if (!rule->arg.hlua_rule) { |
| 10109 | memprintf(err, "out of memory error"); |
| 10110 | return ACT_RET_PRS_ERR; |
| 10111 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10112 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10113 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10114 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10115 | |
| 10116 | /* TODO: later accept arguments. */ |
| 10117 | rule->arg.hlua_rule->args = NULL; |
| 10118 | |
| 10119 | /* Add applet pointer in the rule. */ |
| 10120 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10121 | rule->applet.name = fcn->name; |
| 10122 | rule->applet.init = hlua_applet_tcp_init; |
| 10123 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10124 | rule->applet.release = hlua_applet_tcp_release; |
| 10125 | rule->applet.timeout = hlua_timeout_applet; |
| 10126 | |
| 10127 | return 0; |
| 10128 | } |
| 10129 | |
| 10130 | /* This function is an LUA binding used for registering |
| 10131 | * "sample-conv" functions. It expects a converter name used |
| 10132 | * in the haproxy configuration file, and an LUA function. |
| 10133 | */ |
| 10134 | __LJMP static int hlua_register_service(lua_State *L) |
| 10135 | { |
| 10136 | struct action_kw_list *akl; |
| 10137 | const char *name; |
| 10138 | const char *env; |
| 10139 | int ref; |
| 10140 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10141 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10142 | struct buffer *trash; |
| 10143 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10144 | |
| 10145 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10146 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10147 | if (hlua_gethlua(L)) { |
| 10148 | /* runtime processing */ |
| 10149 | WILL_LJMP(luaL_error(L, "register_service: not available outside of body context")); |
| 10150 | } |
| 10151 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10152 | /* First argument : converter name. */ |
| 10153 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10154 | |
| 10155 | /* Second argument : environment. */ |
| 10156 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10157 | |
| 10158 | /* Third argument : lua function. */ |
| 10159 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10160 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10161 | /* Check for service already registered */ |
| 10162 | trash = get_trash_chunk(); |
| 10163 | chunk_printf(trash, "lua.%s", name); |
| 10164 | akw = service_find(trash->area); |
| 10165 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10166 | fcn = akw->private; |
| 10167 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10168 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10169 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10170 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10171 | } |
| 10172 | fcn->function_ref[hlua_state_id] = ref; |
| 10173 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10174 | } |
| 10175 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10176 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10177 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10178 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10179 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10180 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10181 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10182 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10183 | |
| 10184 | /* Fill fcn. */ |
| 10185 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10186 | fcn->name = calloc(1, len); |
| 10187 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10188 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10189 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10190 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10191 | |
| 10192 | /* List head */ |
| 10193 | akl->list.n = akl->list.p = NULL; |
| 10194 | |
| 10195 | /* converter keyword. */ |
| 10196 | len = strlen("lua.") + strlen(name) + 1; |
| 10197 | akl->kw[0].kw = calloc(1, len); |
| 10198 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10199 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10200 | |
| 10201 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10202 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10203 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10204 | if (strcmp(env, "tcp") == 0) |
| 10205 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10206 | else if (strcmp(env, "http") == 0) |
| 10207 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10208 | else { |
| 10209 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10210 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10211 | if (akl) |
| 10212 | ha_free((char **)&(akl->kw[0].kw)); |
| 10213 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10214 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10215 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10216 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10217 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10218 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10219 | akl->kw[0].private = fcn; |
| 10220 | |
| 10221 | /* End of array. */ |
| 10222 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10223 | |
| 10224 | /* Register this new converter */ |
| 10225 | service_keywords_register(akl); |
| 10226 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10227 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10228 | |
| 10229 | alloc_error: |
| 10230 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10231 | hlua_unref(L, ref); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10232 | ha_free(&akl); |
| 10233 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10234 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10235 | } |
| 10236 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10237 | /* This function initialises Lua cli handler. It copies the |
| 10238 | * arguments in the Lua stack and create channel IO objects. |
| 10239 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10240 | 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] | 10241 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10242 | 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] | 10243 | struct hlua *hlua; |
| 10244 | struct hlua_function *fcn; |
| 10245 | int i; |
| 10246 | const char *error; |
| 10247 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10248 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10249 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10250 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10251 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10252 | if (!hlua) { |
| 10253 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10254 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10255 | } |
| 10256 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10257 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10258 | |
| 10259 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10260 | * 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] | 10261 | * applet_http. It is absolutely compatible. |
| 10262 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10263 | ctx->task = task_new_here(); |
| 10264 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10265 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10266 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10267 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10268 | ctx->task->nice = 0; |
| 10269 | ctx->task->context = appctx; |
| 10270 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10271 | |
| 10272 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10273 | 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] | 10274 | 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] | 10275 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10276 | } |
| 10277 | |
| 10278 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10279 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10280 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10281 | error = lua_tostring(hlua->T, -1); |
| 10282 | else |
| 10283 | error = "critical error"; |
| 10284 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10285 | goto error; |
| 10286 | } |
| 10287 | |
| 10288 | /* Check stack available size. */ |
| 10289 | if (!lua_checkstack(hlua->T, 2)) { |
| 10290 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10291 | goto error; |
| 10292 | } |
| 10293 | |
| 10294 | /* Restore the function in the stack. */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10295 | hlua_pushref(hlua->T, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10296 | |
| 10297 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10298 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10299 | */ |
| 10300 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10301 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10302 | goto error; |
| 10303 | } |
| 10304 | hlua->nargs = 1; |
| 10305 | |
| 10306 | /* push keywords in the stack. */ |
| 10307 | for (i = 0; *args[i]; i++) { |
| 10308 | /* Check stack available size. */ |
| 10309 | if (!lua_checkstack(hlua->T, 1)) { |
| 10310 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10311 | goto error; |
| 10312 | } |
| 10313 | lua_pushstring(hlua->T, args[i]); |
| 10314 | hlua->nargs++; |
| 10315 | } |
| 10316 | |
| 10317 | /* We must initialize the execution timeouts. */ |
| 10318 | hlua->max_time = hlua_timeout_session; |
| 10319 | |
| 10320 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10321 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10322 | |
| 10323 | /* It's ok */ |
| 10324 | return 0; |
| 10325 | |
| 10326 | /* It's not ok. */ |
| 10327 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10328 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10329 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10330 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10331 | return 1; |
| 10332 | } |
| 10333 | |
| 10334 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10335 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10336 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10337 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10338 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10339 | struct hlua_function *fcn; |
| 10340 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10341 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10342 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10343 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10344 | |
| 10345 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10346 | 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] | 10347 | return 1; |
| 10348 | |
| 10349 | /* Execute the function. */ |
| 10350 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10351 | |
| 10352 | /* finished. */ |
| 10353 | case HLUA_E_OK: |
| 10354 | return 1; |
| 10355 | |
| 10356 | /* yield. */ |
| 10357 | case HLUA_E_AGAIN: |
| 10358 | /* We want write. */ |
| 10359 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10360 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10361 | /* Set the timeout. */ |
| 10362 | if (hlua->wake_time != TICK_ETERNITY) |
| 10363 | task_schedule(hlua->task, hlua->wake_time); |
| 10364 | return 0; |
| 10365 | |
| 10366 | /* finished with error. */ |
| 10367 | case HLUA_E_ERRMSG: |
| 10368 | /* Display log. */ |
| 10369 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10370 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10371 | lua_pop(hlua->T, 1); |
| 10372 | return 1; |
| 10373 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10374 | case HLUA_E_ETMOUT: |
| 10375 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10376 | fcn->name); |
| 10377 | return 1; |
| 10378 | |
| 10379 | case HLUA_E_NOMEM: |
| 10380 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10381 | fcn->name); |
| 10382 | return 1; |
| 10383 | |
| 10384 | case HLUA_E_YIELD: /* unexpected */ |
| 10385 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10386 | fcn->name); |
| 10387 | return 1; |
| 10388 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10389 | case HLUA_E_ERR: |
| 10390 | /* Display log. */ |
| 10391 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10392 | fcn->name); |
| 10393 | return 1; |
| 10394 | |
| 10395 | default: |
| 10396 | return 1; |
| 10397 | } |
| 10398 | |
| 10399 | return 1; |
| 10400 | } |
| 10401 | |
| 10402 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10403 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10404 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10405 | |
| 10406 | hlua_ctx_destroy(ctx->hlua); |
| 10407 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10408 | } |
| 10409 | |
| 10410 | /* This function is an LUA binding used for registering |
| 10411 | * new keywords in the cli. It expects a list of keywords |
| 10412 | * which are the "path". It is limited to 5 keywords. A |
| 10413 | * description of the command, a function to be executed |
| 10414 | * for the parsing and a function for io handlers. |
| 10415 | */ |
| 10416 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10417 | { |
| 10418 | struct cli_kw_list *cli_kws; |
| 10419 | const char *message; |
| 10420 | int ref_io; |
| 10421 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10422 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10423 | int index; |
| 10424 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10425 | struct buffer *trash; |
| 10426 | const char *kw[5]; |
| 10427 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10428 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10429 | |
| 10430 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10431 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 10432 | if (hlua_gethlua(L)) { |
| 10433 | /* runtime processing */ |
| 10434 | WILL_LJMP(luaL_error(L, "register_cli: not available outside of body context")); |
| 10435 | } |
| 10436 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10437 | /* First argument : an array of maximum 5 keywords. */ |
| 10438 | if (!lua_istable(L, 1)) |
| 10439 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10440 | |
| 10441 | /* Second argument : string with contextual message. */ |
| 10442 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10443 | |
| 10444 | /* Third and fourth argument : lua function. */ |
| 10445 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10446 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10447 | /* Check for CLI service already registered */ |
| 10448 | trash = get_trash_chunk(); |
| 10449 | index = 0; |
| 10450 | lua_pushnil(L); |
| 10451 | memset(kw, 0, sizeof(kw)); |
| 10452 | while (lua_next(L, 1) != 0) { |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10453 | if (index >= CLI_PREFIX_KW_NB) { |
| 10454 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10455 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10456 | } |
| 10457 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10458 | hlua_unref(L, ref_io); |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10459 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10460 | } |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10461 | kw[index] = lua_tostring(L, -1); |
| 10462 | if (index == 0) |
| 10463 | chunk_printf(trash, "%s", kw[index]); |
| 10464 | else |
| 10465 | chunk_appendf(trash, " %s", kw[index]); |
| 10466 | index++; |
| 10467 | lua_pop(L, 1); |
| 10468 | } |
| 10469 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10470 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10471 | fcn = cli_kw->private; |
| 10472 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10473 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10474 | "This will become a hard error in version 2.5.\n", trash->area); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10475 | hlua_unref(L, fcn->function_ref[hlua_state_id]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10476 | } |
| 10477 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10478 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10479 | } |
| 10480 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10481 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10482 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10483 | if (!cli_kws) { |
| 10484 | errmsg = "Lua out of memory error."; |
| 10485 | goto error; |
| 10486 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10487 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10488 | if (!fcn) { |
| 10489 | errmsg = "Lua out of memory error."; |
| 10490 | goto error; |
| 10491 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10492 | |
| 10493 | /* Fill path. */ |
| 10494 | index = 0; |
| 10495 | lua_pushnil(L); |
| 10496 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10497 | if (index >= 5) { |
| 10498 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10499 | goto error; |
| 10500 | } |
| 10501 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10502 | errmsg = "1st argument must be a table filled with strings"; |
| 10503 | goto error; |
| 10504 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10505 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10506 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10507 | errmsg = "Lua out of memory error."; |
| 10508 | goto error; |
| 10509 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10510 | index++; |
| 10511 | lua_pop(L, 1); |
| 10512 | } |
| 10513 | |
| 10514 | /* Copy help message. */ |
| 10515 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10516 | if (!cli_kws->kw[0].usage) { |
| 10517 | errmsg = "Lua out of memory error."; |
| 10518 | goto error; |
| 10519 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10520 | |
| 10521 | /* Fill fcn io handler. */ |
| 10522 | len = strlen("<lua.cli>") + 1; |
| 10523 | for (i = 0; i < index; i++) |
| 10524 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10525 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10526 | if (!fcn->name) { |
| 10527 | errmsg = "Lua out of memory error."; |
| 10528 | goto error; |
| 10529 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10530 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10531 | for (i = 0; i < index; i++) { |
| 10532 | strncat((char *)fcn->name, ".", len); |
| 10533 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10534 | } |
| 10535 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10536 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10537 | |
| 10538 | /* Fill last entries. */ |
| 10539 | cli_kws->kw[0].private = fcn; |
| 10540 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10541 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10542 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10543 | |
| 10544 | /* Register this new converter */ |
| 10545 | cli_register_kw(cli_kws); |
| 10546 | |
| 10547 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10548 | |
| 10549 | error: |
| 10550 | release_hlua_function(fcn); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 10551 | hlua_unref(L, ref_io); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10552 | if (cli_kws) { |
| 10553 | for (i = 0; i < index; i++) |
| 10554 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10555 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10556 | } |
| 10557 | ha_free(&cli_kws); |
| 10558 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10559 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10560 | } |
| 10561 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10562 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10563 | { |
| 10564 | struct hlua_flt_config *conf = fconf->conf; |
| 10565 | lua_State *L; |
| 10566 | int error, pos, state_id, flt_ref; |
| 10567 | |
| 10568 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10569 | L = hlua_states[state_id]; |
| 10570 | pos = lua_gettop(L); |
| 10571 | |
| 10572 | /* The filter parsing function */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10573 | hlua_pushref(L, conf->reg->fun_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10574 | |
| 10575 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10576 | hlua_pushref(L, conf->reg->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10577 | |
| 10578 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10579 | lua_newtable(L); |
| 10580 | lua_pushnil(L); |
| 10581 | |
| 10582 | while (lua_next(L, pos+2)) { |
| 10583 | lua_pushvalue(L, -2); |
| 10584 | lua_insert(L, -2); |
| 10585 | lua_settable(L, -4); |
| 10586 | } |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 10587 | flt_ref = hlua_ref(L); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10588 | |
| 10589 | /* Remove the original lua filter class from the stack */ |
| 10590 | lua_pop(L, 1); |
| 10591 | |
| 10592 | /* Push the copy on the stack */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10593 | hlua_pushref(L, flt_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10594 | |
| 10595 | /* extra args are pushed in a table */ |
| 10596 | lua_newtable(L); |
| 10597 | for (pos = 0; conf->args[pos]; pos++) { |
| 10598 | /* Check stack available size. */ |
| 10599 | if (!lua_checkstack(L, 1)) { |
| 10600 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10601 | goto error; |
| 10602 | } |
| 10603 | lua_pushstring(L, conf->args[pos]); |
| 10604 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10605 | } |
| 10606 | |
| 10607 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10608 | switch (error) { |
| 10609 | case LUA_OK: |
| 10610 | /* replace the filter ref */ |
| 10611 | conf->ref[state_id] = flt_ref; |
| 10612 | break; |
| 10613 | case LUA_ERRRUN: |
| 10614 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10615 | goto error; |
| 10616 | case LUA_ERRMEM: |
| 10617 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10618 | goto error; |
| 10619 | case LUA_ERRERR: |
| 10620 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10621 | goto error; |
| 10622 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10623 | case LUA_ERRGCMM: |
| 10624 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10625 | goto error; |
| 10626 | #endif |
| 10627 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10628 | 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] | 10629 | goto error; |
| 10630 | } |
| 10631 | |
| 10632 | lua_settop(L, 0); |
| 10633 | return 0; |
| 10634 | |
| 10635 | error: |
| 10636 | lua_settop(L, 0); |
| 10637 | return -1; |
| 10638 | } |
| 10639 | |
| 10640 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10641 | { |
| 10642 | struct hlua_flt_config *conf = fconf->conf; |
| 10643 | lua_State *L; |
| 10644 | int state_id; |
| 10645 | |
| 10646 | if (!conf) |
| 10647 | return; |
| 10648 | |
| 10649 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10650 | L = hlua_states[state_id]; |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 10651 | hlua_unref(L, conf->ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10652 | } |
| 10653 | |
| 10654 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10655 | { |
| 10656 | struct hlua_flt_config *conf = fconf->conf; |
| 10657 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10658 | |
| 10659 | /* Rely on per-thread init for global scripts */ |
| 10660 | if (!state_id) |
| 10661 | return hlua_filter_init_per_thread(px, fconf); |
| 10662 | return 0; |
| 10663 | } |
| 10664 | |
| 10665 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10666 | { |
| 10667 | |
| 10668 | if (fconf->conf) { |
| 10669 | struct hlua_flt_config *conf = fconf->conf; |
| 10670 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10671 | int pos; |
| 10672 | |
| 10673 | /* Rely on per-thread deinit for global scripts */ |
| 10674 | if (!state_id) |
| 10675 | hlua_filter_deinit_per_thread(px, fconf); |
| 10676 | |
| 10677 | for (pos = 0; conf->args[pos]; pos++) |
| 10678 | free(conf->args[pos]); |
| 10679 | free(conf->args); |
| 10680 | } |
| 10681 | ha_free(&fconf->conf); |
| 10682 | ha_free((char **)&fconf->id); |
| 10683 | ha_free(&fconf->ops); |
| 10684 | } |
| 10685 | |
| 10686 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10687 | { |
| 10688 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10689 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10690 | int ret = 1; |
| 10691 | |
| 10692 | /* In the execution wrappers linked with a stream, the |
| 10693 | * Lua context can be not initialized. This behavior |
| 10694 | * permits to save performances because a systematic |
| 10695 | * Lua initialization cause 5% performances loss. |
| 10696 | */ |
| 10697 | if (!s->hlua) { |
| 10698 | struct hlua *hlua; |
| 10699 | |
| 10700 | hlua = pool_alloc(pool_head_hlua); |
| 10701 | if (!hlua) { |
| 10702 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10703 | conf->reg->name); |
| 10704 | ret = 0; |
| 10705 | goto end; |
| 10706 | } |
| 10707 | HLUA_INIT(hlua); |
| 10708 | s->hlua = hlua; |
| 10709 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10710 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10711 | conf->reg->name); |
| 10712 | ret = 0; |
| 10713 | goto end; |
| 10714 | } |
| 10715 | } |
| 10716 | |
| 10717 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10718 | if (!flt_ctx) { |
| 10719 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10720 | conf->reg->name); |
| 10721 | ret = 0; |
| 10722 | goto end; |
| 10723 | } |
| 10724 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10725 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10726 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10727 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10728 | conf->reg->name); |
| 10729 | ret = 0; |
| 10730 | goto end; |
| 10731 | } |
| 10732 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10733 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10734 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10735 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10736 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10737 | conf->reg->name); |
| 10738 | ret = 0; |
| 10739 | goto end; |
| 10740 | } |
| 10741 | |
| 10742 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10743 | /* The following Lua calls can fail. */ |
| 10744 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10745 | const char *error; |
| 10746 | |
| 10747 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10748 | error = lua_tostring(s->hlua->T, -1); |
| 10749 | else |
| 10750 | error = "critical error"; |
| 10751 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10752 | ret = 0; |
| 10753 | goto end; |
| 10754 | } |
| 10755 | |
| 10756 | /* Check stack size. */ |
| 10757 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10758 | 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] | 10759 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10760 | ret = 0; |
| 10761 | goto end; |
| 10762 | } |
| 10763 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10764 | hlua_pushref(s->hlua->T, conf->ref[s->hlua->state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10765 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10766 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10767 | conf->reg->name); |
| 10768 | RESET_SAFE_LJMP(s->hlua); |
| 10769 | ret = 0; |
| 10770 | goto end; |
| 10771 | } |
| 10772 | lua_insert(s->hlua->T, -2); |
| 10773 | |
| 10774 | /* Push the copy on the stack */ |
| 10775 | s->hlua->nargs = 1; |
| 10776 | |
| 10777 | /* We must initialize the execution timeouts. */ |
| 10778 | s->hlua->max_time = hlua_timeout_session; |
| 10779 | |
| 10780 | /* At this point the execution is safe. */ |
| 10781 | RESET_SAFE_LJMP(s->hlua); |
| 10782 | } |
| 10783 | |
| 10784 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10785 | case HLUA_E_OK: |
| 10786 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10787 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10788 | ret = 0; |
| 10789 | goto end; |
| 10790 | } |
| 10791 | |
| 10792 | /* Attached the filter pointer to the ctx */ |
| 10793 | lua_pushstring(s->hlua->T, "__filter"); |
| 10794 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10795 | lua_settable(s->hlua->T, -3); |
| 10796 | |
| 10797 | /* Save a ref on the filter ctx */ |
| 10798 | lua_pushvalue(s->hlua->T, 1); |
Aurelien DARRAGON | 73d1a98 | 2023-03-20 17:42:08 +0100 | [diff] [blame] | 10799 | flt_ctx->ref = hlua_ref(s->hlua->T); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10800 | filter->ctx = flt_ctx; |
| 10801 | break; |
| 10802 | case HLUA_E_ERRMSG: |
| 10803 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10804 | ret = -1; |
| 10805 | goto end; |
| 10806 | case HLUA_E_ETMOUT: |
| 10807 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10808 | ret = 0; |
| 10809 | goto end; |
| 10810 | case HLUA_E_NOMEM: |
| 10811 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10812 | ret = 0; |
| 10813 | goto end; |
| 10814 | case HLUA_E_AGAIN: |
| 10815 | case HLUA_E_YIELD: |
| 10816 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10817 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10818 | ret = 0; |
| 10819 | goto end; |
| 10820 | case HLUA_E_ERR: |
| 10821 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10822 | ret = 0; |
| 10823 | goto end; |
| 10824 | default: |
| 10825 | ret = 0; |
| 10826 | goto end; |
| 10827 | } |
| 10828 | |
| 10829 | end: |
| 10830 | if (s->hlua) |
| 10831 | lua_settop(s->hlua->T, 0); |
| 10832 | if (ret <= 0) { |
| 10833 | if (flt_ctx) { |
| 10834 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10835 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10836 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10837 | } |
| 10838 | } |
| 10839 | return ret; |
| 10840 | } |
| 10841 | |
| 10842 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10843 | { |
| 10844 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10845 | |
Aurelien DARRAGON | fde199d | 2023-03-20 15:09:33 +0100 | [diff] [blame] | 10846 | hlua_unref(s->hlua->T, flt_ctx->ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10847 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10848 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10849 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10850 | filter->ctx = NULL; |
| 10851 | } |
| 10852 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10853 | static int hlua_filter_from_payload(struct filter *filter) |
| 10854 | { |
| 10855 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10856 | |
| 10857 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10858 | } |
| 10859 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10860 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10861 | int dir, unsigned int flags) |
| 10862 | { |
| 10863 | struct hlua *flt_hlua; |
| 10864 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10865 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10866 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10867 | int ret = 1; |
| 10868 | |
| 10869 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10870 | if (!flt_hlua) |
| 10871 | goto end; |
| 10872 | |
| 10873 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10874 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10875 | |
| 10876 | /* The following Lua calls can fail. */ |
| 10877 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10878 | const char *error; |
| 10879 | |
| 10880 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10881 | error = lua_tostring(flt_hlua->T, -1); |
| 10882 | else |
| 10883 | error = "critical error"; |
| 10884 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10885 | goto end; |
| 10886 | } |
| 10887 | |
| 10888 | /* Check stack size. */ |
| 10889 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10890 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10891 | RESET_SAFE_LJMP(flt_hlua); |
| 10892 | goto end; |
| 10893 | } |
| 10894 | |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 10895 | hlua_pushref(flt_hlua->T, flt_ctx->ref); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10896 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10897 | RESET_SAFE_LJMP(flt_hlua); |
| 10898 | goto end; |
| 10899 | } |
| 10900 | lua_insert(flt_hlua->T, -2); |
| 10901 | |
| 10902 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10903 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10904 | RESET_SAFE_LJMP(flt_hlua); |
| 10905 | goto end; |
| 10906 | } |
| 10907 | flt_hlua->nargs = 2; |
| 10908 | |
| 10909 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10910 | if (dir == SMP_OPT_DIR_REQ) |
| 10911 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10912 | else |
| 10913 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10914 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10915 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10916 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10917 | lua_settable(flt_hlua->T, -3); |
| 10918 | } |
| 10919 | flt_hlua->nargs++; |
| 10920 | } |
| 10921 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10922 | if (dir == SMP_OPT_DIR_REQ) |
| 10923 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10924 | else |
| 10925 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10926 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10927 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10928 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10929 | lua_settable(flt_hlua->T, -3); |
| 10930 | } |
| 10931 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10932 | } |
| 10933 | |
| 10934 | /* Check stack size. */ |
| 10935 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10936 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10937 | RESET_SAFE_LJMP(flt_hlua); |
| 10938 | goto end; |
| 10939 | } |
| 10940 | |
| 10941 | while (extra_idx--) { |
| 10942 | lua_pushvalue(flt_hlua->T, 1); |
| 10943 | lua_remove(flt_hlua->T, 1); |
| 10944 | flt_hlua->nargs++; |
| 10945 | } |
| 10946 | |
| 10947 | /* We must initialize the execution timeouts. */ |
| 10948 | flt_hlua->max_time = hlua_timeout_session; |
| 10949 | |
| 10950 | /* At this point the execution is safe. */ |
| 10951 | RESET_SAFE_LJMP(flt_hlua); |
| 10952 | } |
| 10953 | |
| 10954 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10955 | case HLUA_E_OK: |
| 10956 | /* Catch the return value if it required */ |
| 10957 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10958 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10959 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10960 | } |
| 10961 | |
| 10962 | /* Set timeout in the required channel. */ |
| 10963 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10964 | if (dir == SMP_OPT_DIR_REQ) |
| 10965 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10966 | else |
| 10967 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10968 | } |
| 10969 | break; |
| 10970 | case HLUA_E_AGAIN: |
| 10971 | /* Set timeout in the required channel. */ |
| 10972 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10973 | if (dir == SMP_OPT_DIR_REQ) |
| 10974 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10975 | else |
| 10976 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10977 | } |
| 10978 | /* Some actions can be wake up when a "write" event |
| 10979 | * is detected on a response channel. This is useful |
| 10980 | * only for actions targeted on the requests. |
| 10981 | */ |
| 10982 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10983 | s->res.flags |= CF_WAKE_WRITE; |
| 10984 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10985 | s->req.flags |= CF_WAKE_WRITE; |
| 10986 | ret = 0; |
| 10987 | goto end; |
| 10988 | case HLUA_E_ERRMSG: |
| 10989 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10990 | ret = -1; |
| 10991 | goto end; |
| 10992 | case HLUA_E_ETMOUT: |
| 10993 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10994 | goto end; |
| 10995 | case HLUA_E_NOMEM: |
| 10996 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10997 | goto end; |
| 10998 | case HLUA_E_YIELD: |
| 10999 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 11000 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 11001 | goto end; |
| 11002 | case HLUA_E_ERR: |
| 11003 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 11004 | goto end; |
| 11005 | default: |
| 11006 | goto end; |
| 11007 | } |
| 11008 | |
| 11009 | |
| 11010 | end: |
| 11011 | return ret; |
| 11012 | } |
| 11013 | |
| 11014 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11015 | { |
| 11016 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11017 | |
| 11018 | flt_ctx->flags = 0; |
| 11019 | return hlua_filter_callback(s, filter, "start_analyze", |
| 11020 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11021 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11022 | } |
| 11023 | |
| 11024 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 11025 | { |
| 11026 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11027 | |
| 11028 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11029 | return hlua_filter_callback(s, filter, "end_analyze", |
| 11030 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11031 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 11032 | } |
| 11033 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11034 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11035 | { |
| 11036 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11037 | |
| 11038 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11039 | return hlua_filter_callback(s, filter, "http_headers", |
| 11040 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11041 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11042 | } |
| 11043 | |
| 11044 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 11045 | unsigned int offset, unsigned int len) |
| 11046 | { |
| 11047 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11048 | struct hlua *flt_hlua; |
| 11049 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11050 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11051 | int ret; |
| 11052 | |
| 11053 | flt_hlua = flt_ctx->hlua[idx]; |
| 11054 | flt_ctx->cur_off[idx] = offset; |
| 11055 | flt_ctx->cur_len[idx] = len; |
| 11056 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11057 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11058 | if (ret != -1) { |
| 11059 | ret = flt_ctx->cur_len[idx]; |
| 11060 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11061 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11062 | if (ret > flt_ctx->cur_len[idx]) |
| 11063 | ret = flt_ctx->cur_len[idx]; |
| 11064 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11065 | } |
| 11066 | } |
| 11067 | return ret; |
| 11068 | } |
| 11069 | |
| 11070 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 11071 | { |
| 11072 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11073 | |
| 11074 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 11075 | return hlua_filter_callback(s, filter, "http_end", |
| 11076 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 11077 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 11078 | } |
| 11079 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11080 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 11081 | unsigned int offset, unsigned int len) |
| 11082 | { |
| 11083 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 11084 | struct hlua *flt_hlua; |
| 11085 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 11086 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 11087 | int ret; |
| 11088 | |
| 11089 | flt_hlua = flt_ctx->hlua[idx]; |
| 11090 | flt_ctx->cur_off[idx] = offset; |
| 11091 | flt_ctx->cur_len[idx] = len; |
| 11092 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 11093 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 11094 | if (ret != -1) { |
| 11095 | ret = flt_ctx->cur_len[idx]; |
| 11096 | if (lua_gettop(flt_hlua->T) > 0) { |
| 11097 | ret = lua_tointeger(flt_hlua->T, -1); |
| 11098 | if (ret > flt_ctx->cur_len[idx]) |
| 11099 | ret = flt_ctx->cur_len[idx]; |
| 11100 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 11101 | } |
| 11102 | } |
| 11103 | return ret; |
| 11104 | } |
| 11105 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11106 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11107 | struct flt_conf *fconf, char **err, void *private) |
| 11108 | { |
| 11109 | struct hlua_reg_filter *reg_flt = private; |
| 11110 | lua_State *L; |
| 11111 | struct hlua_flt_config *conf = NULL; |
| 11112 | const char *flt_id = NULL; |
| 11113 | int state_id, pos, flt_flags = 0; |
| 11114 | struct flt_ops *hlua_flt_ops = NULL; |
| 11115 | |
| 11116 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11117 | L = hlua_states[state_id]; |
| 11118 | |
| 11119 | /* Initialize the filter ops with default callbacks */ |
| 11120 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11121 | if (!hlua_flt_ops) |
| 11122 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11123 | hlua_flt_ops->init = hlua_filter_init; |
| 11124 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11125 | if (state_id) { |
| 11126 | /* Set per-thread callback if script is loaded per-thread */ |
| 11127 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11128 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11129 | } |
| 11130 | hlua_flt_ops->attach = hlua_filter_new; |
| 11131 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11132 | |
| 11133 | /* Push the filter class on the stack and resolve all callbacks */ |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11134 | hlua_pushref(L, reg_flt->flt_ref[state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11135 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11136 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11137 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11138 | lua_pop(L, 1); |
| 11139 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11140 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11141 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11142 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11143 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11144 | lua_pop(L, 1); |
| 11145 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11146 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11147 | lua_pop(L, 1); |
| 11148 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11149 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11150 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11151 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11152 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11153 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11154 | |
| 11155 | /* Get id and flags of the filter class */ |
| 11156 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11157 | flt_id = lua_tostring(L, -1); |
| 11158 | lua_pop(L, 1); |
| 11159 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11160 | flt_flags = lua_tointeger(L, -1); |
| 11161 | lua_pop(L, 1); |
| 11162 | |
| 11163 | /* Create the filter config */ |
| 11164 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11165 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11166 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11167 | conf->reg = reg_flt; |
| 11168 | |
| 11169 | /* duplicate args */ |
| 11170 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11171 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11172 | if (!conf->args) |
| 11173 | goto error; |
| 11174 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11175 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11176 | if (!conf->args[pos]) |
| 11177 | goto error; |
| 11178 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11179 | conf->args[pos] = NULL; |
| 11180 | *cur_arg += pos + 1; |
| 11181 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11182 | if (flt_id) { |
| 11183 | fconf->id = strdup(flt_id); |
| 11184 | if (!fconf->id) |
| 11185 | goto error; |
| 11186 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11187 | fconf->flags = flt_flags; |
| 11188 | fconf->conf = conf; |
| 11189 | fconf->ops = hlua_flt_ops; |
| 11190 | |
| 11191 | lua_settop(L, 0); |
| 11192 | return 0; |
| 11193 | |
| 11194 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11195 | 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] | 11196 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11197 | if (conf && conf->args) { |
| 11198 | for (pos = 0; conf->args[pos]; pos++) |
| 11199 | free(conf->args[pos]); |
| 11200 | free(conf->args); |
| 11201 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11202 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11203 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11204 | lua_settop(L, 0); |
| 11205 | return -1; |
| 11206 | } |
| 11207 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11208 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11209 | { |
| 11210 | struct filter *filter; |
| 11211 | struct channel *chn; |
| 11212 | |
| 11213 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11214 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11215 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11216 | |
| 11217 | lua_getfield(L, 1, "__filter"); |
| 11218 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11219 | filter = lua_touserdata (L, -1); |
| 11220 | lua_pop(L, 1); |
| 11221 | |
| 11222 | register_data_filter(chn_strm(chn), chn, filter); |
| 11223 | return 1; |
| 11224 | } |
| 11225 | |
| 11226 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11227 | { |
| 11228 | struct filter *filter; |
| 11229 | struct channel *chn; |
| 11230 | |
| 11231 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11232 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11233 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11234 | |
| 11235 | lua_getfield(L, 1, "__filter"); |
| 11236 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11237 | filter = lua_touserdata (L, -1); |
| 11238 | lua_pop(L, 1); |
| 11239 | |
| 11240 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11241 | return 1; |
| 11242 | } |
| 11243 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11244 | /* 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] | 11245 | * 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] | 11246 | * parse configuration arguments. |
| 11247 | */ |
| 11248 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11249 | { |
| 11250 | struct buffer *trash; |
| 11251 | struct flt_kw_list *fkl; |
| 11252 | struct flt_kw *fkw; |
| 11253 | const char *name; |
| 11254 | struct hlua_reg_filter *reg_flt= NULL; |
| 11255 | int flt_ref, fun_ref; |
| 11256 | int len; |
| 11257 | |
| 11258 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11259 | |
Aurelien DARRAGON | 87f5297 | 2023-02-24 09:43:54 +0100 | [diff] [blame] | 11260 | if (hlua_gethlua(L)) { |
| 11261 | /* runtime processing */ |
| 11262 | WILL_LJMP(luaL_error(L, "register_filter: not available outside of body context")); |
| 11263 | } |
| 11264 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11265 | /* First argument : filter name. */ |
| 11266 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11267 | |
| 11268 | /* Second argument : The filter class */ |
| 11269 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11270 | |
| 11271 | /* Third argument : lua function. */ |
| 11272 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11273 | |
| 11274 | trash = get_trash_chunk(); |
| 11275 | chunk_printf(trash, "lua.%s", name); |
| 11276 | fkw = flt_find_kw(trash->area); |
| 11277 | if (fkw != NULL) { |
| 11278 | reg_flt = fkw->private; |
| 11279 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11280 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11281 | "This will become a hard error in version 2.5.\n", name); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11282 | if (reg_flt->flt_ref[hlua_state_id] != -1) |
| 11283 | hlua_unref(L, reg_flt->flt_ref[hlua_state_id]); |
| 11284 | if (reg_flt->fun_ref[hlua_state_id] != -1) |
| 11285 | hlua_unref(L, reg_flt->fun_ref[hlua_state_id]); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11286 | } |
| 11287 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11288 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11289 | return 0; |
| 11290 | } |
| 11291 | |
| 11292 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11293 | if (!fkl) |
| 11294 | goto alloc_error; |
| 11295 | fkl->scope = "HLUA"; |
| 11296 | |
| 11297 | reg_flt = new_hlua_reg_filter(name); |
| 11298 | if (!reg_flt) |
| 11299 | goto alloc_error; |
| 11300 | |
| 11301 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11302 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11303 | |
| 11304 | /* The filter keyword */ |
| 11305 | len = strlen("lua.") + strlen(name) + 1; |
| 11306 | fkl->kw[0].kw = calloc(1, len); |
| 11307 | if (!fkl->kw[0].kw) |
| 11308 | goto alloc_error; |
| 11309 | |
| 11310 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11311 | |
| 11312 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11313 | fkl->kw[0].private = reg_flt; |
| 11314 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11315 | |
| 11316 | /* Register this new filter */ |
| 11317 | flt_register_keywords(fkl); |
| 11318 | |
| 11319 | return 0; |
| 11320 | |
| 11321 | alloc_error: |
| 11322 | release_hlua_reg_filter(reg_flt); |
Aurelien DARRAGON | 55afbed | 2023-03-02 18:42:06 +0100 | [diff] [blame] | 11323 | hlua_unref(L, flt_ref); |
| 11324 | hlua_unref(L, fun_ref); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11325 | ha_free(&fkl); |
| 11326 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11327 | return 0; /* Never reached */ |
| 11328 | } |
| 11329 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11330 | 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] | 11331 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11332 | char **err, unsigned int *timeout) |
| 11333 | { |
| 11334 | const char *error; |
| 11335 | |
| 11336 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11337 | if (error == PARSE_TIME_OVER) { |
| 11338 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11339 | args[1], args[0]); |
| 11340 | return -1; |
| 11341 | } |
| 11342 | else if (error == PARSE_TIME_UNDER) { |
| 11343 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11344 | args[1], args[0]); |
| 11345 | return -1; |
| 11346 | } |
| 11347 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11348 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11349 | return -1; |
| 11350 | } |
| 11351 | return 0; |
| 11352 | } |
| 11353 | |
| 11354 | 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] | 11355 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11356 | char **err) |
| 11357 | { |
| 11358 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11359 | file, line, err, &hlua_timeout_session); |
| 11360 | } |
| 11361 | |
| 11362 | 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] | 11363 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11364 | char **err) |
| 11365 | { |
| 11366 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11367 | file, line, err, &hlua_timeout_task); |
| 11368 | } |
| 11369 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11370 | 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] | 11371 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11372 | char **err) |
| 11373 | { |
| 11374 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11375 | file, line, err, &hlua_timeout_applet); |
| 11376 | } |
| 11377 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11378 | 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] | 11379 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11380 | char **err) |
| 11381 | { |
| 11382 | char *error; |
| 11383 | |
| 11384 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11385 | if (*error != '\0') { |
| 11386 | memprintf(err, "%s: invalid number", args[0]); |
| 11387 | return -1; |
| 11388 | } |
| 11389 | return 0; |
| 11390 | } |
| 11391 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11392 | 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] | 11393 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11394 | char **err) |
| 11395 | { |
| 11396 | char *error; |
| 11397 | |
| 11398 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11399 | 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] | 11400 | return -1; |
| 11401 | } |
| 11402 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11403 | if (*error != '\0') { |
| 11404 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11405 | return -1; |
| 11406 | } |
| 11407 | return 0; |
| 11408 | } |
| 11409 | |
| 11410 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11411 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11412 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11413 | * the main lua entry point. |
| 11414 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11415 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11416 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11417 | * |
| 11418 | * In some error case, LUA set an error message in top of the stack. This function |
| 11419 | * 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] | 11420 | * |
| 11421 | * This function can fail with an abort() due to an Lua critical error. |
| 11422 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11423 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11424 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11425 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11426 | { |
| 11427 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11428 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11429 | |
| 11430 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11431 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11432 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11433 | 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] | 11434 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11435 | return -1; |
| 11436 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11437 | |
| 11438 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11439 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11440 | /* Check stack size. */ |
| 11441 | if (!lua_checkstack(L, 1)) { |
| 11442 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11443 | return -1; |
| 11444 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11445 | lua_pushstring(L, args[nargs]); |
| 11446 | } |
| 11447 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11448 | |
| 11449 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11450 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11451 | switch (error) { |
| 11452 | case LUA_OK: |
| 11453 | break; |
| 11454 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11455 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11456 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11457 | return -1; |
| 11458 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11459 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11460 | return -1; |
| 11461 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11462 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11463 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11464 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11465 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11466 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11467 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11468 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11469 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11470 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11471 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11472 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11473 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11474 | return -1; |
| 11475 | } |
| 11476 | |
| 11477 | return 0; |
| 11478 | } |
| 11479 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11480 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11481 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11482 | char **err) |
| 11483 | { |
| 11484 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11485 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11486 | return -1; |
| 11487 | } |
| 11488 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11489 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11490 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11491 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11492 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11493 | } |
| 11494 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11495 | 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] | 11496 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11497 | char **err) |
| 11498 | { |
| 11499 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11500 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11501 | |
| 11502 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11503 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11504 | return -1; |
| 11505 | } |
| 11506 | |
| 11507 | if (per_thread_load == NULL) { |
| 11508 | /* allocate the first entry large enough to store the final NULL */ |
| 11509 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11510 | if (per_thread_load == NULL) { |
| 11511 | memprintf(err, "out of memory error"); |
| 11512 | return -1; |
| 11513 | } |
| 11514 | } |
| 11515 | |
| 11516 | /* count used entries */ |
| 11517 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11518 | ; |
| 11519 | |
| 11520 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11521 | if (per_thread_load == NULL) { |
| 11522 | memprintf(err, "out of memory error"); |
| 11523 | return -1; |
| 11524 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11525 | per_thread_load[len + 1] = NULL; |
| 11526 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11527 | /* count args excepting the first, allocate array and copy args */ |
| 11528 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11529 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11530 | if (per_thread_load[len] == NULL) { |
| 11531 | memprintf(err, "out of memory error"); |
| 11532 | return -1; |
| 11533 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11534 | for (i = 1; *(args[i]) != 0; i++) { |
| 11535 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11536 | if (per_thread_load[len][i - 1] == NULL) { |
| 11537 | memprintf(err, "out of memory error"); |
| 11538 | return -1; |
| 11539 | } |
| 11540 | } |
| 11541 | per_thread_load[len][i - 1] = strdup(""); |
| 11542 | if (per_thread_load[len][i - 1] == NULL) { |
| 11543 | memprintf(err, "out of memory error"); |
| 11544 | return -1; |
| 11545 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11546 | |
| 11547 | /* loading for thread 1 only */ |
| 11548 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11549 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11550 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11551 | } |
| 11552 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11553 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11554 | * in the given <ctx>. |
| 11555 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11556 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11557 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11558 | lua_getglobal(L, "package"); /* push package variable */ |
| 11559 | lua_pushstring(L, path); /* push given path */ |
| 11560 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11561 | lua_getfield(L, -3, type); /* push old path */ |
| 11562 | lua_concat(L, 3); /* concatenate to new path */ |
| 11563 | lua_setfield(L, -2, type); /* store new path */ |
| 11564 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11565 | |
| 11566 | return 0; |
| 11567 | } |
| 11568 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11569 | 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] | 11570 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11571 | char **err) |
| 11572 | { |
| 11573 | char *path; |
| 11574 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11575 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11576 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11577 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11578 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11579 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11580 | } |
| 11581 | |
| 11582 | if (!(*args[1])) { |
| 11583 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11584 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11585 | } |
| 11586 | path = args[1]; |
| 11587 | |
| 11588 | if (*args[2]) { |
| 11589 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11590 | 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] | 11591 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11592 | } |
| 11593 | type = args[2]; |
| 11594 | } |
| 11595 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11596 | p = calloc(1, sizeof(*p)); |
| 11597 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11598 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11599 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11600 | } |
| 11601 | p->path = strdup(path); |
| 11602 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11603 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11604 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11605 | } |
| 11606 | p->type = strdup(type); |
| 11607 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11608 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11609 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11610 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11611 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11612 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11613 | /* Handle the global state and the per-thread state for the first |
| 11614 | * thread. The remaining threads will be initialized based on |
| 11615 | * prepend_path_list. |
| 11616 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11617 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11618 | lua_State *L = hlua_states[i]; |
| 11619 | const char *error; |
| 11620 | |
| 11621 | if (setjmp(safe_ljmp_env) != 0) { |
| 11622 | lua_atpanic(L, hlua_panic_safe); |
| 11623 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11624 | error = lua_tostring(L, -1); |
| 11625 | else |
| 11626 | error = "critical error"; |
| 11627 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11628 | exit(1); |
| 11629 | } else { |
| 11630 | lua_atpanic(L, hlua_panic_ljmp); |
| 11631 | } |
| 11632 | |
| 11633 | hlua_prepend_path(L, type, path); |
| 11634 | |
| 11635 | lua_atpanic(L, hlua_panic_safe); |
| 11636 | } |
| 11637 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11638 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11639 | |
| 11640 | err2: |
| 11641 | free(p->type); |
| 11642 | free(p->path); |
| 11643 | err: |
| 11644 | free(p); |
| 11645 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11646 | } |
| 11647 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11648 | /* configuration keywords declaration */ |
| 11649 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11650 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11651 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11652 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11653 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11654 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11655 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11656 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11657 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11658 | { 0, NULL, NULL }, |
| 11659 | }}; |
| 11660 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11661 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11662 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11663 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11664 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11665 | /* |
| 11666 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11667 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11668 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11669 | * way. |
| 11670 | */ |
| 11671 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11672 | { |
| 11673 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11674 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11675 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11676 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11677 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11678 | struct hlua *hlua; |
| 11679 | char *err = NULL; |
| 11680 | int y = 1; |
| 11681 | |
| 11682 | hlua = hlua_gethlua(L); |
| 11683 | |
| 11684 | /* get the first ckchi to copy */ |
| 11685 | if (ckchi == NULL) |
| 11686 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11687 | |
| 11688 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11689 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11690 | struct ckch_inst *new_inst; |
| 11691 | |
| 11692 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11693 | if (y % 10 == 0) { |
| 11694 | |
| 11695 | *lua_ckchi = ckchi; |
| 11696 | |
| 11697 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11698 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11699 | } |
| 11700 | |
| 11701 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11702 | goto error; |
| 11703 | |
| 11704 | /* link the new ckch_inst to the duplicate */ |
| 11705 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11706 | y++; |
| 11707 | } |
| 11708 | |
| 11709 | /* The generation is finished, we can insert everything */ |
| 11710 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11711 | |
| 11712 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11713 | |
| 11714 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11715 | |
| 11716 | return 0; |
| 11717 | |
| 11718 | error: |
| 11719 | ckch_store_free(new_ckchs); |
| 11720 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11721 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11722 | free(err); |
| 11723 | |
| 11724 | return 0; |
| 11725 | } |
| 11726 | |
| 11727 | /* |
| 11728 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11729 | * from the table in parameter. |
| 11730 | * |
| 11731 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11732 | * means it does not need to have a transaction since everything is done in the |
| 11733 | * same function. |
| 11734 | * |
| 11735 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11736 | * |
| 11737 | */ |
| 11738 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11739 | { |
| 11740 | struct hlua *hlua; |
| 11741 | struct ckch_inst **lua_ckchi; |
| 11742 | struct ckch_store **lua_ckchs; |
| 11743 | struct ckch_store *old_ckchs = NULL; |
| 11744 | struct ckch_store *new_ckchs = NULL; |
| 11745 | int errcode = 0; |
| 11746 | char *err = NULL; |
| 11747 | struct cert_exts *cert_ext = NULL; |
| 11748 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11749 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11750 | int ret; |
| 11751 | |
| 11752 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11753 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11754 | |
| 11755 | hlua = hlua_gethlua(L); |
| 11756 | |
| 11757 | /* FIXME: this should not return an error but should come back later */ |
| 11758 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11759 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11760 | |
| 11761 | ret = lua_getfield(L, -1, "filename"); |
| 11762 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11763 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11764 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11765 | goto end; |
| 11766 | } |
| 11767 | filename = (char *)lua_tostring(L, -1); |
| 11768 | |
| 11769 | |
| 11770 | /* look for the filename in the tree */ |
| 11771 | old_ckchs = ckchs_lookup(filename); |
| 11772 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11773 | 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] | 11774 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11775 | goto end; |
| 11776 | } |
| 11777 | /* TODO: handle extra_files_noext */ |
| 11778 | |
| 11779 | new_ckchs = ckchs_dup(old_ckchs); |
| 11780 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11781 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11782 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11783 | goto end; |
| 11784 | } |
| 11785 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11786 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11787 | |
| 11788 | /* loop on the field in the table, which have the same name as the |
| 11789 | * possible extensions of files */ |
| 11790 | lua_pushnil(L); |
| 11791 | while (lua_next(L, 1)) { |
| 11792 | int i; |
| 11793 | const char *field = lua_tostring(L, -2); |
| 11794 | char *payload = (char *)lua_tostring(L, -1); |
| 11795 | |
| 11796 | if (!field || strcmp(field, "filename") == 0) { |
| 11797 | lua_pop(L, 1); |
| 11798 | continue; |
| 11799 | } |
| 11800 | |
| 11801 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11802 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11803 | cert_ext = &cert_exts[i]; |
| 11804 | break; |
| 11805 | } |
| 11806 | } |
| 11807 | |
| 11808 | /* this is the default type, the field is not supported */ |
| 11809 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11810 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11811 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11812 | goto end; |
| 11813 | } |
| 11814 | |
| 11815 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11816 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11817 | 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] | 11818 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11819 | goto end; |
| 11820 | } |
| 11821 | lua_pop(L, 1); |
| 11822 | } |
| 11823 | |
| 11824 | /* store the pointers on the lua stack */ |
| 11825 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11826 | lua_ckchs[0] = old_ckchs; |
| 11827 | lua_ckchs[1] = new_ckchs; |
| 11828 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11829 | *lua_ckchi = NULL; |
| 11830 | |
| 11831 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11832 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11833 | |
| 11834 | end: |
| 11835 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11836 | |
| 11837 | if (errcode & ERR_CODE) { |
| 11838 | ckch_store_free(new_ckchs); |
| 11839 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11840 | } |
| 11841 | free(err); |
| 11842 | |
| 11843 | return 0; |
| 11844 | } |
| 11845 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11846 | #else |
| 11847 | |
| 11848 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11849 | { |
| 11850 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11851 | |
| 11852 | return 0; |
| 11853 | } |
| 11854 | #endif /* ! USE_OPENSSL */ |
| 11855 | |
| 11856 | |
| 11857 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11858 | /* This function can fail with an abort() due to an Lua critical error. |
| 11859 | * We are in the initialisation process of HAProxy, this abort() is |
| 11860 | * tolerated. |
| 11861 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11862 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11863 | { |
| 11864 | struct hlua_init_function *init; |
| 11865 | const char *msg; |
| 11866 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11867 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11868 | const char *kind; |
| 11869 | const char *trace; |
| 11870 | int return_status = 1; |
| 11871 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11872 | int nres; |
| 11873 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11874 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11875 | /* disable memory limit checks if limit is not set */ |
| 11876 | if (!hlua_global_allocator.limit) |
| 11877 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11878 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11879 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11880 | if (setjmp(safe_ljmp_env) != 0) { |
| 11881 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11882 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11883 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11884 | else |
| 11885 | error = "critical error"; |
| 11886 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11887 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11888 | } else { |
| 11889 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11890 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11891 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11892 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Aurelien DARRAGON | 4fdf8b5 | 2023-03-20 17:22:37 +0100 | [diff] [blame] | 11893 | hlua_pushref(L, init->function_ref); |
Aurelien DARRAGON | 16d047b | 2023-03-20 16:29:55 +0100 | [diff] [blame] | 11894 | /* function ref should be released right away since it was pushed |
| 11895 | * on the stack and will not be used anymore |
| 11896 | */ |
| 11897 | hlua_unref(L, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11898 | |
| 11899 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11900 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11901 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11902 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11903 | #endif |
| 11904 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11905 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11906 | |
| 11907 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11908 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11909 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11910 | |
| 11911 | case LUA_ERRERR: |
| 11912 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11913 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11914 | case LUA_ERRRUN: |
| 11915 | if (!kind) |
| 11916 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11917 | msg = lua_tostring(L, -1); |
| 11918 | lua_settop(L, 0); /* Empty the stack. */ |
| 11919 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11920 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11921 | if (msg) |
| 11922 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11923 | else |
| 11924 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11925 | return_status = 0; |
| 11926 | break; |
| 11927 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11928 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11929 | /* Unknown error */ |
| 11930 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11931 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11932 | case LUA_YIELD: |
| 11933 | /* yield is not configured at this step, this state doesn't happen */ |
| 11934 | if (!kind) |
| 11935 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11936 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11937 | case LUA_ERRMEM: |
| 11938 | if (!kind) |
| 11939 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11940 | lua_settop(L, 0); |
| 11941 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11942 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11943 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11944 | return_status = 0; |
| 11945 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11946 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11947 | if (!return_status) |
| 11948 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11949 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11950 | |
| 11951 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11952 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11953 | } |
| 11954 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11955 | int hlua_post_init() |
| 11956 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11957 | int ret; |
| 11958 | int i; |
| 11959 | int errors; |
| 11960 | char *err = NULL; |
| 11961 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11962 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11963 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11964 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11965 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11966 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11967 | int saved_used_backed = global.ssl_used_backend; |
| 11968 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11969 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11970 | global.ssl_used_backend = saved_used_backed; |
| 11971 | } |
| 11972 | #endif |
| 11973 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11974 | /* Perform post init of common thread */ |
| 11975 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11976 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11977 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11978 | if (ret == 0) |
| 11979 | return 0; |
| 11980 | |
| 11981 | /* init remaining lua states and load files */ |
| 11982 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11983 | |
| 11984 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11985 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11986 | |
| 11987 | /* Init lua state */ |
| 11988 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11989 | |
| 11990 | /* Load lua files */ |
| 11991 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11992 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11993 | if (ret != 0) { |
| 11994 | ha_alert("Lua init: %s\n", err); |
| 11995 | return 0; |
| 11996 | } |
| 11997 | } |
| 11998 | } |
| 11999 | |
| 12000 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12001 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12002 | |
| 12003 | /* Execute post init for all states */ |
| 12004 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 12005 | |
| 12006 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12007 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12008 | |
| 12009 | /* run post init */ |
| 12010 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 12011 | if (ret == 0) |
| 12012 | return 0; |
| 12013 | } |
| 12014 | |
| 12015 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12016 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12017 | |
| 12018 | /* control functions registering. Each function must have: |
| 12019 | * - only the function_ref[0] set positive and all other to -1 |
| 12020 | * - only the function_ref[0] set to -1 and all other positive |
| 12021 | * This ensure a same reference is not used both in shared |
| 12022 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12023 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12024 | * complicated to found for the end user. |
| 12025 | */ |
| 12026 | errors = 0; |
| 12027 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 12028 | ret = 0; |
| 12029 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12030 | if (fcn->function_ref[i] == -1) |
| 12031 | ret--; |
| 12032 | else |
| 12033 | ret++; |
| 12034 | } |
| 12035 | if (abs(ret) != global.nbthread) { |
| 12036 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 12037 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 12038 | errors++; |
| 12039 | continue; |
| 12040 | } |
| 12041 | |
| 12042 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12043 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12044 | "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] | 12045 | "exclusive.\n", fcn->name); |
| 12046 | errors++; |
| 12047 | } |
| 12048 | } |
| 12049 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12050 | /* Do the same with registered filters */ |
| 12051 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 12052 | ret = 0; |
| 12053 | for (i = 1; i < global.nbthread + 1; i++) { |
| 12054 | if (reg_flt->flt_ref[i] == -1) |
| 12055 | ret--; |
| 12056 | else |
| 12057 | ret++; |
| 12058 | } |
| 12059 | if (abs(ret) != global.nbthread) { |
| 12060 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 12061 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 12062 | errors++; |
| 12063 | continue; |
| 12064 | } |
| 12065 | |
| 12066 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 12067 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 12068 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 12069 | "exclusive.\n", fcn->name); |
| 12070 | errors++; |
| 12071 | } |
| 12072 | } |
| 12073 | |
| 12074 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12075 | if (errors > 0) |
| 12076 | return 0; |
| 12077 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 12078 | /* 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] | 12079 | * -1 in order to have probably a segfault if someone use it |
| 12080 | */ |
| 12081 | hlua_state_id = -1; |
| 12082 | |
| 12083 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 12084 | } |
| 12085 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12086 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 12087 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 12088 | * 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] | 12089 | * allocation. <nsize> is the requested new size. A new allocation is |
| 12090 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12091 | * zero. This one verifies that the limits are respected but is optimized |
| 12092 | * 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] | 12093 | * |
| 12094 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 12095 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 12096 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 12097 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 12098 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12099 | */ |
| 12100 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 12101 | { |
| 12102 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12103 | size_t limit, old, new; |
| 12104 | |
| 12105 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 12106 | * for accounting anymore. |
| 12107 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12108 | if (likely(~zone->limit == 0)) { |
| 12109 | if (!nsize) |
| 12110 | ha_free(&ptr); |
| 12111 | else |
| 12112 | ptr = realloc(ptr, nsize); |
| 12113 | return ptr; |
| 12114 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12115 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12116 | if (!ptr) |
| 12117 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12118 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12119 | /* enforce strict limits across all threads */ |
| 12120 | limit = zone->limit; |
| 12121 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12122 | do { |
| 12123 | new = old + nsize - osize; |
| 12124 | if (unlikely(nsize && limit && new > limit)) |
| 12125 | return NULL; |
| 12126 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12127 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12128 | if (!nsize) |
| 12129 | ha_free(&ptr); |
| 12130 | else |
| 12131 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12132 | |
| 12133 | if (unlikely(!ptr && nsize)) // failed |
| 12134 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12135 | |
| 12136 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12137 | return ptr; |
| 12138 | } |
| 12139 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12140 | /* 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] | 12141 | * We are in the initialisation process of HAProxy, this abort() is |
| 12142 | * tolerated. |
| 12143 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12144 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12145 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12146 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12147 | int idx; |
| 12148 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12149 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12150 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12151 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12152 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12153 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12154 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12155 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12156 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12157 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12158 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12159 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12160 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12161 | *context = NULL; |
| 12162 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12163 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12164 | * the Lua function can fail with an abort. We are in the initialisation |
| 12165 | * process of HAProxy, this abort() is tolerated. |
| 12166 | */ |
| 12167 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12168 | /* Call post initialisation function in safe environment. */ |
| 12169 | if (setjmp(safe_ljmp_env) != 0) { |
| 12170 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12171 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12172 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12173 | else |
| 12174 | error_msg = "critical error"; |
| 12175 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12176 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12177 | } else { |
| 12178 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12179 | } |
| 12180 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12181 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12182 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12183 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12184 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12185 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12186 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12187 | #endif |
| 12188 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12189 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12190 | #endif |
| 12191 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12192 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12193 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12194 | /* Apply configured prepend path */ |
| 12195 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12196 | hlua_prepend_path(L, pp->type, pp->path); |
| 12197 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12198 | /* |
| 12199 | * |
| 12200 | * Create "core" object. |
| 12201 | * |
| 12202 | */ |
| 12203 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12204 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12205 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12206 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12207 | /* set the thread id */ |
| 12208 | hlua_class_const_int(L, "thread", thread_num); |
| 12209 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12210 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12211 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12212 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12213 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12214 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12215 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12216 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12217 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12218 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12219 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12220 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12221 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12222 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12223 | hlua_class_function(L, "yield", hlua_yield); |
| 12224 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12225 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12226 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12227 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12228 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12229 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12230 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12231 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12232 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12233 | hlua_class_function(L, "log", hlua_log); |
| 12234 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12235 | hlua_class_function(L, "Info", hlua_log_info); |
| 12236 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12237 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12238 | hlua_class_function(L, "done", hlua_done); |
| 12239 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12240 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12241 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12242 | |
| 12243 | /* |
| 12244 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12245 | * Create "act" object. |
| 12246 | * |
| 12247 | */ |
| 12248 | |
| 12249 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12250 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12251 | |
| 12252 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12253 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12254 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12255 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12256 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12257 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12258 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12259 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12260 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12261 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12262 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12263 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12264 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12265 | |
| 12266 | /* |
| 12267 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12268 | * Create "Filter" object. |
| 12269 | * |
| 12270 | */ |
| 12271 | |
| 12272 | /* This table entry is the object "filter" base. */ |
| 12273 | lua_newtable(L); |
| 12274 | |
| 12275 | /* push flags and constants */ |
| 12276 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12277 | hlua_class_const_int(L, "WAIT", 0); |
| 12278 | hlua_class_const_int(L, "ERROR", -1); |
| 12279 | |
| 12280 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12281 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12282 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12283 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12284 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12285 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12286 | lua_setglobal(L, "filter"); |
| 12287 | |
| 12288 | /* |
| 12289 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12290 | * Register class Map |
| 12291 | * |
| 12292 | */ |
| 12293 | |
| 12294 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12295 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12296 | |
| 12297 | /* register pattern types. */ |
| 12298 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12299 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12300 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12301 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12302 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12303 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12304 | |
| 12305 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12306 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12307 | |
| 12308 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12309 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12310 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12311 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12312 | lua_pushstring(L, "__index"); |
| 12313 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12314 | |
| 12315 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12316 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12317 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12318 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12319 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12320 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12321 | /* Register previous table in the registry with reference and named entry. |
| 12322 | * The function hlua_register_metatable() pops the stack, so we |
| 12323 | * previously create a copy of the table. |
| 12324 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12325 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12326 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12327 | |
| 12328 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12329 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12330 | |
| 12331 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12332 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12333 | |
| 12334 | /* |
| 12335 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12336 | * Register "CertCache" class |
| 12337 | * |
| 12338 | */ |
| 12339 | |
| 12340 | /* Create and fill the metatable. */ |
| 12341 | lua_newtable(L); |
| 12342 | /* Register */ |
| 12343 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12344 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12345 | |
| 12346 | /* |
| 12347 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12348 | * Register class Channel |
| 12349 | * |
| 12350 | */ |
| 12351 | |
| 12352 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12353 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12354 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12355 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12356 | lua_pushstring(L, "__index"); |
| 12357 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12358 | |
| 12359 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12360 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12361 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12362 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12363 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12364 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12365 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12366 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12367 | hlua_class_function(L, "send", hlua_channel_send); |
| 12368 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12369 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12370 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12371 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12372 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12373 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12374 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12375 | /* Deprecated API */ |
| 12376 | hlua_class_function(L, "get", hlua_channel_get); |
| 12377 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12378 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12379 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12380 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12381 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12382 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12383 | |
| 12384 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12385 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12386 | |
| 12387 | /* |
| 12388 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12389 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12390 | * |
| 12391 | */ |
| 12392 | |
| 12393 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12394 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12395 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12396 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12397 | lua_pushstring(L, "__index"); |
| 12398 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12399 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12400 | /* Browse existing fetches and create the associated |
| 12401 | * object method. |
| 12402 | */ |
| 12403 | sf = NULL; |
| 12404 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12405 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12406 | * by an underscore. |
| 12407 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12408 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12409 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12410 | if (*p == '.' || *p == '-' || *p == '+') |
| 12411 | *p = '_'; |
| 12412 | |
| 12413 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12414 | lua_pushstring(L, trash.area); |
| 12415 | lua_pushlightuserdata(L, sf); |
| 12416 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12417 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12418 | } |
| 12419 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12420 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12421 | |
| 12422 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12423 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12424 | |
| 12425 | /* |
| 12426 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12427 | * Register class Converters |
| 12428 | * |
| 12429 | */ |
| 12430 | |
| 12431 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12432 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12433 | |
| 12434 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12435 | lua_pushstring(L, "__index"); |
| 12436 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12437 | |
| 12438 | /* Browse existing converters and create the associated |
| 12439 | * object method. |
| 12440 | */ |
| 12441 | sc = NULL; |
| 12442 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12443 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12444 | * by an underscore. |
| 12445 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12446 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12447 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12448 | if (*p == '.' || *p == '-' || *p == '+') |
| 12449 | *p = '_'; |
| 12450 | |
| 12451 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12452 | lua_pushstring(L, trash.area); |
| 12453 | lua_pushlightuserdata(L, sc); |
| 12454 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12455 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12456 | } |
| 12457 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12458 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12459 | |
| 12460 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12461 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12462 | |
| 12463 | /* |
| 12464 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12465 | * Register class HTTP |
| 12466 | * |
| 12467 | */ |
| 12468 | |
| 12469 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12470 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12471 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12472 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12473 | lua_pushstring(L, "__index"); |
| 12474 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12475 | |
| 12476 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12477 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12478 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12479 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12480 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12481 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12482 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12483 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12484 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12485 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12486 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12487 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12488 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12489 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12490 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12491 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12492 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12493 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12494 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12495 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12496 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12497 | |
| 12498 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12499 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12500 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12501 | /* |
| 12502 | * |
| 12503 | * Register class HTTPMessage |
| 12504 | * |
| 12505 | */ |
| 12506 | |
| 12507 | /* Create and fill the metatable. */ |
| 12508 | lua_newtable(L); |
| 12509 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12510 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12511 | lua_pushstring(L, "__index"); |
| 12512 | lua_newtable(L); |
| 12513 | |
| 12514 | /* Register Lua functions. */ |
| 12515 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12516 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12517 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12518 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12519 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12520 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12521 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12522 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12523 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12524 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12525 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12526 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12527 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12528 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12529 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12530 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12531 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12532 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12533 | |
| 12534 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12535 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12536 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12537 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12538 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12539 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12540 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12541 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12542 | |
| 12543 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12544 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12545 | |
| 12546 | lua_rawset(L, -3); |
| 12547 | |
| 12548 | /* Register previous table in the registry with reference and named entry. */ |
| 12549 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12550 | |
| 12551 | /* |
| 12552 | * |
| 12553 | * Register class HTTPClient |
| 12554 | * |
| 12555 | */ |
| 12556 | |
| 12557 | /* Create and fill the metatable. */ |
| 12558 | lua_newtable(L); |
| 12559 | lua_pushstring(L, "__index"); |
| 12560 | lua_newtable(L); |
| 12561 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12562 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12563 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12564 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12565 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12566 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12567 | /* Register the garbage collector entry. */ |
| 12568 | lua_pushstring(L, "__gc"); |
| 12569 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12570 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12571 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12572 | |
| 12573 | |
| 12574 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12575 | /* |
| 12576 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12577 | * Register class AppletTCP |
| 12578 | * |
| 12579 | */ |
| 12580 | |
| 12581 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12582 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12583 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12584 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12585 | lua_pushstring(L, "__index"); |
| 12586 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12587 | |
| 12588 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12589 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12590 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12591 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12592 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12593 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12594 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12595 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12596 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12597 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12598 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12599 | |
| 12600 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12601 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12602 | |
| 12603 | /* |
| 12604 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12605 | * Register class AppletHTTP |
| 12606 | * |
| 12607 | */ |
| 12608 | |
| 12609 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12610 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12611 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12612 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12613 | lua_pushstring(L, "__index"); |
| 12614 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12615 | |
| 12616 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12617 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12618 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12619 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12620 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12621 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12622 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12623 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12624 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12625 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12626 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12627 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12628 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12629 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12630 | |
| 12631 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12632 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12633 | |
| 12634 | /* |
| 12635 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12636 | * Register class TXN |
| 12637 | * |
| 12638 | */ |
| 12639 | |
| 12640 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12641 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12642 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12643 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12644 | lua_pushstring(L, "__index"); |
| 12645 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12646 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12647 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12648 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12649 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12650 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12651 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12652 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12653 | hlua_class_function(L, "done", hlua_txn_done); |
| 12654 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12655 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12656 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12657 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12658 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12659 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12660 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12661 | hlua_class_function(L, "log", hlua_txn_log); |
| 12662 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12663 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12664 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12665 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12666 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12667 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12668 | |
| 12669 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12670 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12671 | |
| 12672 | /* |
| 12673 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12674 | * Register class reply |
| 12675 | * |
| 12676 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12677 | lua_newtable(L); |
| 12678 | lua_pushstring(L, "__index"); |
| 12679 | lua_newtable(L); |
| 12680 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12681 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12682 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12683 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12684 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12685 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12686 | |
| 12687 | |
| 12688 | /* |
| 12689 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12690 | * Register class Socket |
| 12691 | * |
| 12692 | */ |
| 12693 | |
| 12694 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12695 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12696 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12697 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12698 | lua_pushstring(L, "__index"); |
| 12699 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12700 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12701 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12702 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12703 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12704 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12705 | hlua_class_function(L, "send", hlua_socket_send); |
| 12706 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12707 | hlua_class_function(L, "close", hlua_socket_close); |
| 12708 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12709 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12710 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12711 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12712 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12713 | 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] | 12714 | |
| 12715 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12716 | lua_pushstring(L, "__gc"); |
| 12717 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12718 | 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] | 12719 | |
| 12720 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12721 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12722 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12723 | lua_atpanic(L, hlua_panic_safe); |
| 12724 | |
| 12725 | return L; |
| 12726 | } |
| 12727 | |
| 12728 | void hlua_init(void) { |
| 12729 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12730 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12731 | #ifdef USE_OPENSSL |
| 12732 | struct srv_kw *kw; |
| 12733 | int tmp_error; |
| 12734 | char *error; |
| 12735 | char *args[] = { /* SSL client configuration. */ |
| 12736 | "ssl", |
| 12737 | "verify", |
| 12738 | "none", |
| 12739 | NULL |
| 12740 | }; |
| 12741 | #endif |
| 12742 | |
| 12743 | /* Init post init function list head */ |
| 12744 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12745 | LIST_INIT(&hlua_init_functions[i]); |
| 12746 | |
| 12747 | /* Init state for common/shared lua parts */ |
| 12748 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12749 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12750 | hlua_states[0] = hlua_init_state(0); |
| 12751 | |
| 12752 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12753 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12754 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12755 | hlua_states[1] = hlua_init_state(1); |
| 12756 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12757 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12758 | 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] | 12759 | if (!socket_proxy) { |
| 12760 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12761 | exit(1); |
| 12762 | } |
| 12763 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12764 | |
| 12765 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12766 | socket_tcp = new_server(socket_proxy); |
| 12767 | if (!socket_tcp) { |
| 12768 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12769 | exit(1); |
| 12770 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12771 | |
| 12772 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12773 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12774 | socket_ssl = new_server(socket_proxy); |
| 12775 | if (!socket_ssl) { |
| 12776 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12777 | exit(1); |
| 12778 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12779 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12780 | socket_ssl->use_ssl = 1; |
| 12781 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12782 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12783 | for (i = 0; args[i] != NULL; i++) { |
| 12784 | 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] | 12785 | /* |
| 12786 | * |
| 12787 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12788 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12789 | * features like client certificates and ssl_verify. |
| 12790 | * |
| 12791 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12792 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12793 | if (tmp_error != 0) { |
| 12794 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12795 | abort(); /* This must be never arrives because the command line |
| 12796 | not editable by the user. */ |
| 12797 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12798 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12799 | } |
| 12800 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12801 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12802 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12803 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12804 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12805 | static void hlua_deinit() |
| 12806 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12807 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12808 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12809 | |
| 12810 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12811 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12812 | |
| 12813 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12814 | if (hlua_states[thr]) |
| 12815 | lua_close(hlua_states[thr]); |
| 12816 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12817 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12818 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12819 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12820 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12821 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12822 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12823 | |
| 12824 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12825 | } |
| 12826 | |
| 12827 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12828 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12829 | static void hlua_register_build_options(void) |
| 12830 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12831 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12832 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12833 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12834 | hap_register_build_opts(ptr, 1); |
| 12835 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12836 | |
| 12837 | INITCALL0(STG_REGISTER, hlua_register_build_options); |