Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 54 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 55 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 56 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
| 58 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 59 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 60 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
| 68 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 148 | static char ***per_thread_load = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
| 155 | void lua_take_global_lock() |
| 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 165 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | ({ \ |
| 167 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 168 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 169 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 170 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 171 | lua_atpanic(__L, hlua_panic_safe); \ |
| 172 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 173 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 174 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 175 | } else { \ |
| 176 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 177 | ret = 1; \ |
| 178 | } \ |
| 179 | ret; \ |
| 180 | }) |
| 181 | |
| 182 | /* If we are the last function catching Lua errors, we |
| 183 | * must reset the panic function. |
| 184 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | do { \ |
| 187 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 188 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 189 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | } while(0) |
| 191 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 192 | #define SET_SAFE_LJMP(__HLUA) \ |
| 193 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 196 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 197 | |
| 198 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
| 201 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 202 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 204 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 206 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 208 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 209 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 210 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 211 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 212 | /* The main Lua execution context. The 0 index is the |
| 213 | * common state shared by all threads. |
| 214 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 215 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 216 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 217 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 218 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 219 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 220 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 221 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 222 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 223 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 224 | struct hlua_reg_filter { |
| 225 | char *name; |
| 226 | int flt_ref[MAX_THREADS + 1]; |
| 227 | int fun_ref[MAX_THREADS + 1]; |
| 228 | struct list l; |
| 229 | }; |
| 230 | |
| 231 | struct hlua_flt_config { |
| 232 | struct hlua_reg_filter *reg; |
| 233 | int ref[MAX_THREADS + 1]; |
| 234 | char **args; |
| 235 | }; |
| 236 | |
| 237 | struct hlua_flt_ctx { |
| 238 | int ref; /* ref to the filter lua object */ |
| 239 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 240 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 241 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 242 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 243 | }; |
| 244 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 245 | /* appctx context used by the cosockets */ |
| 246 | struct hlua_csk_ctx { |
| 247 | int connected; |
| 248 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 249 | struct list wake_on_read; |
| 250 | struct list wake_on_write; |
| 251 | struct appctx *appctx; |
| 252 | int die; |
| 253 | }; |
| 254 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 255 | /* appctx context used by TCP services */ |
| 256 | struct hlua_tcp_ctx { |
| 257 | struct hlua *hlua; |
| 258 | int flags; |
| 259 | struct task *task; |
| 260 | }; |
| 261 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 262 | /* appctx context used by HTTP services */ |
| 263 | struct hlua_http_ctx { |
| 264 | struct hlua *hlua; |
| 265 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 266 | int flags; |
| 267 | int status; |
| 268 | const char *reason; |
| 269 | struct task *task; |
| 270 | }; |
| 271 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 272 | /* used by registered CLI keywords */ |
| 273 | struct hlua_cli_ctx { |
| 274 | struct hlua *hlua; |
| 275 | struct task *task; |
| 276 | struct hlua_function *fcn; |
| 277 | }; |
| 278 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 279 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 280 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 281 | static int hlua_filter_from_payload(struct filter *filter); |
| 282 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 283 | /* This is the chained list of struct hlua_flt referenced |
| 284 | * for haproxy filters. It is used for a post-initialisation control. |
| 285 | */ |
| 286 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 287 | |
| 288 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 289 | /* This is the memory pool containing struct lua for applets |
| 290 | * (including cli). |
| 291 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 292 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 294 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 295 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 296 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 297 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 298 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 301 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 302 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 303 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 304 | /* The following variables contains the reference of the different |
| 305 | * Lua classes. These references are useful for identify metadata |
| 306 | * associated with an object. |
| 307 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 308 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 309 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 310 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 311 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 312 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 313 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 314 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 315 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 316 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 317 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 318 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 319 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 320 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 321 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 322 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 323 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 324 | * because a task may remain alive during all the haproxy execution. |
| 325 | */ |
| 326 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 327 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 328 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 329 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 330 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 331 | * it is used for preventing infinite loops. |
| 332 | * |
| 333 | * I test the scheer with an infinite loop containing one incrementation |
| 334 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 335 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 336 | * to one interrupt each 10 000 instructions. |
| 337 | * |
| 338 | * configured | Number of |
| 339 | * instructions | loops executed |
| 340 | * between two | in milions |
| 341 | * forced yields | |
| 342 | * ---------------+--------------- |
| 343 | * 10 | 160 |
| 344 | * 500 | 670 |
| 345 | * 1000 | 680 |
| 346 | * 5000 | 700 |
| 347 | * 7000 | 700 |
| 348 | * 8000 | 700 |
| 349 | * 9000 | 710 <- ceil |
| 350 | * 10000 | 710 |
| 351 | * 100000 | 710 |
| 352 | * 1000000 | 710 |
| 353 | * |
| 354 | */ |
| 355 | static unsigned int hlua_nb_instruction = 10000; |
| 356 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 357 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 358 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 359 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 360 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 361 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 362 | */ |
| 363 | struct hlua_mem_allocator { |
| 364 | size_t allocated; |
| 365 | size_t limit; |
| 366 | }; |
| 367 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 368 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 369 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 370 | /* These functions converts types between HAProxy internal args or |
| 371 | * sample and LUA types. Another function permits to check if the |
| 372 | * LUA stack contains arguments according with an required ARG_T |
| 373 | * format. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 376 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 377 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 378 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 380 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 381 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 382 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 383 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 384 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 385 | struct prepend_path { |
| 386 | struct list l; |
| 387 | char *type; |
| 388 | char *path; |
| 389 | }; |
| 390 | |
| 391 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 392 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 393 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 394 | do { \ |
| 395 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 396 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 397 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 398 | } while (0) |
| 399 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 400 | static inline struct hlua_function *new_hlua_function() |
| 401 | { |
| 402 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 403 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 404 | |
| 405 | fcn = calloc(1, sizeof(*fcn)); |
| 406 | if (!fcn) |
| 407 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 408 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 409 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 410 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 411 | return fcn; |
| 412 | } |
| 413 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 414 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 415 | { |
| 416 | if (!fcn) |
| 417 | return; |
| 418 | if (fcn->name) |
| 419 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 420 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 421 | ha_free(&fcn); |
| 422 | } |
| 423 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 424 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 425 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 426 | { |
| 427 | if (fcn->function_ref[0] == -1) |
| 428 | return tid + 1; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 432 | /* Create a new registered filter. Only its name is filled */ |
| 433 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 434 | { |
| 435 | struct hlua_reg_filter *reg_flt; |
| 436 | int i; |
| 437 | |
| 438 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 439 | if (!reg_flt) |
| 440 | return NULL; |
| 441 | reg_flt->name = strdup(name); |
| 442 | if (!reg_flt->name) { |
| 443 | free(reg_flt); |
| 444 | return NULL; |
| 445 | } |
| 446 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 447 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 448 | reg_flt->flt_ref[i] = -1; |
| 449 | reg_flt->fun_ref[i] = -1; |
| 450 | } |
| 451 | return reg_flt; |
| 452 | } |
| 453 | |
| 454 | /* Release a registered filter */ |
| 455 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 456 | { |
| 457 | if (!reg_flt) |
| 458 | return; |
| 459 | if (reg_flt->name) |
| 460 | ha_free(®_flt->name); |
| 461 | LIST_DELETE(®_flt->l); |
| 462 | ha_free(®_flt); |
| 463 | } |
| 464 | |
| 465 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 466 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 467 | { |
| 468 | if (reg_flt->fun_ref[0] == -1) |
| 469 | return tid + 1; |
| 470 | return 0; |
| 471 | } |
| 472 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 473 | /* Used to check an Lua function type in the stack. It creates and |
| 474 | * returns a reference of the function. This function throws an |
| 475 | * error if the rgument is not a "function". |
| 476 | */ |
| 477 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 478 | { |
| 479 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 480 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 481 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 482 | } |
| 483 | lua_pushvalue(L, argno); |
| 484 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 485 | } |
| 486 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 487 | /* Used to check an Lua table type in the stack. It creates and |
| 488 | * returns a reference of the table. This function throws an |
| 489 | * error if the rgument is not a "table". |
| 490 | */ |
| 491 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 492 | { |
| 493 | if (!lua_istable(L, argno)) { |
| 494 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 495 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 496 | } |
| 497 | lua_pushvalue(L, argno); |
| 498 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 499 | } |
| 500 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | { |
| 503 | lua_Debug ar; |
| 504 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 505 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 506 | |
| 507 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 508 | /* Fill fields: |
| 509 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 510 | * 'l': fills in the field currentline; |
| 511 | * 'n': fills in the field name and namewhat; |
| 512 | * 't': fills in the field istailcall; |
| 513 | */ |
| 514 | lua_getinfo(L, "Slnt", &ar); |
| 515 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 516 | /* skip these empty entries, usually they come from deep C functions */ |
| 517 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 518 | continue; |
| 519 | |
| 520 | /* Add separator */ |
| 521 | if (b_data(msg)) |
| 522 | chunk_appendf(msg, "%s", sep); |
| 523 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 524 | /* Append code localisation */ |
| 525 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 526 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 527 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 528 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * Get function name |
| 532 | * |
| 533 | * if namewhat is no empty, name is defined. |
| 534 | * what contains "Lua" for Lua function, "C" for C function, |
| 535 | * or "main" for main code. |
| 536 | */ |
| 537 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 538 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 539 | |
| 540 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 541 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 542 | |
| 543 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 544 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 545 | |
| 546 | else /* nothing left... */ |
| 547 | chunk_appendf(msg, "?"); |
| 548 | |
| 549 | |
| 550 | /* Display tailed call */ |
| 551 | if (ar.istailcall) |
| 552 | chunk_appendf(msg, " ..."); |
| 553 | } |
| 554 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 555 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 559 | /* This function check the number of arguments available in the |
| 560 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 561 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 562 | */ |
| 563 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 564 | { |
| 565 | if (lua_gettop(L) == nb) |
| 566 | return; |
| 567 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 568 | } |
| 569 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 570 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 571 | * and the line number where the error is encountered. |
| 572 | */ |
| 573 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 574 | { |
| 575 | va_list argp; |
| 576 | va_start(argp, fmt); |
| 577 | luaL_where(L, 1); |
| 578 | lua_pushvfstring(L, fmt, argp); |
| 579 | va_end(argp); |
| 580 | lua_concat(L, 2); |
| 581 | return 1; |
| 582 | } |
| 583 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | /* This functions is used with sample fetch and converters. It |
| 585 | * converts the HAProxy configuration argument in a lua stack |
| 586 | * values. |
| 587 | * |
| 588 | * It takes an array of "arg", and each entry of the array is |
| 589 | * converted and pushed in the LUA stack. |
| 590 | */ |
| 591 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 592 | { |
| 593 | switch (arg->type) { |
| 594 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | case ARGT_TIME: |
| 596 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 597 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 601 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case ARGT_IPV4: |
| 605 | case ARGT_IPV6: |
| 606 | case ARGT_MSK4: |
| 607 | case ARGT_MSK6: |
| 608 | case ARGT_FE: |
| 609 | case ARGT_BE: |
| 610 | case ARGT_TAB: |
| 611 | case ARGT_SRV: |
| 612 | case ARGT_USR: |
| 613 | case ARGT_MAP: |
| 614 | default: |
| 615 | lua_pushnil(L); |
| 616 | break; |
| 617 | } |
| 618 | return 1; |
| 619 | } |
| 620 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 621 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 623 | * with sample fetch wrappers. The input arguments are given to the |
| 624 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 625 | */ |
| 626 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 627 | { |
| 628 | switch (lua_type(L, ud)) { |
| 629 | |
| 630 | case LUA_TNUMBER: |
| 631 | case LUA_TBOOLEAN: |
| 632 | arg->type = ARGT_SINT; |
| 633 | arg->data.sint = lua_tointeger(L, ud); |
| 634 | break; |
| 635 | |
| 636 | case LUA_TSTRING: |
| 637 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 638 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 639 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 640 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 641 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | case LUA_TUSERDATA: |
| 645 | case LUA_TNIL: |
| 646 | case LUA_TTABLE: |
| 647 | case LUA_TFUNCTION: |
| 648 | case LUA_TTHREAD: |
| 649 | case LUA_TLIGHTUSERDATA: |
| 650 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 651 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* the following functions are used to convert a struct sample |
| 658 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 659 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 660 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 661 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 663 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 666 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | break; |
| 668 | |
| 669 | case SMP_T_BIN: |
| 670 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 675 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 677 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 678 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 679 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 680 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 681 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 682 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 683 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 684 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 685 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 686 | break; |
| 687 | default: |
| 688 | lua_pushnil(L); |
| 689 | break; |
| 690 | } |
| 691 | break; |
| 692 | |
| 693 | case SMP_T_IPV4: |
| 694 | case SMP_T_IPV6: |
| 695 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 696 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 697 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 699 | else |
| 700 | lua_pushnil(L); |
| 701 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 702 | default: |
| 703 | lua_pushnil(L); |
| 704 | break; |
| 705 | } |
| 706 | return 1; |
| 707 | } |
| 708 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 709 | /* the following functions are used to convert a struct sample |
| 710 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 711 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 712 | */ |
| 713 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 714 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 715 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 716 | |
| 717 | case SMP_T_BIN: |
| 718 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 719 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 720 | break; |
| 721 | |
| 722 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 724 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 725 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 726 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 727 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 728 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 729 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 730 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 731 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 732 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 734 | break; |
| 735 | default: |
| 736 | lua_pushstring(L, ""); |
| 737 | break; |
| 738 | } |
| 739 | break; |
| 740 | |
| 741 | case SMP_T_SINT: |
| 742 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 743 | case SMP_T_IPV4: |
| 744 | case SMP_T_IPV6: |
| 745 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 746 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 747 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 749 | else |
| 750 | lua_pushstring(L, ""); |
| 751 | break; |
| 752 | default: |
| 753 | lua_pushstring(L, ""); |
| 754 | break; |
| 755 | } |
| 756 | return 1; |
| 757 | } |
| 758 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 759 | /* the following functions are used to convert an Lua type in a |
| 760 | * struct sample. This is useful to provide data from a converter |
| 761 | * to the LUA code. |
| 762 | */ |
| 763 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 764 | { |
| 765 | switch (lua_type(L, ud)) { |
| 766 | |
| 767 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 768 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 769 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 770 | break; |
| 771 | |
| 772 | |
| 773 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 774 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 775 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 779 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 781 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 782 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 783 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 784 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 785 | break; |
| 786 | |
| 787 | case LUA_TUSERDATA: |
| 788 | case LUA_TNIL: |
| 789 | case LUA_TTABLE: |
| 790 | case LUA_TFUNCTION: |
| 791 | case LUA_TTHREAD: |
| 792 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 793 | case LUA_TNONE: |
| 794 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 795 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 796 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | return 1; |
| 800 | } |
| 801 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 802 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 803 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 804 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 805 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 806 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 807 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 808 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 809 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 810 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 811 | { |
| 812 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 813 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 814 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 815 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 816 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 818 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 819 | |
| 820 | idx = 0; |
| 821 | min_arg = ARGM(mask); |
| 822 | mask >>= ARGM_BITS; |
| 823 | |
| 824 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 825 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | /* Check for mandatory arguments. */ |
| 828 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 829 | if (idx < min_arg) { |
| 830 | |
| 831 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 832 | if (idx > 0) { |
| 833 | msg = "Mandatory argument expected"; |
| 834 | goto error; |
| 835 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 836 | |
| 837 | /* If first argument have a certain type, some default values |
| 838 | * may be used. See the function smp_resolve_args(). |
| 839 | */ |
| 840 | switch (mask & ARGT_MASK) { |
| 841 | |
| 842 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | if (!(p->cap & PR_CAP_FE)) { |
| 844 | msg = "Mandatory argument expected"; |
| 845 | goto error; |
| 846 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 847 | argp[idx].data.prx = p; |
| 848 | argp[idx].type = ARGT_FE; |
| 849 | argp[idx+1].type = ARGT_STOP; |
| 850 | break; |
| 851 | |
| 852 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 853 | if (!(p->cap & PR_CAP_BE)) { |
| 854 | msg = "Mandatory argument expected"; |
| 855 | goto error; |
| 856 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 857 | argp[idx].data.prx = p; |
| 858 | argp[idx].type = ARGT_BE; |
| 859 | argp[idx+1].type = ARGT_STOP; |
| 860 | break; |
| 861 | |
| 862 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 863 | if (!p->table) { |
| 864 | msg = "Mandatory argument expected"; |
| 865 | goto error; |
| 866 | } |
| 867 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_TAB; |
| 869 | argp[idx+1].type = ARGT_STOP; |
| 870 | break; |
| 871 | |
| 872 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 873 | msg = "Mandatory argument expected"; |
| 874 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 878 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 881 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 883 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | msg = "Last argument expected"; |
| 885 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 889 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | /* Convert some argument types. All string in argp[] are for not |
| 894 | * duplicated yet. |
| 895 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 896 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 897 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 898 | if (argp[idx].type != ARGT_SINT) { |
| 899 | msg = "integer expected"; |
| 900 | goto error; |
| 901 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 902 | argp[idx].type = ARGT_SINT; |
| 903 | break; |
| 904 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 906 | if (argp[idx].type != ARGT_SINT) { |
| 907 | msg = "integer expected"; |
| 908 | goto error; |
| 909 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 910 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 914 | if (argp[idx].type != ARGT_SINT) { |
| 915 | msg = "integer expected"; |
| 916 | goto error; |
| 917 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 918 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 919 | break; |
| 920 | |
| 921 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 922 | if (argp[idx].type != ARGT_STR) { |
| 923 | msg = "string expected"; |
| 924 | goto error; |
| 925 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 926 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 927 | if (!argp[idx].data.prx) { |
| 928 | msg = "frontend doesn't exist"; |
| 929 | goto error; |
| 930 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 931 | argp[idx].type = ARGT_FE; |
| 932 | break; |
| 933 | |
| 934 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | if (argp[idx].type != ARGT_STR) { |
| 936 | msg = "string expected"; |
| 937 | goto error; |
| 938 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 939 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | if (!argp[idx].data.prx) { |
| 941 | msg = "backend doesn't exist"; |
| 942 | goto error; |
| 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | argp[idx].type = ARGT_BE; |
| 945 | break; |
| 946 | |
| 947 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 948 | if (argp[idx].type != ARGT_STR) { |
| 949 | msg = "string expected"; |
| 950 | goto error; |
| 951 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 952 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 953 | if (!argp[idx].data.t) { |
| 954 | msg = "table doesn't exist"; |
| 955 | goto error; |
| 956 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 957 | argp[idx].type = ARGT_TAB; |
| 958 | break; |
| 959 | |
| 960 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 961 | if (argp[idx].type != ARGT_STR) { |
| 962 | msg = "string expected"; |
| 963 | goto error; |
| 964 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 965 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 966 | if (sname) { |
| 967 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 969 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 970 | if (!px) { |
| 971 | msg = "backend doesn't exist"; |
| 972 | goto error; |
| 973 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 974 | } |
| 975 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 976 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 978 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 979 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 980 | if (!argp[idx].data.srv) { |
| 981 | msg = "server doesn't exist"; |
| 982 | goto error; |
| 983 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 984 | argp[idx].type = ARGT_SRV; |
| 985 | break; |
| 986 | |
| 987 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 988 | if (argp[idx].type != ARGT_STR) { |
| 989 | msg = "string expected"; |
| 990 | goto error; |
| 991 | } |
| 992 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 993 | msg = "invalid IPv4 address"; |
| 994 | goto error; |
| 995 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 996 | argp[idx].type = ARGT_IPV4; |
| 997 | break; |
| 998 | |
| 999 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1000 | if (argp[idx].type == ARGT_SINT) |
| 1001 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1002 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1003 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1004 | msg = "invalid IPv4 mask"; |
| 1005 | goto error; |
| 1006 | } |
| 1007 | } |
| 1008 | else { |
| 1009 | msg = "integer or string expected"; |
| 1010 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1011 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1012 | argp[idx].type = ARGT_MSK4; |
| 1013 | break; |
| 1014 | |
| 1015 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1016 | if (argp[idx].type != ARGT_STR) { |
| 1017 | msg = "string expected"; |
| 1018 | goto error; |
| 1019 | } |
| 1020 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1021 | msg = "invalid IPv6 address"; |
| 1022 | goto error; |
| 1023 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1024 | argp[idx].type = ARGT_IPV6; |
| 1025 | break; |
| 1026 | |
| 1027 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1028 | if (argp[idx].type == ARGT_SINT) |
| 1029 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1030 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1031 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1032 | msg = "invalid IPv6 mask"; |
| 1033 | goto error; |
| 1034 | } |
| 1035 | } |
| 1036 | else { |
| 1037 | msg = "integer or string expected"; |
| 1038 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1039 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1040 | argp[idx].type = ARGT_MSK6; |
| 1041 | break; |
| 1042 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1043 | case ARGT_REG: |
| 1044 | if (argp[idx].type != ARGT_STR) { |
| 1045 | msg = "string expected"; |
| 1046 | goto error; |
| 1047 | } |
| 1048 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1049 | if (!reg) { |
| 1050 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1051 | argp[idx].data.str.area, err); |
| 1052 | free(err); |
| 1053 | goto error; |
| 1054 | } |
| 1055 | argp[idx].type = ARGT_REG; |
| 1056 | argp[idx].data.reg = reg; |
| 1057 | break; |
| 1058 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1059 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1060 | if (argp[idx].type != ARGT_STR) { |
| 1061 | msg = "string expected"; |
| 1062 | goto error; |
| 1063 | } |
| 1064 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1065 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1066 | ul = p->uri_auth->userlist; |
| 1067 | else |
| 1068 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1069 | |
| 1070 | if (!ul) { |
| 1071 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1072 | goto error; |
| 1073 | } |
| 1074 | argp[idx].type = ARGT_USR; |
| 1075 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | case ARGT_STR: |
| 1079 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1080 | msg = "unable to duplicate string arg"; |
| 1081 | goto error; |
| 1082 | } |
| 1083 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1084 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1085 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1086 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1087 | msg = "type not yet supported"; |
| 1088 | goto error; |
| 1089 | break; |
| 1090 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* Check for type of argument. */ |
| 1094 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1095 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1096 | arg_type_names[(mask & ARGT_MASK)], |
| 1097 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1098 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* Next argument. */ |
| 1102 | mask >>= ARGT_BITS; |
| 1103 | idx++; |
| 1104 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1105 | return 0; |
| 1106 | |
| 1107 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1108 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1109 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1110 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1111 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1115 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1117 | * |
| 1118 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1120 | */ |
| 1121 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1122 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1123 | struct hlua **hlua = lua_getextraspace(L); |
| 1124 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1127 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1128 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1129 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1132 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1133 | * currently is executing from within a Lua function. One line per entry will |
| 1134 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1135 | * Lua function is not detected, NULL is returned. |
| 1136 | */ |
| 1137 | const char *hlua_show_current_location(const char *pfx) |
| 1138 | { |
| 1139 | lua_State *L; |
| 1140 | lua_Debug ar; |
| 1141 | |
| 1142 | /* global or per-thread stack initializing ? */ |
| 1143 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1144 | return hlua_traceback(L, pfx); |
| 1145 | |
| 1146 | /* per-thread stack running ? */ |
| 1147 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1148 | return hlua_traceback(L, pfx); |
| 1149 | |
| 1150 | /* global stack running ? */ |
| 1151 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1152 | return hlua_traceback(L, pfx); |
| 1153 | |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1157 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1158 | * and on the default syslog server. |
| 1159 | */ |
| 1160 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1161 | { |
| 1162 | struct tm tm; |
| 1163 | char *p; |
| 1164 | |
| 1165 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1168 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1169 | /* Break the message if exceed the buffer size. */ |
| 1170 | *(p-4) = ' '; |
| 1171 | *(p-3) = '.'; |
| 1172 | *(p-2) = '.'; |
| 1173 | *(p-1) = '.'; |
| 1174 | break; |
| 1175 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1176 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1177 | *p = *msg; |
| 1178 | else |
| 1179 | *p = '.'; |
| 1180 | } |
| 1181 | *p = '\0'; |
| 1182 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1183 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1184 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1185 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1186 | return; |
| 1187 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1188 | get_localtime(date.tv_sec, &tm); |
| 1189 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1190 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1191 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1192 | fflush(stderr); |
| 1193 | } |
| 1194 | } |
| 1195 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | /* This function just ensure that the yield will be always |
| 1197 | * returned with a timeout and permit to set some flags |
| 1198 | */ |
| 1199 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1200 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1201 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1202 | struct hlua *hlua; |
| 1203 | |
| 1204 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1205 | hlua = hlua_gethlua(L); |
| 1206 | if (!hlua) { |
| 1207 | return; |
| 1208 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* Set the wake timeout. If timeout is required, we set |
| 1211 | * the expiration time. |
| 1212 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1213 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1214 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1215 | hlua->flags |= flags; |
| 1216 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1217 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1218 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1221 | /* This function initialises the Lua environment stored in the stream. |
| 1222 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1224 | * |
| 1225 | * This function is particular. it initialises a new Lua thread. If the |
| 1226 | * initialisation fails (example: out of memory error), the lua function |
| 1227 | * throws an error (longjmp). |
| 1228 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1229 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1230 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1231 | * threads appear, the safe environment set a lock to ensure only one |
| 1232 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1233 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1234 | * |
| 1235 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1236 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1237 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1238 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1239 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1240 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1241 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1242 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1243 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | { |
| 1245 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1246 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1247 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1248 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1249 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1250 | LIST_INIT(&lua->com); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1251 | MT_LIST_INIT(&lua->hc_list); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1252 | if (!already_safe) { |
| 1253 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1254 | lua->Tref = LUA_REFNIL; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1258 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | if (!lua->T) { |
| 1260 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1261 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1262 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |
| 1265 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1268 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1269 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1270 | return 1; |
| 1271 | } |
| 1272 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1273 | /* kill all associated httpclient to this hlua task |
| 1274 | * We must take extra precautions as we're manipulating lua-exposed |
| 1275 | * objects without the main lua lock. |
| 1276 | */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1277 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1278 | { |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1279 | struct hlua_httpclient *hlua_hc; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1280 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 1281 | /* use thread-safe accessors for hc_list since GC cycle initiated by |
| 1282 | * another thread sharing the same main lua stack (lua coroutine) |
| 1283 | * could execute hlua_httpclient_gc() on the hlua->hc_list items |
| 1284 | * in parallel: Lua GC applies on the main stack, it is not limited to |
| 1285 | * a single coroutine stack, see Github issue #2037 for reference. |
| 1286 | * Remember, coroutines created using lua_newthread() are not meant to |
| 1287 | * be thread safe in Lua. (From lua co-author: |
| 1288 | * http://lua-users.org/lists/lua-l/2011-07/msg00072.html) |
| 1289 | * |
| 1290 | * This security measure is superfluous when 'lua-load-per-thread' is used |
| 1291 | * since in this case coroutines exclusively run on the same thread |
| 1292 | * (main stack is not shared between OS threads). |
| 1293 | */ |
| 1294 | while ((hlua_hc = MT_LIST_POP(&hlua->hc_list, typeof(hlua_hc), by_hlua))) { |
| 1295 | httpclient_stop_and_destroy(hlua_hc->hc); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1296 | hlua_hc->hc = NULL; |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1297 | } |
| 1298 | } |
| 1299 | |
| 1300 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1301 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1302 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1303 | * is not freed. |
| 1304 | */ |
| 1305 | void hlua_ctx_destroy(struct hlua *lua) |
| 1306 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1307 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1308 | return; |
| 1309 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1310 | if (!lua->T) |
| 1311 | goto end; |
| 1312 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1313 | /* clean all running httpclient */ |
| 1314 | hlua_httpclient_destroy_all(lua); |
| 1315 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1316 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1317 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1318 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1319 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1320 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1321 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1322 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1323 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1324 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1325 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1326 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1327 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1328 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1329 | * without error, we run the GC on the thread pointer. Its freed all |
| 1330 | * the unused memory. |
| 1331 | * If the thread is finnish with an error or is currently yielded, |
| 1332 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1333 | * so e run the GC on the main thread. |
| 1334 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1335 | * the garbage collection. |
| 1336 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1337 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1338 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1339 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1340 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1341 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1342 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1343 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1344 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1345 | |
| 1346 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1347 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* This function is used to restore the Lua context when a coroutine |
| 1351 | * fails. This function copy the common memory between old coroutine |
| 1352 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1353 | * replaced by the new coroutine. |
| 1354 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1355 | * as string error message and it is copied in the new stack. |
| 1356 | */ |
| 1357 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1358 | { |
| 1359 | lua_State *T; |
| 1360 | int new_ref; |
| 1361 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1362 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1363 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1364 | if (!T) |
| 1365 | return 0; |
| 1366 | |
| 1367 | /* Copy last error message. */ |
| 1368 | if (keep_msg) |
| 1369 | lua_xmove(lua->T, T, 1); |
| 1370 | |
| 1371 | /* Copy data between the coroutines. */ |
| 1372 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1373 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1374 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1375 | |
| 1376 | /* Destroy old data. */ |
| 1377 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1378 | |
| 1379 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1380 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1381 | |
| 1382 | /* Fill the struct with the new coroutine values. */ |
| 1383 | lua->Mref = new_ref; |
| 1384 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1385 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1386 | |
| 1387 | /* Set context. */ |
| 1388 | hlua_sethlua(lua); |
| 1389 | |
| 1390 | return 1; |
| 1391 | } |
| 1392 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1393 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1394 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1395 | struct hlua *hlua; |
| 1396 | |
| 1397 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1398 | hlua = hlua_gethlua(L); |
| 1399 | if (!hlua) |
| 1400 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1401 | |
| 1402 | /* Lua cannot yield when its returning from a function, |
| 1403 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1404 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1405 | */ |
| 1406 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1407 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1408 | return; |
| 1409 | } |
| 1410 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1411 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1412 | * If the state is not yieldable, trying yield causes an error. |
| 1413 | */ |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1414 | if (lua_isyieldable(L)) { |
| 1415 | /* note: for converters/fetches.. where yielding is not allowed |
| 1416 | * hlua_ctx_resume() will simply perform a goto resume_execution |
| 1417 | * instead of rescheduling hlua->task. |
| 1418 | * also: hlua_ctx_resume() will take care of checking execution |
| 1419 | * timeout and re-applying the hook as needed. |
| 1420 | */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1421 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Aurelien DARRAGON | 0ebd41f | 2022-11-24 09:51:40 +0100 | [diff] [blame] | 1422 | /* lua docs says that the hook should return immediately after lua_yieldk |
| 1423 | * |
| 1424 | * From: https://www.lua.org/manual/5.3/manual.html#lua_yieldk |
| 1425 | * |
| 1426 | * Moreover, it seems that we don't want to continue after the yield |
| 1427 | * because the end of the function is about handling unyieldable function, |
| 1428 | * which is not the case here. |
| 1429 | * |
| 1430 | * ->if we don't return lua_sethook gets incorrectly set with MASKRET later |
| 1431 | * in the function. |
| 1432 | */ |
| 1433 | return; |
| 1434 | } |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1435 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1436 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1437 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1438 | hlua->run_time += now_ms - hlua->start_time; |
| 1439 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1440 | lua_pushfstring(L, "execution timeout"); |
| 1441 | WILL_LJMP(lua_error(L)); |
| 1442 | } |
| 1443 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1444 | /* Update the start time. */ |
| 1445 | hlua->start_time = now_ms; |
| 1446 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1447 | /* Try to interrupt the process at the end of the current |
| 1448 | * unyieldable function. |
| 1449 | */ |
| 1450 | 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] | 1451 | } |
| 1452 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1453 | /* This function start or resumes the Lua stack execution. If the flag |
| 1454 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1455 | * The function return an error. |
| 1456 | * |
| 1457 | * The function can returns 4 values: |
| 1458 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1459 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1460 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1461 | * - 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] | 1462 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1463 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1464 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1465 | * 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] | 1466 | * LUA code. |
| 1467 | */ |
| 1468 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1469 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1470 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1471 | int nres; |
| 1472 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1473 | int ret; |
| 1474 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1475 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1476 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1477 | /* Initialise run time counter. */ |
| 1478 | if (!HLUA_IS_RUNNING(lua)) |
| 1479 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1480 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1481 | /* Lock the whole Lua execution. This lock must be before the |
| 1482 | * label "resume_execution". |
| 1483 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1484 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1485 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1486 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1487 | resume_execution: |
| 1488 | |
| 1489 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1490 | * instructions. it is used for preventing infinite loops. |
| 1491 | */ |
| 1492 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1493 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1494 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1495 | HLUA_SET_RUN(lua); |
| 1496 | HLUA_CLR_CTRLYIELD(lua); |
| 1497 | HLUA_CLR_WAKERESWR(lua); |
| 1498 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1499 | HLUA_CLR_NOYIELD(lua); |
| 1500 | if (!yield_allowed) |
| 1501 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1502 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1503 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1504 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1505 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1506 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1507 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1508 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1509 | 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] | 1510 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1511 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1512 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1513 | switch (ret) { |
| 1514 | |
| 1515 | case LUA_OK: |
| 1516 | ret = HLUA_E_OK; |
| 1517 | break; |
| 1518 | |
| 1519 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1520 | /* Check if the execution timeout is expired. It it is the case, we |
| 1521 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1522 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1523 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1524 | lua->run_time += now_ms - lua->start_time; |
| 1525 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1526 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1527 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | /* Process the forced yield. if the general yield is not allowed or |
| 1531 | * if no task were associated this the current Lua execution |
| 1532 | * coroutine, we resume the execution. Else we want to return in the |
| 1533 | * scheduler and we want to be waked up again, to continue the |
| 1534 | * current Lua execution. So we schedule our own task. |
| 1535 | */ |
| 1536 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1537 | if (!yield_allowed || !lua->task) |
| 1538 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1539 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1540 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | if (!yield_allowed) { |
| 1542 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1543 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1544 | break; |
| 1545 | } |
| 1546 | ret = HLUA_E_AGAIN; |
| 1547 | break; |
| 1548 | |
| 1549 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1550 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1551 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1552 | * because the errors ares the only one mean to return immediately |
| 1553 | * from and lua execution. |
| 1554 | */ |
| 1555 | if (lua->flags & HLUA_EXIT) { |
| 1556 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1557 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1558 | break; |
| 1559 | } |
| 1560 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1561 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1562 | if (!lua_checkstack(lua->T, 1)) { |
| 1563 | ret = HLUA_E_ERR; |
| 1564 | break; |
| 1565 | } |
| 1566 | msg = lua_tostring(lua->T, -1); |
| 1567 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1568 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1569 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1570 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1571 | 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] | 1572 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1573 | 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] | 1574 | ret = HLUA_E_ERRMSG; |
| 1575 | break; |
| 1576 | |
| 1577 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1578 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1579 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1580 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1581 | break; |
| 1582 | |
| 1583 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1584 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1585 | if (!lua_checkstack(lua->T, 1)) { |
| 1586 | ret = HLUA_E_ERR; |
| 1587 | break; |
| 1588 | } |
| 1589 | msg = lua_tostring(lua->T, -1); |
| 1590 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1591 | lua_pop(lua->T, 1); |
| 1592 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1593 | 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] | 1594 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1595 | 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] | 1596 | ret = HLUA_E_ERRMSG; |
| 1597 | break; |
| 1598 | |
| 1599 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1600 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1601 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1602 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1603 | break; |
| 1604 | } |
| 1605 | |
| 1606 | switch (ret) { |
| 1607 | case HLUA_E_AGAIN: |
| 1608 | break; |
| 1609 | |
| 1610 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1611 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1612 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1613 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1614 | break; |
| 1615 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1616 | case HLUA_E_ETMOUT: |
| 1617 | case HLUA_E_NOMEM: |
| 1618 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1619 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1620 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1621 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1622 | hlua_ctx_renew(lua, 0); |
| 1623 | break; |
| 1624 | |
| 1625 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1626 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1627 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1628 | break; |
| 1629 | } |
| 1630 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1631 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1632 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1633 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1634 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1635 | return ret; |
| 1636 | } |
| 1637 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1638 | /* This function exit the current code. */ |
| 1639 | __LJMP static int hlua_done(lua_State *L) |
| 1640 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1641 | struct hlua *hlua; |
| 1642 | |
| 1643 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1644 | hlua = hlua_gethlua(L); |
| 1645 | if (!hlua) |
| 1646 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1647 | |
| 1648 | hlua->flags |= HLUA_EXIT; |
| 1649 | WILL_LJMP(lua_error(L)); |
| 1650 | |
| 1651 | return 0; |
| 1652 | } |
| 1653 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1654 | /* This function is an LUA binding. It provides a function |
| 1655 | * for deleting ACL from a referenced ACL file. |
| 1656 | */ |
| 1657 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1658 | { |
| 1659 | const char *name; |
| 1660 | const char *key; |
| 1661 | struct pat_ref *ref; |
| 1662 | |
| 1663 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1664 | |
| 1665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1666 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1667 | |
| 1668 | ref = pat_ref_lookup(name); |
| 1669 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1670 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1672 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1673 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1674 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1675 | return 0; |
| 1676 | } |
| 1677 | |
| 1678 | /* This function is an LUA binding. It provides a function |
| 1679 | * for deleting map entry from a referenced map file. |
| 1680 | */ |
| 1681 | static int hlua_del_map(lua_State *L) |
| 1682 | { |
| 1683 | const char *name; |
| 1684 | const char *key; |
| 1685 | struct pat_ref *ref; |
| 1686 | |
| 1687 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1688 | |
| 1689 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1690 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1691 | |
| 1692 | ref = pat_ref_lookup(name); |
| 1693 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1694 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1695 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1696 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1697 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1698 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1699 | return 0; |
| 1700 | } |
| 1701 | |
| 1702 | /* This function is an LUA binding. It provides a function |
| 1703 | * for adding ACL pattern from a referenced ACL file. |
| 1704 | */ |
| 1705 | static int hlua_add_acl(lua_State *L) |
| 1706 | { |
| 1707 | const char *name; |
| 1708 | const char *key; |
| 1709 | struct pat_ref *ref; |
| 1710 | |
| 1711 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1712 | |
| 1713 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1714 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1715 | |
| 1716 | ref = pat_ref_lookup(name); |
| 1717 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1718 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1719 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1720 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1721 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1722 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1723 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
| 1727 | /* This function is an LUA binding. It provides a function |
| 1728 | * for setting map pattern and sample from a referenced map |
| 1729 | * file. |
| 1730 | */ |
| 1731 | static int hlua_set_map(lua_State *L) |
| 1732 | { |
| 1733 | const char *name; |
| 1734 | const char *key; |
| 1735 | const char *value; |
| 1736 | struct pat_ref *ref; |
| 1737 | |
| 1738 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1739 | |
| 1740 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1741 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1742 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1743 | |
| 1744 | ref = pat_ref_lookup(name); |
| 1745 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1746 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1747 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1748 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1749 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1750 | pat_ref_set(ref, key, value, NULL); |
| 1751 | else |
| 1752 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1753 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1754 | return 0; |
| 1755 | } |
| 1756 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1757 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1758 | * 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] | 1759 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1760 | * the name of the object (_G[<name>] = <metable> ). |
| 1761 | * |
| 1762 | * A metable is a table that modify the standard behavior of a standard |
| 1763 | * access to the associated data. The entries of this new metatable are |
| 1764 | * defined as is: |
| 1765 | * |
| 1766 | * http://lua-users.org/wiki/MetatableEvents |
| 1767 | * |
| 1768 | * __index |
| 1769 | * |
| 1770 | * we access an absent field in a table, the result is nil. This is |
| 1771 | * true, but it is not the whole truth. Actually, such access triggers |
| 1772 | * the interpreter to look for an __index metamethod: If there is no |
| 1773 | * such method, as usually happens, then the access results in nil; |
| 1774 | * otherwise, the metamethod will provide the result. |
| 1775 | * |
| 1776 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1777 | * the key does not appear in the table, but the metatable has an __index |
| 1778 | * property: |
| 1779 | * |
| 1780 | * - if the value is a function, the function is called, passing in the |
| 1781 | * table and the key; the return value of that function is returned as |
| 1782 | * the result. |
| 1783 | * |
| 1784 | * - if the value is another table, the value of the key in that table is |
| 1785 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1786 | * table's metatable has an __index property, then it continues on up) |
| 1787 | * |
| 1788 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1789 | * |
| 1790 | * http://www.lua.org/pil/13.4.1.html |
| 1791 | * |
| 1792 | * __newindex |
| 1793 | * |
| 1794 | * Like __index, but control property assignment. |
| 1795 | * |
| 1796 | * __mode - Control weak references. A string value with one or both |
| 1797 | * of the characters 'k' and 'v' which specifies that the the |
| 1798 | * keys and/or values in the table are weak references. |
| 1799 | * |
| 1800 | * __call - Treat a table like a function. When a table is followed by |
| 1801 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1802 | * a __call key pointing to a function, that function is invoked |
| 1803 | * (passing any specified arguments) and the return value is |
| 1804 | * returned. |
| 1805 | * |
| 1806 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1807 | * called, if the metatable for myTable has a __metatable |
| 1808 | * key, the value of that key is returned instead of the |
| 1809 | * actual metatable. |
| 1810 | * |
| 1811 | * __tostring - Control string representation. When the builtin |
| 1812 | * "tostring( myTable )" function is called, if the metatable |
| 1813 | * for myTable has a __tostring property set to a function, |
| 1814 | * that function is invoked (passing myTable to it) and the |
| 1815 | * return value is used as the string representation. |
| 1816 | * |
| 1817 | * __len - Control table length. When the table length is requested using |
| 1818 | * the length operator ( '#' ), if the metatable for myTable has |
| 1819 | * a __len key pointing to a function, that function is invoked |
| 1820 | * (passing myTable to it) and the return value used as the value |
| 1821 | * of "#myTable". |
| 1822 | * |
| 1823 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1824 | * collected, if the metatable has a __gc field pointing to a |
| 1825 | * function, that function is first invoked, passing the userdata |
| 1826 | * to it. The __gc metamethod is not called for tables. |
| 1827 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1828 | * |
| 1829 | * Special metamethods for redefining standard operators: |
| 1830 | * http://www.lua.org/pil/13.1.html |
| 1831 | * |
| 1832 | * __add "+" |
| 1833 | * __sub "-" |
| 1834 | * __mul "*" |
| 1835 | * __div "/" |
| 1836 | * __unm "!" |
| 1837 | * __pow "^" |
| 1838 | * __concat ".." |
| 1839 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1840 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1841 | * http://www.lua.org/pil/13.2.html |
| 1842 | * |
| 1843 | * __eq "==" |
| 1844 | * __lt "<" |
| 1845 | * __le "<=" |
| 1846 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | |
| 1848 | /* |
| 1849 | * |
| 1850 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1851 | * Class Map |
| 1852 | * |
| 1853 | * |
| 1854 | */ |
| 1855 | |
| 1856 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1857 | * a class session, otherwise it throws an error. |
| 1858 | */ |
| 1859 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1860 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1861 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1862 | } |
| 1863 | |
| 1864 | /* This function is the map constructor. It don't need |
| 1865 | * the class Map object. It creates and return a new Map |
| 1866 | * object. It must be called only during "body" or "init" |
| 1867 | * context because it process some filesystem accesses. |
| 1868 | */ |
| 1869 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1870 | { |
| 1871 | const char *fn; |
| 1872 | int match = PAT_MATCH_STR; |
| 1873 | struct sample_conv conv; |
| 1874 | const char *file = ""; |
| 1875 | int line = 0; |
| 1876 | lua_Debug ar; |
| 1877 | char *err = NULL; |
| 1878 | struct arg args[2]; |
| 1879 | |
| 1880 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1881 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1882 | |
| 1883 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1884 | |
| 1885 | if (lua_gettop(L) >= 2) { |
| 1886 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1887 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1888 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1889 | } |
| 1890 | |
| 1891 | /* Get Lua filename and line number. */ |
| 1892 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1893 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1894 | if (ar.currentline > 0) { /* is there info? */ |
| 1895 | file = ar.short_src; |
| 1896 | line = ar.currentline; |
| 1897 | } |
| 1898 | } |
| 1899 | |
| 1900 | /* fill fake sample_conv struct. */ |
| 1901 | conv.kw = ""; /* unused. */ |
| 1902 | conv.process = NULL; /* unused. */ |
| 1903 | conv.arg_mask = 0; /* unused. */ |
| 1904 | conv.val_args = NULL; /* unused. */ |
| 1905 | conv.out_type = SMP_T_STR; |
| 1906 | conv.private = (void *)(long)match; |
| 1907 | switch (match) { |
| 1908 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1909 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1910 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1911 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1912 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1913 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1914 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1915 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1916 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1917 | default: |
| 1918 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1919 | } |
| 1920 | |
| 1921 | /* fill fake args. */ |
| 1922 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1923 | args[0].data.str.area = strdup(fn); |
| 1924 | args[0].data.str.data = strlen(fn); |
| 1925 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1926 | args[1].type = ARGT_STOP; |
| 1927 | |
| 1928 | /* load the map. */ |
| 1929 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1930 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1931 | * free the err variable. |
| 1932 | */ |
| 1933 | luaL_where(L, 1); |
| 1934 | lua_pushfstring(L, "'new': %s.", err); |
| 1935 | lua_concat(L, 2); |
| 1936 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1937 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1938 | WILL_LJMP(lua_error(L)); |
| 1939 | } |
| 1940 | |
| 1941 | /* create the lua object. */ |
| 1942 | lua_newtable(L); |
| 1943 | lua_pushlightuserdata(L, args[0].data.map); |
| 1944 | lua_rawseti(L, -2, 0); |
| 1945 | |
| 1946 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1947 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1948 | lua_setmetatable(L, -2); |
| 1949 | |
| 1950 | |
| 1951 | return 1; |
| 1952 | } |
| 1953 | |
| 1954 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1955 | { |
| 1956 | struct map_descriptor *desc; |
| 1957 | struct pattern *pat; |
| 1958 | struct sample smp; |
| 1959 | |
| 1960 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1961 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1962 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1963 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1964 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1965 | } |
| 1966 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1967 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1968 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1969 | 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] | 1970 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1971 | } |
| 1972 | |
| 1973 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1974 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1975 | if (str) |
| 1976 | lua_pushstring(L, ""); |
| 1977 | else |
| 1978 | lua_pushnil(L); |
| 1979 | return 1; |
| 1980 | } |
| 1981 | |
| 1982 | /* 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] | 1983 | 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] | 1984 | return 1; |
| 1985 | } |
| 1986 | |
| 1987 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1988 | { |
| 1989 | return _hlua_map_lookup(L, 0); |
| 1990 | } |
| 1991 | |
| 1992 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1993 | { |
| 1994 | return _hlua_map_lookup(L, 1); |
| 1995 | } |
| 1996 | |
| 1997 | /* |
| 1998 | * |
| 1999 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | * Class Socket |
| 2001 | * |
| 2002 | * |
| 2003 | */ |
| 2004 | |
| 2005 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 2006 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2007 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2008 | } |
| 2009 | |
| 2010 | /* 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] | 2011 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | * received. |
| 2013 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2014 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2016 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2017 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2019 | if (unlikely(se_fl_test(appctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) { |
| 2020 | notification_wake(&ctx->wake_on_read); |
| 2021 | notification_wake(&ctx->wake_on_write); |
| 2022 | return; |
| 2023 | } |
| 2024 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2025 | if (ctx->die) { |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2026 | se_fl_set(appctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2027 | notification_wake(&ctx->wake_on_read); |
| 2028 | notification_wake(&ctx->wake_on_write); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 2029 | return; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2030 | } |
| 2031 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2032 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2033 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2034 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2035 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2036 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2037 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2038 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2039 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2040 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2041 | * to be notified whenever the connection completes. |
| 2042 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2043 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2044 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2045 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2046 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2047 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2048 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | |
| 2050 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2051 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2052 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2053 | /* 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] | 2054 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2055 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2056 | |
| 2057 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2058 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2059 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2060 | |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2061 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2062 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2063 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2064 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2065 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2068 | static int hlua_socket_init(struct appctx *appctx) |
| 2069 | { |
| 2070 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2071 | struct stream *s; |
| 2072 | |
| 2073 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2074 | goto error; |
| 2075 | |
| 2076 | s = appctx_strm(appctx); |
| 2077 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2078 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2079 | * and retrieve data from the server. The connection is initialized |
| 2080 | * with the "struct server". |
| 2081 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2082 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2083 | |
| 2084 | /* Force destination server. */ |
| 2085 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2086 | s->target = &socket_tcp->obj_type; |
| 2087 | |
| 2088 | ctx->appctx = appctx; |
| 2089 | return 0; |
| 2090 | |
| 2091 | error: |
| 2092 | return -1; |
| 2093 | } |
| 2094 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2095 | /* This function is called when the "struct stream" is destroyed. |
| 2096 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | * Wake all the pending signals. |
| 2098 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2099 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2101 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2102 | struct xref *peer; |
| 2103 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2104 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2105 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2106 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2107 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2108 | |
| 2109 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2110 | notification_wake(&ctx->wake_on_read); |
| 2111 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | } |
| 2113 | |
| 2114 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2115 | * uses this object. If the stream does not exists, just quit. |
| 2116 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2117 | * pending signal can rest in the read and write lists. destroy |
| 2118 | * it. |
| 2119 | */ |
| 2120 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2121 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2122 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2123 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2124 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2125 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2126 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2127 | |
| 2128 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2129 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2130 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2132 | |
| 2133 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2135 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2136 | ctx->die = 1; |
| 2137 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2138 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2139 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2140 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | return 0; |
| 2142 | } |
| 2143 | |
| 2144 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2145 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2147 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2149 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2150 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2151 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2152 | struct hlua *hlua; |
| 2153 | |
| 2154 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2155 | hlua = hlua_gethlua(L); |
| 2156 | if (!hlua) |
| 2157 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2159 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2160 | |
| 2161 | /* Check if we run on the same thread than the xreator thread. |
| 2162 | * We cannot access to the socket if the thread is different. |
| 2163 | */ |
| 2164 | if (socket->tid != tid) |
| 2165 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2166 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2167 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2168 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2170 | |
| 2171 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2172 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2174 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2175 | ctx->die = 1; |
| 2176 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2178 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2179 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | return 0; |
| 2181 | } |
| 2182 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2183 | /* The close function calls close_helper. |
| 2184 | */ |
| 2185 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2186 | { |
| 2187 | MAY_LJMP(check_args(L, 1, "close")); |
| 2188 | return hlua_socket_close_helper(L); |
| 2189 | } |
| 2190 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2191 | /* This Lua function assumes that the stack contain three parameters. |
| 2192 | * 1 - USERDATA containing a struct socket |
| 2193 | * 2 - INTEGER with values of the macro defined below |
| 2194 | * If the integer is -1, we must read at most one line. |
| 2195 | * If the integer is -2, we ust read all the data until the |
| 2196 | * end of the stream. |
| 2197 | * If the integer is positive value, we must read a number of |
| 2198 | * bytes corresponding to this value. |
| 2199 | */ |
| 2200 | #define HLSR_READ_LINE (-1) |
| 2201 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2202 | __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] | 2203 | { |
| 2204 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2205 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2206 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2207 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2208 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2209 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2210 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2211 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2212 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2213 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2214 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2215 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2216 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2217 | struct stream *s; |
| 2218 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2219 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2221 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2222 | hlua = hlua_gethlua(L); |
| 2223 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2224 | /* Check if this lua stack is schedulable. */ |
| 2225 | if (!hlua || !hlua->task) |
| 2226 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2227 | "'frontend', 'backend' or 'task'")); |
| 2228 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2229 | /* Check if we run on the same thread than the xreator thread. |
| 2230 | * We cannot access to the socket if the thread is different. |
| 2231 | */ |
| 2232 | if (socket->tid != tid) |
| 2233 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2234 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2235 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2236 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2237 | if (!peer) |
| 2238 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2239 | |
| 2240 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2241 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2242 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2243 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2247 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | if (nblk < 0) /* Connection close. */ |
| 2249 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2250 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2252 | |
| 2253 | /* remove final \r\n. */ |
| 2254 | if (nblk == 1) { |
| 2255 | if (blk1[len1-1] == '\n') { |
| 2256 | len1--; |
| 2257 | skip_at_end++; |
| 2258 | if (blk1[len1-1] == '\r') { |
| 2259 | len1--; |
| 2260 | skip_at_end++; |
| 2261 | } |
| 2262 | } |
| 2263 | } |
| 2264 | else { |
| 2265 | if (blk2[len2-1] == '\n') { |
| 2266 | len2--; |
| 2267 | skip_at_end++; |
| 2268 | if (blk2[len2-1] == '\r') { |
| 2269 | len2--; |
| 2270 | skip_at_end++; |
| 2271 | } |
| 2272 | } |
| 2273 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2274 | } |
| 2275 | |
| 2276 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2277 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2278 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2279 | if (nblk < 0) /* Connection close. */ |
| 2280 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2281 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2282 | goto connection_empty; |
| 2283 | } |
| 2284 | |
| 2285 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2287 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | if (nblk < 0) /* Connection close. */ |
| 2289 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2290 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | goto connection_empty; |
| 2292 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2293 | missing_bytes = wanted - socket->b.n; |
| 2294 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2295 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2296 | len1 = missing_bytes; |
| 2297 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2298 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2299 | } |
| 2300 | |
| 2301 | len = len1; |
| 2302 | |
| 2303 | luaL_addlstring(&socket->b, blk1, len1); |
| 2304 | if (nblk == 2) { |
| 2305 | len += len2; |
| 2306 | luaL_addlstring(&socket->b, blk2, len2); |
| 2307 | } |
| 2308 | |
| 2309 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2310 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2311 | |
| 2312 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2313 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | |
| 2315 | /* If the pattern reclaim to read all the data |
| 2316 | * in the connection, got out. |
| 2317 | */ |
| 2318 | if (wanted == HLSR_READ_ALL) |
| 2319 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2320 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | goto connection_empty; |
| 2322 | |
| 2323 | /* Return result. */ |
| 2324 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2325 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | return 1; |
| 2327 | |
| 2328 | connection_closed: |
| 2329 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2330 | xref_unlock(&socket->xref, peer); |
| 2331 | |
| 2332 | no_peer: |
| 2333 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | /* If the buffer containds data. */ |
| 2335 | if (socket->b.n > 0) { |
| 2336 | luaL_pushresult(&socket->b); |
| 2337 | return 1; |
| 2338 | } |
| 2339 | lua_pushnil(L); |
| 2340 | lua_pushstring(L, "connection closed."); |
| 2341 | return 2; |
| 2342 | |
| 2343 | connection_empty: |
| 2344 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2345 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2346 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2348 | } |
| 2349 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2350 | 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] | 2351 | return 0; |
| 2352 | } |
| 2353 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2354 | /* This Lua function gets two parameters. The first one can be string |
| 2355 | * or a number. If the string is "*l", the user requires one line. If |
| 2356 | * 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] | 2357 | * If the value is a number, the user require a number of bytes equal |
| 2358 | * to the value. The default value is "*l" (a line). |
| 2359 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2360 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | * integer takes this values: |
| 2362 | * -1 : read a line |
| 2363 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2364 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2366 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2367 | * concatenated with the read data. |
| 2368 | */ |
| 2369 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2370 | { |
| 2371 | int wanted = HLSR_READ_LINE; |
| 2372 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2373 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2374 | char *error; |
| 2375 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2376 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | |
| 2378 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2379 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2380 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2381 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2383 | /* Check if we run on the same thread than the xreator thread. |
| 2384 | * We cannot access to the socket if the thread is different. |
| 2385 | */ |
| 2386 | if (socket->tid != tid) |
| 2387 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2388 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | /* check for pattern. */ |
| 2390 | if (lua_gettop(L) >= 2) { |
| 2391 | type = lua_type(L, 2); |
| 2392 | if (type == LUA_TSTRING) { |
| 2393 | pattern = lua_tostring(L, 2); |
| 2394 | if (strcmp(pattern, "*a") == 0) |
| 2395 | wanted = HLSR_READ_ALL; |
| 2396 | else if (strcmp(pattern, "*l") == 0) |
| 2397 | wanted = HLSR_READ_LINE; |
| 2398 | else { |
| 2399 | wanted = strtoll(pattern, &error, 10); |
| 2400 | if (*error != '\0') |
| 2401 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2402 | } |
| 2403 | } |
| 2404 | else if (type == LUA_TNUMBER) { |
| 2405 | wanted = lua_tointeger(L, 2); |
| 2406 | if (wanted < 0) |
| 2407 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2408 | } |
| 2409 | } |
| 2410 | |
| 2411 | /* Set pattern. */ |
| 2412 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2413 | |
| 2414 | /* Check if we would replace the top by itself. */ |
| 2415 | if (lua_gettop(L) != 2) |
| 2416 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2418 | /* Save index of the top of the stack because since buffers are used, it |
| 2419 | * may change |
| 2420 | */ |
| 2421 | lastarg = lua_gettop(L); |
| 2422 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2423 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2424 | luaL_buffinit(L, &socket->b); |
| 2425 | |
| 2426 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2427 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2429 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2430 | pattern = lua_tolstring(L, 3, &len); |
| 2431 | luaL_addlstring(&socket->b, pattern, len); |
| 2432 | } |
| 2433 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2434 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | } |
| 2436 | |
| 2437 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2438 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2440 | 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] | 2441 | { |
| 2442 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2443 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2444 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2445 | struct appctx *appctx; |
| 2446 | size_t buf_len; |
| 2447 | const char *buf; |
| 2448 | int len; |
| 2449 | int send_len; |
| 2450 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2451 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2452 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2453 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2455 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2456 | hlua = hlua_gethlua(L); |
| 2457 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | /* Check if this lua stack is schedulable. */ |
| 2459 | if (!hlua || !hlua->task) |
| 2460 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2461 | "'frontend', 'backend' or 'task'")); |
| 2462 | |
| 2463 | /* Get object */ |
| 2464 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2465 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2466 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2467 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2468 | /* Check if we run on the same thread than the xreator thread. |
| 2469 | * We cannot access to the socket if the thread is different. |
| 2470 | */ |
| 2471 | if (socket->tid != tid) |
| 2472 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2473 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2474 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2475 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2476 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2477 | lua_pushinteger(L, -1); |
| 2478 | return 1; |
| 2479 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2480 | |
| 2481 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2482 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2483 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2484 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2485 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2487 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | lua_pushinteger(L, -1); |
| 2490 | return 1; |
| 2491 | } |
| 2492 | |
| 2493 | /* Update the input buffer data. */ |
| 2494 | buf += sent; |
| 2495 | send_len = buf_len - sent; |
| 2496 | |
| 2497 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | if (sent >= buf_len) { |
| 2499 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2503 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2504 | * the request buffer if its not required. |
| 2505 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2506 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2507 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2508 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2509 | } |
| 2510 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2511 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2512 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2513 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2514 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2515 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
| 2517 | /* send data */ |
| 2518 | if (len < send_len) |
| 2519 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2520 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2521 | |
| 2522 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2523 | * the data" (-2) are not expected because the available length |
| 2524 | * is tested. |
| 2525 | * Other unknown error are also not expected. |
| 2526 | */ |
| 2527 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2528 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2530 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2531 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2532 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2533 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2534 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | return 1; |
| 2536 | } |
| 2537 | |
| 2538 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2539 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2540 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2541 | /* Update length sent. */ |
| 2542 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2543 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2544 | |
| 2545 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2546 | if (sent + len >= buf_len) { |
| 2547 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2548 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2550 | |
| 2551 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2552 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2553 | xref_unlock(&socket->xref, peer); |
| 2554 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2555 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2556 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2557 | 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] | 2558 | return 0; |
| 2559 | } |
| 2560 | |
| 2561 | /* This function initiate the send of data. It just check the input |
| 2562 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2563 | * 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] | 2564 | * "hlua_socket_write_yield" that can yield. |
| 2565 | * |
| 2566 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2567 | * the associated object. The second is a string buffer. The third is |
| 2568 | * a facultative integer that represents where is the buffer position |
| 2569 | * of the start of the data that can send. The first byte is the |
| 2570 | * position "1". The default value is "1". The fourth argument is a |
| 2571 | * facultative integer that represents where is the buffer position |
| 2572 | * of the end of the data that can send. The default is the last byte. |
| 2573 | */ |
| 2574 | static int hlua_socket_send(struct lua_State *L) |
| 2575 | { |
| 2576 | int i; |
| 2577 | int j; |
| 2578 | const char *buf; |
| 2579 | size_t buf_len; |
| 2580 | |
| 2581 | /* Check number of arguments. */ |
| 2582 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2583 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2584 | |
| 2585 | /* Get the string. */ |
| 2586 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2587 | |
| 2588 | /* Get and check j. */ |
| 2589 | if (lua_gettop(L) == 4) { |
| 2590 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2591 | if (j < 0) |
| 2592 | j = buf_len + j + 1; |
| 2593 | if (j > buf_len) |
| 2594 | j = buf_len + 1; |
| 2595 | lua_pop(L, 1); |
| 2596 | } |
| 2597 | else |
| 2598 | j = buf_len; |
| 2599 | |
| 2600 | /* Get and check i. */ |
| 2601 | if (lua_gettop(L) == 3) { |
| 2602 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2603 | if (i < 0) |
| 2604 | i = buf_len + i + 1; |
| 2605 | if (i > buf_len) |
| 2606 | i = buf_len + 1; |
| 2607 | lua_pop(L, 1); |
| 2608 | } else |
| 2609 | i = 1; |
| 2610 | |
| 2611 | /* Check bth i and j. */ |
| 2612 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2613 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | return 1; |
| 2615 | } |
| 2616 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2617 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2618 | return 1; |
| 2619 | } |
| 2620 | if (i == 0) |
| 2621 | i = 1; |
| 2622 | if (j == 0) |
| 2623 | j = 1; |
| 2624 | |
| 2625 | /* Pop the string. */ |
| 2626 | lua_pop(L, 1); |
| 2627 | |
| 2628 | /* Update the buffer length. */ |
| 2629 | buf += i - 1; |
| 2630 | buf_len = j - i + 1; |
| 2631 | lua_pushlstring(L, buf, buf_len); |
| 2632 | |
| 2633 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2634 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2636 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | } |
| 2638 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2639 | #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] | 2640 | __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] | 2641 | { |
| 2642 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2643 | int ret; |
| 2644 | int len; |
| 2645 | char *p; |
| 2646 | |
| 2647 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2648 | if (ret <= 0) { |
| 2649 | lua_pushnil(L); |
| 2650 | return 1; |
| 2651 | } |
| 2652 | |
| 2653 | if (ret == AF_UNIX) { |
| 2654 | lua_pushstring(L, buffer+1); |
| 2655 | return 1; |
| 2656 | } |
| 2657 | else if (ret == AF_INET6) { |
| 2658 | buffer[0] = '['; |
| 2659 | len = strlen(buffer); |
| 2660 | buffer[len] = ']'; |
| 2661 | len++; |
| 2662 | buffer[len] = ':'; |
| 2663 | len++; |
| 2664 | p = buffer; |
| 2665 | } |
| 2666 | else if (ret == AF_INET) { |
| 2667 | p = buffer + 1; |
| 2668 | len = strlen(p); |
| 2669 | p[len] = ':'; |
| 2670 | len++; |
| 2671 | } |
| 2672 | else { |
| 2673 | lua_pushnil(L); |
| 2674 | return 1; |
| 2675 | } |
| 2676 | |
| 2677 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2678 | lua_pushnil(L); |
| 2679 | return 1; |
| 2680 | } |
| 2681 | |
| 2682 | lua_pushstring(L, p); |
| 2683 | return 1; |
| 2684 | } |
| 2685 | |
| 2686 | /* Returns information about the peer of the connection. */ |
| 2687 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2688 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2689 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2690 | struct xref *peer; |
| 2691 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2692 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2693 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2694 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2695 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2696 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2697 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2698 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2699 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2700 | /* Check if we run on the same thread than the xreator thread. |
| 2701 | * We cannot access to the socket if the thread is different. |
| 2702 | */ |
| 2703 | if (socket->tid != tid) |
| 2704 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2705 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2706 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2707 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2708 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2709 | lua_pushnil(L); |
| 2710 | return 1; |
| 2711 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2712 | |
| 2713 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2714 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2715 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2716 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2718 | lua_pushnil(L); |
| 2719 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2720 | } |
| 2721 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2722 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2723 | xref_unlock(&socket->xref, peer); |
| 2724 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | } |
| 2726 | |
| 2727 | /* Returns information about my connection side. */ |
| 2728 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2729 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2730 | struct hlua_socket *socket; |
| 2731 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2732 | struct appctx *appctx; |
| 2733 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2734 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2735 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2736 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2737 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2738 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2739 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2740 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2741 | /* Check if we run on the same thread than the xreator thread. |
| 2742 | * We cannot access to the socket if the thread is different. |
| 2743 | */ |
| 2744 | if (socket->tid != tid) |
| 2745 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2746 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2747 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2748 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2749 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2750 | lua_pushnil(L); |
| 2751 | return 1; |
| 2752 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2753 | |
| 2754 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2755 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2756 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2757 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2758 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2759 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2760 | lua_pushnil(L); |
| 2761 | return 1; |
| 2762 | } |
| 2763 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2764 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | xref_unlock(&socket->xref, peer); |
| 2766 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2767 | } |
| 2768 | |
| 2769 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2770 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2771 | .obj_type = OBJ_TYPE_APPLET, |
| 2772 | .name = "<LUA_TCP>", |
| 2773 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2774 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2775 | .release = hlua_socket_release, |
| 2776 | }; |
| 2777 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2778 | __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] | 2779 | { |
| 2780 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2781 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2782 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2783 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2784 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2785 | struct stream *s; |
| 2786 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2787 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2788 | hlua = hlua_gethlua(L); |
| 2789 | if (!hlua) |
| 2790 | return 0; |
| 2791 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2792 | /* Check if we run on the same thread than the xreator thread. |
| 2793 | * We cannot access to the socket if the thread is different. |
| 2794 | */ |
| 2795 | if (socket->tid != tid) |
| 2796 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2797 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2798 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2799 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2800 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2801 | lua_pushnil(L); |
| 2802 | lua_pushstring(L, "Can't connect"); |
| 2803 | return 2; |
| 2804 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2805 | |
| 2806 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2807 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2808 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2809 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2810 | /* Check if we run on the same thread than the xreator thread. |
| 2811 | * We cannot access to the socket if the thread is different. |
| 2812 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2813 | if (socket->tid != tid) { |
| 2814 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2815 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2816 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2817 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2819 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2820 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2821 | lua_pushnil(L); |
| 2822 | lua_pushstring(L, "Can't connect"); |
| 2823 | return 2; |
| 2824 | } |
| 2825 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2826 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | |
| 2828 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2829 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2830 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2831 | lua_pushinteger(L, 1); |
| 2832 | return 1; |
| 2833 | } |
| 2834 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2835 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2836 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2837 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2838 | } |
| 2839 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2840 | 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] | 2841 | return 0; |
| 2842 | } |
| 2843 | |
| 2844 | /* This function fail or initite the connection. */ |
| 2845 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2846 | { |
| 2847 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2848 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2849 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2850 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2851 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2852 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2853 | int low, high; |
| 2854 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2855 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2856 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2857 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2858 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2859 | if (lua_gettop(L) < 2) |
| 2860 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2861 | |
| 2862 | /* Get args. */ |
| 2863 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2864 | |
| 2865 | /* Check if we run on the same thread than the xreator thread. |
| 2866 | * We cannot access to the socket if the thread is different. |
| 2867 | */ |
| 2868 | if (socket->tid != tid) |
| 2869 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2870 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2871 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2872 | if (lua_gettop(L) >= 3) { |
| 2873 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2874 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2875 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2876 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2877 | * that are not enclosed within square brackets. |
| 2878 | */ |
| 2879 | if (port > 0) { |
| 2880 | luaL_buffinit(L, &b); |
| 2881 | luaL_addstring(&b, ip); |
| 2882 | luaL_addchar(&b, ':'); |
| 2883 | luaL_pushresult(&b); |
| 2884 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2885 | } |
| 2886 | } |
| 2887 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2888 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2889 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2890 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2891 | lua_pushnil(L); |
| 2892 | return 1; |
| 2893 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2894 | |
| 2895 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2896 | 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] | 2897 | if (!addr) { |
| 2898 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2899 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2900 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2901 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2902 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2903 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2904 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2905 | if (port == -1) { |
| 2906 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2907 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2908 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2909 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2910 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2911 | if (port == -1) { |
| 2912 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2913 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2914 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2915 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2916 | } |
| 2917 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2918 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2919 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2920 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2921 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2922 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2923 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2924 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2925 | xref_unlock(&socket->xref, peer); |
| 2926 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2927 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2928 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2929 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2930 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2931 | if (!hlua) |
| 2932 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2933 | |
| 2934 | /* inform the stream that we want to be notified whenever the |
| 2935 | * connection completes. |
| 2936 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2937 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2938 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2939 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2940 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2941 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2942 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2943 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2944 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2945 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2946 | } |
| 2947 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2948 | |
| 2949 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2950 | /* Return yield waiting for connection. */ |
| 2951 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2952 | 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] | 2953 | |
| 2954 | return 0; |
| 2955 | } |
| 2956 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2957 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2958 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2959 | { |
| 2960 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2961 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2962 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2963 | |
| 2964 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2965 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2966 | |
| 2967 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2968 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2969 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2970 | lua_pushnil(L); |
| 2971 | return 1; |
| 2972 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2973 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2974 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2975 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2976 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2977 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2978 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2979 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2980 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2981 | |
| 2982 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2983 | { |
| 2984 | return 0; |
| 2985 | } |
| 2986 | |
| 2987 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2988 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2989 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2990 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2991 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2992 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2993 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2994 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2995 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2996 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2997 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2998 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2999 | /* convert the timeout to millis */ |
| 3000 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3001 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3002 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3003 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 3004 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 3005 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3006 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3007 | 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] | 3008 | |
| 3009 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 3010 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3011 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3012 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3013 | /* Check if we run on the same thread than the xreator thread. |
| 3014 | * We cannot access to the socket if the thread is different. |
| 3015 | */ |
| 3016 | if (socket->tid != tid) |
| 3017 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 3018 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 3019 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3020 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3021 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3022 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 3023 | WILL_LJMP(lua_error(L)); |
| 3024 | return 0; |
| 3025 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3026 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3027 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3028 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3029 | s->sess->fe->timeout.connect = tmout; |
Christopher Faulet | 5aaacfb | 2023-02-15 08:13:33 +0100 | [diff] [blame] | 3030 | s->scf->ioto = tmout; |
| 3031 | s->scb->ioto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3032 | |
| 3033 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3034 | task_queue(s->task); |
| 3035 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3036 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3037 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3038 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3039 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3040 | } |
| 3041 | |
| 3042 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3043 | { |
| 3044 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3045 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3046 | struct appctx *appctx; |
| 3047 | |
| 3048 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3049 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3050 | hlua_pusherror(L, "socket: full stack"); |
| 3051 | goto out_fail_conf; |
| 3052 | } |
| 3053 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3054 | /* Create the object: obj[0] = userdata. */ |
| 3055 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3056 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3057 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3058 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3059 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3060 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3061 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3062 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3063 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3064 | goto out_fail_conf; |
| 3065 | } |
| 3066 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3067 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3068 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3069 | lua_setmetatable(L, -2); |
| 3070 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3071 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3072 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3073 | if (!appctx) { |
| 3074 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3075 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3076 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3077 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3078 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3079 | ctx->die = 0; |
| 3080 | LIST_INIT(&ctx->wake_on_write); |
| 3081 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3082 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3083 | if (appctx_init(appctx) == -1) { |
| 3084 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3085 | goto out_fail_appctx; |
| 3086 | } |
| 3087 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3088 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3089 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3090 | return 1; |
| 3091 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3092 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3093 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3094 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3095 | WILL_LJMP(lua_error(L)); |
| 3096 | return 0; |
| 3097 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3098 | |
| 3099 | /* |
| 3100 | * |
| 3101 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3102 | * Class Channel |
| 3103 | * |
| 3104 | * |
| 3105 | */ |
| 3106 | |
| 3107 | /* Returns the struct hlua_channel join to the class channel in the |
| 3108 | * stack entry "ud" or throws an argument error. |
| 3109 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3110 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3112 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3113 | } |
| 3114 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3115 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3116 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3118 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3120 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3121 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3122 | return 0; |
| 3123 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3124 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3125 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3126 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | |
| 3128 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3129 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3130 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3131 | return 1; |
| 3132 | } |
| 3133 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3134 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3135 | * 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] | 3136 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3137 | * initialized. |
| 3138 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3139 | 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] | 3140 | { |
| 3141 | struct filter *filter = NULL; |
| 3142 | |
| 3143 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3144 | struct hlua_flt_ctx *flt_ctx; |
| 3145 | |
| 3146 | filter = lua_touserdata (L, -1); |
| 3147 | flt_ctx = filter->ctx; |
| 3148 | if (hlua_filter_from_payload(filter)) { |
| 3149 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3150 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3151 | } |
| 3152 | } |
| 3153 | |
| 3154 | lua_pop(L, 1); |
| 3155 | return filter; |
| 3156 | } |
| 3157 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3158 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3159 | * 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] | 3160 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3161 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3162 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3163 | */ |
| 3164 | 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] | 3165 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3166 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | luaL_Buffer b; |
| 3168 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3169 | block1 = len; |
| 3170 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3171 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3172 | block2 = len - block1; |
| 3173 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3175 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3176 | if (block2) |
| 3177 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3178 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3179 | return len; |
| 3180 | } |
| 3181 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3182 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3183 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3184 | * number of bytes copied. |
| 3185 | */ |
| 3186 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3187 | { |
| 3188 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3190 | /* Nothing to do, just return */ |
| 3191 | if (unlikely(istlen(str) == 0)) |
| 3192 | goto end; |
| 3193 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3194 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3195 | ret = -1; |
| 3196 | goto end; |
| 3197 | } |
| 3198 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3199 | |
| 3200 | end: |
| 3201 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3202 | } |
| 3203 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3204 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3205 | */ |
| 3206 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3207 | { |
| 3208 | size_t end = offset + len; |
| 3209 | |
| 3210 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3211 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3212 | b_data(&chn->buf) - end, -len); |
| 3213 | b_sub(&chn->buf, len); |
| 3214 | } |
| 3215 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3216 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3217 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3218 | * offset by default). If there is not enough input data and more data can be |
| 3219 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3220 | * |
| 3221 | * From an action, All input data are considered. For a filter, the offset and |
| 3222 | * 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] | 3223 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3224 | __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] | 3225 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3226 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | struct filter *filter; |
| 3228 | size_t input, output; |
| 3229 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3230 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3231 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3232 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3233 | output = co_data(chn); |
| 3234 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3235 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3237 | if (filter && !hlua_filter_from_payload(filter)) |
| 3238 | WILL_LJMP(lua_error(L)); |
| 3239 | |
| 3240 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3241 | if (lua_gettop(L) > 1) { |
| 3242 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3243 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3244 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3245 | offset += output; |
| 3246 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3247 | lua_pushfstring(L, "offset out of range."); |
| 3248 | WILL_LJMP(lua_error(L)); |
| 3249 | } |
| 3250 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3251 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3252 | if (lua_gettop(L) == 3) { |
| 3253 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3254 | if (!len) |
| 3255 | goto dup; |
| 3256 | if (len == -1) |
| 3257 | len = global.tune.bufsize; |
| 3258 | if (len < 0) { |
| 3259 | lua_pushfstring(L, "length out of range."); |
| 3260 | WILL_LJMP(lua_error(L)); |
| 3261 | } |
| 3262 | } |
| 3263 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3264 | /* Wait for more data if possible if no length was specified and there |
| 3265 | * is no data or not enough data was received. |
| 3266 | */ |
| 3267 | if (!len || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3268 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3269 | /* Yield waiting for more data, as requested */ |
| 3270 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3271 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3272 | |
| 3273 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3274 | if (!len) { |
| 3275 | lua_pushnil(L); |
| 3276 | return -1; |
| 3277 | } |
| 3278 | |
| 3279 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3280 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3281 | } |
| 3282 | |
| 3283 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3284 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3285 | return 1; |
| 3286 | } |
| 3287 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3288 | /* Copies the first line (including the trailing LF) of input data in the |
| 3289 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3290 | * a length (all remaining input data starting for the offset by default). If |
| 3291 | * there is not enough input data and more data can be received, the function |
| 3292 | * yields. If a length is explicitly specified, no more data are |
| 3293 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3294 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | * |
| 3296 | * From an action, All input data are considered. For a filter, the offset and |
| 3297 | * 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] | 3298 | */ |
| 3299 | __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] | 3300 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3301 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3302 | struct filter *filter; |
| 3303 | size_t l, input, output; |
| 3304 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3305 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3306 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3307 | output = co_data(chn); |
| 3308 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3309 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3310 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3311 | if (filter && !hlua_filter_from_payload(filter)) |
| 3312 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3313 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3314 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3315 | if (lua_gettop(L) > 1) { |
| 3316 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3317 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3318 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3319 | offset += output; |
| 3320 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3321 | lua_pushfstring(L, "offset out of range."); |
| 3322 | WILL_LJMP(lua_error(L)); |
| 3323 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3324 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3325 | |
| 3326 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3327 | if (lua_gettop(L) == 3) { |
| 3328 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3329 | if (!len) |
| 3330 | goto dup; |
| 3331 | if (len == -1) |
| 3332 | len = global.tune.bufsize; |
| 3333 | if (len < 0) { |
| 3334 | lua_pushfstring(L, "length out of range."); |
| 3335 | WILL_LJMP(lua_error(L)); |
| 3336 | } |
| 3337 | } |
| 3338 | |
| 3339 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3340 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3342 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3343 | len = l+1; |
| 3344 | goto dup; |
| 3345 | } |
| 3346 | } |
| 3347 | |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3348 | /* Wait for more data if possible if no line is found and no length was |
| 3349 | * specified or not enough data was received. |
| 3350 | */ |
| 3351 | if (lua_gettop(L) != 3 || offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3352 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3353 | /* Yield waiting for more data */ |
| 3354 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3355 | } |
Christopher Faulet | 0ae2e63 | 2023-01-10 15:29:54 +0100 | [diff] [blame] | 3356 | |
| 3357 | /* Return 'nil' if there is no data and the channel can't receive more data */ |
| 3358 | if (!len) { |
| 3359 | lua_pushnil(L); |
| 3360 | return -1; |
| 3361 | } |
| 3362 | |
| 3363 | /* Otherwise, return all data */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3364 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3365 | } |
| 3366 | |
| 3367 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3368 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3369 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3370 | } |
| 3371 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3372 | /* [ DEPRECATED ] |
| 3373 | * |
| 3374 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3375 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3376 | * |
| 3377 | * From an action, All input data are considered. For a filter, the offset and |
| 3378 | * 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] | 3379 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3380 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3381 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3382 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3383 | struct filter *filter; |
| 3384 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3385 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3386 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3387 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3388 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3389 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3390 | WILL_LJMP(lua_error(L)); |
| 3391 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3392 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3393 | offset = co_data(chn); |
| 3394 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3395 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3396 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3397 | if (filter && !hlua_filter_from_payload(filter)) |
| 3398 | WILL_LJMP(lua_error(L)); |
| 3399 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3400 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3401 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3402 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3403 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3404 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3405 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3406 | return 1; |
| 3407 | } |
| 3408 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3409 | /* [ DEPRECATED ] |
| 3410 | * |
| 3411 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3412 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3413 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3414 | * |
| 3415 | * From an action, All input data are considered. For a filter, the offset and |
| 3416 | * 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] | 3417 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3418 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3419 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3420 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3421 | struct filter *filter; |
| 3422 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3423 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3424 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3425 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3426 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3427 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3428 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3429 | WILL_LJMP(lua_error(L)); |
| 3430 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3431 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3432 | offset = co_data(chn); |
| 3433 | len = ci_data(chn); |
| 3434 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3435 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3436 | if (filter && !hlua_filter_from_payload(filter)) |
| 3437 | WILL_LJMP(lua_error(L)); |
| 3438 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3439 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3440 | lua_pushnil(L); |
| 3441 | return 1; |
| 3442 | } |
| 3443 | |
| 3444 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3445 | _hlua_channel_delete(chn, offset, ret); |
| 3446 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3447 | } |
| 3448 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3449 | /* This functions consumes and returns one line. If the channel is closed, |
| 3450 | * 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] | 3451 | * 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] | 3452 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3453 | * |
| 3454 | * From an action, All input data are considered. For a filter, the offset and |
| 3455 | * 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] | 3456 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3457 | __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] | 3458 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3459 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3460 | struct filter *filter; |
| 3461 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3462 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3463 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3464 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3465 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3466 | offset = co_data(chn); |
| 3467 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3468 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3469 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3470 | if (filter && !hlua_filter_from_payload(filter)) |
| 3471 | WILL_LJMP(lua_error(L)); |
| 3472 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3473 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3474 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3475 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3476 | } |
| 3477 | |
| 3478 | for (l = 0; l < len; l++) { |
| 3479 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3480 | len = l+1; |
| 3481 | goto dup; |
| 3482 | } |
| 3483 | } |
| 3484 | |
| 3485 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3486 | /* Yield waiting for more data */ |
| 3487 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3488 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3489 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3490 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3491 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3492 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3493 | return 1; |
| 3494 | } |
| 3495 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3496 | /* [ DEPRECATED ] |
| 3497 | * |
| 3498 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3499 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3500 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3501 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3502 | struct channel *chn; |
| 3503 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3504 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3505 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3506 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3507 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3508 | WILL_LJMP(lua_error(L)); |
| 3509 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3510 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3511 | } |
| 3512 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3513 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3514 | * available input data are returned. The offset may be negactive to start from |
| 3515 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3516 | * size. |
| 3517 | */ |
| 3518 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3519 | { |
| 3520 | struct channel *chn; |
| 3521 | |
| 3522 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3523 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3524 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3525 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3526 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3527 | WILL_LJMP(lua_error(L)); |
| 3528 | } |
| 3529 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3530 | } |
| 3531 | |
| 3532 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3533 | * available input data are returned. The offset may be negactive to start from |
| 3534 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3535 | * size. |
| 3536 | */ |
| 3537 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3538 | { |
| 3539 | struct channel *chn; |
| 3540 | |
| 3541 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3542 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3543 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3544 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3545 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3546 | WILL_LJMP(lua_error(L)); |
| 3547 | } |
| 3548 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3549 | } |
| 3550 | |
| 3551 | /* Appends a string into the input side of channel. It returns the length of the |
| 3552 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3553 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3554 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3555 | * |
| 3556 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3557 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3558 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3559 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3560 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3561 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3562 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3563 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3564 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3565 | |
| 3566 | MAY_LJMP(check_args(L, 2, "append")); |
| 3567 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3568 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3569 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3570 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3571 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3572 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3573 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | offset = co_data(chn); |
| 3575 | len = ci_data(chn); |
| 3576 | |
| 3577 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3578 | if (filter && !hlua_filter_from_payload(filter)) |
| 3579 | WILL_LJMP(lua_error(L)); |
| 3580 | |
| 3581 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3582 | if (ret > 0 && filter) { |
| 3583 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3584 | |
| 3585 | flt_update_offsets(filter, chn, ret); |
| 3586 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3587 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3588 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3589 | return 1; |
| 3590 | } |
| 3591 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3592 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3593 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3594 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3595 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3596 | * |
| 3597 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3598 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3599 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3600 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3601 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3602 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3603 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3604 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3605 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3606 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3607 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3608 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3609 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3610 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3611 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3612 | WILL_LJMP(lua_error(L)); |
| 3613 | } |
| 3614 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3615 | offset = co_data(chn); |
| 3616 | len = ci_data(chn); |
| 3617 | |
| 3618 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3619 | if (filter && !hlua_filter_from_payload(filter)) |
| 3620 | WILL_LJMP(lua_error(L)); |
| 3621 | |
| 3622 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3623 | if (ret > 0 && filter) { |
| 3624 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3625 | |
| 3626 | flt_update_offsets(filter, chn, ret); |
| 3627 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3628 | } |
| 3629 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | lua_pushinteger(L, ret); |
| 3631 | return 1; |
| 3632 | } |
| 3633 | |
| 3634 | /* 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] | 3635 | * 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] | 3636 | * returns the length of the written string, or -1 if the channel is closed or |
| 3637 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3638 | * |
| 3639 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3640 | */ |
| 3641 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3642 | { |
| 3643 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3644 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3645 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3646 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3647 | int ret, offset; |
| 3648 | |
| 3649 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3650 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3651 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3652 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3653 | |
| 3654 | output = co_data(chn); |
| 3655 | input = ci_data(chn); |
| 3656 | |
| 3657 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3658 | if (filter && !hlua_filter_from_payload(filter)) |
| 3659 | WILL_LJMP(lua_error(L)); |
| 3660 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3661 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3662 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3663 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3664 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3665 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3666 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3667 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3668 | lua_pushfstring(L, "offset out of range."); |
| 3669 | WILL_LJMP(lua_error(L)); |
| 3670 | } |
| 3671 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3672 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3673 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3674 | WILL_LJMP(lua_error(L)); |
| 3675 | } |
| 3676 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3677 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3678 | if (ret > 0 && filter) { |
| 3679 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3680 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3681 | flt_update_offsets(filter, chn, ret); |
| 3682 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3683 | } |
| 3684 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3685 | lua_pushinteger(L, ret); |
| 3686 | return 1; |
| 3687 | } |
| 3688 | /* Replaces a given amount of input data at the given offset by a string |
| 3689 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3690 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3691 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3692 | * |
| 3693 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3694 | */ |
| 3695 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3696 | { |
| 3697 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3698 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3699 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3700 | size_t sz, input, output; |
| 3701 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3702 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3703 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3704 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3705 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3706 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3707 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3708 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3709 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3710 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3711 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3712 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3713 | output = co_data(chn); |
| 3714 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3715 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3716 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3717 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3718 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3719 | |
| 3720 | offset = output; |
| 3721 | if (lua_gettop(L) > 2) { |
| 3722 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3723 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3724 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3725 | offset += output; |
| 3726 | if (offset < output || offset > input + output) { |
| 3727 | lua_pushfstring(L, "offset out of range."); |
| 3728 | WILL_LJMP(lua_error(L)); |
| 3729 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3730 | } |
| 3731 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3732 | len = output + input - offset; |
| 3733 | if (lua_gettop(L) == 4) { |
| 3734 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3735 | if (!len) |
| 3736 | goto set; |
| 3737 | if (len == -1) |
| 3738 | len = output + input - offset; |
| 3739 | if (len < 0 || offset + len > output + input) { |
| 3740 | lua_pushfstring(L, "length out of range."); |
| 3741 | WILL_LJMP(lua_error(L)); |
| 3742 | } |
| 3743 | } |
| 3744 | |
| 3745 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3746 | /* 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] | 3747 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3748 | lua_pushinteger(L, -1); |
| 3749 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3750 | _hlua_channel_delete(chn, offset, len); |
| 3751 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3752 | if (filter) { |
| 3753 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3754 | |
| 3755 | len -= (ret > 0 ? ret : 0); |
| 3756 | flt_update_offsets(filter, chn, -len); |
| 3757 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3758 | } |
| 3759 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3760 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3761 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3762 | return 1; |
| 3763 | } |
| 3764 | |
| 3765 | /* 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] | 3766 | * 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] | 3767 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3768 | * |
| 3769 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3770 | */ |
| 3771 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3772 | { |
| 3773 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3774 | struct filter *filter; |
| 3775 | size_t input, output; |
| 3776 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3777 | |
| 3778 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3779 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3780 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3781 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3782 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3783 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3784 | WILL_LJMP(lua_error(L)); |
| 3785 | } |
| 3786 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3787 | output = co_data(chn); |
| 3788 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3789 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3790 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3791 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3792 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3793 | |
| 3794 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3795 | if (lua_gettop(L) > 1) { |
| 3796 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3797 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3798 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3799 | offset += output; |
| 3800 | if (offset < output || offset > input + output) { |
| 3801 | lua_pushfstring(L, "offset out of range."); |
| 3802 | WILL_LJMP(lua_error(L)); |
| 3803 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3804 | } |
| 3805 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3806 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3807 | if (lua_gettop(L) == 3) { |
| 3808 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3809 | if (!len) |
| 3810 | goto end; |
| 3811 | if (len == -1) |
| 3812 | len = output + input - offset; |
| 3813 | if (len < 0 || offset + len > output + input) { |
| 3814 | lua_pushfstring(L, "length out of range."); |
| 3815 | WILL_LJMP(lua_error(L)); |
| 3816 | } |
| 3817 | } |
| 3818 | |
| 3819 | _hlua_channel_delete(chn, offset, len); |
| 3820 | if (filter) { |
| 3821 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3822 | |
| 3823 | flt_update_offsets(filter, chn, -len); |
| 3824 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3825 | } |
| 3826 | |
| 3827 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3828 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3829 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3830 | } |
| 3831 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3832 | /* Append data in the output side of the buffer. This data is immediately |
| 3833 | * sent. The function returns the amount of data written. If the buffer |
| 3834 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3835 | * if the channel is closed. |
| 3836 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3837 | __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] | 3838 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3839 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3840 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3841 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3843 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3844 | struct hlua *hlua; |
| 3845 | |
| 3846 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3847 | hlua = hlua_gethlua(L); |
| 3848 | if (!hlua) { |
| 3849 | lua_pushnil(L); |
| 3850 | return 1; |
| 3851 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3852 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3853 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3854 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3855 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3856 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3857 | offset = co_data(chn); |
| 3858 | len = ci_data(chn); |
| 3859 | |
| 3860 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3861 | if (filter && !hlua_filter_from_payload(filter)) |
| 3862 | WILL_LJMP(lua_error(L)); |
| 3863 | |
| 3864 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3865 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3866 | lua_pushinteger(L, -1); |
| 3867 | return 1; |
| 3868 | } |
| 3869 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3870 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3871 | if (len > sz -l) { |
| 3872 | if (filter) { |
| 3873 | lua_pushinteger(L, -1); |
| 3874 | return 1; |
| 3875 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3876 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3877 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3878 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3879 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3880 | if (ret == -1) { |
| 3881 | lua_pop(L, 1); |
| 3882 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3883 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3884 | } |
| 3885 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3886 | if (filter) { |
| 3887 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3888 | |
| 3889 | |
| 3890 | flt_update_offsets(filter, chn, ret); |
| 3891 | FLT_OFF(filter, chn) += ret; |
| 3892 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3893 | } |
| 3894 | else |
| 3895 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3896 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3897 | l += ret; |
| 3898 | lua_pop(L, 1); |
| 3899 | lua_pushinteger(L, l); |
| 3900 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3901 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3902 | if (l < sz) { |
| 3903 | /* Yield only if the channel's output is not empty. |
| 3904 | * Otherwise it means we cannot add more data. */ |
| 3905 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3906 | return 1; |
| 3907 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3908 | /* If we are waiting for space in the response buffer, we |
| 3909 | * must set the flag WAKERESWR. This flag required the task |
| 3910 | * wake up if any activity is detected on the response buffer. |
| 3911 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3912 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3913 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3914 | else |
| 3915 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3916 | 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] | 3917 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3918 | |
| 3919 | return 1; |
| 3920 | } |
| 3921 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3922 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3923 | * yield the LUA process, and resume it without checking the |
| 3924 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3925 | * |
| 3926 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3927 | */ |
| 3928 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3929 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3930 | struct channel *chn; |
| 3931 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3932 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3933 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3934 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3935 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3936 | WILL_LJMP(lua_error(L)); |
| 3937 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3938 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3939 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3940 | } |
| 3941 | |
| 3942 | /* This function forward and amount of butes. The data pass from |
| 3943 | * the input side of the buffer to the output side, and can be |
| 3944 | * forwarded. This function never fails. |
| 3945 | * |
| 3946 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3947 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3948 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3949 | __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] | 3950 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3951 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3952 | struct filter *filter; |
| 3953 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3954 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3955 | struct hlua *hlua; |
| 3956 | |
| 3957 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3958 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3959 | if (!hlua) { |
| 3960 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3961 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3962 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3963 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3964 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3965 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3966 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3967 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3968 | offset = co_data(chn); |
| 3969 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3970 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3971 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3972 | if (filter && !hlua_filter_from_payload(filter)) |
| 3973 | WILL_LJMP(lua_error(L)); |
| 3974 | |
| 3975 | max = fwd - l; |
| 3976 | if (max > len) |
| 3977 | max = len; |
| 3978 | |
| 3979 | if (filter) { |
| 3980 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3981 | |
| 3982 | FLT_OFF(filter, chn) += max; |
| 3983 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3984 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3985 | } |
| 3986 | else |
| 3987 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3988 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3989 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3990 | lua_pop(L, 1); |
| 3991 | lua_pushinteger(L, l); |
| 3992 | |
| 3993 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3994 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3995 | /* The the input channel or the output channel are closed, we |
| 3996 | * must return the amount of data forwarded. |
| 3997 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3998 | 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] | 3999 | return 1; |
| 4000 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4001 | /* If we are waiting for space data in the response buffer, we |
| 4002 | * must set the flag WAKERESWR. This flag required the task |
| 4003 | * wake up if any activity is detected on the response buffer. |
| 4004 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4005 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4006 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 4007 | else |
| 4008 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 4009 | |
Ilya Shipitsin | 4a689da | 2022-10-29 09:34:32 +0500 | [diff] [blame] | 4010 | /* Otherwise, we can yield waiting for new data in the input side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4011 | 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] | 4012 | } |
| 4013 | |
| 4014 | return 1; |
| 4015 | } |
| 4016 | |
| 4017 | /* Just check the input and prepare the stack for the previous |
| 4018 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4019 | * |
| 4020 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4021 | */ |
| 4022 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 4023 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4024 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4025 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 4026 | MAY_LJMP(check_args(L, 2, "forward")); |
| 4027 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4028 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 4029 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 4030 | WILL_LJMP(lua_error(L)); |
| 4031 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4032 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 4033 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4034 | } |
| 4035 | |
| 4036 | /* Just returns the number of bytes available in the input |
| 4037 | * side of the buffer. This function never fails. |
| 4038 | */ |
| 4039 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 4040 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4041 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4042 | struct filter *filter; |
| 4043 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4044 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4045 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4046 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4047 | |
| 4048 | output = co_data(chn); |
| 4049 | input = ci_data(chn); |
| 4050 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4051 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4052 | lua_pushinteger(L, input); |
| 4053 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4054 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4055 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4056 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4057 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4058 | return 1; |
| 4059 | } |
| 4060 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4061 | /* Returns true if the channel is full. */ |
| 4062 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4063 | { |
| 4064 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4065 | |
| 4066 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4067 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4068 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4069 | * applet). |
| 4070 | */ |
| 4071 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4072 | return 1; |
| 4073 | } |
| 4074 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4075 | /* Returns true if the channel may still receive data. */ |
| 4076 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4077 | { |
| 4078 | struct channel *chn; |
| 4079 | |
| 4080 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4081 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4082 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4083 | return 1; |
| 4084 | } |
| 4085 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4086 | /* Returns true if the channel is the response channel. */ |
| 4087 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4088 | { |
| 4089 | struct channel *chn; |
| 4090 | |
| 4091 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4092 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4093 | |
| 4094 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4095 | return 1; |
| 4096 | } |
| 4097 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4098 | /* Just returns the number of bytes available in the output |
| 4099 | * side of the buffer. This function never fails. |
| 4100 | */ |
| 4101 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4102 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4103 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4104 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4105 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4106 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4107 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4108 | |
| 4109 | output = co_data(chn); |
| 4110 | input = ci_data(chn); |
| 4111 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4112 | |
| 4113 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4114 | return 1; |
| 4115 | } |
| 4116 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4117 | /* |
| 4118 | * |
| 4119 | * |
| 4120 | * Class Fetches |
| 4121 | * |
| 4122 | * |
| 4123 | */ |
| 4124 | |
| 4125 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4126 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4127 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4128 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4129 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4130 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4131 | } |
| 4132 | |
| 4133 | /* This function creates and push in the stack a fetch object according |
| 4134 | * with a current TXN. |
| 4135 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4136 | 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] | 4137 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4138 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4139 | |
| 4140 | /* Check stack size. */ |
| 4141 | if (!lua_checkstack(L, 3)) |
| 4142 | return 0; |
| 4143 | |
| 4144 | /* Create the object: obj[0] = userdata. |
| 4145 | * Note that the base of the Fetches object is the |
| 4146 | * transaction object. |
| 4147 | */ |
| 4148 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4149 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4150 | lua_rawseti(L, -2, 0); |
| 4151 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4152 | hsmp->s = txn->s; |
| 4153 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4154 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4155 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4156 | |
| 4157 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4158 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4159 | lua_setmetatable(L, -2); |
| 4160 | |
| 4161 | return 1; |
| 4162 | } |
| 4163 | |
| 4164 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4165 | * It uses closure argument to store the associated sample-fetch. It |
| 4166 | * returns only one argument or throws an error. An error is thrown |
| 4167 | * only if an error is encountered during the argument parsing. If |
| 4168 | * the "sample-fetch" function fails, nil is returned. |
| 4169 | */ |
| 4170 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4171 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4172 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4173 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4174 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4175 | int i; |
| 4176 | struct sample smp; |
| 4177 | |
| 4178 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4179 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4180 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4181 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4182 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4183 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4184 | /* Check execution authorization. */ |
| 4185 | if (f->use & SMP_USE_HTTP_ANY && |
| 4186 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4187 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4188 | "is not available in Lua services", f->kw); |
| 4189 | WILL_LJMP(lua_error(L)); |
| 4190 | } |
| 4191 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4192 | /* Get extra arguments. */ |
| 4193 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4194 | if (i >= ARGM_NBARGS) |
| 4195 | break; |
| 4196 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4197 | } |
| 4198 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4199 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4200 | |
| 4201 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4202 | 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] | 4203 | |
| 4204 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4205 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4206 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4207 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4208 | } |
| 4209 | |
| 4210 | /* Initialise the sample. */ |
| 4211 | memset(&smp, 0, sizeof(smp)); |
| 4212 | |
| 4213 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4214 | 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] | 4215 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4216 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4217 | lua_pushstring(L, ""); |
| 4218 | else |
| 4219 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4220 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4221 | } |
| 4222 | |
| 4223 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4224 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4225 | hlua_smp2lua_str(L, &smp); |
| 4226 | else |
| 4227 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4228 | |
| 4229 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4230 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4231 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4232 | |
| 4233 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4234 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4235 | WILL_LJMP(lua_error(L)); |
| 4236 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4237 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4238 | |
| 4239 | /* |
| 4240 | * |
| 4241 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4242 | * Class Converters |
| 4243 | * |
| 4244 | * |
| 4245 | */ |
| 4246 | |
| 4247 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4248 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4249 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4250 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4251 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4252 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4253 | } |
| 4254 | |
| 4255 | /* This function creates and push in the stack a Converters object |
| 4256 | * according with a current TXN. |
| 4257 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4258 | 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] | 4259 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4260 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4261 | |
| 4262 | /* Check stack size. */ |
| 4263 | if (!lua_checkstack(L, 3)) |
| 4264 | return 0; |
| 4265 | |
| 4266 | /* Create the object: obj[0] = userdata. |
| 4267 | * Note that the base of the Converters object is the |
| 4268 | * same than the TXN object. |
| 4269 | */ |
| 4270 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4271 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4272 | lua_rawseti(L, -2, 0); |
| 4273 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4274 | hsmp->s = txn->s; |
| 4275 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4276 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4277 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4278 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4279 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4280 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4281 | lua_setmetatable(L, -2); |
| 4282 | |
| 4283 | return 1; |
| 4284 | } |
| 4285 | |
| 4286 | /* This function is an LUA binding. It is called with each converter. |
| 4287 | * It uses closure argument to store the associated converter. It |
| 4288 | * returns only one argument or throws an error. An error is thrown |
| 4289 | * only if an error is encountered during the argument parsing. If |
| 4290 | * the converter function function fails, nil is returned. |
| 4291 | */ |
| 4292 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4293 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4294 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4295 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4296 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4297 | int i; |
| 4298 | struct sample smp; |
| 4299 | |
| 4300 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4301 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4302 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4303 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4304 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4305 | |
| 4306 | /* Get extra arguments. */ |
| 4307 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4308 | if (i >= ARGM_NBARGS) |
| 4309 | break; |
| 4310 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4311 | } |
| 4312 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4313 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4314 | |
| 4315 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4316 | 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] | 4317 | |
| 4318 | /* Run the special args checker. */ |
| 4319 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4320 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4321 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4322 | } |
| 4323 | |
| 4324 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4325 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4326 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4327 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4328 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4329 | } |
| 4330 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4331 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4332 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4333 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4334 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4335 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4336 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4337 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4338 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4339 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4340 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4341 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4342 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4343 | } |
| 4344 | |
| 4345 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4346 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4347 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4348 | lua_pushstring(L, ""); |
| 4349 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4350 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4351 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4352 | } |
| 4353 | |
| 4354 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4355 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4356 | hlua_smp2lua_str(L, &smp); |
| 4357 | else |
| 4358 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4359 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4360 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4361 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4362 | |
| 4363 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4364 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4365 | WILL_LJMP(lua_error(L)); |
| 4366 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4367 | } |
| 4368 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4369 | /* |
| 4370 | * |
| 4371 | * |
| 4372 | * Class AppletTCP |
| 4373 | * |
| 4374 | * |
| 4375 | */ |
| 4376 | |
| 4377 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4378 | * a class stream, otherwise it throws an error. |
| 4379 | */ |
| 4380 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4381 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4382 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4383 | } |
| 4384 | |
| 4385 | /* This function creates and push in the stack an Applet object |
| 4386 | * according with a current TXN. |
| 4387 | */ |
| 4388 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4389 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4390 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4391 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4392 | struct proxy *p; |
| 4393 | |
| 4394 | ALREADY_CHECKED(s); |
| 4395 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4396 | |
| 4397 | /* Check stack size. */ |
| 4398 | if (!lua_checkstack(L, 3)) |
| 4399 | return 0; |
| 4400 | |
| 4401 | /* Create the object: obj[0] = userdata. |
| 4402 | * Note that the base of the Converters object is the |
| 4403 | * same than the TXN object. |
| 4404 | */ |
| 4405 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4406 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4407 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4408 | luactx->appctx = ctx; |
| 4409 | luactx->htxn.s = s; |
| 4410 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4411 | |
| 4412 | /* Create the "f" field that contains a list of fetches. */ |
| 4413 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4414 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4415 | return 0; |
| 4416 | lua_settable(L, -3); |
| 4417 | |
| 4418 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4419 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4420 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4421 | return 0; |
| 4422 | lua_settable(L, -3); |
| 4423 | |
| 4424 | /* Create the "c" field that contains a list of converters. */ |
| 4425 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4426 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4427 | return 0; |
| 4428 | lua_settable(L, -3); |
| 4429 | |
| 4430 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4431 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4432 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4433 | return 0; |
| 4434 | lua_settable(L, -3); |
| 4435 | |
| 4436 | /* Pop a class stream metatable and affect it to the table. */ |
| 4437 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4438 | lua_setmetatable(L, -2); |
| 4439 | |
| 4440 | return 1; |
| 4441 | } |
| 4442 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4443 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4444 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4445 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4446 | struct stream *s; |
| 4447 | const char *name; |
| 4448 | size_t len; |
| 4449 | struct sample smp; |
| 4450 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4451 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4452 | 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] | 4453 | |
| 4454 | /* It is useles to retrieve the stream, but this function |
| 4455 | * runs only in a stream context. |
| 4456 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4457 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4458 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4459 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4460 | |
| 4461 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4462 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4463 | hlua_lua2smp(L, 3, &smp); |
| 4464 | |
| 4465 | /* Store the sample in a variable. */ |
| 4466 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4467 | |
| 4468 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4469 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4470 | else |
| 4471 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4472 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4473 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4474 | } |
| 4475 | |
| 4476 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4477 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4478 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4479 | struct stream *s; |
| 4480 | const char *name; |
| 4481 | size_t len; |
| 4482 | struct sample smp; |
| 4483 | |
| 4484 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4485 | |
| 4486 | /* It is useles to retrieve the stream, but this function |
| 4487 | * runs only in a stream context. |
| 4488 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4489 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4490 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4491 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4492 | |
| 4493 | /* Unset the variable. */ |
| 4494 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4495 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4496 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4497 | } |
| 4498 | |
| 4499 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4500 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4501 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4502 | struct stream *s; |
| 4503 | const char *name; |
| 4504 | size_t len; |
| 4505 | struct sample smp; |
| 4506 | |
| 4507 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4508 | |
| 4509 | /* It is useles to retrieve the stream, but this function |
| 4510 | * runs only in a stream context. |
| 4511 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4512 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4513 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4514 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4515 | |
| 4516 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4517 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4518 | lua_pushnil(L); |
| 4519 | return 1; |
| 4520 | } |
| 4521 | |
| 4522 | return hlua_smp2lua(L, &smp); |
| 4523 | } |
| 4524 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4525 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4526 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4527 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4528 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4529 | struct hlua *hlua; |
| 4530 | |
| 4531 | /* 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] | 4532 | if (!s->hlua) |
| 4533 | return 0; |
| 4534 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4535 | |
| 4536 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4537 | |
| 4538 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4539 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4540 | |
| 4541 | /* Get and store new value. */ |
| 4542 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4543 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4544 | |
| 4545 | return 0; |
| 4546 | } |
| 4547 | |
| 4548 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4549 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4550 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4551 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4552 | struct hlua *hlua; |
| 4553 | |
| 4554 | /* 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] | 4555 | if (!s->hlua) { |
| 4556 | lua_pushnil(L); |
| 4557 | return 1; |
| 4558 | } |
| 4559 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4560 | |
| 4561 | /* Push configuration index in the stack. */ |
| 4562 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4563 | |
| 4564 | return 1; |
| 4565 | } |
| 4566 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4567 | /* If expected data not yet available, it returns a yield. This function |
| 4568 | * consumes the data in the buffer. It returns a string containing the |
| 4569 | * data. This string can be empty. |
| 4570 | */ |
| 4571 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4572 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4573 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4574 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4575 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4576 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4577 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4578 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4579 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4580 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4581 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4582 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4583 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4584 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4585 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4586 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4587 | 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] | 4588 | } |
| 4589 | |
| 4590 | /* End of data: commit the total strings and return. */ |
| 4591 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4592 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4593 | return 1; |
| 4594 | } |
| 4595 | |
| 4596 | /* Ensure that the block 2 length is usable. */ |
| 4597 | if (ret == 1) |
| 4598 | len2 = 0; |
| 4599 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4600 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4601 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4602 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4603 | |
| 4604 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4605 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4607 | return 1; |
| 4608 | } |
| 4609 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4610 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4611 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4612 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4613 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4614 | |
| 4615 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4616 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4617 | |
| 4618 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4619 | } |
| 4620 | |
| 4621 | /* If expected data not yet available, it returns a yield. This function |
| 4622 | * consumes the data in the buffer. It returns a string containing the |
| 4623 | * data. This string can be empty. |
| 4624 | */ |
| 4625 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4626 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4627 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4628 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4629 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4630 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4631 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4632 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4633 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4634 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4635 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4636 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4637 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4638 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4639 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4640 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4641 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4642 | 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] | 4643 | } |
| 4644 | |
| 4645 | /* End of data: commit the total strings and return. */ |
| 4646 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4647 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4648 | return 1; |
| 4649 | } |
| 4650 | |
| 4651 | /* Ensure that the block 2 length is usable. */ |
| 4652 | if (ret == 1) |
| 4653 | len2 = 0; |
| 4654 | |
| 4655 | if (len == -1) { |
| 4656 | |
| 4657 | /* If len == -1, catenate all the data avalaile and |
| 4658 | * yield because we want to get all the data until |
| 4659 | * the end of data stream. |
| 4660 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4661 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4662 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4663 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4664 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4665 | 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] | 4666 | |
| 4667 | } else { |
| 4668 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4669 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4670 | if (len1 > len) |
| 4671 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4672 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4673 | len -= len1; |
| 4674 | |
| 4675 | /* Copy the second block. */ |
| 4676 | if (len2 > len) |
| 4677 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4678 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4679 | len -= len2; |
| 4680 | |
| 4681 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4682 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4684 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4685 | if (len > 0) { |
| 4686 | lua_pushinteger(L, len); |
| 4687 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4688 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4689 | 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] | 4690 | } |
| 4691 | |
| 4692 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4693 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4694 | return 1; |
| 4695 | } |
| 4696 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4697 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4698 | hlua_pusherror(L, "Lua: internal error"); |
| 4699 | WILL_LJMP(lua_error(L)); |
| 4700 | return 0; |
| 4701 | } |
| 4702 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4703 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4704 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4705 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4706 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4707 | int len = -1; |
| 4708 | |
| 4709 | if (lua_gettop(L) > 2) |
| 4710 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4711 | if (lua_gettop(L) >= 2) { |
| 4712 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4713 | lua_pop(L, 1); |
| 4714 | } |
| 4715 | |
| 4716 | /* Confirm or set the required length */ |
| 4717 | lua_pushinteger(L, len); |
| 4718 | |
| 4719 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4720 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4721 | |
| 4722 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4723 | } |
| 4724 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4725 | /* Append data in the output side of the buffer. This data is immediately |
| 4726 | * sent. The function returns the amount of data written. If the buffer |
| 4727 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4728 | * if the channel is closed. |
| 4729 | */ |
| 4730 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4731 | { |
| 4732 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4733 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4734 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4735 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4736 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4737 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4738 | int max; |
| 4739 | |
| 4740 | /* Get the max amount of data which can write as input in the channel. */ |
| 4741 | max = channel_recv_max(chn); |
| 4742 | if (max > (len - l)) |
| 4743 | max = len - l; |
| 4744 | |
| 4745 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4746 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4747 | |
| 4748 | /* update counters. */ |
| 4749 | l += max; |
| 4750 | lua_pop(L, 1); |
| 4751 | lua_pushinteger(L, l); |
| 4752 | |
| 4753 | /* If some data is not send, declares the situation to the |
| 4754 | * applet, and returns a yield. |
| 4755 | */ |
| 4756 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4757 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4758 | 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] | 4759 | } |
| 4760 | |
| 4761 | return 1; |
| 4762 | } |
| 4763 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4764 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4765 | * yield the LUA process, and resume it without checking the |
| 4766 | * input arguments. |
| 4767 | */ |
| 4768 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4769 | { |
| 4770 | MAY_LJMP(check_args(L, 2, "send")); |
| 4771 | lua_pushinteger(L, 0); |
| 4772 | |
| 4773 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4774 | } |
| 4775 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4776 | /* |
| 4777 | * |
| 4778 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4779 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4780 | * |
| 4781 | * |
| 4782 | */ |
| 4783 | |
| 4784 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4785 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4786 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4787 | __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] | 4788 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4789 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4790 | } |
| 4791 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4792 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4793 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4794 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4795 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4796 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4798 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4799 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4800 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4801 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4802 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | struct htx *htx; |
| 4805 | struct htx_blk *blk; |
| 4806 | struct htx_sl *sl; |
| 4807 | struct ist path; |
| 4808 | unsigned long long len = 0; |
| 4809 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4810 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4811 | |
| 4812 | /* Check stack size. */ |
| 4813 | if (!lua_checkstack(L, 3)) |
| 4814 | return 0; |
| 4815 | |
| 4816 | /* Create the object: obj[0] = userdata. |
| 4817 | * Note that the base of the Converters object is the |
| 4818 | * same than the TXN object. |
| 4819 | */ |
| 4820 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4821 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4822 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4823 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4824 | http_ctx->status = 200; /* Default status code returned. */ |
| 4825 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4826 | luactx->htxn.s = s; |
| 4827 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4828 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4829 | /* Create the "f" field that contains a list of fetches. */ |
| 4830 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4831 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4832 | return 0; |
| 4833 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4834 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4835 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4836 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4837 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4838 | return 0; |
| 4839 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4840 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4841 | /* Create the "c" field that contains a list of converters. */ |
| 4842 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4843 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4844 | return 0; |
| 4845 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4846 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4847 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4848 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4849 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4850 | return 0; |
| 4851 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4852 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4853 | htx = htxbuf(&s->req.buf); |
| 4854 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4855 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4856 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4857 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4858 | /* Stores the request method. */ |
| 4859 | lua_pushstring(L, "method"); |
| 4860 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4861 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4862 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4863 | /* Stores the http version. */ |
| 4864 | lua_pushstring(L, "version"); |
| 4865 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4866 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4867 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4868 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4869 | * the array on the top of the stack. |
| 4870 | */ |
| 4871 | lua_pushstring(L, "headers"); |
| 4872 | htxn.s = s; |
| 4873 | htxn.p = px; |
| 4874 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4875 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4876 | return 0; |
| 4877 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4878 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4879 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4880 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4881 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4882 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4883 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4884 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4885 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4886 | q = p; |
| 4887 | while (q < end && *q != '?') |
| 4888 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4889 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4890 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4891 | lua_pushstring(L, "path"); |
| 4892 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4893 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4894 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | /* Stores the query string. */ |
| 4896 | lua_pushstring(L, "qs"); |
| 4897 | if (*q == '?') |
| 4898 | q++; |
| 4899 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4900 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4901 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4902 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4903 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4904 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4905 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4906 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4907 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4908 | break; |
| 4909 | if (type == HTX_BLK_DATA) |
| 4910 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4911 | } |
Christopher Faulet | 2e47e3a | 2023-01-13 11:40:24 +0100 | [diff] [blame] | 4912 | if (htx->extra != HTX_UNKOWN_PAYLOAD_LENGTH) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4913 | len += htx->extra; |
| 4914 | |
| 4915 | /* Stores the request path. */ |
| 4916 | lua_pushstring(L, "length"); |
| 4917 | lua_pushinteger(L, len); |
| 4918 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4919 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4920 | /* Create an empty array of HTTP request headers. */ |
| 4921 | lua_pushstring(L, "response"); |
| 4922 | lua_newtable(L); |
| 4923 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4924 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4925 | /* Pop a class stream metatable and affect it to the table. */ |
| 4926 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4927 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4928 | |
| 4929 | return 1; |
| 4930 | } |
| 4931 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4932 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4933 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4934 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4935 | struct stream *s; |
| 4936 | const char *name; |
| 4937 | size_t len; |
| 4938 | struct sample smp; |
| 4939 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4940 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4941 | 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] | 4942 | |
| 4943 | /* It is useles to retrieve the stream, but this function |
| 4944 | * runs only in a stream context. |
| 4945 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4946 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4947 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4948 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4949 | |
| 4950 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4951 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4952 | hlua_lua2smp(L, 3, &smp); |
| 4953 | |
| 4954 | /* Store the sample in a variable. */ |
| 4955 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4956 | |
| 4957 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4958 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4959 | else |
| 4960 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4961 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4962 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4963 | } |
| 4964 | |
| 4965 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4966 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4967 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4968 | struct stream *s; |
| 4969 | const char *name; |
| 4970 | size_t len; |
| 4971 | struct sample smp; |
| 4972 | |
| 4973 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4974 | |
| 4975 | /* It is useles to retrieve the stream, but this function |
| 4976 | * runs only in a stream context. |
| 4977 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4978 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4979 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4980 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4981 | |
| 4982 | /* Unset the variable. */ |
| 4983 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4984 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4985 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4986 | } |
| 4987 | |
| 4988 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4989 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4990 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4991 | struct stream *s; |
| 4992 | const char *name; |
| 4993 | size_t len; |
| 4994 | struct sample smp; |
| 4995 | |
| 4996 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4997 | |
| 4998 | /* It is useles to retrieve the stream, but this function |
| 4999 | * runs only in a stream context. |
| 5000 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5001 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5002 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5003 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5004 | |
| 5005 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 5006 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 5007 | lua_pushnil(L); |
| 5008 | return 1; |
| 5009 | } |
| 5010 | |
| 5011 | return hlua_smp2lua(L, &smp); |
| 5012 | } |
| 5013 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5014 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 5015 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5016 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5017 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5018 | struct hlua *hlua; |
| 5019 | |
| 5020 | /* 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] | 5021 | if (!s->hlua) |
| 5022 | return 0; |
| 5023 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5024 | |
| 5025 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5026 | |
| 5027 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5028 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5029 | |
| 5030 | /* Get and store new value. */ |
| 5031 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5032 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5033 | |
| 5034 | return 0; |
| 5035 | } |
| 5036 | |
| 5037 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 5038 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5039 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5040 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5041 | struct hlua *hlua; |
| 5042 | |
| 5043 | /* 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] | 5044 | if (!s->hlua) { |
| 5045 | lua_pushnil(L); |
| 5046 | return 1; |
| 5047 | } |
| 5048 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5049 | |
| 5050 | /* Push configuration index in the stack. */ |
| 5051 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5052 | |
| 5053 | return 1; |
| 5054 | } |
| 5055 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5056 | /* If expected data not yet available, it returns a yield. This function |
| 5057 | * consumes the data in the buffer. It returns a string containing the |
| 5058 | * data. This string can be empty. |
| 5059 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5060 | __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] | 5061 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5062 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5063 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5064 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5065 | struct htx *htx; |
| 5066 | struct htx_blk *blk; |
| 5067 | size_t count; |
| 5068 | int stop = 0; |
| 5069 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5070 | htx = htx_from_buf(&req->buf); |
| 5071 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5072 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5073 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5074 | while (count && !stop && blk) { |
| 5075 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5076 | uint32_t sz = htx_get_blksz(blk); |
| 5077 | struct ist v; |
| 5078 | uint32_t vlen; |
| 5079 | char *nl; |
| 5080 | |
| 5081 | vlen = sz; |
| 5082 | if (vlen > count) { |
| 5083 | if (type != HTX_BLK_DATA) |
| 5084 | break; |
| 5085 | vlen = count; |
| 5086 | } |
| 5087 | |
| 5088 | switch (type) { |
| 5089 | case HTX_BLK_UNUSED: |
| 5090 | break; |
| 5091 | |
| 5092 | case HTX_BLK_DATA: |
| 5093 | v = htx_get_blk_value(htx, blk); |
| 5094 | v.len = vlen; |
| 5095 | nl = istchr(v, '\n'); |
| 5096 | if (nl != NULL) { |
| 5097 | stop = 1; |
| 5098 | vlen = nl - v.ptr + 1; |
| 5099 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5100 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5101 | break; |
| 5102 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5103 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5104 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5105 | stop = 1; |
| 5106 | break; |
| 5107 | |
| 5108 | default: |
| 5109 | break; |
| 5110 | } |
| 5111 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5112 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5113 | count -= vlen; |
| 5114 | if (sz == vlen) |
| 5115 | blk = htx_remove_blk(htx, blk); |
| 5116 | else { |
| 5117 | htx_cut_data_blk(htx, blk, vlen); |
| 5118 | break; |
| 5119 | } |
| 5120 | } |
| 5121 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5122 | /* The message was fully consumed and no more data are expected |
| 5123 | * (EOM flag set). |
| 5124 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5125 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5126 | stop = 1; |
| 5127 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5128 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5129 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5130 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5131 | 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] | 5132 | } |
| 5133 | |
| 5134 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5135 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5136 | return 1; |
| 5137 | } |
| 5138 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5139 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5140 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5141 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5142 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5143 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5144 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5145 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5146 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5147 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5148 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5149 | } |
| 5150 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5151 | /* If expected data not yet available, it returns a yield. This function |
| 5152 | * consumes the data in the buffer. It returns a string containing the |
| 5153 | * data. This string can be empty. |
| 5154 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5155 | __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] | 5156 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5157 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5158 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5159 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5160 | struct htx *htx; |
| 5161 | struct htx_blk *blk; |
| 5162 | size_t count; |
| 5163 | int len; |
| 5164 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5165 | htx = htx_from_buf(&req->buf); |
| 5166 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5167 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5168 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5169 | while (count && len && blk) { |
| 5170 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5171 | uint32_t sz = htx_get_blksz(blk); |
| 5172 | struct ist v; |
| 5173 | uint32_t vlen; |
| 5174 | |
| 5175 | vlen = sz; |
| 5176 | if (len > 0 && vlen > len) |
| 5177 | vlen = len; |
| 5178 | if (vlen > count) { |
| 5179 | if (type != HTX_BLK_DATA) |
| 5180 | break; |
| 5181 | vlen = count; |
| 5182 | } |
| 5183 | |
| 5184 | switch (type) { |
| 5185 | case HTX_BLK_UNUSED: |
| 5186 | break; |
| 5187 | |
| 5188 | case HTX_BLK_DATA: |
| 5189 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5190 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5191 | break; |
| 5192 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5193 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5194 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5195 | len = 0; |
| 5196 | break; |
| 5197 | |
| 5198 | default: |
| 5199 | break; |
| 5200 | } |
| 5201 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5202 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5203 | count -= vlen; |
| 5204 | if (len > 0) |
| 5205 | len -= vlen; |
| 5206 | if (sz == vlen) |
| 5207 | blk = htx_remove_blk(htx, blk); |
| 5208 | else { |
| 5209 | htx_cut_data_blk(htx, blk, vlen); |
| 5210 | break; |
| 5211 | } |
| 5212 | } |
| 5213 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5214 | /* The message was fully consumed and no more data are expected |
| 5215 | * (EOM flag set). |
| 5216 | */ |
Christopher Faulet | 904763f | 2023-03-22 14:53:11 +0100 | [diff] [blame] | 5217 | if (htx_is_empty(htx) && (sc->flags & SC_FL_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5218 | len = 0; |
| 5219 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5220 | htx_to_buf(htx, &req->buf); |
| 5221 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5222 | /* If we are no other data available, yield waiting for new data. */ |
| 5223 | if (len) { |
| 5224 | if (len > 0) { |
| 5225 | lua_pushinteger(L, len); |
| 5226 | lua_replace(L, 2); |
| 5227 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5228 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5229 | 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] | 5230 | } |
| 5231 | |
| 5232 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5233 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5234 | return 1; |
| 5235 | } |
| 5236 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5237 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5238 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5239 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5240 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5241 | int len = -1; |
| 5242 | |
| 5243 | /* Check arguments. */ |
| 5244 | if (lua_gettop(L) > 2) |
| 5245 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5246 | if (lua_gettop(L) >= 2) { |
| 5247 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5248 | lua_pop(L, 1); |
| 5249 | } |
| 5250 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5251 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5252 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5253 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5254 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5255 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5256 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5257 | } |
| 5258 | |
| 5259 | /* Append data in the output side of the buffer. This data is immediately |
| 5260 | * sent. The function returns the amount of data written. If the buffer |
| 5261 | * cannot contain the data, the function yields. The function returns -1 |
| 5262 | * if the channel is closed. |
| 5263 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5264 | __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] | 5265 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5266 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5267 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5268 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5269 | struct htx *htx = htx_from_buf(&res->buf); |
| 5270 | const char *data; |
| 5271 | size_t len; |
| 5272 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5273 | int max; |
| 5274 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5275 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5276 | if (!max) |
| 5277 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5278 | |
| 5279 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5280 | |
| 5281 | /* Get the max amount of data which can write as input in the channel. */ |
| 5282 | if (max > (len - l)) |
| 5283 | max = len - l; |
| 5284 | |
| 5285 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5286 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5287 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5288 | |
| 5289 | /* update counters. */ |
| 5290 | l += max; |
| 5291 | lua_pop(L, 1); |
| 5292 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5293 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5294 | /* If some data is not send, declares the situation to the |
| 5295 | * applet, and returns a yield. |
| 5296 | */ |
| 5297 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5298 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5299 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5300 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5301 | 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] | 5302 | } |
| 5303 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5304 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5305 | return 1; |
| 5306 | } |
| 5307 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5308 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5309 | * yield the LUA process, and resume it without checking the |
| 5310 | * input arguments. |
| 5311 | */ |
| 5312 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5313 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5314 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5315 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5316 | |
| 5317 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5318 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5319 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5320 | WILL_LJMP(lua_error(L)); |
| 5321 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5322 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5323 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5324 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5325 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5326 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5327 | } |
| 5328 | |
| 5329 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5330 | { |
| 5331 | const char *name; |
| 5332 | int ret; |
| 5333 | |
| 5334 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5335 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5336 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5337 | |
| 5338 | /* Push in the stack the "response" entry. */ |
| 5339 | ret = lua_getfield(L, 1, "response"); |
| 5340 | if (ret != LUA_TTABLE) { |
| 5341 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5342 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5343 | WILL_LJMP(lua_error(L)); |
| 5344 | } |
| 5345 | |
| 5346 | /* check if the header is already registered if it is not |
| 5347 | * the case, register it. |
| 5348 | */ |
| 5349 | ret = lua_getfield(L, -1, name); |
| 5350 | if (ret == LUA_TNIL) { |
| 5351 | |
| 5352 | /* Entry not found. */ |
| 5353 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5354 | |
| 5355 | /* Insert the new header name in the array in the top of the stack. |
| 5356 | * It left the new array in the top of the stack. |
| 5357 | */ |
| 5358 | lua_newtable(L); |
| 5359 | lua_pushvalue(L, 2); |
| 5360 | lua_pushvalue(L, -2); |
| 5361 | lua_settable(L, -4); |
| 5362 | |
| 5363 | } else if (ret != LUA_TTABLE) { |
| 5364 | |
| 5365 | /* corruption error. */ |
| 5366 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5367 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5368 | WILL_LJMP(lua_error(L)); |
| 5369 | } |
| 5370 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5371 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5372 | * the header value as new entry. |
| 5373 | */ |
| 5374 | lua_pushvalue(L, 3); |
| 5375 | ret = lua_rawlen(L, -2); |
| 5376 | lua_rawseti(L, -2, ret + 1); |
| 5377 | lua_pushboolean(L, 1); |
| 5378 | return 1; |
| 5379 | } |
| 5380 | |
| 5381 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5382 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5383 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5384 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5385 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5386 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5387 | |
| 5388 | if (status < 100 || status > 599) { |
| 5389 | lua_pushboolean(L, 0); |
| 5390 | return 1; |
| 5391 | } |
| 5392 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5393 | http_ctx->status = status; |
| 5394 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5395 | lua_pushboolean(L, 1); |
| 5396 | return 1; |
| 5397 | } |
| 5398 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5399 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5400 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5401 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5402 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5403 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5404 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5405 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5406 | struct htx *htx; |
| 5407 | struct htx_sl *sl; |
| 5408 | struct h1m h1m; |
| 5409 | const char *status, *reason; |
| 5410 | const char *name, *value; |
| 5411 | size_t nlen, vlen; |
| 5412 | unsigned int flags; |
| 5413 | |
| 5414 | /* Send the message at once. */ |
| 5415 | htx = htx_from_buf(&res->buf); |
| 5416 | h1m_init_res(&h1m); |
| 5417 | |
| 5418 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5419 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5420 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5421 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5422 | reason = http_get_reason(http_ctx->status); |
| 5423 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5424 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5425 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5426 | } |
| 5427 | else { |
| 5428 | flags = HTX_SL_F_IS_RESP; |
| 5429 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5430 | } |
| 5431 | if (!sl) { |
| 5432 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5433 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5434 | WILL_LJMP(lua_error(L)); |
| 5435 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5436 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5437 | |
| 5438 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5439 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5440 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5441 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5442 | WILL_LJMP(lua_error(L)); |
| 5443 | } |
| 5444 | |
| 5445 | /* Browse the list of headers. */ |
| 5446 | lua_pushnil(L); |
| 5447 | while(lua_next(L, -2) != 0) { |
| 5448 | /* We expect a string as -2. */ |
| 5449 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5450 | 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] | 5451 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5452 | lua_typename(L, lua_type(L, -2))); |
| 5453 | WILL_LJMP(lua_error(L)); |
| 5454 | } |
| 5455 | name = lua_tolstring(L, -2, &nlen); |
| 5456 | |
| 5457 | /* We expect an array as -1. */ |
| 5458 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5459 | 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] | 5460 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5461 | name, |
| 5462 | lua_typename(L, lua_type(L, -1))); |
| 5463 | WILL_LJMP(lua_error(L)); |
| 5464 | } |
| 5465 | |
| 5466 | /* Browse the table who is on the top of the stack. */ |
| 5467 | lua_pushnil(L); |
| 5468 | while(lua_next(L, -2) != 0) { |
| 5469 | int id; |
| 5470 | |
| 5471 | /* We expect a number as -2. */ |
| 5472 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5473 | 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] | 5474 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5475 | name, |
| 5476 | lua_typename(L, lua_type(L, -2))); |
| 5477 | WILL_LJMP(lua_error(L)); |
| 5478 | } |
| 5479 | id = lua_tointeger(L, -2); |
| 5480 | |
| 5481 | /* We expect a string as -2. */ |
| 5482 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5483 | 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] | 5484 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5485 | name, id, |
| 5486 | lua_typename(L, lua_type(L, -1))); |
| 5487 | WILL_LJMP(lua_error(L)); |
| 5488 | } |
| 5489 | value = lua_tolstring(L, -1, &vlen); |
| 5490 | |
| 5491 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5492 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5493 | int ret; |
| 5494 | |
| 5495 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5496 | if (ret < 0) { |
| 5497 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5498 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5499 | name); |
| 5500 | WILL_LJMP(lua_error(L)); |
| 5501 | } |
| 5502 | else if (ret == 0) |
| 5503 | goto next; /* Skip it */ |
| 5504 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5505 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5506 | struct ist v = ist2(value, vlen); |
| 5507 | int ret; |
| 5508 | |
| 5509 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5510 | if (ret < 0) { |
| 5511 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5512 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5513 | name); |
| 5514 | WILL_LJMP(lua_error(L)); |
| 5515 | } |
| 5516 | else if (ret == 0) |
| 5517 | goto next; /* Skip it */ |
| 5518 | } |
| 5519 | |
| 5520 | /* Add a new header */ |
| 5521 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5522 | 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] | 5523 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5524 | name); |
| 5525 | WILL_LJMP(lua_error(L)); |
| 5526 | } |
| 5527 | next: |
| 5528 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5529 | lua_pop(L, 1); |
| 5530 | } |
| 5531 | |
| 5532 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5533 | lua_pop(L, 1); |
| 5534 | } |
| 5535 | |
| 5536 | if (h1m.flags & H1_MF_CHNK) |
| 5537 | h1m.flags &= ~H1_MF_CLEN; |
| 5538 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5539 | h1m.flags |= H1_MF_XFER_LEN; |
| 5540 | |
| 5541 | /* Uset HTX start-line flags */ |
| 5542 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5543 | flags |= HTX_SL_F_XFER_ENC; |
| 5544 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5545 | flags |= HTX_SL_F_XFER_LEN; |
| 5546 | if (h1m.flags & H1_MF_CHNK) |
| 5547 | flags |= HTX_SL_F_CHNK; |
| 5548 | else if (h1m.flags & H1_MF_CLEN) |
| 5549 | flags |= HTX_SL_F_CLEN; |
| 5550 | if (h1m.body_len == 0) |
| 5551 | flags |= HTX_SL_F_BODYLESS; |
| 5552 | } |
| 5553 | sl->flags |= flags; |
| 5554 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5555 | /* 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] | 5556 | * and the status code implies the presence of a message body, we must |
| 5557 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5558 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5559 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5560 | */ |
| 5561 | 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] | 5562 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5563 | /* Add a new header */ |
| 5564 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5565 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5566 | 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] | 5567 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5568 | WILL_LJMP(lua_error(L)); |
| 5569 | } |
| 5570 | } |
| 5571 | |
| 5572 | /* Finalize headers. */ |
| 5573 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5574 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5575 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5576 | WILL_LJMP(lua_error(L)); |
| 5577 | } |
| 5578 | |
| 5579 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5580 | b_reset(&res->buf); |
| 5581 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5582 | WILL_LJMP(lua_error(L)); |
| 5583 | } |
| 5584 | |
| 5585 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5586 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5587 | |
| 5588 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5589 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5590 | return 0; |
| 5591 | |
| 5592 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5593 | /* We will build the status line and the headers of the HTTP response. |
| 5594 | * We will try send at once if its not possible, we give back the hand |
| 5595 | * waiting for more room. |
| 5596 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5597 | __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] | 5598 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5599 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5600 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5601 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5602 | |
| 5603 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5604 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5605 | 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] | 5606 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5607 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5608 | } |
| 5609 | |
| 5610 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5611 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5612 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5613 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5614 | } |
| 5615 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5616 | /* |
| 5617 | * |
| 5618 | * |
| 5619 | * Class HTTP |
| 5620 | * |
| 5621 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5622 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5623 | |
| 5624 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5625 | * a class stream, otherwise it throws an error. |
| 5626 | */ |
| 5627 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5628 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5629 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5630 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5631 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5632 | /* This function creates and push in the stack a HTTP object |
| 5633 | * according with a current TXN. |
| 5634 | */ |
| 5635 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5636 | { |
| 5637 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5638 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5639 | /* Check stack size. */ |
| 5640 | if (!lua_checkstack(L, 3)) |
| 5641 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5642 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5643 | /* Create the object: obj[0] = userdata. |
| 5644 | * Note that the base of the Converters object is the |
| 5645 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5646 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5647 | lua_newtable(L); |
| 5648 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5649 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5650 | |
| 5651 | htxn->s = txn->s; |
| 5652 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5653 | htxn->dir = txn->dir; |
| 5654 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5655 | |
| 5656 | /* Pop a class stream metatable and affect it to the table. */ |
| 5657 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5658 | lua_setmetatable(L, -2); |
| 5659 | |
| 5660 | return 1; |
| 5661 | } |
| 5662 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5663 | /* This function creates and returns an array containing the status-line |
| 5664 | * elements. This function does not fails. |
| 5665 | */ |
| 5666 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5667 | { |
| 5668 | /* Create the table. */ |
| 5669 | lua_newtable(L); |
| 5670 | |
| 5671 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5672 | lua_pushstring(L, "version"); |
| 5673 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5674 | lua_settable(L, -3); |
| 5675 | lua_pushstring(L, "code"); |
| 5676 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5677 | lua_settable(L, -3); |
| 5678 | lua_pushstring(L, "reason"); |
| 5679 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5680 | lua_settable(L, -3); |
| 5681 | } |
| 5682 | else { |
| 5683 | lua_pushstring(L, "method"); |
| 5684 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5685 | lua_settable(L, -3); |
| 5686 | lua_pushstring(L, "uri"); |
| 5687 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5688 | lua_settable(L, -3); |
| 5689 | lua_pushstring(L, "version"); |
| 5690 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5691 | lua_settable(L, -3); |
| 5692 | } |
| 5693 | return 1; |
| 5694 | } |
| 5695 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5696 | /* This function creates ans returns an array of HTTP headers. |
| 5697 | * This function does not fails. It is used as wrapper with the |
| 5698 | * 2 following functions. |
| 5699 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5700 | __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] | 5701 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5702 | struct htx *htx; |
| 5703 | int32_t pos; |
| 5704 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5705 | /* Create the table. */ |
| 5706 | lua_newtable(L); |
| 5707 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5708 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5709 | htx = htxbuf(&msg->chn->buf); |
| 5710 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5711 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5712 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5713 | struct ist n, v; |
| 5714 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5715 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5716 | if (type == HTX_BLK_HDR) { |
| 5717 | n = htx_get_blk_name(htx,blk); |
| 5718 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5719 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5720 | else if (type == HTX_BLK_EOH) |
| 5721 | break; |
| 5722 | else |
| 5723 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5724 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5725 | /* Check for existing entry: |
| 5726 | * assume that the table is on the top of the stack, and |
| 5727 | * push the key in the stack, the function lua_gettable() |
| 5728 | * perform the lookup. |
| 5729 | */ |
| 5730 | lua_pushlstring(L, n.ptr, n.len); |
| 5731 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5732 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5733 | switch (lua_type(L, -1)) { |
| 5734 | case LUA_TNIL: |
| 5735 | /* Table not found, create it. */ |
| 5736 | lua_pop(L, 1); /* remove the nil value. */ |
| 5737 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5738 | lua_newtable(L); /* create and push empty table. */ |
| 5739 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5740 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5741 | 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] | 5742 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5743 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5744 | case LUA_TTABLE: |
| 5745 | /* Entry found: push the value in the table. */ |
| 5746 | len = lua_rawlen(L, -1); |
| 5747 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5748 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5749 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5750 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5751 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5752 | default: |
| 5753 | /* Other cases are errors. */ |
| 5754 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5755 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5756 | } |
| 5757 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5758 | return 1; |
| 5759 | } |
| 5760 | |
| 5761 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5762 | { |
| 5763 | struct hlua_txn *htxn; |
| 5764 | |
| 5765 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5766 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5767 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5768 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5769 | WILL_LJMP(lua_error(L)); |
| 5770 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5771 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5772 | } |
| 5773 | |
| 5774 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5775 | { |
| 5776 | struct hlua_txn *htxn; |
| 5777 | |
| 5778 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5779 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5780 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5781 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5782 | WILL_LJMP(lua_error(L)); |
| 5783 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5784 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5785 | } |
| 5786 | |
| 5787 | /* This function replace full header, or just a value in |
| 5788 | * the request or in the response. It is a wrapper fir the |
| 5789 | * 4 following functions. |
| 5790 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5791 | __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] | 5792 | { |
| 5793 | size_t name_len; |
| 5794 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5795 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5796 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5797 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5798 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5799 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5800 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5801 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5802 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5803 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5804 | 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] | 5805 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5806 | return 0; |
| 5807 | } |
| 5808 | |
| 5809 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5810 | { |
| 5811 | struct hlua_txn *htxn; |
| 5812 | |
| 5813 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5814 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5815 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5816 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5817 | WILL_LJMP(lua_error(L)); |
| 5818 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5819 | 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] | 5820 | } |
| 5821 | |
| 5822 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5823 | { |
| 5824 | struct hlua_txn *htxn; |
| 5825 | |
| 5826 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5827 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5828 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5829 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5830 | WILL_LJMP(lua_error(L)); |
| 5831 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5832 | 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] | 5833 | } |
| 5834 | |
| 5835 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5836 | { |
| 5837 | struct hlua_txn *htxn; |
| 5838 | |
| 5839 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5840 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5841 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5842 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5843 | WILL_LJMP(lua_error(L)); |
| 5844 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5845 | 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] | 5846 | } |
| 5847 | |
| 5848 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5849 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5850 | struct hlua_txn *htxn; |
| 5851 | |
| 5852 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5853 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5854 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5855 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5856 | WILL_LJMP(lua_error(L)); |
| 5857 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5858 | 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] | 5859 | } |
| 5860 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5861 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5862 | * It is a wrapper for the 2 following functions. |
| 5863 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5864 | __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] | 5865 | { |
| 5866 | size_t len; |
| 5867 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5868 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5869 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5870 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5871 | ctx.blk = NULL; |
| 5872 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5873 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5874 | return 0; |
| 5875 | } |
| 5876 | |
| 5877 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5878 | { |
| 5879 | struct hlua_txn *htxn; |
| 5880 | |
| 5881 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5882 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5883 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5884 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5885 | WILL_LJMP(lua_error(L)); |
| 5886 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5887 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5888 | } |
| 5889 | |
| 5890 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5891 | { |
| 5892 | struct hlua_txn *htxn; |
| 5893 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5894 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5895 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5896 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5897 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5898 | WILL_LJMP(lua_error(L)); |
| 5899 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5900 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5901 | } |
| 5902 | |
| 5903 | /* This function adds an header. It is a wrapper used by |
| 5904 | * the 2 following functions. |
| 5905 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5906 | __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] | 5907 | { |
| 5908 | size_t name_len; |
| 5909 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5910 | size_t value_len; |
| 5911 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5912 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5913 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5914 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5915 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5916 | return 0; |
| 5917 | } |
| 5918 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5919 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5920 | { |
| 5921 | struct hlua_txn *htxn; |
| 5922 | |
| 5923 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5924 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5925 | |
| 5926 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5927 | WILL_LJMP(lua_error(L)); |
| 5928 | |
| 5929 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5930 | } |
| 5931 | |
| 5932 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5933 | { |
| 5934 | struct hlua_txn *htxn; |
| 5935 | |
| 5936 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5937 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5938 | |
| 5939 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5940 | WILL_LJMP(lua_error(L)); |
| 5941 | |
| 5942 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5943 | } |
| 5944 | |
| 5945 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5946 | { |
| 5947 | struct hlua_txn *htxn; |
| 5948 | |
| 5949 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5950 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5951 | |
| 5952 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5953 | WILL_LJMP(lua_error(L)); |
| 5954 | |
| 5955 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5956 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5957 | } |
| 5958 | |
| 5959 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5960 | { |
| 5961 | struct hlua_txn *htxn; |
| 5962 | |
| 5963 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5964 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5965 | |
| 5966 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5967 | WILL_LJMP(lua_error(L)); |
| 5968 | |
| 5969 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5970 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5971 | } |
| 5972 | |
| 5973 | /* This function set the method. */ |
| 5974 | static int hlua_http_req_set_meth(lua_State *L) |
| 5975 | { |
| 5976 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5977 | size_t name_len; |
| 5978 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5979 | |
| 5980 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5981 | WILL_LJMP(lua_error(L)); |
| 5982 | |
| 5983 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5984 | return 1; |
| 5985 | } |
| 5986 | |
| 5987 | /* This function set the method. */ |
| 5988 | static int hlua_http_req_set_path(lua_State *L) |
| 5989 | { |
| 5990 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5991 | size_t name_len; |
| 5992 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5993 | |
| 5994 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5995 | WILL_LJMP(lua_error(L)); |
| 5996 | |
| 5997 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5998 | return 1; |
| 5999 | } |
| 6000 | |
| 6001 | /* This function set the query-string. */ |
| 6002 | static int hlua_http_req_set_query(lua_State *L) |
| 6003 | { |
| 6004 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6005 | size_t name_len; |
| 6006 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6007 | |
| 6008 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6009 | WILL_LJMP(lua_error(L)); |
| 6010 | |
| 6011 | /* Check length. */ |
| 6012 | if (name_len > trash.size - 1) { |
| 6013 | lua_pushboolean(L, 0); |
| 6014 | return 1; |
| 6015 | } |
| 6016 | |
| 6017 | /* Add the mark question as prefix. */ |
| 6018 | chunk_reset(&trash); |
| 6019 | trash.area[trash.data++] = '?'; |
| 6020 | memcpy(trash.area + trash.data, name, name_len); |
| 6021 | trash.data += name_len; |
| 6022 | |
| 6023 | lua_pushboolean(L, |
| 6024 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 6025 | return 1; |
| 6026 | } |
| 6027 | |
| 6028 | /* This function set the uri. */ |
| 6029 | static int hlua_http_req_set_uri(lua_State *L) |
| 6030 | { |
| 6031 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6032 | size_t name_len; |
| 6033 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6034 | |
| 6035 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 6036 | WILL_LJMP(lua_error(L)); |
| 6037 | |
| 6038 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 6039 | return 1; |
| 6040 | } |
| 6041 | |
| 6042 | /* This function set the response code & optionally reason. */ |
| 6043 | static int hlua_http_res_set_status(lua_State *L) |
| 6044 | { |
| 6045 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6046 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6047 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6048 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6049 | |
| 6050 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6051 | WILL_LJMP(lua_error(L)); |
| 6052 | |
| 6053 | http_res_set_status(code, reason, htxn->s); |
| 6054 | return 0; |
| 6055 | } |
| 6056 | |
| 6057 | /* |
| 6058 | * |
| 6059 | * |
| 6060 | * Class HTTPMessage |
| 6061 | * |
| 6062 | * |
| 6063 | */ |
| 6064 | |
| 6065 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6066 | * otherwise it throws an error. |
| 6067 | */ |
| 6068 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6069 | { |
| 6070 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6071 | } |
| 6072 | |
| 6073 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6074 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6075 | 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] | 6076 | { |
| 6077 | /* Check stack size. */ |
| 6078 | if (!lua_checkstack(L, 3)) |
| 6079 | return 0; |
| 6080 | |
| 6081 | lua_newtable(L); |
| 6082 | lua_pushlightuserdata(L, msg); |
| 6083 | lua_rawseti(L, -2, 0); |
| 6084 | |
| 6085 | /* Create the "channel" field that contains the request channel object. */ |
| 6086 | lua_pushstring(L, "channel"); |
| 6087 | if (!hlua_channel_new(L, msg->chn)) |
| 6088 | return 0; |
| 6089 | lua_rawset(L, -3); |
| 6090 | |
| 6091 | /* Pop a class stream metatable and affect it to the table. */ |
| 6092 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6093 | lua_setmetatable(L, -2); |
| 6094 | |
| 6095 | return 1; |
| 6096 | } |
| 6097 | |
| 6098 | /* Helper function returning a filter attached to the HTTP message at the |
| 6099 | * 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] | 6100 | * 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] | 6101 | * filled with output and input length respectively. |
| 6102 | */ |
| 6103 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6104 | { |
| 6105 | struct channel *chn = msg->chn; |
| 6106 | struct htx *htx = htxbuf(&chn->buf); |
| 6107 | struct filter *filter = NULL; |
| 6108 | |
| 6109 | *offset = co_data(msg->chn); |
| 6110 | *len = htx->data - co_data(msg->chn); |
| 6111 | |
| 6112 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6113 | filter = lua_touserdata (L, -1); |
| 6114 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6115 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6116 | |
| 6117 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6118 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6119 | } |
| 6120 | } |
| 6121 | |
| 6122 | lua_pop(L, 1); |
| 6123 | return filter; |
| 6124 | } |
| 6125 | |
| 6126 | /* Returns true if the channel attached to the HTTP message is the response |
| 6127 | * channel. |
| 6128 | */ |
| 6129 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6130 | { |
| 6131 | struct http_msg *msg; |
| 6132 | |
| 6133 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6134 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6135 | |
| 6136 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6137 | return 1; |
| 6138 | } |
| 6139 | |
| 6140 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6141 | * on hlua_http_get_stline(). |
| 6142 | */ |
| 6143 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6144 | { |
| 6145 | struct http_msg *msg; |
| 6146 | struct htx *htx; |
| 6147 | struct htx_sl *sl; |
| 6148 | |
| 6149 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6150 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6151 | |
| 6152 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6153 | WILL_LJMP(lua_error(L)); |
| 6154 | |
| 6155 | htx = htxbuf(&msg->chn->buf); |
| 6156 | sl = http_get_stline(htx); |
| 6157 | if (!sl) |
| 6158 | return 0; |
| 6159 | return hlua_http_get_stline(L, sl); |
| 6160 | } |
| 6161 | |
| 6162 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6163 | * hlua_http_get_headers(). |
| 6164 | */ |
| 6165 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6166 | { |
| 6167 | struct http_msg *msg; |
| 6168 | |
| 6169 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6170 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6171 | |
| 6172 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6173 | WILL_LJMP(lua_error(L)); |
| 6174 | |
| 6175 | return hlua_http_get_headers(L, msg); |
| 6176 | } |
| 6177 | |
| 6178 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6179 | * name. It relies on hlua_http_del_hdr(). |
| 6180 | */ |
| 6181 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6182 | { |
| 6183 | struct http_msg *msg; |
| 6184 | |
| 6185 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6186 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6187 | |
| 6188 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6189 | WILL_LJMP(lua_error(L)); |
| 6190 | |
| 6191 | return hlua_http_del_hdr(L, msg); |
| 6192 | } |
| 6193 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6194 | /* 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] | 6195 | * message given its name against a regex and replaces it if it matches. It |
| 6196 | * relies on hlua_http_rep_hdr(). |
| 6197 | */ |
| 6198 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6199 | { |
| 6200 | struct http_msg *msg; |
| 6201 | |
| 6202 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6203 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6204 | |
| 6205 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6206 | WILL_LJMP(lua_error(L)); |
| 6207 | |
| 6208 | return hlua_http_rep_hdr(L, msg, 1); |
| 6209 | } |
| 6210 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6211 | /* 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] | 6212 | * message given its name against a regex and replaces it if it matches. It |
| 6213 | * relies on hlua_http_rep_hdr(). |
| 6214 | */ |
| 6215 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6216 | { |
| 6217 | struct http_msg *msg; |
| 6218 | |
| 6219 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6220 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6221 | |
| 6222 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6223 | WILL_LJMP(lua_error(L)); |
| 6224 | |
| 6225 | return hlua_http_rep_hdr(L, msg, 0); |
| 6226 | } |
| 6227 | |
| 6228 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6229 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6230 | { |
| 6231 | struct http_msg *msg; |
| 6232 | |
| 6233 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6234 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6235 | |
| 6236 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6237 | WILL_LJMP(lua_error(L)); |
| 6238 | |
| 6239 | return hlua_http_add_hdr(L, msg); |
| 6240 | } |
| 6241 | |
| 6242 | /* Add an header in the HTTP message removing existing headers with the same |
| 6243 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6244 | */ |
| 6245 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6246 | { |
| 6247 | struct http_msg *msg; |
| 6248 | |
| 6249 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6250 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6251 | |
| 6252 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6253 | WILL_LJMP(lua_error(L)); |
| 6254 | |
| 6255 | hlua_http_del_hdr(L, msg); |
| 6256 | return hlua_http_add_hdr(L, msg); |
| 6257 | } |
| 6258 | |
| 6259 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6260 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6261 | { |
| 6262 | struct stream *s; |
| 6263 | struct http_msg *msg; |
| 6264 | const char *name; |
| 6265 | size_t name_len; |
| 6266 | |
| 6267 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6268 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6269 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6270 | |
| 6271 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6272 | WILL_LJMP(lua_error(L)); |
| 6273 | |
| 6274 | s = chn_strm(msg->chn); |
| 6275 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6276 | return 1; |
| 6277 | } |
| 6278 | |
| 6279 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6280 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6281 | { |
| 6282 | struct stream *s; |
| 6283 | struct http_msg *msg; |
| 6284 | const char *name; |
| 6285 | size_t name_len; |
| 6286 | |
| 6287 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6288 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6289 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6290 | |
| 6291 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6292 | WILL_LJMP(lua_error(L)); |
| 6293 | |
| 6294 | s = chn_strm(msg->chn); |
| 6295 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6296 | return 1; |
| 6297 | } |
| 6298 | |
| 6299 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6300 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6301 | { |
| 6302 | struct stream *s; |
| 6303 | struct http_msg *msg; |
| 6304 | const char *name; |
| 6305 | size_t name_len; |
| 6306 | |
| 6307 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6308 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6309 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6310 | |
| 6311 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6312 | WILL_LJMP(lua_error(L)); |
| 6313 | |
| 6314 | /* Check length. */ |
| 6315 | if (name_len > trash.size - 1) { |
| 6316 | lua_pushboolean(L, 0); |
| 6317 | return 1; |
| 6318 | } |
| 6319 | |
| 6320 | /* Add the mark question as prefix. */ |
| 6321 | chunk_reset(&trash); |
| 6322 | trash.area[trash.data++] = '?'; |
| 6323 | memcpy(trash.area + trash.data, name, name_len); |
| 6324 | trash.data += name_len; |
| 6325 | |
| 6326 | s = chn_strm(msg->chn); |
| 6327 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6328 | return 1; |
| 6329 | } |
| 6330 | |
| 6331 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6332 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6333 | { |
| 6334 | struct stream *s; |
| 6335 | struct http_msg *msg; |
| 6336 | const char *name; |
| 6337 | size_t name_len; |
| 6338 | |
| 6339 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6340 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6341 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6342 | |
| 6343 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6344 | WILL_LJMP(lua_error(L)); |
| 6345 | |
| 6346 | s = chn_strm(msg->chn); |
| 6347 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6348 | return 1; |
| 6349 | } |
| 6350 | |
| 6351 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6352 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6353 | { |
| 6354 | struct http_msg *msg; |
| 6355 | unsigned int code; |
| 6356 | const char *reason; |
| 6357 | size_t reason_len; |
| 6358 | |
| 6359 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6360 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6361 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6362 | |
| 6363 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6364 | WILL_LJMP(lua_error(L)); |
| 6365 | |
| 6366 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6367 | return 1; |
| 6368 | } |
| 6369 | |
| 6370 | /* Returns true if the HTTP message is full. */ |
| 6371 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6372 | { |
| 6373 | struct http_msg *msg; |
| 6374 | |
| 6375 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6376 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6377 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6378 | return 1; |
| 6379 | } |
| 6380 | |
| 6381 | /* Returns true if the HTTP message may still receive data. */ |
| 6382 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6383 | { |
| 6384 | struct http_msg *msg; |
| 6385 | struct htx *htx; |
| 6386 | |
| 6387 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6388 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6389 | htx = htxbuf(&msg->chn->buf); |
| 6390 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6391 | return 1; |
| 6392 | } |
| 6393 | |
| 6394 | /* Returns true if the HTTP message EOM was received */ |
| 6395 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6396 | { |
| 6397 | struct http_msg *msg; |
| 6398 | struct htx *htx; |
| 6399 | |
| 6400 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6401 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6402 | htx = htxbuf(&msg->chn->buf); |
| 6403 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6404 | return 1; |
| 6405 | } |
| 6406 | |
| 6407 | /* Returns the number of bytes available in the input side of the HTTP |
| 6408 | * message. This function never fails. |
| 6409 | */ |
| 6410 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6411 | { |
| 6412 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6413 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6414 | |
| 6415 | MAY_LJMP(check_args(L, 1, "input")); |
| 6416 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6417 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6418 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6419 | return 1; |
| 6420 | } |
| 6421 | |
| 6422 | /* Returns the number of bytes available in the output side of the HTTP |
| 6423 | * message. This function never fails. |
| 6424 | */ |
| 6425 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6426 | { |
| 6427 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6428 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6429 | |
| 6430 | MAY_LJMP(check_args(L, 1, "output")); |
| 6431 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6432 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6433 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6434 | return 1; |
| 6435 | } |
| 6436 | |
| 6437 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6438 | * 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] | 6439 | * 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] | 6440 | * block. This function is called during the payload filtering, so the headers |
| 6441 | * are already scheduled for output (from the filter point of view). |
| 6442 | */ |
| 6443 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6444 | { |
| 6445 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6446 | struct htx_blk *blk; |
| 6447 | struct htx_ret htxret; |
| 6448 | luaL_Buffer b; |
| 6449 | int ret = 0; |
| 6450 | |
| 6451 | luaL_buffinit(L, &b); |
| 6452 | htxret = htx_find_offset(htx, offset); |
| 6453 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6454 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6455 | struct ist v; |
| 6456 | |
| 6457 | switch (type) { |
| 6458 | case HTX_BLK_UNUSED: |
| 6459 | break; |
| 6460 | |
| 6461 | case HTX_BLK_DATA: |
| 6462 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6463 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6464 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6465 | |
| 6466 | luaL_addlstring(&b, v.ptr, v.len); |
| 6467 | ret += v.len; |
| 6468 | break; |
| 6469 | |
| 6470 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6471 | if (!ret) |
| 6472 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6473 | goto end; |
| 6474 | } |
| 6475 | offset = 0; |
| 6476 | } |
| 6477 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6478 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6479 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6480 | goto no_data; |
| 6481 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6482 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6483 | |
| 6484 | no_data: |
| 6485 | /* Remove the empty string and push nil on the stack */ |
| 6486 | lua_pop(L, 1); |
| 6487 | lua_pushnil(L); |
| 6488 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6489 | } |
| 6490 | |
| 6491 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6492 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6493 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6494 | * the filter context. |
| 6495 | */ |
| 6496 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6497 | { |
| 6498 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6499 | struct htx_ret htxret; |
| 6500 | int /*max, */ret = 0; |
| 6501 | |
| 6502 | /* Nothing to do, just return */ |
| 6503 | if (unlikely(istlen(str) == 0)) |
| 6504 | goto end; |
| 6505 | |
| 6506 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6507 | ret = -1; |
| 6508 | goto end; |
| 6509 | } |
| 6510 | |
| 6511 | htxret = htx_find_offset(htx, offset); |
| 6512 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6513 | if (!htx_add_last_data(htx, str)) |
| 6514 | goto end; |
| 6515 | } |
| 6516 | else { |
| 6517 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6518 | v.ptr += htxret.ret; |
| 6519 | v.len = 0; |
| 6520 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6521 | goto end; |
| 6522 | } |
| 6523 | ret = str.len; |
| 6524 | if (ret) { |
| 6525 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6526 | flt_update_offsets(filter, msg->chn, ret); |
| 6527 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6528 | } |
| 6529 | |
| 6530 | end: |
| 6531 | htx_to_buf(htx, &msg->chn->buf); |
| 6532 | return ret; |
| 6533 | } |
| 6534 | |
| 6535 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6536 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6537 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6538 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6539 | * update the filter context. |
| 6540 | */ |
| 6541 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6542 | { |
| 6543 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6544 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6545 | struct htx_blk *blk; |
| 6546 | struct htx_ret htxret; |
| 6547 | size_t ret = 0; |
| 6548 | |
| 6549 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6550 | 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] | 6551 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6552 | * of special blocks like HTX_BLK_EOT. |
| 6553 | * We simply truncate after offset |
| 6554 | * (truncate targeted blk and discard the following ones) |
| 6555 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6556 | htx_truncate(htx, offset); |
| 6557 | ret = len; |
| 6558 | goto end; |
| 6559 | } |
| 6560 | |
| 6561 | htxret = htx_find_offset(htx, offset); |
| 6562 | blk = htxret.blk; |
| 6563 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6564 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6565 | struct ist v; |
| 6566 | |
| 6567 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6568 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6569 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6570 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6571 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6572 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6573 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6574 | /* trimming data in blk: discard everything after the offset |
| 6575 | * (replace 'v' with 'IST_NULL') |
| 6576 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6577 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6578 | if (blk && v.len < len) { |
| 6579 | /* In this case, caller wants to keep removing data, |
| 6580 | * but we need to spare current blk |
| 6581 | * because it was already trimmed |
| 6582 | */ |
| 6583 | blk = htx_get_next_blk(htx, blk); |
| 6584 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6585 | len -= v.len; |
| 6586 | ret += v.len; |
| 6587 | } |
| 6588 | |
| 6589 | |
| 6590 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6591 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6592 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6593 | uint32_t sz = htx_get_blksz(blk); |
| 6594 | |
| 6595 | switch (type) { |
| 6596 | case HTX_BLK_UNUSED: |
| 6597 | break; |
| 6598 | |
| 6599 | case HTX_BLK_DATA: |
| 6600 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6601 | /* don't discard whole blk, only part of it |
| 6602 | * (from the beginning) |
| 6603 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6604 | htx_cut_data_blk(htx, blk, len); |
| 6605 | ret += len; |
| 6606 | goto end; |
| 6607 | } |
| 6608 | break; |
| 6609 | |
| 6610 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6611 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6612 | goto end; |
| 6613 | } |
| 6614 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6615 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6616 | len -= sz; |
| 6617 | ret += sz; |
| 6618 | blk = htx_remove_blk(htx, blk); |
| 6619 | } |
| 6620 | |
| 6621 | end: |
| 6622 | flt_update_offsets(filter, msg->chn, -ret); |
| 6623 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6624 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6625 | * to loose the EOM flag if the message is empty. |
| 6626 | */ |
| 6627 | } |
| 6628 | |
| 6629 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6630 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6631 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6632 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6633 | * the stack. |
| 6634 | */ |
| 6635 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6636 | { |
| 6637 | struct http_msg *msg; |
| 6638 | struct filter *filter; |
| 6639 | size_t output, input; |
| 6640 | int offset, len; |
| 6641 | |
| 6642 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6643 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6644 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6645 | |
| 6646 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6647 | WILL_LJMP(lua_error(L)); |
| 6648 | |
| 6649 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6650 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6651 | WILL_LJMP(lua_error(L)); |
| 6652 | |
| 6653 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6654 | lua_pushnil(L); |
| 6655 | return 1; |
| 6656 | } |
| 6657 | |
| 6658 | offset = output; |
| 6659 | if (lua_gettop(L) > 1) { |
| 6660 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6661 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6662 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6663 | offset += output; |
| 6664 | if (offset < output || offset > input + output) { |
| 6665 | lua_pushfstring(L, "offset out of range."); |
| 6666 | WILL_LJMP(lua_error(L)); |
| 6667 | } |
| 6668 | } |
| 6669 | len = output + input - offset; |
| 6670 | if (lua_gettop(L) == 3) { |
| 6671 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6672 | if (!len) |
| 6673 | goto dup; |
| 6674 | if (len == -1) |
| 6675 | len = global.tune.bufsize; |
| 6676 | if (len < 0) { |
| 6677 | lua_pushfstring(L, "length out of range."); |
| 6678 | WILL_LJMP(lua_error(L)); |
| 6679 | } |
| 6680 | } |
| 6681 | |
| 6682 | dup: |
| 6683 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6684 | return 1; |
| 6685 | } |
| 6686 | |
| 6687 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6688 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6689 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6690 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6691 | * yield. An exception is returned if it is called from another callback. |
| 6692 | */ |
| 6693 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6694 | { |
| 6695 | struct http_msg *msg; |
| 6696 | struct filter *filter; |
| 6697 | const char *str; |
| 6698 | size_t offset, len, sz; |
| 6699 | int ret; |
| 6700 | |
| 6701 | MAY_LJMP(check_args(L, 2, "append")); |
| 6702 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6703 | |
| 6704 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6705 | WILL_LJMP(lua_error(L)); |
| 6706 | |
| 6707 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6708 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6709 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6710 | WILL_LJMP(lua_error(L)); |
| 6711 | |
| 6712 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6713 | lua_pushinteger(L, ret); |
| 6714 | return 1; |
| 6715 | } |
| 6716 | |
| 6717 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6718 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6719 | * channel function used to prepend data, this one can only be called inside a |
| 6720 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6721 | * returned if it is called from another callback. |
| 6722 | */ |
| 6723 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6724 | { |
| 6725 | struct http_msg *msg; |
| 6726 | struct filter *filter; |
| 6727 | const char *str; |
| 6728 | size_t offset, len, sz; |
| 6729 | int ret; |
| 6730 | |
| 6731 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6732 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6733 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6734 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6735 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6736 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6737 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6738 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6739 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6740 | WILL_LJMP(lua_error(L)); |
| 6741 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6742 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6743 | lua_pushinteger(L, ret); |
| 6744 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6745 | } |
| 6746 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6747 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6748 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6749 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6750 | * this one can only be called inside a filter, from http_payload callback. So |
| 6751 | * it cannot yield. An exception is returned if it is called from another |
| 6752 | * callback. |
| 6753 | */ |
| 6754 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6755 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6756 | struct http_msg *msg; |
| 6757 | struct filter *filter; |
| 6758 | const char *str; |
| 6759 | size_t input, output, sz; |
| 6760 | int offset; |
| 6761 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6762 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6763 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6764 | 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] | 6765 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6766 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6768 | WILL_LJMP(lua_error(L)); |
| 6769 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6770 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6771 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6772 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6773 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6774 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6775 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6776 | if (lua_gettop(L) > 2) { |
| 6777 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6778 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6779 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6781 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6782 | lua_pushfstring(L, "offset out of range."); |
| 6783 | WILL_LJMP(lua_error(L)); |
| 6784 | } |
| 6785 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6786 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6787 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6788 | lua_pushinteger(L, ret); |
| 6789 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6790 | } |
| 6791 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6792 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6793 | * default all DATA blocks are removed. It returns the amount of data |
| 6794 | * removed. Unlike the channel function used to remove data, this one can only |
| 6795 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6796 | * exception is returned if it is called from another callback. |
| 6797 | */ |
| 6798 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6799 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6800 | struct http_msg *msg; |
| 6801 | struct filter *filter; |
| 6802 | size_t input, output; |
| 6803 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6804 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6805 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6806 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6807 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6808 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6809 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6810 | WILL_LJMP(lua_error(L)); |
| 6811 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6812 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6813 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6814 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6815 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6816 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6817 | if (lua_gettop(L) > 1) { |
| 6818 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6819 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6820 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6821 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6822 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6823 | lua_pushfstring(L, "offset out of range."); |
| 6824 | WILL_LJMP(lua_error(L)); |
| 6825 | } |
| 6826 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6827 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6828 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6829 | if (lua_gettop(L) == 3) { |
| 6830 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6831 | if (!len) |
| 6832 | goto end; |
| 6833 | if (len == -1) |
| 6834 | len = output + input - offset; |
| 6835 | if (len < 0 || offset + len > output + input) { |
| 6836 | lua_pushfstring(L, "length out of range."); |
| 6837 | WILL_LJMP(lua_error(L)); |
| 6838 | } |
| 6839 | } |
| 6840 | |
| 6841 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6842 | |
| 6843 | end: |
| 6844 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6845 | return 1; |
| 6846 | } |
| 6847 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6848 | /* 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] | 6849 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6850 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6851 | * function used to replace data, this one can only be called inside a filter, |
| 6852 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6853 | * it is called from another callback. |
| 6854 | */ |
| 6855 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6856 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6857 | struct http_msg *msg; |
| 6858 | struct filter *filter; |
| 6859 | struct htx *htx; |
| 6860 | const char *str; |
| 6861 | size_t input, output, sz; |
| 6862 | int offset, len; |
| 6863 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6864 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6865 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6866 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6867 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6868 | |
| 6869 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6870 | WILL_LJMP(lua_error(L)); |
| 6871 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6872 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6873 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6874 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6875 | WILL_LJMP(lua_error(L)); |
| 6876 | |
| 6877 | offset = output; |
| 6878 | if (lua_gettop(L) > 2) { |
| 6879 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6880 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6881 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6882 | offset += output; |
| 6883 | if (offset < output || offset > input + output) { |
| 6884 | lua_pushfstring(L, "offset out of range."); |
| 6885 | WILL_LJMP(lua_error(L)); |
| 6886 | } |
| 6887 | } |
| 6888 | |
| 6889 | len = output + input - offset; |
| 6890 | if (lua_gettop(L) == 4) { |
| 6891 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6892 | if (!len) |
| 6893 | goto set; |
| 6894 | if (len == -1) |
| 6895 | len = output + input - offset; |
| 6896 | if (len < 0 || offset + len > output + input) { |
| 6897 | lua_pushfstring(L, "length out of range."); |
| 6898 | WILL_LJMP(lua_error(L)); |
| 6899 | } |
| 6900 | } |
| 6901 | |
| 6902 | set: |
| 6903 | /* Be sure we can copied the string once input data will be removed. */ |
| 6904 | htx = htx_from_buf(&msg->chn->buf); |
| 6905 | if (sz > htx_free_data_space(htx) + len) |
| 6906 | lua_pushinteger(L, -1); |
| 6907 | else { |
| 6908 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6909 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6910 | lua_pushinteger(L, ret); |
| 6911 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6912 | return 1; |
| 6913 | } |
| 6914 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6915 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6916 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6917 | * the channel function used to send data, this one can only be called inside a |
| 6918 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6919 | * returned if it is called from another callback. |
| 6920 | */ |
| 6921 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6922 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6923 | struct http_msg *msg; |
| 6924 | struct filter *filter; |
| 6925 | struct htx *htx; |
| 6926 | const char *str; |
| 6927 | size_t offset, len, sz; |
| 6928 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6929 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6930 | MAY_LJMP(check_args(L, 2, "send")); |
| 6931 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6932 | |
| 6933 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6934 | WILL_LJMP(lua_error(L)); |
| 6935 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6936 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6937 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6938 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6939 | WILL_LJMP(lua_error(L)); |
| 6940 | |
| 6941 | /* Return an error if the channel's output is closed */ |
| 6942 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6943 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6944 | return 1; |
| 6945 | } |
| 6946 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6947 | htx = htx_from_buf(&msg->chn->buf); |
| 6948 | if (sz > htx_free_data_space(htx)) { |
| 6949 | lua_pushinteger(L, -1); |
| 6950 | return 1; |
| 6951 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6952 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6953 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6954 | if (ret > 0) { |
| 6955 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6956 | |
| 6957 | FLT_OFF(filter, msg->chn) += ret; |
| 6958 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6959 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6960 | } |
| 6961 | |
| 6962 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6963 | return 1; |
| 6964 | } |
| 6965 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6966 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6967 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6968 | * channel function used to forward data, this one can only be called inside a |
| 6969 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6970 | * returned if it is called from another callback. All other functions deal with |
| 6971 | * DATA block, this one not. |
| 6972 | */ |
| 6973 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6974 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6975 | struct http_msg *msg; |
| 6976 | struct filter *filter; |
| 6977 | size_t offset, len; |
| 6978 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6979 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6980 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6981 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6982 | |
| 6983 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6984 | WILL_LJMP(lua_error(L)); |
| 6985 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6986 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6987 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6988 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6989 | WILL_LJMP(lua_error(L)); |
| 6990 | |
| 6991 | /* Nothing to do, just return */ |
| 6992 | if (!fwd) |
| 6993 | goto end; |
| 6994 | |
| 6995 | /* Return an error if the channel's output is closed */ |
| 6996 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6997 | ret = -1; |
| 6998 | goto end; |
| 6999 | } |
| 7000 | |
| 7001 | ret = fwd; |
| 7002 | if (ret > len) |
| 7003 | ret = len; |
| 7004 | |
| 7005 | if (ret) { |
| 7006 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 7007 | |
| 7008 | FLT_OFF(filter, msg->chn) += ret; |
| 7009 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 7010 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 7011 | } |
| 7012 | |
| 7013 | end: |
| 7014 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7015 | return 1; |
| 7016 | } |
| 7017 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7018 | /* Set EOM flag on the HTX message. |
| 7019 | * |
| 7020 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7021 | * really know how to do without this feature. |
| 7022 | */ |
| 7023 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7024 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7025 | struct http_msg *msg; |
| 7026 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7027 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7028 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7029 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7030 | htx = htxbuf(&msg->chn->buf); |
| 7031 | htx->flags |= HTX_FL_EOM; |
| 7032 | return 0; |
| 7033 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 7034 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 7035 | /* Unset EOM flag on the HTX message. |
| 7036 | * |
| 7037 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 7038 | * really know how to do without this feature. |
| 7039 | */ |
| 7040 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7041 | { |
| 7042 | struct http_msg *msg; |
| 7043 | struct htx *htx; |
| 7044 | |
| 7045 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7046 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7047 | htx = htxbuf(&msg->chn->buf); |
| 7048 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7049 | return 0; |
| 7050 | } |
| 7051 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7052 | /* |
| 7053 | * |
| 7054 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7055 | * Class HTTPClient |
| 7056 | * |
| 7057 | * |
| 7058 | */ |
| 7059 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7060 | { |
| 7061 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7062 | } |
| 7063 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7064 | |
| 7065 | /* stops the httpclient and ask it to kill itself */ |
| 7066 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7067 | { |
| 7068 | struct hlua_httpclient *hlua_hc; |
| 7069 | |
| 7070 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7071 | |
| 7072 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7073 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7074 | if (MT_LIST_DELETE(&hlua_hc->by_hlua)) { |
| 7075 | /* we won the race against hlua_httpclient_destroy_all() */ |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7076 | httpclient_stop_and_destroy(hlua_hc->hc); |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7077 | hlua_hc->hc = NULL; |
| 7078 | } |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7079 | |
| 7080 | return 0; |
| 7081 | } |
| 7082 | |
| 7083 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7084 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7085 | { |
| 7086 | struct hlua_httpclient *hlua_hc; |
| 7087 | struct hlua *hlua; |
| 7088 | |
| 7089 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7090 | hlua = hlua_gethlua(L); |
| 7091 | if (!hlua) |
| 7092 | return 0; |
| 7093 | |
| 7094 | /* Check stack size. */ |
| 7095 | if (!lua_checkstack(L, 3)) { |
| 7096 | hlua_pusherror(L, "httpclient: full stack"); |
| 7097 | goto err; |
| 7098 | } |
| 7099 | /* Create the object: obj[0] = userdata. */ |
| 7100 | lua_newtable(L); |
| 7101 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7102 | lua_rawseti(L, -2, 0); |
| 7103 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7104 | |
| 7105 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7106 | if (!hlua_hc->hc) |
| 7107 | goto err; |
| 7108 | |
Aurelien DARRAGON | 3ffbf38 | 2023-02-09 17:02:57 +0100 | [diff] [blame] | 7109 | MT_LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7110 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7111 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7112 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7113 | lua_setmetatable(L, -2); |
| 7114 | |
| 7115 | return 1; |
| 7116 | |
| 7117 | err: |
| 7118 | WILL_LJMP(lua_error(L)); |
| 7119 | return 0; |
| 7120 | } |
| 7121 | |
| 7122 | |
| 7123 | /* |
| 7124 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7125 | * httpclient receives some data |
| 7126 | * |
| 7127 | */ |
| 7128 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7129 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7130 | { |
| 7131 | struct hlua *hlua = hc->caller; |
| 7132 | |
| 7133 | if (!hlua || !hlua->task) |
| 7134 | return; |
| 7135 | |
| 7136 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7137 | } |
| 7138 | |
| 7139 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7140 | * Fill the lua stack with headers from the httpclient response |
| 7141 | * This works the same way as the hlua_http_get_headers() function |
| 7142 | */ |
| 7143 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7144 | { |
| 7145 | struct http_hdr *hdr; |
| 7146 | |
| 7147 | lua_newtable(L); |
| 7148 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7149 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7150 | struct ist n, v; |
| 7151 | int len; |
| 7152 | |
| 7153 | n = hdr->n; |
| 7154 | v = hdr->v; |
| 7155 | |
| 7156 | /* Check for existing entry: |
| 7157 | * assume that the table is on the top of the stack, and |
| 7158 | * push the key in the stack, the function lua_gettable() |
| 7159 | * perform the lookup. |
| 7160 | */ |
| 7161 | |
| 7162 | lua_pushlstring(L, n.ptr, n.len); |
| 7163 | lua_gettable(L, -2); |
| 7164 | |
| 7165 | switch (lua_type(L, -1)) { |
| 7166 | case LUA_TNIL: |
| 7167 | /* Table not found, create it. */ |
| 7168 | lua_pop(L, 1); /* remove the nil value. */ |
| 7169 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7170 | lua_newtable(L); /* create and push empty table. */ |
| 7171 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7172 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7173 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7174 | break; |
| 7175 | |
| 7176 | case LUA_TTABLE: |
| 7177 | /* Entry found: push the value in the table. */ |
| 7178 | len = lua_rawlen(L, -1); |
| 7179 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7180 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7181 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7182 | break; |
| 7183 | |
| 7184 | default: |
| 7185 | /* Other cases are errors. */ |
| 7186 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7187 | WILL_LJMP(lua_error(L)); |
| 7188 | } |
| 7189 | } |
| 7190 | return 1; |
| 7191 | } |
| 7192 | |
| 7193 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7194 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7195 | * |
| 7196 | * Caller must free the result |
| 7197 | */ |
| 7198 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7199 | { |
| 7200 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7201 | struct http_hdr *result = NULL; |
| 7202 | uint32_t hdr_num = 0; |
| 7203 | |
| 7204 | lua_pushnil(L); |
| 7205 | while (lua_next(L, -2) != 0) { |
| 7206 | struct ist name, value; |
| 7207 | const char *n, *v; |
| 7208 | size_t nlen, vlen; |
| 7209 | |
| 7210 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7211 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7212 | goto next_hdr; |
| 7213 | } |
| 7214 | |
| 7215 | n = lua_tolstring(L, -2, &nlen); |
| 7216 | name = ist2(n, nlen); |
| 7217 | |
| 7218 | /* Loop on header's values */ |
| 7219 | lua_pushnil(L); |
| 7220 | while (lua_next(L, -2)) { |
| 7221 | if (!lua_isstring(L, -1)) { |
| 7222 | /* Skip the value if it is not a string */ |
| 7223 | goto next_value; |
| 7224 | } |
| 7225 | |
| 7226 | v = lua_tolstring(L, -1, &vlen); |
| 7227 | value = ist2(v, vlen); |
| 7228 | name = ist2(n, nlen); |
| 7229 | |
| 7230 | hdrs[hdr_num].n = istdup(name); |
| 7231 | hdrs[hdr_num].v = istdup(value); |
| 7232 | |
| 7233 | hdr_num++; |
| 7234 | |
| 7235 | next_value: |
| 7236 | lua_pop(L, 1); |
| 7237 | } |
| 7238 | |
| 7239 | next_hdr: |
| 7240 | lua_pop(L, 1); |
| 7241 | |
| 7242 | } |
| 7243 | |
| 7244 | if (hdr_num) { |
| 7245 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7246 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7247 | if (!result) |
| 7248 | goto skip_headers; |
| 7249 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7250 | |
| 7251 | result[hdr_num].n = IST_NULL; |
| 7252 | result[hdr_num].v = IST_NULL; |
| 7253 | } |
| 7254 | |
| 7255 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7256 | |
| 7257 | return result; |
| 7258 | } |
| 7259 | |
| 7260 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7261 | /* |
| 7262 | * For each yield, checks if there is some data in the httpclient and push them |
| 7263 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7264 | * the stack |
| 7265 | */ |
| 7266 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7267 | { |
| 7268 | struct buffer *tr; |
| 7269 | int res; |
| 7270 | struct hlua *hlua = hlua_gethlua(L); |
| 7271 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7272 | |
| 7273 | |
| 7274 | tr = get_trash_chunk(); |
| 7275 | |
| 7276 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7277 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7278 | |
| 7279 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7280 | |
| 7281 | luaL_pushresult(&hlua_hc->b); |
| 7282 | lua_settable(L, -3); |
| 7283 | |
| 7284 | lua_pushstring(L, "status"); |
| 7285 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7286 | lua_settable(L, -3); |
| 7287 | |
| 7288 | |
| 7289 | lua_pushstring(L, "reason"); |
| 7290 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7291 | lua_settable(L, -3); |
| 7292 | |
| 7293 | lua_pushstring(L, "headers"); |
| 7294 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7295 | lua_settable(L, -3); |
| 7296 | |
| 7297 | return 1; |
| 7298 | } |
| 7299 | |
| 7300 | if (httpclient_data(hlua_hc->hc)) |
| 7301 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7302 | |
| 7303 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7304 | |
| 7305 | return 0; |
| 7306 | } |
| 7307 | |
| 7308 | /* |
| 7309 | * Call this when trying to stream a body during a request |
| 7310 | */ |
| 7311 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7312 | { |
| 7313 | struct hlua *hlua; |
| 7314 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7315 | const char *body_str = NULL; |
| 7316 | int ret; |
| 7317 | int end = 0; |
| 7318 | size_t buf_len; |
| 7319 | size_t to_send = 0; |
| 7320 | |
| 7321 | hlua = hlua_gethlua(L); |
| 7322 | |
| 7323 | if (!hlua || !hlua->task) |
| 7324 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7325 | "'frontend', 'backend' or 'task'")); |
| 7326 | |
| 7327 | ret = lua_getfield(L, -1, "body"); |
| 7328 | if (ret != LUA_TSTRING) |
| 7329 | goto rcv; |
| 7330 | |
| 7331 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7332 | lua_pop(L, 1); |
| 7333 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7334 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7335 | |
| 7336 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7337 | end = 1; |
| 7338 | |
| 7339 | /* the end flag is always set since we are using the whole remaining size */ |
| 7340 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7341 | |
| 7342 | if (buf_len > hlua_hc->sent) { |
| 7343 | /* still need to process the buffer */ |
| 7344 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7345 | } else { |
| 7346 | goto rcv; |
| 7347 | /* we sent the whole request buffer we can recv */ |
| 7348 | } |
| 7349 | return 0; |
| 7350 | |
| 7351 | rcv: |
| 7352 | |
| 7353 | /* we return a "res" object */ |
| 7354 | lua_newtable(L); |
| 7355 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7356 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7357 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7358 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7359 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7360 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7361 | |
| 7362 | return 1; |
| 7363 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7364 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7365 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7366 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7367 | */ |
| 7368 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7369 | __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] | 7370 | { |
| 7371 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7372 | struct http_hdr *hdrs = NULL; |
| 7373 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7374 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7375 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7376 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7377 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7378 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7379 | |
| 7380 | hlua = hlua_gethlua(L); |
| 7381 | |
| 7382 | if (!hlua || !hlua->task) |
| 7383 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7384 | "'frontend', 'backend' or 'task'")); |
| 7385 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7386 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7387 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7388 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7389 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7390 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7391 | lua_pushnil(L); /* first key */ |
| 7392 | while (lua_next(L, 2)) { |
| 7393 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7394 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7395 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7396 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7397 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7398 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7399 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7400 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7401 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7402 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7403 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7404 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7405 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7406 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7407 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7408 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7409 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7410 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7411 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7412 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7413 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7414 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7415 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7416 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7417 | } else { |
| 7418 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7419 | } |
| 7420 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7421 | lua_pop(L, 1); |
| 7422 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7423 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7424 | if (!url_str) { |
| 7425 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7426 | return 0; |
| 7427 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7428 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7429 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7430 | |
Aurelien DARRAGON | 0356407 | 2023-02-09 15:26:25 +0100 | [diff] [blame] | 7431 | istfree(&hlua_hc->hc->req.url); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7432 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7433 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7434 | |
| 7435 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7436 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7437 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7438 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7439 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7440 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7441 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7442 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7443 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7444 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7445 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7446 | 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] | 7447 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7448 | /* free the temporary headers array */ |
| 7449 | hdrs_i = hdrs; |
| 7450 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7451 | istfree(&hdrs_i->n); |
| 7452 | istfree(&hdrs_i->v); |
| 7453 | hdrs_i++; |
| 7454 | } |
| 7455 | ha_free(&hdrs); |
| 7456 | |
| 7457 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7458 | if (ret != ERR_NONE) { |
| 7459 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7460 | return 0; |
| 7461 | } |
| 7462 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7463 | if (!httpclient_start(hlua_hc->hc)) |
| 7464 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7465 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7466 | 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] | 7467 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7468 | return 0; |
| 7469 | } |
| 7470 | |
| 7471 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7472 | * Sends an HTTP HEAD request and wait for a response |
| 7473 | * |
| 7474 | * httpclient:head(url, headers, payload) |
| 7475 | */ |
| 7476 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7477 | { |
| 7478 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7479 | } |
| 7480 | |
| 7481 | /* |
| 7482 | * Send an HTTP GET request and wait for a response |
| 7483 | * |
| 7484 | * httpclient:get(url, headers, payload) |
| 7485 | */ |
| 7486 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7487 | { |
| 7488 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7489 | |
| 7490 | } |
| 7491 | |
| 7492 | /* |
| 7493 | * Sends an HTTP PUT request and wait for a response |
| 7494 | * |
| 7495 | * httpclient:put(url, headers, payload) |
| 7496 | */ |
| 7497 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7498 | { |
| 7499 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7500 | } |
| 7501 | |
| 7502 | /* |
| 7503 | * Send an HTTP POST request and wait for a response |
| 7504 | * |
| 7505 | * httpclient:post(url, headers, payload) |
| 7506 | */ |
| 7507 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7508 | { |
| 7509 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7510 | } |
| 7511 | |
| 7512 | |
| 7513 | /* |
| 7514 | * Sends an HTTP DELETE request and wait for a response |
| 7515 | * |
| 7516 | * httpclient:delete(url, headers, payload) |
| 7517 | */ |
| 7518 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7519 | { |
| 7520 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7521 | } |
| 7522 | |
| 7523 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7524 | * |
| 7525 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7526 | * Class TXN |
| 7527 | * |
| 7528 | * |
| 7529 | */ |
| 7530 | |
| 7531 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7532 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7533 | */ |
| 7534 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7535 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7536 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7537 | } |
| 7538 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7539 | __LJMP static int hlua_set_var(lua_State *L) |
| 7540 | { |
| 7541 | struct hlua_txn *htxn; |
| 7542 | const char *name; |
| 7543 | size_t len; |
| 7544 | struct sample smp; |
| 7545 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7546 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7547 | 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] | 7548 | |
| 7549 | /* It is useles to retrieve the stream, but this function |
| 7550 | * runs only in a stream context. |
| 7551 | */ |
| 7552 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7553 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7554 | |
| 7555 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7556 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7557 | hlua_lua2smp(L, 3, &smp); |
| 7558 | |
| 7559 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7560 | 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] | 7561 | |
| 7562 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7563 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7564 | else |
| 7565 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7566 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7567 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7568 | } |
| 7569 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7570 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7571 | { |
| 7572 | struct hlua_txn *htxn; |
| 7573 | const char *name; |
| 7574 | size_t len; |
| 7575 | struct sample smp; |
| 7576 | |
| 7577 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7578 | |
| 7579 | /* It is useles to retrieve the stream, but this function |
| 7580 | * runs only in a stream context. |
| 7581 | */ |
| 7582 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7583 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7584 | |
| 7585 | /* Unset the variable. */ |
| 7586 | 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] | 7587 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7588 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7589 | } |
| 7590 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7591 | __LJMP static int hlua_get_var(lua_State *L) |
| 7592 | { |
| 7593 | struct hlua_txn *htxn; |
| 7594 | const char *name; |
| 7595 | size_t len; |
| 7596 | struct sample smp; |
| 7597 | |
| 7598 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7599 | |
| 7600 | /* It is useles to retrieve the stream, but this function |
| 7601 | * runs only in a stream context. |
| 7602 | */ |
| 7603 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7604 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7605 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7606 | 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] | 7607 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7608 | lua_pushnil(L); |
| 7609 | return 1; |
| 7610 | } |
| 7611 | |
| 7612 | return hlua_smp2lua(L, &smp); |
| 7613 | } |
| 7614 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7615 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7616 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7617 | struct hlua *hlua; |
| 7618 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7619 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7620 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7621 | /* It is useles to retrieve the stream, but this function |
| 7622 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7623 | */ |
| 7624 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7625 | |
| 7626 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7627 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7628 | if (!hlua) |
| 7629 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7630 | |
| 7631 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7632 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7633 | |
| 7634 | /* Get and store new value. */ |
| 7635 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7636 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7637 | |
| 7638 | return 0; |
| 7639 | } |
| 7640 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7641 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7642 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7643 | struct hlua *hlua; |
| 7644 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7645 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7646 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7647 | /* It is useles to retrieve the stream, but this function |
| 7648 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7649 | */ |
| 7650 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7651 | |
| 7652 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7653 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7654 | if (!hlua) { |
| 7655 | lua_pushnil(L); |
| 7656 | return 1; |
| 7657 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7658 | |
| 7659 | /* Push configuration index in the stack. */ |
| 7660 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7661 | |
| 7662 | return 1; |
| 7663 | } |
| 7664 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7665 | /* Create stack entry containing a class TXN. This function |
| 7666 | * return 0 if the stack does not contains free slots, |
| 7667 | * otherwise it returns 1. |
| 7668 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7669 | 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] | 7670 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7671 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7672 | |
| 7673 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7674 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7675 | return 0; |
| 7676 | |
| 7677 | /* NOTE: The allocation never fails. The failure |
| 7678 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7679 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7680 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7681 | /* Create the object: obj[0] = userdata. */ |
| 7682 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7683 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7684 | lua_rawseti(L, -2, 0); |
| 7685 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7686 | htxn->s = s; |
| 7687 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7688 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7689 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7690 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7691 | /* Create the "f" field that contains a list of fetches. */ |
| 7692 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7693 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7694 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7695 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7696 | |
| 7697 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7698 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7699 | 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] | 7700 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7701 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7702 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7703 | /* Create the "c" field that contains a list of converters. */ |
| 7704 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7705 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7706 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7707 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7708 | |
| 7709 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7710 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7711 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7712 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7713 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7714 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7715 | /* Create the "req" field that contains the request channel object. */ |
| 7716 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7717 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7718 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7719 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7720 | |
| 7721 | /* Create the "res" field that contains the response channel object. */ |
| 7722 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7723 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7724 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7725 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7726 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7727 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7728 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7729 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7730 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7731 | return 0; |
| 7732 | } |
| 7733 | else |
| 7734 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7735 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7736 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7737 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7738 | /* HTTPMessage object are created when a lua TXN is created from |
| 7739 | * a filter context only |
| 7740 | */ |
| 7741 | |
| 7742 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7743 | lua_pushstring(L, "http_req"); |
| 7744 | if (p->mode == PR_MODE_HTTP) { |
| 7745 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7746 | return 0; |
| 7747 | } |
| 7748 | else |
| 7749 | lua_pushnil(L); |
| 7750 | lua_rawset(L, -3); |
| 7751 | |
| 7752 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7753 | lua_pushstring(L, "http_res"); |
| 7754 | if (p->mode == PR_MODE_HTTP) { |
| 7755 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7756 | return 0; |
| 7757 | } |
| 7758 | else |
| 7759 | lua_pushnil(L); |
| 7760 | lua_rawset(L, -3); |
| 7761 | } |
| 7762 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7763 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7764 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7765 | lua_setmetatable(L, -2); |
| 7766 | |
| 7767 | return 1; |
| 7768 | } |
| 7769 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7770 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7771 | { |
| 7772 | const char *msg; |
| 7773 | struct hlua_txn *htxn; |
| 7774 | |
| 7775 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7776 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7777 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7778 | |
| 7779 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7780 | return 0; |
| 7781 | } |
| 7782 | |
| 7783 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7784 | { |
| 7785 | int level; |
| 7786 | const char *msg; |
| 7787 | struct hlua_txn *htxn; |
| 7788 | |
| 7789 | MAY_LJMP(check_args(L, 3, "log")); |
| 7790 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7791 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7792 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7793 | |
| 7794 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7795 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7796 | |
| 7797 | hlua_sendlog(htxn->s->be, level, msg); |
| 7798 | return 0; |
| 7799 | } |
| 7800 | |
| 7801 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7802 | { |
| 7803 | const char *msg; |
| 7804 | struct hlua_txn *htxn; |
| 7805 | |
| 7806 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7807 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7808 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7809 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7810 | return 0; |
| 7811 | } |
| 7812 | |
| 7813 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7814 | { |
| 7815 | const char *msg; |
| 7816 | struct hlua_txn *htxn; |
| 7817 | |
| 7818 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7819 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7820 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7821 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7822 | return 0; |
| 7823 | } |
| 7824 | |
| 7825 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7826 | { |
| 7827 | const char *msg; |
| 7828 | struct hlua_txn *htxn; |
| 7829 | |
| 7830 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7831 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7832 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7833 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7834 | return 0; |
| 7835 | } |
| 7836 | |
| 7837 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7838 | { |
| 7839 | const char *msg; |
| 7840 | struct hlua_txn *htxn; |
| 7841 | |
| 7842 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7843 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7844 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7845 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7846 | return 0; |
| 7847 | } |
| 7848 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7849 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7850 | { |
| 7851 | struct hlua_txn *htxn; |
| 7852 | int ll; |
| 7853 | |
| 7854 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7855 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7856 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7857 | |
| 7858 | if (ll < 0 || ll > 7) |
| 7859 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7860 | |
| 7861 | htxn->s->logs.level = ll; |
| 7862 | return 0; |
| 7863 | } |
| 7864 | |
| 7865 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7866 | { |
| 7867 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7868 | int tos; |
| 7869 | |
| 7870 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7871 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7872 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7873 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7874 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7875 | return 0; |
| 7876 | } |
| 7877 | |
| 7878 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7879 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7880 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7881 | int mark; |
| 7882 | |
| 7883 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7884 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7885 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7886 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7887 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7888 | return 0; |
| 7889 | } |
| 7890 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7891 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7892 | { |
| 7893 | struct hlua_txn *htxn; |
| 7894 | |
| 7895 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7896 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7897 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7898 | return 0; |
| 7899 | } |
| 7900 | |
| 7901 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7902 | { |
| 7903 | struct hlua_txn *htxn; |
| 7904 | |
| 7905 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7906 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7907 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7908 | return 0; |
| 7909 | } |
| 7910 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7911 | /* 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] | 7912 | * 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] | 7913 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7914 | * error. The Reply must be on top of the stack. |
| 7915 | */ |
| 7916 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7917 | { |
| 7918 | struct htx *htx; |
| 7919 | struct htx_sl *sl; |
| 7920 | struct h1m h1m; |
| 7921 | const char *status, *reason, *body; |
| 7922 | size_t status_len, reason_len, body_len; |
| 7923 | int ret, code, flags; |
| 7924 | |
| 7925 | code = 200; |
| 7926 | status = "200"; |
| 7927 | status_len = 3; |
| 7928 | ret = lua_getfield(L, -1, "status"); |
| 7929 | if (ret == LUA_TNUMBER) { |
| 7930 | code = lua_tointeger(L, -1); |
| 7931 | status = lua_tolstring(L, -1, &status_len); |
| 7932 | } |
| 7933 | lua_pop(L, 1); |
| 7934 | |
| 7935 | reason = http_get_reason(code); |
| 7936 | reason_len = strlen(reason); |
| 7937 | ret = lua_getfield(L, -1, "reason"); |
| 7938 | if (ret == LUA_TSTRING) |
| 7939 | reason = lua_tolstring(L, -1, &reason_len); |
| 7940 | lua_pop(L, 1); |
| 7941 | |
| 7942 | body = NULL; |
| 7943 | body_len = 0; |
| 7944 | ret = lua_getfield(L, -1, "body"); |
| 7945 | if (ret == LUA_TSTRING) |
| 7946 | body = lua_tolstring(L, -1, &body_len); |
| 7947 | lua_pop(L, 1); |
| 7948 | |
| 7949 | /* Prepare the response before inserting the headers */ |
| 7950 | h1m_init_res(&h1m); |
| 7951 | htx = htx_from_buf(&s->res.buf); |
| 7952 | channel_htx_truncate(&s->res, htx); |
| 7953 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7954 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7955 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7956 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7957 | } |
| 7958 | else { |
| 7959 | flags = HTX_SL_F_IS_RESP; |
| 7960 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7961 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7962 | } |
| 7963 | if (!sl) |
| 7964 | goto fail; |
| 7965 | sl->info.res.status = code; |
| 7966 | |
| 7967 | /* Push in the stack the "headers" entry. */ |
| 7968 | ret = lua_getfield(L, -1, "headers"); |
| 7969 | if (ret != LUA_TTABLE) |
| 7970 | goto skip_headers; |
| 7971 | |
| 7972 | lua_pushnil(L); |
| 7973 | while (lua_next(L, -2) != 0) { |
| 7974 | struct ist name, value; |
| 7975 | const char *n, *v; |
| 7976 | size_t nlen, vlen; |
| 7977 | |
| 7978 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7979 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7980 | goto next_hdr; |
| 7981 | } |
| 7982 | |
| 7983 | n = lua_tolstring(L, -2, &nlen); |
| 7984 | name = ist2(n, nlen); |
| 7985 | if (isteqi(name, ist("content-length"))) { |
| 7986 | /* Always skip content-length header. It will be added |
| 7987 | * later with the correct len |
| 7988 | */ |
| 7989 | goto next_hdr; |
| 7990 | } |
| 7991 | |
| 7992 | /* Loop on header's values */ |
| 7993 | lua_pushnil(L); |
| 7994 | while (lua_next(L, -2)) { |
| 7995 | if (!lua_isstring(L, -1)) { |
| 7996 | /* Skip the value if it is not a string */ |
| 7997 | goto next_value; |
| 7998 | } |
| 7999 | |
| 8000 | v = lua_tolstring(L, -1, &vlen); |
| 8001 | value = ist2(v, vlen); |
| 8002 | |
| 8003 | if (isteqi(name, ist("transfer-encoding"))) |
| 8004 | h1_parse_xfer_enc_header(&h1m, value); |
| 8005 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 8006 | goto fail; |
| 8007 | |
| 8008 | next_value: |
| 8009 | lua_pop(L, 1); |
| 8010 | } |
| 8011 | |
| 8012 | next_hdr: |
| 8013 | lua_pop(L, 1); |
| 8014 | } |
| 8015 | skip_headers: |
| 8016 | lua_pop(L, 1); |
| 8017 | |
| 8018 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 8019 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 8020 | const char *clen = ultoa(body_len); |
| 8021 | |
| 8022 | h1m.flags |= H1_MF_CLEN; |
| 8023 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 8024 | goto fail; |
| 8025 | } |
| 8026 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 8027 | h1m.flags |= H1_MF_XFER_LEN; |
| 8028 | |
| 8029 | /* Update HTX start-line flags */ |
| 8030 | if (h1m.flags & H1_MF_XFER_ENC) |
| 8031 | flags |= HTX_SL_F_XFER_ENC; |
| 8032 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 8033 | flags |= HTX_SL_F_XFER_LEN; |
| 8034 | if (h1m.flags & H1_MF_CHNK) |
| 8035 | flags |= HTX_SL_F_CHNK; |
| 8036 | else if (h1m.flags & H1_MF_CLEN) |
| 8037 | flags |= HTX_SL_F_CLEN; |
| 8038 | if (h1m.body_len == 0) |
| 8039 | flags |= HTX_SL_F_BODYLESS; |
| 8040 | } |
| 8041 | sl->flags |= flags; |
| 8042 | |
| 8043 | |
| 8044 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8045 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8046 | goto fail; |
| 8047 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8048 | htx->flags |= HTX_FL_EOM; |
| 8049 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8050 | /* Now, forward the response and terminate the transaction */ |
| 8051 | s->txn->status = code; |
| 8052 | htx_to_buf(htx, &s->res.buf); |
| 8053 | if (!http_forward_proxy_resp(s, 1)) |
| 8054 | goto fail; |
| 8055 | |
| 8056 | return 1; |
| 8057 | |
| 8058 | fail: |
| 8059 | channel_htx_truncate(&s->res, htx); |
| 8060 | return 0; |
| 8061 | } |
| 8062 | |
| 8063 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8064 | * processing is just aborted. Nothing is returned to the client and all |
| 8065 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8066 | * is forwarded to the client before terminating the transaction. On success, |
| 8067 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8068 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8069 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8070 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8071 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8072 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8073 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8074 | struct stream *s; |
| 8075 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8076 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8077 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8078 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8079 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8080 | * just end the execution of the current lua code. */ |
| 8081 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8082 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8083 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8084 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8085 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8086 | struct channel *req = &s->req; |
| 8087 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8088 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8089 | channel_auto_read(req); |
| 8090 | channel_abort(req); |
| 8091 | channel_auto_close(req); |
| 8092 | channel_erase(req); |
| 8093 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8094 | channel_auto_read(res); |
| 8095 | channel_auto_close(res); |
| 8096 | channel_shutr_now(res); |
| 8097 | |
| 8098 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8099 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8100 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8101 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8102 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8103 | /* No reply or invalid reply */ |
| 8104 | s->txn->status = 0; |
| 8105 | http_reply_and_close(s, 0, NULL); |
| 8106 | } |
| 8107 | else { |
| 8108 | /* Remove extra args to have the reply on top of the stack */ |
| 8109 | if (lua_gettop(L) > 2) |
| 8110 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8111 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8112 | if (!hlua_txn_forward_reply(L, s)) { |
| 8113 | if (!(s->flags & SF_ERR_MASK)) |
| 8114 | s->flags |= SF_ERR_PRXCOND; |
| 8115 | lua_pushinteger(L, ACT_RET_ERR); |
| 8116 | WILL_LJMP(hlua_done(L)); |
| 8117 | return 0; /* Never reached */ |
| 8118 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8119 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8120 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8121 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8122 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8123 | /* let's log the request time */ |
| 8124 | s->logs.tv_request = now; |
| 8125 | 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] | 8126 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8127 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8128 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8129 | done: |
| 8130 | if (!(s->flags & SF_ERR_MASK)) |
| 8131 | s->flags |= SF_ERR_LOCAL; |
| 8132 | if (!(s->flags & SF_FINST_MASK)) |
| 8133 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8134 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8135 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8136 | lua_pushinteger(L, -1); |
| 8137 | else |
| 8138 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8139 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8140 | return 0; |
| 8141 | } |
| 8142 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8143 | /* |
| 8144 | * |
| 8145 | * |
| 8146 | * Class REPLY |
| 8147 | * |
| 8148 | * |
| 8149 | */ |
| 8150 | |
| 8151 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8152 | * free slots, the function fails and returns 0; |
| 8153 | */ |
| 8154 | static int hlua_txn_reply_new(lua_State *L) |
| 8155 | { |
| 8156 | struct hlua_txn *htxn; |
| 8157 | const char *reason, *body = NULL; |
| 8158 | int ret, status; |
| 8159 | |
| 8160 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8161 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8162 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8163 | WILL_LJMP(lua_error(L)); |
| 8164 | } |
| 8165 | |
| 8166 | /* Default value */ |
| 8167 | status = 200; |
| 8168 | reason = http_get_reason(status); |
| 8169 | |
| 8170 | if (lua_istable(L, 2)) { |
| 8171 | /* load status and reason from the table argument at index 2 */ |
| 8172 | ret = lua_getfield(L, 2, "status"); |
| 8173 | if (ret == LUA_TNIL) |
| 8174 | goto reason; |
| 8175 | else if (ret != LUA_TNUMBER) { |
| 8176 | /* invalid status: ignore the reason */ |
| 8177 | goto body; |
| 8178 | } |
| 8179 | status = lua_tointeger(L, -1); |
| 8180 | |
| 8181 | reason: |
| 8182 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8183 | ret = lua_getfield(L, 2, "reason"); |
| 8184 | if (ret == LUA_TSTRING) |
| 8185 | reason = lua_tostring(L, -1); |
| 8186 | |
| 8187 | body: |
| 8188 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8189 | ret = lua_getfield(L, 2, "body"); |
| 8190 | if (ret == LUA_TSTRING) |
| 8191 | body = lua_tostring(L, -1); |
| 8192 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8193 | } |
| 8194 | |
| 8195 | /* Create the Reply table */ |
| 8196 | lua_newtable(L); |
| 8197 | |
| 8198 | /* Add status element */ |
| 8199 | lua_pushstring(L, "status"); |
| 8200 | lua_pushinteger(L, status); |
| 8201 | lua_settable(L, -3); |
| 8202 | |
| 8203 | /* Add reason element */ |
| 8204 | reason = http_get_reason(status); |
| 8205 | lua_pushstring(L, "reason"); |
| 8206 | lua_pushstring(L, reason); |
| 8207 | lua_settable(L, -3); |
| 8208 | |
| 8209 | /* Add body element, nil if undefined */ |
| 8210 | lua_pushstring(L, "body"); |
| 8211 | if (body) |
| 8212 | lua_pushstring(L, body); |
| 8213 | else |
| 8214 | lua_pushnil(L); |
| 8215 | lua_settable(L, -3); |
| 8216 | |
| 8217 | /* Add headers element */ |
| 8218 | lua_pushstring(L, "headers"); |
| 8219 | lua_newtable(L); |
| 8220 | |
| 8221 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8222 | if (lua_istable(L, 2)) { |
| 8223 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8224 | ret = lua_getfield(L, 2, "headers"); |
| 8225 | if (ret == LUA_TTABLE) { |
| 8226 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8227 | lua_pushnil(L); |
| 8228 | while (lua_next(L, -2) != 0) { |
| 8229 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8230 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8231 | /* invalid value type, skip it */ |
| 8232 | lua_pop(L, 1); |
| 8233 | continue; |
| 8234 | } |
| 8235 | |
| 8236 | |
| 8237 | /* Duplicate the key and swap it with the value. */ |
| 8238 | lua_pushvalue(L, -2); |
| 8239 | lua_insert(L, -2); |
| 8240 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8241 | |
| 8242 | lua_newtable(L); |
| 8243 | lua_insert(L, -2); |
| 8244 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8245 | |
| 8246 | if (lua_isstring(L, -1)) { |
| 8247 | /* push the value in the inner table */ |
| 8248 | lua_rawseti(L, -2, 1); |
| 8249 | } |
| 8250 | else { /* table */ |
| 8251 | lua_pushnil(L); |
| 8252 | while (lua_next(L, -2) != 0) { |
| 8253 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8254 | if (!lua_isstring(L, -1)) { |
| 8255 | /* invalid value type, skip it*/ |
| 8256 | lua_pop(L, 1); |
| 8257 | continue; |
| 8258 | } |
| 8259 | /* push the value in the inner table */ |
| 8260 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8261 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8262 | } |
| 8263 | lua_pop(L, 1); |
| 8264 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8265 | } |
| 8266 | |
| 8267 | /* push (k,v) on the stack in the headers table: |
| 8268 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8269 | */ |
| 8270 | lua_settable(L, -5); |
| 8271 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8272 | } |
| 8273 | } |
| 8274 | lua_pop(L, 1); |
| 8275 | } |
| 8276 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8277 | lua_settable(L, -3); |
| 8278 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8279 | |
| 8280 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8281 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8282 | lua_setmetatable(L, -2); |
| 8283 | return 1; |
| 8284 | } |
| 8285 | |
| 8286 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8287 | * provided, the default one corresponding to the status code is used. |
| 8288 | */ |
| 8289 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8290 | { |
| 8291 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8292 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8293 | |
| 8294 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8295 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8296 | |
| 8297 | if (status < 100 || status > 599) { |
| 8298 | lua_pushboolean(L, 0); |
| 8299 | return 1; |
| 8300 | } |
| 8301 | if (!reason) |
| 8302 | reason = http_get_reason(status); |
| 8303 | |
| 8304 | lua_pushinteger(L, status); |
| 8305 | lua_setfield(L, 1, "status"); |
| 8306 | |
| 8307 | lua_pushstring(L, reason); |
| 8308 | lua_setfield(L, 1, "reason"); |
| 8309 | |
| 8310 | lua_pushboolean(L, 1); |
| 8311 | return 1; |
| 8312 | } |
| 8313 | |
| 8314 | /* Add a header into the reply object. Each header name is associated to an |
| 8315 | * array of values in the "headers" table. If the header name is not found, a |
| 8316 | * new entry is created. |
| 8317 | */ |
| 8318 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8319 | { |
| 8320 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8321 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8322 | int ret; |
| 8323 | |
| 8324 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8325 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8326 | |
| 8327 | /* Push in the stack the "headers" entry. */ |
| 8328 | ret = lua_getfield(L, 1, "headers"); |
| 8329 | if (ret != LUA_TTABLE) { |
| 8330 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8331 | WILL_LJMP(lua_error(L)); |
| 8332 | } |
| 8333 | |
| 8334 | /* check if the header is already registered. If not, register it. */ |
| 8335 | ret = lua_getfield(L, -1, name); |
| 8336 | if (ret == LUA_TNIL) { |
| 8337 | /* Entry not found. */ |
| 8338 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8339 | |
| 8340 | /* Insert the new header name in the array in the top of the stack. |
| 8341 | * It left the new array in the top of the stack. |
| 8342 | */ |
| 8343 | lua_newtable(L); |
| 8344 | lua_pushstring(L, name); |
| 8345 | lua_pushvalue(L, -2); |
| 8346 | lua_settable(L, -4); |
| 8347 | } |
| 8348 | else if (ret != LUA_TTABLE) { |
| 8349 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8350 | WILL_LJMP(lua_error(L)); |
| 8351 | } |
| 8352 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8353 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8354 | * the header value as new entry. |
| 8355 | */ |
| 8356 | lua_pushstring(L, value); |
| 8357 | ret = lua_rawlen(L, -2); |
| 8358 | lua_rawseti(L, -2, ret + 1); |
| 8359 | |
| 8360 | lua_pushboolean(L, 1); |
| 8361 | return 1; |
| 8362 | } |
| 8363 | |
| 8364 | /* Remove all occurrences of a given header name. */ |
| 8365 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8366 | { |
| 8367 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8368 | int ret; |
| 8369 | |
| 8370 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8371 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8372 | |
| 8373 | /* Push in the stack the "headers" entry. */ |
| 8374 | ret = lua_getfield(L, 1, "headers"); |
| 8375 | if (ret != LUA_TTABLE) { |
| 8376 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8377 | WILL_LJMP(lua_error(L)); |
| 8378 | } |
| 8379 | |
| 8380 | lua_pushstring(L, name); |
| 8381 | lua_pushnil(L); |
| 8382 | lua_settable(L, -3); |
| 8383 | |
| 8384 | lua_pushboolean(L, 1); |
| 8385 | return 1; |
| 8386 | } |
| 8387 | |
| 8388 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8389 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8390 | { |
| 8391 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8392 | |
| 8393 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8394 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8395 | |
| 8396 | lua_pushstring(L, payload); |
| 8397 | lua_setfield(L, 1, "body"); |
| 8398 | |
| 8399 | lua_pushboolean(L, 1); |
| 8400 | return 1; |
| 8401 | } |
| 8402 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8403 | __LJMP static int hlua_log(lua_State *L) |
| 8404 | { |
| 8405 | int level; |
| 8406 | const char *msg; |
| 8407 | |
| 8408 | MAY_LJMP(check_args(L, 2, "log")); |
| 8409 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8410 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8411 | |
| 8412 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8413 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8414 | |
| 8415 | hlua_sendlog(NULL, level, msg); |
| 8416 | return 0; |
| 8417 | } |
| 8418 | |
| 8419 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8420 | { |
| 8421 | const char *msg; |
| 8422 | |
| 8423 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8424 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8425 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8426 | return 0; |
| 8427 | } |
| 8428 | |
| 8429 | __LJMP static int hlua_log_info(lua_State *L) |
| 8430 | { |
| 8431 | const char *msg; |
| 8432 | |
| 8433 | MAY_LJMP(check_args(L, 1, "info")); |
| 8434 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8435 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8436 | return 0; |
| 8437 | } |
| 8438 | |
| 8439 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8440 | { |
| 8441 | const char *msg; |
| 8442 | |
| 8443 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8444 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8445 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8446 | return 0; |
| 8447 | } |
| 8448 | |
| 8449 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8450 | { |
| 8451 | const char *msg; |
| 8452 | |
| 8453 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8454 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8455 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8456 | return 0; |
| 8457 | } |
| 8458 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8459 | __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] | 8460 | { |
| 8461 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8462 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8463 | 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] | 8464 | return 0; |
| 8465 | } |
| 8466 | |
| 8467 | __LJMP static int hlua_sleep(lua_State *L) |
| 8468 | { |
| 8469 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8470 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8471 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8472 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8473 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8474 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8475 | wakeup_ms = tick_add(now_ms, delay); |
| 8476 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8477 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8478 | 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] | 8479 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8480 | } |
| 8481 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8482 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8483 | { |
| 8484 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8485 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8486 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8487 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8488 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8489 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8490 | wakeup_ms = tick_add(now_ms, delay); |
| 8491 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8492 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8493 | 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] | 8494 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8495 | } |
| 8496 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8497 | /* This functionis an LUA binding. it permits to give back |
| 8498 | * the hand at the HAProxy scheduler. It is used when the |
| 8499 | * LUA processing consumes a lot of time. |
| 8500 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8501 | __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] | 8502 | { |
| 8503 | return 0; |
| 8504 | } |
| 8505 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8506 | __LJMP static int hlua_yield(lua_State *L) |
| 8507 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8508 | 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] | 8509 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8510 | } |
| 8511 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8512 | /* This function change the nice of the currently executed |
| 8513 | * task. It is used set low or high priority at the current |
| 8514 | * task. |
| 8515 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8516 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8517 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8518 | struct hlua *hlua; |
| 8519 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8520 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8521 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8522 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8523 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8524 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8525 | hlua = hlua_gethlua(L); |
| 8526 | |
| 8527 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8528 | if (!hlua || !hlua->task) |
| 8529 | return 0; |
| 8530 | |
| 8531 | if (nice < -1024) |
| 8532 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8533 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8534 | nice = 1024; |
| 8535 | |
| 8536 | hlua->task->nice = nice; |
| 8537 | return 0; |
| 8538 | } |
| 8539 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8540 | /* 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] | 8541 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8542 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8543 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8544 | * |
| 8545 | * Task wrapper are longjmp safe because the only one Lua code |
| 8546 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8547 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8548 | 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] | 8549 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8550 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8551 | enum hlua_exec status; |
| 8552 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8553 | if (task->tid < 0) |
| 8554 | task->tid = tid; |
| 8555 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8556 | /* If it is the first call to the task, we must initialize the |
| 8557 | * execution timeouts. |
| 8558 | */ |
| 8559 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8560 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8561 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8562 | /* Execute the Lua code. */ |
| 8563 | status = hlua_ctx_resume(hlua, 1); |
| 8564 | |
| 8565 | switch (status) { |
| 8566 | /* finished or yield */ |
| 8567 | case HLUA_E_OK: |
| 8568 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8569 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8570 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8571 | break; |
| 8572 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8573 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8574 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8575 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8576 | break; |
| 8577 | |
| 8578 | /* finished with error. */ |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame^] | 8579 | case HLUA_E_ETMOUT: |
| 8580 | SEND_ERR(NULL, "Lua task: execution timeout.\n"); |
| 8581 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8582 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8583 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame^] | 8584 | goto err_task_abort; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8585 | case HLUA_E_ERR: |
| 8586 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8587 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Aurelien DARRAGON | 7954410 | 2022-11-24 14:27:15 +0100 | [diff] [blame^] | 8588 | err_task_abort: |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8589 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8590 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8591 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8592 | break; |
| 8593 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8594 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8595 | } |
| 8596 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8597 | /* This function is an LUA binding that register LUA function to be |
| 8598 | * executed after the HAProxy configuration parsing and before the |
| 8599 | * HAProxy scheduler starts. This function expect only one LUA |
| 8600 | * argument that is a function. This function returns nothing, but |
| 8601 | * throws if an error is encountered. |
| 8602 | */ |
| 8603 | __LJMP static int hlua_register_init(lua_State *L) |
| 8604 | { |
| 8605 | struct hlua_init_function *init; |
| 8606 | int ref; |
| 8607 | |
| 8608 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8609 | |
| 8610 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8611 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8612 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8613 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8614 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8615 | |
| 8616 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8617 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8618 | return 0; |
| 8619 | } |
| 8620 | |
Ilya Shipitsin | 6f86eaa | 2022-11-30 16:22:42 +0500 | [diff] [blame] | 8621 | /* This function is an LUA binding. It permits to register a task |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8622 | * executed in parallel of the main HAroxy activity. The task is |
| 8623 | * created and it is set in the HAProxy scheduler. It can be called |
| 8624 | * from the "init" section, "post init" or during the runtime. |
| 8625 | * |
| 8626 | * Lua prototype: |
| 8627 | * |
| 8628 | * <none> core.register_task(<function>) |
| 8629 | */ |
| 8630 | static int hlua_register_task(lua_State *L) |
| 8631 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8632 | struct hlua *hlua = NULL; |
| 8633 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8634 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8635 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8636 | |
| 8637 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8638 | |
| 8639 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8640 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8641 | /* Get the reference state. If the reference is NULL, L is the master |
| 8642 | * state, otherwise hlua->T is. |
| 8643 | */ |
| 8644 | hlua = hlua_gethlua(L); |
| 8645 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8646 | /* we are in runtime processing */ |
| 8647 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8648 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8649 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8650 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8651 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8652 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8653 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8654 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8655 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8656 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8657 | /* We are in the common lua state, execute the task anywhere, |
| 8658 | * otherwise, inherit the current thread identifier |
| 8659 | */ |
| 8660 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8661 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8662 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8663 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8664 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8665 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8666 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8667 | task->context = hlua; |
| 8668 | task->process = hlua_process_task; |
| 8669 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8670 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8671 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8672 | |
| 8673 | /* Restore the function in the stack. */ |
| 8674 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8675 | hlua->nargs = 0; |
| 8676 | |
| 8677 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8678 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8679 | |
| 8680 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8681 | |
| 8682 | alloc_error: |
| 8683 | task_destroy(task); |
| 8684 | hlua_ctx_destroy(hlua); |
| 8685 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8686 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8687 | } |
| 8688 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8689 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8690 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8691 | * resume converters. |
| 8692 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8693 | 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] | 8694 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8695 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8696 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8697 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8698 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8699 | if (!stream) |
| 8700 | return 0; |
| 8701 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8702 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8703 | * Lua context can be not initialized. This behavior |
| 8704 | * permits to save performances because a systematic |
| 8705 | * Lua initialization cause 5% performances loss. |
| 8706 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8707 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8708 | struct hlua *hlua; |
| 8709 | |
| 8710 | hlua = pool_alloc(pool_head_hlua); |
| 8711 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8712 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8713 | return 0; |
| 8714 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8715 | HLUA_INIT(hlua); |
| 8716 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8717 | 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] | 8718 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8719 | return 0; |
| 8720 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8721 | } |
| 8722 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8723 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8724 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8725 | |
| 8726 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8727 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8728 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8729 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8730 | else |
| 8731 | error = "critical error"; |
| 8732 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8733 | return 0; |
| 8734 | } |
| 8735 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8736 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8737 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8738 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8739 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8740 | return 0; |
| 8741 | } |
| 8742 | |
| 8743 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8744 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8745 | |
| 8746 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8747 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8748 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8749 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8750 | return 0; |
| 8751 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8752 | hlua_smp2lua(stream->hlua->T, smp); |
| 8753 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8754 | |
| 8755 | /* push keywords in the stack. */ |
| 8756 | if (arg_p) { |
| 8757 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8758 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8759 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8760 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8761 | return 0; |
| 8762 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8763 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8764 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8765 | } |
| 8766 | } |
| 8767 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8768 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8769 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8770 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8771 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8772 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8773 | } |
| 8774 | |
| 8775 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8776 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8777 | /* finished. */ |
| 8778 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8779 | /* If the stack is empty, the function fails. */ |
| 8780 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8781 | return 0; |
| 8782 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8783 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8784 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8785 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8786 | return 1; |
| 8787 | |
| 8788 | /* yield. */ |
| 8789 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8790 | 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] | 8791 | return 0; |
| 8792 | |
| 8793 | /* finished with error. */ |
| 8794 | case HLUA_E_ERRMSG: |
| 8795 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8796 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8797 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8798 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8799 | return 0; |
| 8800 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8801 | case HLUA_E_ETMOUT: |
| 8802 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8803 | return 0; |
| 8804 | |
| 8805 | case HLUA_E_NOMEM: |
| 8806 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8807 | return 0; |
| 8808 | |
| 8809 | case HLUA_E_YIELD: |
| 8810 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8811 | return 0; |
| 8812 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8813 | case HLUA_E_ERR: |
| 8814 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8815 | 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] | 8816 | __fallthrough; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8817 | |
| 8818 | default: |
| 8819 | return 0; |
| 8820 | } |
| 8821 | } |
| 8822 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8823 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8824 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8825 | * resume sample-fetches. This function will be called by the sample |
| 8826 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8827 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8828 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8829 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8830 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8831 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8832 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8833 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8834 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8835 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8836 | if (!stream) |
| 8837 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8838 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8839 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8840 | * Lua context can be not initialized. This behavior |
| 8841 | * permits to save performances because a systematic |
| 8842 | * Lua initialization cause 5% performances loss. |
| 8843 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8844 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8845 | struct hlua *hlua; |
| 8846 | |
| 8847 | hlua = pool_alloc(pool_head_hlua); |
| 8848 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8849 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8850 | return 0; |
| 8851 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8852 | hlua->T = NULL; |
| 8853 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8854 | 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] | 8855 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8856 | return 0; |
| 8857 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8858 | } |
| 8859 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8860 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8861 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8862 | |
| 8863 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8864 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8865 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8866 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8867 | else |
| 8868 | error = "critical error"; |
| 8869 | 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] | 8870 | return 0; |
| 8871 | } |
| 8872 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8873 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8874 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8875 | 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] | 8876 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8877 | return 0; |
| 8878 | } |
| 8879 | |
| 8880 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8881 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[stream->hlua->state_id]); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8882 | |
| 8883 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8884 | 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] | 8885 | 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] | 8886 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8887 | return 0; |
| 8888 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8889 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8890 | |
| 8891 | /* push keywords in the stack. */ |
| 8892 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8893 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8894 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8895 | 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] | 8896 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8897 | return 0; |
| 8898 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8899 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8900 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8901 | } |
| 8902 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8903 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8904 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8905 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8906 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8907 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8908 | } |
| 8909 | |
| 8910 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8911 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8912 | /* finished. */ |
| 8913 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8914 | /* If the stack is empty, the function fails. */ |
| 8915 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8916 | return 0; |
| 8917 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8918 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8919 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8920 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8921 | |
| 8922 | /* Set the end of execution flag. */ |
| 8923 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8924 | return 1; |
| 8925 | |
| 8926 | /* yield. */ |
| 8927 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8928 | 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] | 8929 | return 0; |
| 8930 | |
| 8931 | /* finished with error. */ |
| 8932 | case HLUA_E_ERRMSG: |
| 8933 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8934 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8935 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8936 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8937 | return 0; |
| 8938 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8939 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8940 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8941 | return 0; |
| 8942 | |
| 8943 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8944 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8945 | return 0; |
| 8946 | |
| 8947 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8948 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8949 | return 0; |
| 8950 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8951 | case HLUA_E_ERR: |
| 8952 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8953 | 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] | 8954 | __fallthrough; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8955 | |
| 8956 | default: |
| 8957 | return 0; |
| 8958 | } |
| 8959 | } |
| 8960 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8961 | /* This function is an LUA binding used for registering |
| 8962 | * "sample-conv" functions. It expects a converter name used |
| 8963 | * in the haproxy configuration file, and an LUA function. |
| 8964 | */ |
| 8965 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8966 | { |
| 8967 | struct sample_conv_kw_list *sck; |
| 8968 | const char *name; |
| 8969 | int ref; |
| 8970 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8971 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8972 | struct sample_conv *sc; |
| 8973 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8974 | |
| 8975 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8976 | |
| 8977 | /* First argument : converter name. */ |
| 8978 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8979 | |
| 8980 | /* Second argument : lua function. */ |
| 8981 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8982 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8983 | /* Check if the converter is already registered */ |
| 8984 | trash = get_trash_chunk(); |
| 8985 | chunk_printf(trash, "lua.%s", name); |
| 8986 | sc = find_sample_conv(trash->area, trash->data); |
| 8987 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8988 | fcn = sc->private; |
| 8989 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8990 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8991 | "This will become a hard error in version 2.5.\n", name); |
| 8992 | } |
| 8993 | fcn->function_ref[hlua_state_id] = ref; |
| 8994 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8995 | } |
| 8996 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8997 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8998 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8999 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9000 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9001 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9002 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9003 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9004 | |
| 9005 | /* Fill fcn. */ |
| 9006 | fcn->name = strdup(name); |
| 9007 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9008 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9009 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9010 | |
| 9011 | /* List head */ |
| 9012 | sck->list.n = sck->list.p = NULL; |
| 9013 | |
| 9014 | /* converter keyword. */ |
| 9015 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9016 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9017 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9018 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9019 | |
| 9020 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 9021 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9022 | 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] | 9023 | sck->kw[0].val_args = NULL; |
| 9024 | sck->kw[0].in_type = SMP_T_STR; |
| 9025 | sck->kw[0].out_type = SMP_T_STR; |
| 9026 | sck->kw[0].private = fcn; |
| 9027 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9028 | /* Register this new converter */ |
| 9029 | sample_register_convs(sck); |
| 9030 | |
| 9031 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 9032 | |
| 9033 | alloc_error: |
| 9034 | release_hlua_function(fcn); |
| 9035 | ha_free(&sck); |
| 9036 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9037 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 9038 | } |
| 9039 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9040 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9041 | * "sample-fetch" functions. It expects a converter name used |
| 9042 | * in the haproxy configuration file, and an LUA function. |
| 9043 | */ |
| 9044 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9045 | { |
| 9046 | const char *name; |
| 9047 | int ref; |
| 9048 | int len; |
| 9049 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9050 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9051 | struct sample_fetch *sf; |
| 9052 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9053 | |
| 9054 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9055 | |
| 9056 | /* First argument : sample-fetch name. */ |
| 9057 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9058 | |
| 9059 | /* Second argument : lua function. */ |
| 9060 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9061 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9062 | /* Check if the sample-fetch is already registered */ |
| 9063 | trash = get_trash_chunk(); |
| 9064 | chunk_printf(trash, "lua.%s", name); |
| 9065 | sf = find_sample_fetch(trash->area, trash->data); |
| 9066 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9067 | fcn = sf->private; |
| 9068 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9069 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9070 | "This will become a hard error in version 2.5.\n", name); |
| 9071 | } |
| 9072 | fcn->function_ref[hlua_state_id] = ref; |
| 9073 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9074 | } |
| 9075 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9076 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9077 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9078 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9079 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9080 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9081 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9082 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9083 | |
| 9084 | /* Fill fcn. */ |
| 9085 | fcn->name = strdup(name); |
| 9086 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9087 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9088 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9089 | |
| 9090 | /* List head */ |
| 9091 | sfk->list.n = sfk->list.p = NULL; |
| 9092 | |
| 9093 | /* sample-fetch keyword. */ |
| 9094 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9095 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9096 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9097 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9098 | |
| 9099 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9100 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9101 | 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] | 9102 | sfk->kw[0].val_args = NULL; |
| 9103 | sfk->kw[0].out_type = SMP_T_STR; |
| 9104 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9105 | sfk->kw[0].val = 0; |
| 9106 | sfk->kw[0].private = fcn; |
| 9107 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9108 | /* Register this new fetch. */ |
| 9109 | sample_register_fetches(sfk); |
| 9110 | |
| 9111 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9112 | |
| 9113 | alloc_error: |
| 9114 | release_hlua_function(fcn); |
| 9115 | ha_free(&sfk); |
| 9116 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9117 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9118 | } |
| 9119 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9120 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9121 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9122 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9123 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9124 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9125 | unsigned int delay; |
| 9126 | unsigned int wakeup_ms; |
| 9127 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9128 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9129 | hlua = hlua_gethlua(L); |
| 9130 | if (!hlua) { |
| 9131 | return 0; |
| 9132 | } |
| 9133 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9134 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9135 | |
| 9136 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9137 | wakeup_ms = tick_add(now_ms, delay); |
| 9138 | hlua->wake_time = wakeup_ms; |
| 9139 | return 0; |
| 9140 | } |
| 9141 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9142 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9143 | * wrapper during the initialisation period. This function may return any |
| 9144 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9145 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9146 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9147 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9148 | 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] | 9149 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9150 | { |
| 9151 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9152 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9153 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9154 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9155 | |
| 9156 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9157 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9158 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9159 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9160 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9161 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9162 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9163 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9164 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9165 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9166 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9167 | * Lua context can be not initialized. This behavior |
| 9168 | * permits to save performances because a systematic |
| 9169 | * Lua initialization cause 5% performances loss. |
| 9170 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9171 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9172 | struct hlua *hlua; |
| 9173 | |
| 9174 | hlua = pool_alloc(pool_head_hlua); |
| 9175 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9176 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9177 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9178 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9179 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9180 | HLUA_INIT(hlua); |
| 9181 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9182 | 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] | 9183 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9184 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9185 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9186 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9187 | } |
| 9188 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9189 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9190 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9191 | |
| 9192 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9193 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9194 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9195 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9196 | else |
| 9197 | error = "critical error"; |
| 9198 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9199 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9200 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9201 | } |
| 9202 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9203 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9204 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9205 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9206 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9207 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9208 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9209 | } |
| 9210 | |
| 9211 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9212 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn->function_ref[s->hlua->state_id]); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9213 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9214 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9215 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9216 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9217 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9218 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9219 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9220 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9221 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9222 | |
| 9223 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9224 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9225 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9226 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9227 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9228 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9229 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9230 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9231 | lua_pushstring(s->hlua->T, *arg); |
| 9232 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9233 | } |
| 9234 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9235 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9236 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9237 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9238 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9239 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9240 | } |
| 9241 | |
| 9242 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9243 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9244 | /* finished. */ |
| 9245 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9246 | /* Catch the return value */ |
| 9247 | if (lua_gettop(s->hlua->T) > 0) |
| 9248 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9249 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9250 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9251 | if (act_ret == ACT_RET_YIELD) { |
| 9252 | if (flags & ACT_OPT_FINAL) |
| 9253 | goto err_yield; |
| 9254 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9255 | if (dir == SMP_OPT_DIR_REQ) |
| 9256 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9257 | s->hlua->wake_time); |
| 9258 | else |
| 9259 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9260 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9261 | } |
| 9262 | goto end; |
| 9263 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9264 | /* yield. */ |
| 9265 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9266 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9267 | if (dir == SMP_OPT_DIR_REQ) |
| 9268 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9269 | s->hlua->wake_time); |
| 9270 | else |
| 9271 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9272 | s->hlua->wake_time); |
| 9273 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9274 | /* Some actions can be wake up when a "write" event |
| 9275 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9276 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9277 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9278 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9279 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9280 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9281 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9282 | act_ret = ACT_RET_YIELD; |
| 9283 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9284 | |
| 9285 | /* finished with error. */ |
| 9286 | case HLUA_E_ERRMSG: |
| 9287 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9288 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9289 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9290 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9291 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9292 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9293 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9294 | 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] | 9295 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9296 | |
| 9297 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9298 | 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] | 9299 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9300 | |
| 9301 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9302 | err_yield: |
| 9303 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9304 | 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] | 9305 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9306 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9307 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9308 | case HLUA_E_ERR: |
| 9309 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9310 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9311 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9312 | |
| 9313 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9314 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9315 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9316 | |
| 9317 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9318 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9319 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9320 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9321 | } |
| 9322 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9323 | 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] | 9324 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9325 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9326 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9327 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9328 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9329 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9330 | } |
| 9331 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9332 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9333 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9334 | 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] | 9335 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9336 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9337 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9338 | struct task *task; |
| 9339 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9340 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9341 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9342 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9343 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9344 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9345 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9346 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9347 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9348 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9349 | tcp_ctx->hlua = hlua; |
| 9350 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9351 | |
| 9352 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9353 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9354 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9355 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9356 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9357 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9358 | } |
| 9359 | task->nice = 0; |
| 9360 | task->context = ctx; |
| 9361 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9362 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9363 | |
| 9364 | /* In the execution wrappers linked with a stream, the |
| 9365 | * Lua context can be not initialized. This behavior |
| 9366 | * permits to save performances because a systematic |
| 9367 | * Lua initialization cause 5% performances loss. |
| 9368 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9369 | 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] | 9370 | 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] | 9371 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9372 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9373 | } |
| 9374 | |
| 9375 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9376 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9377 | |
| 9378 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9379 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9380 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9381 | error = lua_tostring(hlua->T, -1); |
| 9382 | else |
| 9383 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9384 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9385 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9386 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9387 | } |
| 9388 | |
| 9389 | /* Check stack available size. */ |
| 9390 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9391 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9392 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9393 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9394 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9395 | } |
| 9396 | |
| 9397 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9398 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9399 | |
| 9400 | /* Create and and push object stream in the stack. */ |
| 9401 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9402 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9403 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9404 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9405 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9406 | } |
| 9407 | hlua->nargs = 1; |
| 9408 | |
| 9409 | /* push keywords in the stack. */ |
| 9410 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9411 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9412 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9413 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9414 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9415 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9416 | } |
| 9417 | lua_pushstring(hlua->T, *arg); |
| 9418 | hlua->nargs++; |
| 9419 | } |
| 9420 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9421 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9422 | |
| 9423 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9424 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9425 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9426 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9427 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9428 | } |
| 9429 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9430 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9431 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9432 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9433 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9434 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9435 | struct act_rule *rule = ctx->rule; |
| 9436 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9437 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9438 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9439 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9440 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9441 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9442 | /* The applet execution is already done. */ |
| 9443 | if (tcp_ctx->flags & APPLET_DONE) |
| 9444 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9445 | |
| 9446 | /* Execute the function. */ |
| 9447 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9448 | /* finished. */ |
| 9449 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9450 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9451 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
| 9452 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9453 | |
| 9454 | /* yield. */ |
| 9455 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9456 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9457 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9458 | break; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9459 | |
| 9460 | /* finished with error. */ |
| 9461 | case HLUA_E_ERRMSG: |
| 9462 | /* Display log. */ |
| 9463 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9464 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9465 | lua_pop(hlua->T, 1); |
| 9466 | goto error; |
| 9467 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9468 | case HLUA_E_ETMOUT: |
| 9469 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9470 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9471 | goto error; |
| 9472 | |
| 9473 | case HLUA_E_NOMEM: |
| 9474 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9475 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9476 | goto error; |
| 9477 | |
| 9478 | case HLUA_E_YIELD: /* unexpected */ |
| 9479 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9480 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9481 | goto error; |
| 9482 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9483 | case HLUA_E_ERR: |
| 9484 | /* Display log. */ |
| 9485 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9486 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9487 | goto error; |
| 9488 | |
| 9489 | default: |
| 9490 | goto error; |
| 9491 | } |
| 9492 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9493 | out: |
| 9494 | /* eat the whole request */ |
| 9495 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
| 9496 | return; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9497 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9498 | error: |
| 9499 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9500 | tcp_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9501 | goto out; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9502 | } |
| 9503 | |
| 9504 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9505 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9506 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9507 | |
| 9508 | task_destroy(tcp_ctx->task); |
| 9509 | tcp_ctx->task = NULL; |
| 9510 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9511 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9512 | } |
| 9513 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9514 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9515 | * 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] | 9516 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9517 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9518 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9519 | 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] | 9520 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9521 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9522 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9523 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9524 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9525 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9526 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9527 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9528 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9529 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9530 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9531 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9532 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9533 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9534 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9535 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9536 | http_ctx->hlua = hlua; |
| 9537 | http_ctx->left_bytes = -1; |
| 9538 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9539 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9540 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9541 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9542 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9543 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9544 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9545 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9546 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9547 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9548 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9549 | } |
| 9550 | task->nice = 0; |
| 9551 | task->context = ctx; |
| 9552 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9553 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9554 | |
| 9555 | /* In the execution wrappers linked with a stream, the |
| 9556 | * Lua context can be not initialized. This behavior |
| 9557 | * permits to save performances because a systematic |
| 9558 | * Lua initialization cause 5% performances loss. |
| 9559 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9560 | 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] | 9561 | 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] | 9562 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9563 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9564 | } |
| 9565 | |
| 9566 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9567 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9568 | |
| 9569 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9570 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9571 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9572 | error = lua_tostring(hlua->T, -1); |
| 9573 | else |
| 9574 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9575 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9576 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9577 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9578 | } |
| 9579 | |
| 9580 | /* Check stack available size. */ |
| 9581 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9582 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9583 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9584 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9585 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9586 | } |
| 9587 | |
| 9588 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9589 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9590 | |
| 9591 | /* Create and and push object stream in the stack. */ |
| 9592 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9593 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9594 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9595 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9596 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9597 | } |
| 9598 | hlua->nargs = 1; |
| 9599 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9600 | /* push keywords in the stack. */ |
| 9601 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9602 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9603 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9604 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9605 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9606 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9607 | } |
| 9608 | lua_pushstring(hlua->T, *arg); |
| 9609 | hlua->nargs++; |
| 9610 | } |
| 9611 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9612 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9613 | |
| 9614 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9615 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9616 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9617 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9618 | } |
| 9619 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9620 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9621 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9622 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9623 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9624 | struct stream *strm = __sc_strm(sc); |
| 9625 | struct channel *req = sc_oc(sc); |
| 9626 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9627 | struct act_rule *rule = ctx->rule; |
| 9628 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9629 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9630 | struct htx *req_htx, *res_htx; |
| 9631 | |
| 9632 | res_htx = htx_from_buf(&res->buf); |
| 9633 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9634 | if (unlikely(se_fl_test(ctx->sedesc, (SE_FL_EOS|SE_FL_ERROR|SE_FL_SHR|SE_FL_SHW)))) |
| 9635 | goto out; |
| 9636 | |
| 9637 | /* The applet execution is already done. */ |
| 9638 | if (http_ctx->flags & APPLET_DONE) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9639 | goto out; |
| 9640 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9641 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9642 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9643 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9644 | goto out; |
| 9645 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9646 | |
| 9647 | /* Set the currently running flag. */ |
| 9648 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9649 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9650 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9651 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9652 | goto out; |
| 9653 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9654 | } |
| 9655 | |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9656 | /* Execute the function. */ |
| 9657 | switch (hlua_ctx_resume(hlua, 1)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9658 | /* finished. */ |
| 9659 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9660 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9661 | break; |
| 9662 | |
| 9663 | /* yield. */ |
| 9664 | case HLUA_E_AGAIN: |
| 9665 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9666 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9667 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9668 | |
| 9669 | /* finished with error. */ |
| 9670 | case HLUA_E_ERRMSG: |
| 9671 | /* Display log. */ |
| 9672 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9673 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9674 | lua_pop(hlua->T, 1); |
| 9675 | goto error; |
| 9676 | |
| 9677 | case HLUA_E_ETMOUT: |
| 9678 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9679 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9680 | goto error; |
| 9681 | |
| 9682 | case HLUA_E_NOMEM: |
| 9683 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9684 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9685 | goto error; |
| 9686 | |
| 9687 | case HLUA_E_YIELD: /* unexpected */ |
| 9688 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9689 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9690 | goto error; |
| 9691 | |
| 9692 | case HLUA_E_ERR: |
| 9693 | /* Display log. */ |
| 9694 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9695 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9696 | goto error; |
| 9697 | |
| 9698 | default: |
| 9699 | goto error; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9700 | } |
| 9701 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9702 | if (http_ctx->flags & APPLET_DONE) { |
| 9703 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9704 | goto out; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9705 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9706 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9707 | goto error; |
| 9708 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9709 | /* no more data are expected. If the response buffer is empty |
| 9710 | * for a chunked message, be sure to add something (EOT block in |
| 9711 | * this case) to have something to send. It is important to be |
| 9712 | * sure the EOM flags will be handled by the endpoint. |
| 9713 | */ |
| 9714 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9715 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9716 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9717 | goto out; |
| 9718 | } |
| 9719 | channel_add_input(res, 1); |
| 9720 | } |
| 9721 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9722 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9723 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9724 | strm->txn->status = http_ctx->status; |
| 9725 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9726 | } |
| 9727 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9728 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9729 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9730 | /* eat the whole request */ |
| 9731 | if (co_data(req)) { |
| 9732 | req_htx = htx_from_buf(&req->buf); |
| 9733 | co_htx_skip(req, req_htx, co_data(req)); |
| 9734 | htx_to_buf(req_htx, &req->buf); |
| 9735 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9736 | return; |
| 9737 | |
| 9738 | error: |
| 9739 | |
| 9740 | /* If we are in HTTP mode, and we are not send any |
| 9741 | * data, return a 500 server error in best effort: |
| 9742 | * if there is no room available in the buffer, |
| 9743 | * just close the connection. |
| 9744 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9745 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9746 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9747 | |
| 9748 | channel_erase(res); |
| 9749 | res->buf.data = b_data(err); |
| 9750 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9751 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9752 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9753 | se_fl_set(ctx->sedesc, SE_FL_EOI|SE_FL_EOS); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9754 | } |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9755 | else |
| 9756 | se_fl_set(ctx->sedesc, SE_FL_ERROR); |
| 9757 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9758 | if (!(strm->flags & SF_ERR_MASK)) |
| 9759 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9760 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 3157222 | 2023-03-31 11:13:48 +0200 | [diff] [blame] | 9761 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9762 | } |
| 9763 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9764 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9765 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9766 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9767 | |
| 9768 | task_destroy(http_ctx->task); |
| 9769 | http_ctx->task = NULL; |
| 9770 | hlua_ctx_destroy(http_ctx->hlua); |
| 9771 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9772 | } |
| 9773 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9774 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9775 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9776 | * |
| 9777 | * This function can fail with an abort() due to an Lua critical error. |
| 9778 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9779 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9780 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9781 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9782 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9783 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9784 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9785 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9786 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9787 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9788 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9789 | if (!rule->arg.hlua_rule) { |
| 9790 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9791 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9792 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9793 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9794 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9795 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9796 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9797 | if (!rule->arg.hlua_rule->args) { |
| 9798 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9799 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9800 | } |
| 9801 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9802 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9803 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9804 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9805 | /* Expect some arguments */ |
| 9806 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9807 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9808 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9809 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9810 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9811 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9812 | if (!rule->arg.hlua_rule->args[i]) { |
| 9813 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9814 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9815 | } |
| 9816 | (*cur_arg)++; |
| 9817 | } |
| 9818 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9819 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9820 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9821 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9822 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9823 | |
| 9824 | error: |
| 9825 | if (rule->arg.hlua_rule) { |
| 9826 | if (rule->arg.hlua_rule->args) { |
| 9827 | for (i = 0; i < fcn->nargs; i++) |
| 9828 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9829 | ha_free(&rule->arg.hlua_rule->args); |
| 9830 | } |
| 9831 | ha_free(&rule->arg.hlua_rule); |
| 9832 | } |
| 9833 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9834 | } |
| 9835 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9836 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9837 | struct act_rule *rule, char **err) |
| 9838 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9839 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9840 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9841 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9842 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9843 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9844 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9845 | * the call of this analyzer. |
| 9846 | */ |
| 9847 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9848 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9849 | return ACT_RET_PRS_ERR; |
| 9850 | } |
| 9851 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9852 | /* Memory for the rule. */ |
| 9853 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9854 | if (!rule->arg.hlua_rule) { |
| 9855 | memprintf(err, "out of memory error"); |
| 9856 | return ACT_RET_PRS_ERR; |
| 9857 | } |
| 9858 | |
| 9859 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9860 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9861 | |
| 9862 | /* TODO: later accept arguments. */ |
| 9863 | rule->arg.hlua_rule->args = NULL; |
| 9864 | |
| 9865 | /* Add applet pointer in the rule. */ |
| 9866 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9867 | rule->applet.name = fcn->name; |
| 9868 | rule->applet.init = hlua_applet_http_init; |
| 9869 | rule->applet.fct = hlua_applet_http_fct; |
| 9870 | rule->applet.release = hlua_applet_http_release; |
| 9871 | rule->applet.timeout = hlua_timeout_applet; |
| 9872 | |
| 9873 | return ACT_RET_PRS_OK; |
| 9874 | } |
| 9875 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9876 | /* This function is an LUA binding used for registering |
| 9877 | * "sample-conv" functions. It expects a converter name used |
| 9878 | * in the haproxy configuration file, and an LUA function. |
| 9879 | */ |
| 9880 | __LJMP static int hlua_register_action(lua_State *L) |
| 9881 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9882 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9883 | const char *name; |
| 9884 | int ref; |
| 9885 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9886 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9887 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9888 | struct buffer *trash; |
| 9889 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9890 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9891 | /* Initialise the number of expected arguments at 0. */ |
| 9892 | nargs = 0; |
| 9893 | |
| 9894 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9895 | 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] | 9896 | |
| 9897 | /* First argument : converter name. */ |
| 9898 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9899 | |
| 9900 | /* Second argument : environment. */ |
| 9901 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9902 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9903 | |
| 9904 | /* Third argument : lua function. */ |
| 9905 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9906 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9907 | /* 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] | 9908 | if (lua_gettop(L) >= 4) |
| 9909 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9910 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9911 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9912 | lua_pushnil(L); |
| 9913 | while (lua_next(L, 2) != 0) { |
| 9914 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9915 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9916 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9917 | /* Check if action exists */ |
| 9918 | trash = get_trash_chunk(); |
| 9919 | chunk_printf(trash, "lua.%s", name); |
| 9920 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9921 | akw = tcp_req_cont_action(trash->area); |
| 9922 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9923 | akw = tcp_res_cont_action(trash->area); |
| 9924 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9925 | akw = action_http_req_custom(trash->area); |
| 9926 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9927 | akw = action_http_res_custom(trash->area); |
| 9928 | } else { |
| 9929 | akw = NULL; |
| 9930 | } |
| 9931 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9932 | fcn = akw->private; |
| 9933 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9934 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9935 | "This will become a hard error in version 2.5.\n", name); |
| 9936 | } |
| 9937 | fcn->function_ref[hlua_state_id] = ref; |
| 9938 | |
| 9939 | /* pop the environment string. */ |
| 9940 | lua_pop(L, 1); |
| 9941 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9942 | } |
| 9943 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9944 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9945 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9946 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9947 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9948 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9949 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9950 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9951 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9952 | |
| 9953 | /* Fill fcn. */ |
| 9954 | fcn->name = strdup(name); |
| 9955 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9956 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9957 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9958 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9959 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9960 | fcn->nargs = nargs; |
| 9961 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9962 | /* List head */ |
| 9963 | akl->list.n = akl->list.p = NULL; |
| 9964 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9965 | /* action keyword. */ |
| 9966 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9967 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9968 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9969 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9970 | |
| 9971 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9972 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9973 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9974 | akl->kw[0].private = fcn; |
| 9975 | akl->kw[0].parse = action_register_lua; |
| 9976 | |
| 9977 | /* select the action registering point. */ |
| 9978 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9979 | tcp_req_cont_keywords_register(akl); |
| 9980 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9981 | tcp_res_cont_keywords_register(akl); |
| 9982 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9983 | http_req_keywords_register(akl); |
| 9984 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9985 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9986 | else { |
| 9987 | release_hlua_function(fcn); |
| 9988 | if (akl) |
| 9989 | ha_free((char **)&(akl->kw[0].kw)); |
| 9990 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9991 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9992 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9993 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9994 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9995 | |
| 9996 | /* pop the environment string. */ |
| 9997 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9998 | |
| 9999 | /* reset for next loop */ |
| 10000 | akl = NULL; |
| 10001 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10002 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10003 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 10004 | |
| 10005 | alloc_error: |
| 10006 | release_hlua_function(fcn); |
| 10007 | ha_free(&akl); |
| 10008 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10009 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10010 | } |
| 10011 | |
| 10012 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 10013 | struct act_rule *rule, char **err) |
| 10014 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 10015 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10016 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 10017 | if (px->mode == PR_MODE_HTTP) { |
| 10018 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 10019 | return ACT_RET_PRS_ERR; |
| 10020 | } |
| 10021 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10022 | /* Memory for the rule. */ |
| 10023 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10024 | if (!rule->arg.hlua_rule) { |
| 10025 | memprintf(err, "out of memory error"); |
| 10026 | return ACT_RET_PRS_ERR; |
| 10027 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10028 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10029 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10030 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10031 | |
| 10032 | /* TODO: later accept arguments. */ |
| 10033 | rule->arg.hlua_rule->args = NULL; |
| 10034 | |
| 10035 | /* Add applet pointer in the rule. */ |
| 10036 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10037 | rule->applet.name = fcn->name; |
| 10038 | rule->applet.init = hlua_applet_tcp_init; |
| 10039 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10040 | rule->applet.release = hlua_applet_tcp_release; |
| 10041 | rule->applet.timeout = hlua_timeout_applet; |
| 10042 | |
| 10043 | return 0; |
| 10044 | } |
| 10045 | |
| 10046 | /* This function is an LUA binding used for registering |
| 10047 | * "sample-conv" functions. It expects a converter name used |
| 10048 | * in the haproxy configuration file, and an LUA function. |
| 10049 | */ |
| 10050 | __LJMP static int hlua_register_service(lua_State *L) |
| 10051 | { |
| 10052 | struct action_kw_list *akl; |
| 10053 | const char *name; |
| 10054 | const char *env; |
| 10055 | int ref; |
| 10056 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10057 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10058 | struct buffer *trash; |
| 10059 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10060 | |
| 10061 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10062 | |
| 10063 | /* First argument : converter name. */ |
| 10064 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10065 | |
| 10066 | /* Second argument : environment. */ |
| 10067 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10068 | |
| 10069 | /* Third argument : lua function. */ |
| 10070 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10071 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10072 | /* Check for service already registered */ |
| 10073 | trash = get_trash_chunk(); |
| 10074 | chunk_printf(trash, "lua.%s", name); |
| 10075 | akw = service_find(trash->area); |
| 10076 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10077 | fcn = akw->private; |
| 10078 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10079 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10080 | "This will become a hard error in version 2.5.\n", name); |
| 10081 | } |
| 10082 | fcn->function_ref[hlua_state_id] = ref; |
| 10083 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10084 | } |
| 10085 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10086 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10087 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10088 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10089 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10090 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10091 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10092 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10093 | |
| 10094 | /* Fill fcn. */ |
| 10095 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10096 | fcn->name = calloc(1, len); |
| 10097 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10098 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10099 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10100 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10101 | |
| 10102 | /* List head */ |
| 10103 | akl->list.n = akl->list.p = NULL; |
| 10104 | |
| 10105 | /* converter keyword. */ |
| 10106 | len = strlen("lua.") + strlen(name) + 1; |
| 10107 | akl->kw[0].kw = calloc(1, len); |
| 10108 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10109 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10110 | |
| 10111 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10112 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10113 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10114 | if (strcmp(env, "tcp") == 0) |
| 10115 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10116 | else if (strcmp(env, "http") == 0) |
| 10117 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10118 | else { |
| 10119 | release_hlua_function(fcn); |
| 10120 | if (akl) |
| 10121 | ha_free((char **)&(akl->kw[0].kw)); |
| 10122 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10123 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10124 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10125 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10126 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10127 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10128 | akl->kw[0].private = fcn; |
| 10129 | |
| 10130 | /* End of array. */ |
| 10131 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10132 | |
| 10133 | /* Register this new converter */ |
| 10134 | service_keywords_register(akl); |
| 10135 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10136 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10137 | |
| 10138 | alloc_error: |
| 10139 | release_hlua_function(fcn); |
| 10140 | ha_free(&akl); |
| 10141 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10142 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10143 | } |
| 10144 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10145 | /* This function initialises Lua cli handler. It copies the |
| 10146 | * arguments in the Lua stack and create channel IO objects. |
| 10147 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10148 | 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] | 10149 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10150 | 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] | 10151 | struct hlua *hlua; |
| 10152 | struct hlua_function *fcn; |
| 10153 | int i; |
| 10154 | const char *error; |
| 10155 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10156 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10157 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10158 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10159 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10160 | if (!hlua) { |
| 10161 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10162 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10163 | } |
| 10164 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10165 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10166 | |
| 10167 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10168 | * 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] | 10169 | * applet_http. It is absolutely compatible. |
| 10170 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10171 | ctx->task = task_new_here(); |
| 10172 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10173 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10174 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10175 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10176 | ctx->task->nice = 0; |
| 10177 | ctx->task->context = appctx; |
| 10178 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10179 | |
| 10180 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10181 | 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] | 10182 | 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] | 10183 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10184 | } |
| 10185 | |
| 10186 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10187 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10188 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10189 | error = lua_tostring(hlua->T, -1); |
| 10190 | else |
| 10191 | error = "critical error"; |
| 10192 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10193 | goto error; |
| 10194 | } |
| 10195 | |
| 10196 | /* Check stack available size. */ |
| 10197 | if (!lua_checkstack(hlua->T, 2)) { |
| 10198 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10199 | goto error; |
| 10200 | } |
| 10201 | |
| 10202 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10203 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref[hlua->state_id]); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10204 | |
| 10205 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10206 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10207 | */ |
| 10208 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10209 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10210 | goto error; |
| 10211 | } |
| 10212 | hlua->nargs = 1; |
| 10213 | |
| 10214 | /* push keywords in the stack. */ |
| 10215 | for (i = 0; *args[i]; i++) { |
| 10216 | /* Check stack available size. */ |
| 10217 | if (!lua_checkstack(hlua->T, 1)) { |
| 10218 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10219 | goto error; |
| 10220 | } |
| 10221 | lua_pushstring(hlua->T, args[i]); |
| 10222 | hlua->nargs++; |
| 10223 | } |
| 10224 | |
| 10225 | /* We must initialize the execution timeouts. */ |
| 10226 | hlua->max_time = hlua_timeout_session; |
| 10227 | |
| 10228 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10229 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10230 | |
| 10231 | /* It's ok */ |
| 10232 | return 0; |
| 10233 | |
| 10234 | /* It's not ok. */ |
| 10235 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10236 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10237 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10238 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10239 | return 1; |
| 10240 | } |
| 10241 | |
| 10242 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10243 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10244 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10245 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10246 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10247 | struct hlua_function *fcn; |
| 10248 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10249 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10250 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10251 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10252 | |
| 10253 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10254 | 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] | 10255 | return 1; |
| 10256 | |
| 10257 | /* Execute the function. */ |
| 10258 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10259 | |
| 10260 | /* finished. */ |
| 10261 | case HLUA_E_OK: |
| 10262 | return 1; |
| 10263 | |
| 10264 | /* yield. */ |
| 10265 | case HLUA_E_AGAIN: |
| 10266 | /* We want write. */ |
| 10267 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10268 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10269 | /* Set the timeout. */ |
| 10270 | if (hlua->wake_time != TICK_ETERNITY) |
| 10271 | task_schedule(hlua->task, hlua->wake_time); |
| 10272 | return 0; |
| 10273 | |
| 10274 | /* finished with error. */ |
| 10275 | case HLUA_E_ERRMSG: |
| 10276 | /* Display log. */ |
| 10277 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10278 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10279 | lua_pop(hlua->T, 1); |
| 10280 | return 1; |
| 10281 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10282 | case HLUA_E_ETMOUT: |
| 10283 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10284 | fcn->name); |
| 10285 | return 1; |
| 10286 | |
| 10287 | case HLUA_E_NOMEM: |
| 10288 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10289 | fcn->name); |
| 10290 | return 1; |
| 10291 | |
| 10292 | case HLUA_E_YIELD: /* unexpected */ |
| 10293 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10294 | fcn->name); |
| 10295 | return 1; |
| 10296 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10297 | case HLUA_E_ERR: |
| 10298 | /* Display log. */ |
| 10299 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10300 | fcn->name); |
| 10301 | return 1; |
| 10302 | |
| 10303 | default: |
| 10304 | return 1; |
| 10305 | } |
| 10306 | |
| 10307 | return 1; |
| 10308 | } |
| 10309 | |
| 10310 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10311 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10312 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10313 | |
| 10314 | hlua_ctx_destroy(ctx->hlua); |
| 10315 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10316 | } |
| 10317 | |
| 10318 | /* This function is an LUA binding used for registering |
| 10319 | * new keywords in the cli. It expects a list of keywords |
| 10320 | * which are the "path". It is limited to 5 keywords. A |
| 10321 | * description of the command, a function to be executed |
| 10322 | * for the parsing and a function for io handlers. |
| 10323 | */ |
| 10324 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10325 | { |
| 10326 | struct cli_kw_list *cli_kws; |
| 10327 | const char *message; |
| 10328 | int ref_io; |
| 10329 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10330 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10331 | int index; |
| 10332 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10333 | struct buffer *trash; |
| 10334 | const char *kw[5]; |
| 10335 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10336 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10337 | |
| 10338 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10339 | |
| 10340 | /* First argument : an array of maximum 5 keywords. */ |
| 10341 | if (!lua_istable(L, 1)) |
| 10342 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10343 | |
| 10344 | /* Second argument : string with contextual message. */ |
| 10345 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10346 | |
| 10347 | /* Third and fourth argument : lua function. */ |
| 10348 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10349 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10350 | /* Check for CLI service already registered */ |
| 10351 | trash = get_trash_chunk(); |
| 10352 | index = 0; |
| 10353 | lua_pushnil(L); |
| 10354 | memset(kw, 0, sizeof(kw)); |
| 10355 | while (lua_next(L, 1) != 0) { |
| 10356 | if (index >= CLI_PREFIX_KW_NB) |
| 10357 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10358 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10359 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10360 | kw[index] = lua_tostring(L, -1); |
| 10361 | if (index == 0) |
| 10362 | chunk_printf(trash, "%s", kw[index]); |
| 10363 | else |
| 10364 | chunk_appendf(trash, " %s", kw[index]); |
| 10365 | index++; |
| 10366 | lua_pop(L, 1); |
| 10367 | } |
| 10368 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10369 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10370 | fcn = cli_kw->private; |
| 10371 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10372 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10373 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10374 | } |
| 10375 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10376 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10377 | } |
| 10378 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10379 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10380 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10381 | if (!cli_kws) { |
| 10382 | errmsg = "Lua out of memory error."; |
| 10383 | goto error; |
| 10384 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10385 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10386 | if (!fcn) { |
| 10387 | errmsg = "Lua out of memory error."; |
| 10388 | goto error; |
| 10389 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10390 | |
| 10391 | /* Fill path. */ |
| 10392 | index = 0; |
| 10393 | lua_pushnil(L); |
| 10394 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10395 | if (index >= 5) { |
| 10396 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10397 | goto error; |
| 10398 | } |
| 10399 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10400 | errmsg = "1st argument must be a table filled with strings"; |
| 10401 | goto error; |
| 10402 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10403 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10404 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10405 | errmsg = "Lua out of memory error."; |
| 10406 | goto error; |
| 10407 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10408 | index++; |
| 10409 | lua_pop(L, 1); |
| 10410 | } |
| 10411 | |
| 10412 | /* Copy help message. */ |
| 10413 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10414 | if (!cli_kws->kw[0].usage) { |
| 10415 | errmsg = "Lua out of memory error."; |
| 10416 | goto error; |
| 10417 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10418 | |
| 10419 | /* Fill fcn io handler. */ |
| 10420 | len = strlen("<lua.cli>") + 1; |
| 10421 | for (i = 0; i < index; i++) |
| 10422 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10423 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10424 | if (!fcn->name) { |
| 10425 | errmsg = "Lua out of memory error."; |
| 10426 | goto error; |
| 10427 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10428 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10429 | for (i = 0; i < index; i++) { |
| 10430 | strncat((char *)fcn->name, ".", len); |
| 10431 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10432 | } |
| 10433 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10434 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10435 | |
| 10436 | /* Fill last entries. */ |
| 10437 | cli_kws->kw[0].private = fcn; |
| 10438 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10439 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10440 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10441 | |
| 10442 | /* Register this new converter */ |
| 10443 | cli_register_kw(cli_kws); |
| 10444 | |
| 10445 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10446 | |
| 10447 | error: |
| 10448 | release_hlua_function(fcn); |
| 10449 | if (cli_kws) { |
| 10450 | for (i = 0; i < index; i++) |
| 10451 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10452 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10453 | } |
| 10454 | ha_free(&cli_kws); |
| 10455 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10456 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10457 | } |
| 10458 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10459 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10460 | { |
| 10461 | struct hlua_flt_config *conf = fconf->conf; |
| 10462 | lua_State *L; |
| 10463 | int error, pos, state_id, flt_ref; |
| 10464 | |
| 10465 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10466 | L = hlua_states[state_id]; |
| 10467 | pos = lua_gettop(L); |
| 10468 | |
| 10469 | /* The filter parsing function */ |
| 10470 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10471 | |
| 10472 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10473 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10474 | |
| 10475 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10476 | lua_newtable(L); |
| 10477 | lua_pushnil(L); |
| 10478 | |
| 10479 | while (lua_next(L, pos+2)) { |
| 10480 | lua_pushvalue(L, -2); |
| 10481 | lua_insert(L, -2); |
| 10482 | lua_settable(L, -4); |
| 10483 | } |
| 10484 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10485 | |
| 10486 | /* Remove the original lua filter class from the stack */ |
| 10487 | lua_pop(L, 1); |
| 10488 | |
| 10489 | /* Push the copy on the stack */ |
| 10490 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10491 | |
| 10492 | /* extra args are pushed in a table */ |
| 10493 | lua_newtable(L); |
| 10494 | for (pos = 0; conf->args[pos]; pos++) { |
| 10495 | /* Check stack available size. */ |
| 10496 | if (!lua_checkstack(L, 1)) { |
| 10497 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10498 | goto error; |
| 10499 | } |
| 10500 | lua_pushstring(L, conf->args[pos]); |
| 10501 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10502 | } |
| 10503 | |
| 10504 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10505 | switch (error) { |
| 10506 | case LUA_OK: |
| 10507 | /* replace the filter ref */ |
| 10508 | conf->ref[state_id] = flt_ref; |
| 10509 | break; |
| 10510 | case LUA_ERRRUN: |
| 10511 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10512 | goto error; |
| 10513 | case LUA_ERRMEM: |
| 10514 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10515 | goto error; |
| 10516 | case LUA_ERRERR: |
| 10517 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10518 | goto error; |
| 10519 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10520 | case LUA_ERRGCMM: |
| 10521 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10522 | goto error; |
| 10523 | #endif |
| 10524 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10525 | 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] | 10526 | goto error; |
| 10527 | } |
| 10528 | |
| 10529 | lua_settop(L, 0); |
| 10530 | return 0; |
| 10531 | |
| 10532 | error: |
| 10533 | lua_settop(L, 0); |
| 10534 | return -1; |
| 10535 | } |
| 10536 | |
| 10537 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10538 | { |
| 10539 | struct hlua_flt_config *conf = fconf->conf; |
| 10540 | lua_State *L; |
| 10541 | int state_id; |
| 10542 | |
| 10543 | if (!conf) |
| 10544 | return; |
| 10545 | |
| 10546 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10547 | L = hlua_states[state_id]; |
| 10548 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10549 | } |
| 10550 | |
| 10551 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10552 | { |
| 10553 | struct hlua_flt_config *conf = fconf->conf; |
| 10554 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10555 | |
| 10556 | /* Rely on per-thread init for global scripts */ |
| 10557 | if (!state_id) |
| 10558 | return hlua_filter_init_per_thread(px, fconf); |
| 10559 | return 0; |
| 10560 | } |
| 10561 | |
| 10562 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10563 | { |
| 10564 | |
| 10565 | if (fconf->conf) { |
| 10566 | struct hlua_flt_config *conf = fconf->conf; |
| 10567 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10568 | int pos; |
| 10569 | |
| 10570 | /* Rely on per-thread deinit for global scripts */ |
| 10571 | if (!state_id) |
| 10572 | hlua_filter_deinit_per_thread(px, fconf); |
| 10573 | |
| 10574 | for (pos = 0; conf->args[pos]; pos++) |
| 10575 | free(conf->args[pos]); |
| 10576 | free(conf->args); |
| 10577 | } |
| 10578 | ha_free(&fconf->conf); |
| 10579 | ha_free((char **)&fconf->id); |
| 10580 | ha_free(&fconf->ops); |
| 10581 | } |
| 10582 | |
| 10583 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10584 | { |
| 10585 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10586 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10587 | int ret = 1; |
| 10588 | |
| 10589 | /* In the execution wrappers linked with a stream, the |
| 10590 | * Lua context can be not initialized. This behavior |
| 10591 | * permits to save performances because a systematic |
| 10592 | * Lua initialization cause 5% performances loss. |
| 10593 | */ |
| 10594 | if (!s->hlua) { |
| 10595 | struct hlua *hlua; |
| 10596 | |
| 10597 | hlua = pool_alloc(pool_head_hlua); |
| 10598 | if (!hlua) { |
| 10599 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10600 | conf->reg->name); |
| 10601 | ret = 0; |
| 10602 | goto end; |
| 10603 | } |
| 10604 | HLUA_INIT(hlua); |
| 10605 | s->hlua = hlua; |
| 10606 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10607 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10608 | conf->reg->name); |
| 10609 | ret = 0; |
| 10610 | goto end; |
| 10611 | } |
| 10612 | } |
| 10613 | |
| 10614 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10615 | if (!flt_ctx) { |
| 10616 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10617 | conf->reg->name); |
| 10618 | ret = 0; |
| 10619 | goto end; |
| 10620 | } |
| 10621 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10622 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10623 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10624 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10625 | conf->reg->name); |
| 10626 | ret = 0; |
| 10627 | goto end; |
| 10628 | } |
| 10629 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10630 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10631 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10632 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10633 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10634 | conf->reg->name); |
| 10635 | ret = 0; |
| 10636 | goto end; |
| 10637 | } |
| 10638 | |
| 10639 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10640 | /* The following Lua calls can fail. */ |
| 10641 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10642 | const char *error; |
| 10643 | |
| 10644 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10645 | error = lua_tostring(s->hlua->T, -1); |
| 10646 | else |
| 10647 | error = "critical error"; |
| 10648 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10649 | ret = 0; |
| 10650 | goto end; |
| 10651 | } |
| 10652 | |
| 10653 | /* Check stack size. */ |
| 10654 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10655 | 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] | 10656 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10657 | ret = 0; |
| 10658 | goto end; |
| 10659 | } |
| 10660 | |
| 10661 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10662 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10663 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10664 | conf->reg->name); |
| 10665 | RESET_SAFE_LJMP(s->hlua); |
| 10666 | ret = 0; |
| 10667 | goto end; |
| 10668 | } |
| 10669 | lua_insert(s->hlua->T, -2); |
| 10670 | |
| 10671 | /* Push the copy on the stack */ |
| 10672 | s->hlua->nargs = 1; |
| 10673 | |
| 10674 | /* We must initialize the execution timeouts. */ |
| 10675 | s->hlua->max_time = hlua_timeout_session; |
| 10676 | |
| 10677 | /* At this point the execution is safe. */ |
| 10678 | RESET_SAFE_LJMP(s->hlua); |
| 10679 | } |
| 10680 | |
| 10681 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10682 | case HLUA_E_OK: |
| 10683 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10684 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10685 | ret = 0; |
| 10686 | goto end; |
| 10687 | } |
| 10688 | |
| 10689 | /* Attached the filter pointer to the ctx */ |
| 10690 | lua_pushstring(s->hlua->T, "__filter"); |
| 10691 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10692 | lua_settable(s->hlua->T, -3); |
| 10693 | |
| 10694 | /* Save a ref on the filter ctx */ |
| 10695 | lua_pushvalue(s->hlua->T, 1); |
| 10696 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10697 | filter->ctx = flt_ctx; |
| 10698 | break; |
| 10699 | case HLUA_E_ERRMSG: |
| 10700 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10701 | ret = -1; |
| 10702 | goto end; |
| 10703 | case HLUA_E_ETMOUT: |
| 10704 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10705 | ret = 0; |
| 10706 | goto end; |
| 10707 | case HLUA_E_NOMEM: |
| 10708 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10709 | ret = 0; |
| 10710 | goto end; |
| 10711 | case HLUA_E_AGAIN: |
| 10712 | case HLUA_E_YIELD: |
| 10713 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10714 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10715 | ret = 0; |
| 10716 | goto end; |
| 10717 | case HLUA_E_ERR: |
| 10718 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10719 | ret = 0; |
| 10720 | goto end; |
| 10721 | default: |
| 10722 | ret = 0; |
| 10723 | goto end; |
| 10724 | } |
| 10725 | |
| 10726 | end: |
| 10727 | if (s->hlua) |
| 10728 | lua_settop(s->hlua->T, 0); |
| 10729 | if (ret <= 0) { |
| 10730 | if (flt_ctx) { |
| 10731 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10732 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10733 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10734 | } |
| 10735 | } |
| 10736 | return ret; |
| 10737 | } |
| 10738 | |
| 10739 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10740 | { |
| 10741 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10742 | |
| 10743 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10744 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10745 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10746 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10747 | filter->ctx = NULL; |
| 10748 | } |
| 10749 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10750 | static int hlua_filter_from_payload(struct filter *filter) |
| 10751 | { |
| 10752 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10753 | |
| 10754 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10755 | } |
| 10756 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10757 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10758 | int dir, unsigned int flags) |
| 10759 | { |
| 10760 | struct hlua *flt_hlua; |
| 10761 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10762 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10763 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10764 | int ret = 1; |
| 10765 | |
| 10766 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10767 | if (!flt_hlua) |
| 10768 | goto end; |
| 10769 | |
| 10770 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10771 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10772 | |
| 10773 | /* The following Lua calls can fail. */ |
| 10774 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10775 | const char *error; |
| 10776 | |
| 10777 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10778 | error = lua_tostring(flt_hlua->T, -1); |
| 10779 | else |
| 10780 | error = "critical error"; |
| 10781 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10782 | goto end; |
| 10783 | } |
| 10784 | |
| 10785 | /* Check stack size. */ |
| 10786 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10787 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10788 | RESET_SAFE_LJMP(flt_hlua); |
| 10789 | goto end; |
| 10790 | } |
| 10791 | |
| 10792 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10793 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10794 | RESET_SAFE_LJMP(flt_hlua); |
| 10795 | goto end; |
| 10796 | } |
| 10797 | lua_insert(flt_hlua->T, -2); |
| 10798 | |
| 10799 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10800 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10801 | RESET_SAFE_LJMP(flt_hlua); |
| 10802 | goto end; |
| 10803 | } |
| 10804 | flt_hlua->nargs = 2; |
| 10805 | |
| 10806 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10807 | if (dir == SMP_OPT_DIR_REQ) |
| 10808 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10809 | else |
| 10810 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10811 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10812 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10813 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10814 | lua_settable(flt_hlua->T, -3); |
| 10815 | } |
| 10816 | flt_hlua->nargs++; |
| 10817 | } |
| 10818 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10819 | if (dir == SMP_OPT_DIR_REQ) |
| 10820 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10821 | else |
| 10822 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10823 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10824 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10825 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10826 | lua_settable(flt_hlua->T, -3); |
| 10827 | } |
| 10828 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10829 | } |
| 10830 | |
| 10831 | /* Check stack size. */ |
| 10832 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10833 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10834 | RESET_SAFE_LJMP(flt_hlua); |
| 10835 | goto end; |
| 10836 | } |
| 10837 | |
| 10838 | while (extra_idx--) { |
| 10839 | lua_pushvalue(flt_hlua->T, 1); |
| 10840 | lua_remove(flt_hlua->T, 1); |
| 10841 | flt_hlua->nargs++; |
| 10842 | } |
| 10843 | |
| 10844 | /* We must initialize the execution timeouts. */ |
| 10845 | flt_hlua->max_time = hlua_timeout_session; |
| 10846 | |
| 10847 | /* At this point the execution is safe. */ |
| 10848 | RESET_SAFE_LJMP(flt_hlua); |
| 10849 | } |
| 10850 | |
| 10851 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10852 | case HLUA_E_OK: |
| 10853 | /* Catch the return value if it required */ |
| 10854 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10855 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10856 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10857 | } |
| 10858 | |
| 10859 | /* Set timeout in the required channel. */ |
| 10860 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10861 | if (dir == SMP_OPT_DIR_REQ) |
| 10862 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10863 | else |
| 10864 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10865 | } |
| 10866 | break; |
| 10867 | case HLUA_E_AGAIN: |
| 10868 | /* Set timeout in the required channel. */ |
| 10869 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10870 | if (dir == SMP_OPT_DIR_REQ) |
| 10871 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10872 | else |
| 10873 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10874 | } |
| 10875 | /* Some actions can be wake up when a "write" event |
| 10876 | * is detected on a response channel. This is useful |
| 10877 | * only for actions targeted on the requests. |
| 10878 | */ |
| 10879 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10880 | s->res.flags |= CF_WAKE_WRITE; |
| 10881 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10882 | s->req.flags |= CF_WAKE_WRITE; |
| 10883 | ret = 0; |
| 10884 | goto end; |
| 10885 | case HLUA_E_ERRMSG: |
| 10886 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10887 | ret = -1; |
| 10888 | goto end; |
| 10889 | case HLUA_E_ETMOUT: |
| 10890 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10891 | goto end; |
| 10892 | case HLUA_E_NOMEM: |
| 10893 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10894 | goto end; |
| 10895 | case HLUA_E_YIELD: |
| 10896 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10897 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10898 | goto end; |
| 10899 | case HLUA_E_ERR: |
| 10900 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10901 | goto end; |
| 10902 | default: |
| 10903 | goto end; |
| 10904 | } |
| 10905 | |
| 10906 | |
| 10907 | end: |
| 10908 | return ret; |
| 10909 | } |
| 10910 | |
| 10911 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10912 | { |
| 10913 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10914 | |
| 10915 | flt_ctx->flags = 0; |
| 10916 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10917 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10918 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10919 | } |
| 10920 | |
| 10921 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10922 | { |
| 10923 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10924 | |
| 10925 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10926 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10927 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10928 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10929 | } |
| 10930 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10931 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10932 | { |
| 10933 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10934 | |
| 10935 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10936 | return hlua_filter_callback(s, filter, "http_headers", |
| 10937 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10938 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10939 | } |
| 10940 | |
| 10941 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10942 | unsigned int offset, unsigned int len) |
| 10943 | { |
| 10944 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10945 | struct hlua *flt_hlua; |
| 10946 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10947 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10948 | int ret; |
| 10949 | |
| 10950 | flt_hlua = flt_ctx->hlua[idx]; |
| 10951 | flt_ctx->cur_off[idx] = offset; |
| 10952 | flt_ctx->cur_len[idx] = len; |
| 10953 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10954 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10955 | if (ret != -1) { |
| 10956 | ret = flt_ctx->cur_len[idx]; |
| 10957 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10958 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10959 | if (ret > flt_ctx->cur_len[idx]) |
| 10960 | ret = flt_ctx->cur_len[idx]; |
| 10961 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10962 | } |
| 10963 | } |
| 10964 | return ret; |
| 10965 | } |
| 10966 | |
| 10967 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10968 | { |
| 10969 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10970 | |
| 10971 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10972 | return hlua_filter_callback(s, filter, "http_end", |
| 10973 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10974 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10975 | } |
| 10976 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10977 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10978 | unsigned int offset, unsigned int len) |
| 10979 | { |
| 10980 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10981 | struct hlua *flt_hlua; |
| 10982 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10983 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10984 | int ret; |
| 10985 | |
| 10986 | flt_hlua = flt_ctx->hlua[idx]; |
| 10987 | flt_ctx->cur_off[idx] = offset; |
| 10988 | flt_ctx->cur_len[idx] = len; |
| 10989 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10990 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10991 | if (ret != -1) { |
| 10992 | ret = flt_ctx->cur_len[idx]; |
| 10993 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10994 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10995 | if (ret > flt_ctx->cur_len[idx]) |
| 10996 | ret = flt_ctx->cur_len[idx]; |
| 10997 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10998 | } |
| 10999 | } |
| 11000 | return ret; |
| 11001 | } |
| 11002 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11003 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 11004 | struct flt_conf *fconf, char **err, void *private) |
| 11005 | { |
| 11006 | struct hlua_reg_filter *reg_flt = private; |
| 11007 | lua_State *L; |
| 11008 | struct hlua_flt_config *conf = NULL; |
| 11009 | const char *flt_id = NULL; |
| 11010 | int state_id, pos, flt_flags = 0; |
| 11011 | struct flt_ops *hlua_flt_ops = NULL; |
| 11012 | |
| 11013 | state_id = reg_flt_to_stack_id(reg_flt); |
| 11014 | L = hlua_states[state_id]; |
| 11015 | |
| 11016 | /* Initialize the filter ops with default callbacks */ |
| 11017 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11018 | if (!hlua_flt_ops) |
| 11019 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11020 | hlua_flt_ops->init = hlua_filter_init; |
| 11021 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 11022 | if (state_id) { |
| 11023 | /* Set per-thread callback if script is loaded per-thread */ |
| 11024 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11025 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11026 | } |
| 11027 | hlua_flt_ops->attach = hlua_filter_new; |
| 11028 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11029 | |
| 11030 | /* Push the filter class on the stack and resolve all callbacks */ |
| 11031 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 11032 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11033 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11034 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11035 | lua_pop(L, 1); |
| 11036 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11037 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11038 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11039 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11040 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11041 | lua_pop(L, 1); |
| 11042 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11043 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11044 | lua_pop(L, 1); |
| 11045 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11046 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11047 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11048 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11049 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11050 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11051 | |
| 11052 | /* Get id and flags of the filter class */ |
| 11053 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11054 | flt_id = lua_tostring(L, -1); |
| 11055 | lua_pop(L, 1); |
| 11056 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11057 | flt_flags = lua_tointeger(L, -1); |
| 11058 | lua_pop(L, 1); |
| 11059 | |
| 11060 | /* Create the filter config */ |
| 11061 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11062 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11063 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11064 | conf->reg = reg_flt; |
| 11065 | |
| 11066 | /* duplicate args */ |
| 11067 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11068 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11069 | if (!conf->args) |
| 11070 | goto error; |
| 11071 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11072 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11073 | if (!conf->args[pos]) |
| 11074 | goto error; |
| 11075 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11076 | conf->args[pos] = NULL; |
| 11077 | *cur_arg += pos + 1; |
| 11078 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11079 | if (flt_id) { |
| 11080 | fconf->id = strdup(flt_id); |
| 11081 | if (!fconf->id) |
| 11082 | goto error; |
| 11083 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11084 | fconf->flags = flt_flags; |
| 11085 | fconf->conf = conf; |
| 11086 | fconf->ops = hlua_flt_ops; |
| 11087 | |
| 11088 | lua_settop(L, 0); |
| 11089 | return 0; |
| 11090 | |
| 11091 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11092 | 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] | 11093 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11094 | if (conf && conf->args) { |
| 11095 | for (pos = 0; conf->args[pos]; pos++) |
| 11096 | free(conf->args[pos]); |
| 11097 | free(conf->args); |
| 11098 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11099 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11100 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11101 | lua_settop(L, 0); |
| 11102 | return -1; |
| 11103 | } |
| 11104 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11105 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11106 | { |
| 11107 | struct filter *filter; |
| 11108 | struct channel *chn; |
| 11109 | |
| 11110 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11111 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11112 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11113 | |
| 11114 | lua_getfield(L, 1, "__filter"); |
| 11115 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11116 | filter = lua_touserdata (L, -1); |
| 11117 | lua_pop(L, 1); |
| 11118 | |
| 11119 | register_data_filter(chn_strm(chn), chn, filter); |
| 11120 | return 1; |
| 11121 | } |
| 11122 | |
| 11123 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11124 | { |
| 11125 | struct filter *filter; |
| 11126 | struct channel *chn; |
| 11127 | |
| 11128 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11129 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11130 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11131 | |
| 11132 | lua_getfield(L, 1, "__filter"); |
| 11133 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11134 | filter = lua_touserdata (L, -1); |
| 11135 | lua_pop(L, 1); |
| 11136 | |
| 11137 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11138 | return 1; |
| 11139 | } |
| 11140 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11141 | /* 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] | 11142 | * 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] | 11143 | * parse configuration arguments. |
| 11144 | */ |
| 11145 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11146 | { |
| 11147 | struct buffer *trash; |
| 11148 | struct flt_kw_list *fkl; |
| 11149 | struct flt_kw *fkw; |
| 11150 | const char *name; |
| 11151 | struct hlua_reg_filter *reg_flt= NULL; |
| 11152 | int flt_ref, fun_ref; |
| 11153 | int len; |
| 11154 | |
| 11155 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11156 | |
| 11157 | /* First argument : filter name. */ |
| 11158 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11159 | |
| 11160 | /* Second argument : The filter class */ |
| 11161 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11162 | |
| 11163 | /* Third argument : lua function. */ |
| 11164 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11165 | |
| 11166 | trash = get_trash_chunk(); |
| 11167 | chunk_printf(trash, "lua.%s", name); |
| 11168 | fkw = flt_find_kw(trash->area); |
| 11169 | if (fkw != NULL) { |
| 11170 | reg_flt = fkw->private; |
| 11171 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11172 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11173 | "This will become a hard error in version 2.5.\n", name); |
| 11174 | } |
| 11175 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11176 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11177 | return 0; |
| 11178 | } |
| 11179 | |
| 11180 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11181 | if (!fkl) |
| 11182 | goto alloc_error; |
| 11183 | fkl->scope = "HLUA"; |
| 11184 | |
| 11185 | reg_flt = new_hlua_reg_filter(name); |
| 11186 | if (!reg_flt) |
| 11187 | goto alloc_error; |
| 11188 | |
| 11189 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11190 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11191 | |
| 11192 | /* The filter keyword */ |
| 11193 | len = strlen("lua.") + strlen(name) + 1; |
| 11194 | fkl->kw[0].kw = calloc(1, len); |
| 11195 | if (!fkl->kw[0].kw) |
| 11196 | goto alloc_error; |
| 11197 | |
| 11198 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11199 | |
| 11200 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11201 | fkl->kw[0].private = reg_flt; |
| 11202 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11203 | |
| 11204 | /* Register this new filter */ |
| 11205 | flt_register_keywords(fkl); |
| 11206 | |
| 11207 | return 0; |
| 11208 | |
| 11209 | alloc_error: |
| 11210 | release_hlua_reg_filter(reg_flt); |
| 11211 | ha_free(&fkl); |
| 11212 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11213 | return 0; /* Never reached */ |
| 11214 | } |
| 11215 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11216 | 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] | 11217 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11218 | char **err, unsigned int *timeout) |
| 11219 | { |
| 11220 | const char *error; |
| 11221 | |
| 11222 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11223 | if (error == PARSE_TIME_OVER) { |
| 11224 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11225 | args[1], args[0]); |
| 11226 | return -1; |
| 11227 | } |
| 11228 | else if (error == PARSE_TIME_UNDER) { |
| 11229 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11230 | args[1], args[0]); |
| 11231 | return -1; |
| 11232 | } |
| 11233 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11234 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11235 | return -1; |
| 11236 | } |
| 11237 | return 0; |
| 11238 | } |
| 11239 | |
| 11240 | 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] | 11241 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11242 | char **err) |
| 11243 | { |
| 11244 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11245 | file, line, err, &hlua_timeout_session); |
| 11246 | } |
| 11247 | |
| 11248 | 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] | 11249 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11250 | char **err) |
| 11251 | { |
| 11252 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11253 | file, line, err, &hlua_timeout_task); |
| 11254 | } |
| 11255 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11256 | 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] | 11257 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11258 | char **err) |
| 11259 | { |
| 11260 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11261 | file, line, err, &hlua_timeout_applet); |
| 11262 | } |
| 11263 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11264 | 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] | 11265 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11266 | char **err) |
| 11267 | { |
| 11268 | char *error; |
| 11269 | |
| 11270 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11271 | if (*error != '\0') { |
| 11272 | memprintf(err, "%s: invalid number", args[0]); |
| 11273 | return -1; |
| 11274 | } |
| 11275 | return 0; |
| 11276 | } |
| 11277 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11278 | 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] | 11279 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11280 | char **err) |
| 11281 | { |
| 11282 | char *error; |
| 11283 | |
| 11284 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11285 | 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] | 11286 | return -1; |
| 11287 | } |
| 11288 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11289 | if (*error != '\0') { |
| 11290 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11291 | return -1; |
| 11292 | } |
| 11293 | return 0; |
| 11294 | } |
| 11295 | |
| 11296 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11297 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11298 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11299 | * the main lua entry point. |
| 11300 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11301 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11302 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11303 | * |
| 11304 | * In some error case, LUA set an error message in top of the stack. This function |
| 11305 | * 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] | 11306 | * |
| 11307 | * This function can fail with an abort() due to an Lua critical error. |
| 11308 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11309 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11310 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11311 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11312 | { |
| 11313 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11314 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11315 | |
| 11316 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11317 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11318 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11319 | 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] | 11320 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11321 | return -1; |
| 11322 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11323 | |
| 11324 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11325 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11326 | /* Check stack size. */ |
| 11327 | if (!lua_checkstack(L, 1)) { |
| 11328 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11329 | return -1; |
| 11330 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11331 | lua_pushstring(L, args[nargs]); |
| 11332 | } |
| 11333 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11334 | |
| 11335 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11336 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11337 | switch (error) { |
| 11338 | case LUA_OK: |
| 11339 | break; |
| 11340 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11341 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11342 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11343 | return -1; |
| 11344 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11345 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11346 | return -1; |
| 11347 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11348 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11349 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11350 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11351 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11352 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11353 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11354 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11355 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11356 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11357 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11358 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11359 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11360 | return -1; |
| 11361 | } |
| 11362 | |
| 11363 | return 0; |
| 11364 | } |
| 11365 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11366 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11367 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11368 | char **err) |
| 11369 | { |
| 11370 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11371 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11372 | return -1; |
| 11373 | } |
| 11374 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11375 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11376 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11377 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11378 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11379 | } |
| 11380 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11381 | 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] | 11382 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11383 | char **err) |
| 11384 | { |
| 11385 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11386 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11387 | |
| 11388 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11389 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11390 | return -1; |
| 11391 | } |
| 11392 | |
| 11393 | if (per_thread_load == NULL) { |
| 11394 | /* allocate the first entry large enough to store the final NULL */ |
| 11395 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11396 | if (per_thread_load == NULL) { |
| 11397 | memprintf(err, "out of memory error"); |
| 11398 | return -1; |
| 11399 | } |
| 11400 | } |
| 11401 | |
| 11402 | /* count used entries */ |
| 11403 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11404 | ; |
| 11405 | |
| 11406 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11407 | if (per_thread_load == NULL) { |
| 11408 | memprintf(err, "out of memory error"); |
| 11409 | return -1; |
| 11410 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11411 | per_thread_load[len + 1] = NULL; |
| 11412 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11413 | /* count args excepting the first, allocate array and copy args */ |
| 11414 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11415 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11416 | if (per_thread_load[len] == NULL) { |
| 11417 | memprintf(err, "out of memory error"); |
| 11418 | return -1; |
| 11419 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11420 | for (i = 1; *(args[i]) != 0; i++) { |
| 11421 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11422 | if (per_thread_load[len][i - 1] == NULL) { |
| 11423 | memprintf(err, "out of memory error"); |
| 11424 | return -1; |
| 11425 | } |
| 11426 | } |
| 11427 | per_thread_load[len][i - 1] = strdup(""); |
| 11428 | if (per_thread_load[len][i - 1] == NULL) { |
| 11429 | memprintf(err, "out of memory error"); |
| 11430 | return -1; |
| 11431 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11432 | |
| 11433 | /* loading for thread 1 only */ |
| 11434 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11435 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11436 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11437 | } |
| 11438 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11439 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11440 | * in the given <ctx>. |
| 11441 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11442 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11443 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11444 | lua_getglobal(L, "package"); /* push package variable */ |
| 11445 | lua_pushstring(L, path); /* push given path */ |
| 11446 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11447 | lua_getfield(L, -3, type); /* push old path */ |
| 11448 | lua_concat(L, 3); /* concatenate to new path */ |
| 11449 | lua_setfield(L, -2, type); /* store new path */ |
| 11450 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11451 | |
| 11452 | return 0; |
| 11453 | } |
| 11454 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11455 | 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] | 11456 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11457 | char **err) |
| 11458 | { |
| 11459 | char *path; |
| 11460 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11461 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11462 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11463 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11464 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11465 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11466 | } |
| 11467 | |
| 11468 | if (!(*args[1])) { |
| 11469 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11470 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11471 | } |
| 11472 | path = args[1]; |
| 11473 | |
| 11474 | if (*args[2]) { |
| 11475 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11476 | 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] | 11477 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11478 | } |
| 11479 | type = args[2]; |
| 11480 | } |
| 11481 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11482 | p = calloc(1, sizeof(*p)); |
| 11483 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11484 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11485 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11486 | } |
| 11487 | p->path = strdup(path); |
| 11488 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11489 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11490 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11491 | } |
| 11492 | p->type = strdup(type); |
| 11493 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11494 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11495 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11496 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11497 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11498 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11499 | /* Handle the global state and the per-thread state for the first |
| 11500 | * thread. The remaining threads will be initialized based on |
| 11501 | * prepend_path_list. |
| 11502 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11503 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11504 | lua_State *L = hlua_states[i]; |
| 11505 | const char *error; |
| 11506 | |
| 11507 | if (setjmp(safe_ljmp_env) != 0) { |
| 11508 | lua_atpanic(L, hlua_panic_safe); |
| 11509 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11510 | error = lua_tostring(L, -1); |
| 11511 | else |
| 11512 | error = "critical error"; |
| 11513 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11514 | exit(1); |
| 11515 | } else { |
| 11516 | lua_atpanic(L, hlua_panic_ljmp); |
| 11517 | } |
| 11518 | |
| 11519 | hlua_prepend_path(L, type, path); |
| 11520 | |
| 11521 | lua_atpanic(L, hlua_panic_safe); |
| 11522 | } |
| 11523 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11524 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11525 | |
| 11526 | err2: |
| 11527 | free(p->type); |
| 11528 | free(p->path); |
| 11529 | err: |
| 11530 | free(p); |
| 11531 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11532 | } |
| 11533 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11534 | /* configuration keywords declaration */ |
| 11535 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11536 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11537 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11538 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11539 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11540 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11541 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11542 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11543 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11544 | { 0, NULL, NULL }, |
| 11545 | }}; |
| 11546 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11547 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11548 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11549 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11550 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11551 | /* |
| 11552 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11553 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11554 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11555 | * way. |
| 11556 | */ |
| 11557 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11558 | { |
| 11559 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11560 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11561 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11562 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11563 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11564 | struct hlua *hlua; |
| 11565 | char *err = NULL; |
| 11566 | int y = 1; |
| 11567 | |
| 11568 | hlua = hlua_gethlua(L); |
| 11569 | |
| 11570 | /* get the first ckchi to copy */ |
| 11571 | if (ckchi == NULL) |
| 11572 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11573 | |
| 11574 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11575 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11576 | struct ckch_inst *new_inst; |
| 11577 | |
| 11578 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11579 | if (y % 10 == 0) { |
| 11580 | |
| 11581 | *lua_ckchi = ckchi; |
| 11582 | |
| 11583 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11584 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11585 | } |
| 11586 | |
| 11587 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11588 | goto error; |
| 11589 | |
| 11590 | /* link the new ckch_inst to the duplicate */ |
| 11591 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11592 | y++; |
| 11593 | } |
| 11594 | |
| 11595 | /* The generation is finished, we can insert everything */ |
| 11596 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11597 | |
| 11598 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11599 | |
| 11600 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11601 | |
| 11602 | return 0; |
| 11603 | |
| 11604 | error: |
| 11605 | ckch_store_free(new_ckchs); |
| 11606 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11607 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11608 | free(err); |
| 11609 | |
| 11610 | return 0; |
| 11611 | } |
| 11612 | |
| 11613 | /* |
| 11614 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11615 | * from the table in parameter. |
| 11616 | * |
| 11617 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11618 | * means it does not need to have a transaction since everything is done in the |
| 11619 | * same function. |
| 11620 | * |
| 11621 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11622 | * |
| 11623 | */ |
| 11624 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11625 | { |
| 11626 | struct hlua *hlua; |
| 11627 | struct ckch_inst **lua_ckchi; |
| 11628 | struct ckch_store **lua_ckchs; |
| 11629 | struct ckch_store *old_ckchs = NULL; |
| 11630 | struct ckch_store *new_ckchs = NULL; |
| 11631 | int errcode = 0; |
| 11632 | char *err = NULL; |
| 11633 | struct cert_exts *cert_ext = NULL; |
| 11634 | char *filename; |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11635 | struct ckch_data *data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11636 | int ret; |
| 11637 | |
| 11638 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11639 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11640 | |
| 11641 | hlua = hlua_gethlua(L); |
| 11642 | |
| 11643 | /* FIXME: this should not return an error but should come back later */ |
| 11644 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11645 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11646 | |
| 11647 | ret = lua_getfield(L, -1, "filename"); |
| 11648 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11649 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11650 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11651 | goto end; |
| 11652 | } |
| 11653 | filename = (char *)lua_tostring(L, -1); |
| 11654 | |
| 11655 | |
| 11656 | /* look for the filename in the tree */ |
| 11657 | old_ckchs = ckchs_lookup(filename); |
| 11658 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11659 | 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] | 11660 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11661 | goto end; |
| 11662 | } |
| 11663 | /* TODO: handle extra_files_noext */ |
| 11664 | |
| 11665 | new_ckchs = ckchs_dup(old_ckchs); |
| 11666 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11667 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11668 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11669 | goto end; |
| 11670 | } |
| 11671 | |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11672 | data = new_ckchs->data; |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11673 | |
| 11674 | /* loop on the field in the table, which have the same name as the |
| 11675 | * possible extensions of files */ |
| 11676 | lua_pushnil(L); |
| 11677 | while (lua_next(L, 1)) { |
| 11678 | int i; |
| 11679 | const char *field = lua_tostring(L, -2); |
| 11680 | char *payload = (char *)lua_tostring(L, -1); |
| 11681 | |
| 11682 | if (!field || strcmp(field, "filename") == 0) { |
| 11683 | lua_pop(L, 1); |
| 11684 | continue; |
| 11685 | } |
| 11686 | |
| 11687 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11688 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11689 | cert_ext = &cert_exts[i]; |
| 11690 | break; |
| 11691 | } |
| 11692 | } |
| 11693 | |
| 11694 | /* this is the default type, the field is not supported */ |
| 11695 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11696 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11697 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11698 | goto end; |
| 11699 | } |
| 11700 | |
| 11701 | /* appply the change on the duplicate */ |
William Lallemand | 52ddd99 | 2022-11-22 11:51:53 +0100 | [diff] [blame] | 11702 | if (cert_ext->load(filename, payload, data, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11703 | 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] | 11704 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11705 | goto end; |
| 11706 | } |
| 11707 | lua_pop(L, 1); |
| 11708 | } |
| 11709 | |
| 11710 | /* store the pointers on the lua stack */ |
| 11711 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11712 | lua_ckchs[0] = old_ckchs; |
| 11713 | lua_ckchs[1] = new_ckchs; |
| 11714 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11715 | *lua_ckchi = NULL; |
| 11716 | |
| 11717 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11718 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11719 | |
| 11720 | end: |
| 11721 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11722 | |
| 11723 | if (errcode & ERR_CODE) { |
| 11724 | ckch_store_free(new_ckchs); |
| 11725 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11726 | } |
| 11727 | free(err); |
| 11728 | |
| 11729 | return 0; |
| 11730 | } |
| 11731 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11732 | #else |
| 11733 | |
| 11734 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11735 | { |
| 11736 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11737 | |
| 11738 | return 0; |
| 11739 | } |
| 11740 | #endif /* ! USE_OPENSSL */ |
| 11741 | |
| 11742 | |
| 11743 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11744 | /* This function can fail with an abort() due to an Lua critical error. |
| 11745 | * We are in the initialisation process of HAProxy, this abort() is |
| 11746 | * tolerated. |
| 11747 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11748 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11749 | { |
| 11750 | struct hlua_init_function *init; |
| 11751 | const char *msg; |
| 11752 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11753 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11754 | const char *kind; |
| 11755 | const char *trace; |
| 11756 | int return_status = 1; |
| 11757 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11758 | int nres; |
| 11759 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11760 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11761 | /* disable memory limit checks if limit is not set */ |
| 11762 | if (!hlua_global_allocator.limit) |
| 11763 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11764 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11765 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11766 | if (setjmp(safe_ljmp_env) != 0) { |
| 11767 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11768 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11769 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11770 | else |
| 11771 | error = "critical error"; |
| 11772 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11773 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11774 | } else { |
| 11775 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11776 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11777 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11778 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11779 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11780 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11781 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11782 | |
| 11783 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11784 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11785 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11786 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11787 | #endif |
| 11788 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11789 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11790 | |
| 11791 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11792 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11793 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11794 | |
| 11795 | case LUA_ERRERR: |
| 11796 | kind = "message handler error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11797 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11798 | case LUA_ERRRUN: |
| 11799 | if (!kind) |
| 11800 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11801 | msg = lua_tostring(L, -1); |
| 11802 | lua_settop(L, 0); /* Empty the stack. */ |
| 11803 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11804 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11805 | if (msg) |
| 11806 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11807 | else |
| 11808 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11809 | return_status = 0; |
| 11810 | break; |
| 11811 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11812 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11813 | /* Unknown error */ |
| 11814 | kind = "Unknown error"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11815 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11816 | case LUA_YIELD: |
| 11817 | /* yield is not configured at this step, this state doesn't happen */ |
| 11818 | if (!kind) |
| 11819 | kind = "yield not allowed"; |
Willy Tarreau | 14de395 | 2022-11-14 07:08:28 +0100 | [diff] [blame] | 11820 | __fallthrough; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11821 | case LUA_ERRMEM: |
| 11822 | if (!kind) |
| 11823 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11824 | lua_settop(L, 0); |
| 11825 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11826 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11827 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11828 | return_status = 0; |
| 11829 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11830 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11831 | if (!return_status) |
| 11832 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11833 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11834 | |
| 11835 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11836 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11837 | } |
| 11838 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11839 | int hlua_post_init() |
| 11840 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11841 | int ret; |
| 11842 | int i; |
| 11843 | int errors; |
| 11844 | char *err = NULL; |
| 11845 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11846 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11847 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11848 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11849 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11850 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11851 | int saved_used_backed = global.ssl_used_backend; |
| 11852 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11853 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11854 | global.ssl_used_backend = saved_used_backed; |
| 11855 | } |
| 11856 | #endif |
| 11857 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11858 | /* Perform post init of common thread */ |
| 11859 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11860 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11861 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11862 | if (ret == 0) |
| 11863 | return 0; |
| 11864 | |
| 11865 | /* init remaining lua states and load files */ |
| 11866 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11867 | |
| 11868 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11869 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11870 | |
| 11871 | /* Init lua state */ |
| 11872 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11873 | |
| 11874 | /* Load lua files */ |
| 11875 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11876 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11877 | if (ret != 0) { |
| 11878 | ha_alert("Lua init: %s\n", err); |
| 11879 | return 0; |
| 11880 | } |
| 11881 | } |
| 11882 | } |
| 11883 | |
| 11884 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11885 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11886 | |
| 11887 | /* Execute post init for all states */ |
| 11888 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11889 | |
| 11890 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11891 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11892 | |
| 11893 | /* run post init */ |
| 11894 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11895 | if (ret == 0) |
| 11896 | return 0; |
| 11897 | } |
| 11898 | |
| 11899 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11900 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11901 | |
| 11902 | /* control functions registering. Each function must have: |
| 11903 | * - only the function_ref[0] set positive and all other to -1 |
| 11904 | * - only the function_ref[0] set to -1 and all other positive |
| 11905 | * This ensure a same reference is not used both in shared |
| 11906 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11907 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11908 | * complicated to found for the end user. |
| 11909 | */ |
| 11910 | errors = 0; |
| 11911 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11912 | ret = 0; |
| 11913 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11914 | if (fcn->function_ref[i] == -1) |
| 11915 | ret--; |
| 11916 | else |
| 11917 | ret++; |
| 11918 | } |
| 11919 | if (abs(ret) != global.nbthread) { |
| 11920 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11921 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11922 | errors++; |
| 11923 | continue; |
| 11924 | } |
| 11925 | |
| 11926 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11927 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11928 | "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] | 11929 | "exclusive.\n", fcn->name); |
| 11930 | errors++; |
| 11931 | } |
| 11932 | } |
| 11933 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11934 | /* Do the same with registered filters */ |
| 11935 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11936 | ret = 0; |
| 11937 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11938 | if (reg_flt->flt_ref[i] == -1) |
| 11939 | ret--; |
| 11940 | else |
| 11941 | ret++; |
| 11942 | } |
| 11943 | if (abs(ret) != global.nbthread) { |
| 11944 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11945 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11946 | errors++; |
| 11947 | continue; |
| 11948 | } |
| 11949 | |
| 11950 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11951 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11952 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11953 | "exclusive.\n", fcn->name); |
| 11954 | errors++; |
| 11955 | } |
| 11956 | } |
| 11957 | |
| 11958 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11959 | if (errors > 0) |
| 11960 | return 0; |
| 11961 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11962 | /* 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] | 11963 | * -1 in order to have probably a segfault if someone use it |
| 11964 | */ |
| 11965 | hlua_state_id = -1; |
| 11966 | |
| 11967 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11968 | } |
| 11969 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11970 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11971 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11972 | * 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] | 11973 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11974 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11975 | * zero. This one verifies that the limits are respected but is optimized |
| 11976 | * 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] | 11977 | * |
| 11978 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11979 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11980 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11981 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11982 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11983 | */ |
| 11984 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11985 | { |
| 11986 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11987 | size_t limit, old, new; |
| 11988 | |
| 11989 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11990 | * for accounting anymore. |
| 11991 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11992 | if (likely(~zone->limit == 0)) { |
| 11993 | if (!nsize) |
| 11994 | ha_free(&ptr); |
| 11995 | else |
| 11996 | ptr = realloc(ptr, nsize); |
| 11997 | return ptr; |
| 11998 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11999 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 12000 | if (!ptr) |
| 12001 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12002 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12003 | /* enforce strict limits across all threads */ |
| 12004 | limit = zone->limit; |
| 12005 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 12006 | do { |
| 12007 | new = old + nsize - osize; |
| 12008 | if (unlikely(nsize && limit && new > limit)) |
| 12009 | return NULL; |
| 12010 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12011 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 12012 | if (!nsize) |
| 12013 | ha_free(&ptr); |
| 12014 | else |
| 12015 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 12016 | |
| 12017 | if (unlikely(!ptr && nsize)) // failed |
| 12018 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 12019 | |
| 12020 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 12021 | return ptr; |
| 12022 | } |
| 12023 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12024 | /* 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] | 12025 | * We are in the initialisation process of HAProxy, this abort() is |
| 12026 | * tolerated. |
| 12027 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12028 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12029 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12030 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12031 | int idx; |
| 12032 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12033 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12034 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12035 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12036 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12037 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12038 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12039 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12040 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12041 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12042 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12043 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12044 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12045 | *context = NULL; |
| 12046 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12047 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12048 | * the Lua function can fail with an abort. We are in the initialisation |
| 12049 | * process of HAProxy, this abort() is tolerated. |
| 12050 | */ |
| 12051 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12052 | /* Call post initialisation function in safe environment. */ |
| 12053 | if (setjmp(safe_ljmp_env) != 0) { |
| 12054 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12055 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12056 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12057 | else |
| 12058 | error_msg = "critical error"; |
| 12059 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12060 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12061 | } else { |
| 12062 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12063 | } |
| 12064 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12065 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12066 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12067 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12068 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12069 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12070 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12071 | #endif |
| 12072 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12073 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12074 | #endif |
| 12075 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12076 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12077 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12078 | /* Apply configured prepend path */ |
| 12079 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12080 | hlua_prepend_path(L, pp->type, pp->path); |
| 12081 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12082 | /* |
| 12083 | * |
| 12084 | * Create "core" object. |
| 12085 | * |
| 12086 | */ |
| 12087 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12088 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12089 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12090 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12091 | /* set the thread id */ |
| 12092 | hlua_class_const_int(L, "thread", thread_num); |
| 12093 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12094 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12095 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12096 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12097 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12098 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12099 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12100 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12101 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12102 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12103 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12104 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12105 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12106 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12107 | hlua_class_function(L, "yield", hlua_yield); |
| 12108 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12109 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12110 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12111 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12112 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12113 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12114 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12115 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12116 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12117 | hlua_class_function(L, "log", hlua_log); |
| 12118 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12119 | hlua_class_function(L, "Info", hlua_log_info); |
| 12120 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12121 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12122 | hlua_class_function(L, "done", hlua_done); |
| 12123 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12124 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12125 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12126 | |
| 12127 | /* |
| 12128 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12129 | * Create "act" object. |
| 12130 | * |
| 12131 | */ |
| 12132 | |
| 12133 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12134 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12135 | |
| 12136 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12137 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12138 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12139 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12140 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12141 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12142 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12143 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12144 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12145 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12146 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12147 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12148 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12149 | |
| 12150 | /* |
| 12151 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12152 | * Create "Filter" object. |
| 12153 | * |
| 12154 | */ |
| 12155 | |
| 12156 | /* This table entry is the object "filter" base. */ |
| 12157 | lua_newtable(L); |
| 12158 | |
| 12159 | /* push flags and constants */ |
| 12160 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12161 | hlua_class_const_int(L, "WAIT", 0); |
| 12162 | hlua_class_const_int(L, "ERROR", -1); |
| 12163 | |
| 12164 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12165 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12166 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12167 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12168 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12169 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12170 | lua_setglobal(L, "filter"); |
| 12171 | |
| 12172 | /* |
| 12173 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12174 | * Register class Map |
| 12175 | * |
| 12176 | */ |
| 12177 | |
| 12178 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12179 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12180 | |
| 12181 | /* register pattern types. */ |
| 12182 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12183 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12184 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12185 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12186 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12187 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12188 | |
| 12189 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12190 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12191 | |
| 12192 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12193 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12194 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12195 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12196 | lua_pushstring(L, "__index"); |
| 12197 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12198 | |
| 12199 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12200 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12201 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12202 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12203 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12204 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12205 | /* Register previous table in the registry with reference and named entry. |
| 12206 | * The function hlua_register_metatable() pops the stack, so we |
| 12207 | * previously create a copy of the table. |
| 12208 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12209 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12210 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12211 | |
| 12212 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12213 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12214 | |
| 12215 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12216 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12217 | |
| 12218 | /* |
| 12219 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12220 | * Register "CertCache" class |
| 12221 | * |
| 12222 | */ |
| 12223 | |
| 12224 | /* Create and fill the metatable. */ |
| 12225 | lua_newtable(L); |
| 12226 | /* Register */ |
| 12227 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12228 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12229 | |
| 12230 | /* |
| 12231 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12232 | * Register class Channel |
| 12233 | * |
| 12234 | */ |
| 12235 | |
| 12236 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12237 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12238 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12239 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12240 | lua_pushstring(L, "__index"); |
| 12241 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12242 | |
| 12243 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12244 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12245 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12246 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12247 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12248 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12249 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12250 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12251 | hlua_class_function(L, "send", hlua_channel_send); |
| 12252 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12253 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12254 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12255 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12256 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12257 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12258 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12259 | /* Deprecated API */ |
| 12260 | hlua_class_function(L, "get", hlua_channel_get); |
| 12261 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12262 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12263 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12264 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12265 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12266 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12267 | |
| 12268 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12269 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12270 | |
| 12271 | /* |
| 12272 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12273 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12274 | * |
| 12275 | */ |
| 12276 | |
| 12277 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12278 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12279 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12280 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12281 | lua_pushstring(L, "__index"); |
| 12282 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12283 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12284 | /* Browse existing fetches and create the associated |
| 12285 | * object method. |
| 12286 | */ |
| 12287 | sf = NULL; |
| 12288 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12289 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12290 | * by an underscore. |
| 12291 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12292 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12293 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12294 | if (*p == '.' || *p == '-' || *p == '+') |
| 12295 | *p = '_'; |
| 12296 | |
| 12297 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12298 | lua_pushstring(L, trash.area); |
| 12299 | lua_pushlightuserdata(L, sf); |
| 12300 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12301 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12302 | } |
| 12303 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12304 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12305 | |
| 12306 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12307 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12308 | |
| 12309 | /* |
| 12310 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12311 | * Register class Converters |
| 12312 | * |
| 12313 | */ |
| 12314 | |
| 12315 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12316 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12317 | |
| 12318 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12319 | lua_pushstring(L, "__index"); |
| 12320 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12321 | |
| 12322 | /* Browse existing converters and create the associated |
| 12323 | * object method. |
| 12324 | */ |
| 12325 | sc = NULL; |
| 12326 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12327 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12328 | * by an underscore. |
| 12329 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12330 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12331 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12332 | if (*p == '.' || *p == '-' || *p == '+') |
| 12333 | *p = '_'; |
| 12334 | |
| 12335 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12336 | lua_pushstring(L, trash.area); |
| 12337 | lua_pushlightuserdata(L, sc); |
| 12338 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12339 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12340 | } |
| 12341 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12342 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12343 | |
| 12344 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12345 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12346 | |
| 12347 | /* |
| 12348 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12349 | * Register class HTTP |
| 12350 | * |
| 12351 | */ |
| 12352 | |
| 12353 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12354 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12355 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12356 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12357 | lua_pushstring(L, "__index"); |
| 12358 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12359 | |
| 12360 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12361 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12362 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12363 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12364 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12365 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12366 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12367 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12368 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12369 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12370 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12371 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12372 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12373 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12374 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12375 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12376 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12377 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12378 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12379 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12380 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12381 | |
| 12382 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12383 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12384 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12385 | /* |
| 12386 | * |
| 12387 | * Register class HTTPMessage |
| 12388 | * |
| 12389 | */ |
| 12390 | |
| 12391 | /* Create and fill the metatable. */ |
| 12392 | lua_newtable(L); |
| 12393 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12394 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12395 | lua_pushstring(L, "__index"); |
| 12396 | lua_newtable(L); |
| 12397 | |
| 12398 | /* Register Lua functions. */ |
| 12399 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12400 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12401 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12402 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12403 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12404 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12405 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12406 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12407 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12408 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12409 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12410 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12411 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12412 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12413 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12414 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12415 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12416 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12417 | |
| 12418 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12419 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12420 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12421 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12422 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12423 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12424 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12425 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12426 | |
| 12427 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12428 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12429 | |
| 12430 | lua_rawset(L, -3); |
| 12431 | |
| 12432 | /* Register previous table in the registry with reference and named entry. */ |
| 12433 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12434 | |
| 12435 | /* |
| 12436 | * |
| 12437 | * Register class HTTPClient |
| 12438 | * |
| 12439 | */ |
| 12440 | |
| 12441 | /* Create and fill the metatable. */ |
| 12442 | lua_newtable(L); |
| 12443 | lua_pushstring(L, "__index"); |
| 12444 | lua_newtable(L); |
| 12445 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12446 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12447 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12448 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12449 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12450 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12451 | /* Register the garbage collector entry. */ |
| 12452 | lua_pushstring(L, "__gc"); |
| 12453 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12454 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12455 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12456 | |
| 12457 | |
| 12458 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12459 | /* |
| 12460 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12461 | * Register class AppletTCP |
| 12462 | * |
| 12463 | */ |
| 12464 | |
| 12465 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12466 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12467 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12468 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12469 | lua_pushstring(L, "__index"); |
| 12470 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12471 | |
| 12472 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12473 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12474 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12475 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12476 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12477 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12478 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12479 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12480 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12481 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12482 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12483 | |
| 12484 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12485 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12486 | |
| 12487 | /* |
| 12488 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12489 | * Register class AppletHTTP |
| 12490 | * |
| 12491 | */ |
| 12492 | |
| 12493 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12494 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12495 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12496 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12497 | lua_pushstring(L, "__index"); |
| 12498 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12499 | |
| 12500 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12501 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12502 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12503 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12504 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12505 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12506 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12507 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12508 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12509 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12510 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12511 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12512 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12513 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12514 | |
| 12515 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12516 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12517 | |
| 12518 | /* |
| 12519 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12520 | * Register class TXN |
| 12521 | * |
| 12522 | */ |
| 12523 | |
| 12524 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12525 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12526 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12527 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12528 | lua_pushstring(L, "__index"); |
| 12529 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12530 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12531 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12532 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12533 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12534 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12535 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12536 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12537 | hlua_class_function(L, "done", hlua_txn_done); |
| 12538 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12539 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12540 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12541 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12542 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12543 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12544 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12545 | hlua_class_function(L, "log", hlua_txn_log); |
| 12546 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12547 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12548 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12549 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12550 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12551 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12552 | |
| 12553 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12554 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12555 | |
| 12556 | /* |
| 12557 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12558 | * Register class reply |
| 12559 | * |
| 12560 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12561 | lua_newtable(L); |
| 12562 | lua_pushstring(L, "__index"); |
| 12563 | lua_newtable(L); |
| 12564 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12565 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12566 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12567 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12568 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12569 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12570 | |
| 12571 | |
| 12572 | /* |
| 12573 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12574 | * Register class Socket |
| 12575 | * |
| 12576 | */ |
| 12577 | |
| 12578 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12579 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12580 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12581 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12582 | lua_pushstring(L, "__index"); |
| 12583 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12584 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12585 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12586 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12587 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12588 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12589 | hlua_class_function(L, "send", hlua_socket_send); |
| 12590 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12591 | hlua_class_function(L, "close", hlua_socket_close); |
| 12592 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12593 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12594 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12595 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12596 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12597 | 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] | 12598 | |
| 12599 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12600 | lua_pushstring(L, "__gc"); |
| 12601 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12602 | 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] | 12603 | |
| 12604 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12605 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12606 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12607 | lua_atpanic(L, hlua_panic_safe); |
| 12608 | |
| 12609 | return L; |
| 12610 | } |
| 12611 | |
| 12612 | void hlua_init(void) { |
| 12613 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12614 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12615 | #ifdef USE_OPENSSL |
| 12616 | struct srv_kw *kw; |
| 12617 | int tmp_error; |
| 12618 | char *error; |
| 12619 | char *args[] = { /* SSL client configuration. */ |
| 12620 | "ssl", |
| 12621 | "verify", |
| 12622 | "none", |
| 12623 | NULL |
| 12624 | }; |
| 12625 | #endif |
| 12626 | |
| 12627 | /* Init post init function list head */ |
| 12628 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12629 | LIST_INIT(&hlua_init_functions[i]); |
| 12630 | |
| 12631 | /* Init state for common/shared lua parts */ |
| 12632 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12633 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12634 | hlua_states[0] = hlua_init_state(0); |
| 12635 | |
| 12636 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12637 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12638 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12639 | hlua_states[1] = hlua_init_state(1); |
| 12640 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12641 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12642 | 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] | 12643 | if (!socket_proxy) { |
| 12644 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12645 | exit(1); |
| 12646 | } |
| 12647 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12648 | |
| 12649 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12650 | socket_tcp = new_server(socket_proxy); |
| 12651 | if (!socket_tcp) { |
| 12652 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12653 | exit(1); |
| 12654 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12655 | |
| 12656 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12657 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12658 | socket_ssl = new_server(socket_proxy); |
| 12659 | if (!socket_ssl) { |
| 12660 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12661 | exit(1); |
| 12662 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12663 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12664 | socket_ssl->use_ssl = 1; |
| 12665 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12666 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12667 | for (i = 0; args[i] != NULL; i++) { |
| 12668 | 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] | 12669 | /* |
| 12670 | * |
| 12671 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12672 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12673 | * features like client certificates and ssl_verify. |
| 12674 | * |
| 12675 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12676 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12677 | if (tmp_error != 0) { |
| 12678 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12679 | abort(); /* This must be never arrives because the command line |
| 12680 | not editable by the user. */ |
| 12681 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12682 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12683 | } |
| 12684 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12685 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12686 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12687 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12688 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12689 | static void hlua_deinit() |
| 12690 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12691 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12692 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12693 | |
| 12694 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12695 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12696 | |
| 12697 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12698 | if (hlua_states[thr]) |
| 12699 | lua_close(hlua_states[thr]); |
| 12700 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12701 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12702 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12703 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12704 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12705 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12706 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12707 | |
| 12708 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12709 | } |
| 12710 | |
| 12711 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12712 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12713 | static void hlua_register_build_options(void) |
| 12714 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12715 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12716 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12717 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12718 | hap_register_build_opts(ptr, 1); |
| 12719 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12720 | |
| 12721 | INITCALL0(STG_REGISTER, hlua_register_build_options); |