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: |
| 863 | argp[idx].data.prx = p; |
| 864 | argp[idx].type = ARGT_TAB; |
| 865 | argp[idx+1].type = ARGT_STOP; |
| 866 | break; |
| 867 | |
| 868 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 869 | msg = "Mandatory argument expected"; |
| 870 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 871 | break; |
| 872 | } |
| 873 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 874 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 877 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 878 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 879 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 880 | msg = "Last argument expected"; |
| 881 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | } |
| 883 | |
| 884 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 885 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 886 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 887 | } |
| 888 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 889 | /* Convert some argument types. All string in argp[] are for not |
| 890 | * duplicated yet. |
| 891 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 892 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 893 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 894 | if (argp[idx].type != ARGT_SINT) { |
| 895 | msg = "integer expected"; |
| 896 | goto error; |
| 897 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 898 | argp[idx].type = ARGT_SINT; |
| 899 | break; |
| 900 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 901 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 902 | if (argp[idx].type != ARGT_SINT) { |
| 903 | msg = "integer expected"; |
| 904 | goto error; |
| 905 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 906 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 907 | break; |
| 908 | |
| 909 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 910 | if (argp[idx].type != ARGT_SINT) { |
| 911 | msg = "integer expected"; |
| 912 | goto error; |
| 913 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 914 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 915 | break; |
| 916 | |
| 917 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 918 | if (argp[idx].type != ARGT_STR) { |
| 919 | msg = "string expected"; |
| 920 | goto error; |
| 921 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 922 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 923 | if (!argp[idx].data.prx) { |
| 924 | msg = "frontend doesn't exist"; |
| 925 | goto error; |
| 926 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 927 | argp[idx].type = ARGT_FE; |
| 928 | break; |
| 929 | |
| 930 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 931 | if (argp[idx].type != ARGT_STR) { |
| 932 | msg = "string expected"; |
| 933 | goto error; |
| 934 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 935 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 936 | if (!argp[idx].data.prx) { |
| 937 | msg = "backend doesn't exist"; |
| 938 | goto error; |
| 939 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 940 | argp[idx].type = ARGT_BE; |
| 941 | break; |
| 942 | |
| 943 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 944 | if (argp[idx].type != ARGT_STR) { |
| 945 | msg = "string expected"; |
| 946 | goto error; |
| 947 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 948 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 949 | if (!argp[idx].data.t) { |
| 950 | msg = "table doesn't exist"; |
| 951 | goto error; |
| 952 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 953 | argp[idx].type = ARGT_TAB; |
| 954 | break; |
| 955 | |
| 956 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 957 | if (argp[idx].type != ARGT_STR) { |
| 958 | msg = "string expected"; |
| 959 | goto error; |
| 960 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 961 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 962 | if (sname) { |
| 963 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 964 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 965 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 966 | if (!px) { |
| 967 | msg = "backend doesn't exist"; |
| 968 | goto error; |
| 969 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 970 | } |
| 971 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 972 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 973 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 974 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 975 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 976 | if (!argp[idx].data.srv) { |
| 977 | msg = "server doesn't exist"; |
| 978 | goto error; |
| 979 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 980 | argp[idx].type = ARGT_SRV; |
| 981 | break; |
| 982 | |
| 983 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 984 | if (argp[idx].type != ARGT_STR) { |
| 985 | msg = "string expected"; |
| 986 | goto error; |
| 987 | } |
| 988 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 989 | msg = "invalid IPv4 address"; |
| 990 | goto error; |
| 991 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 992 | argp[idx].type = ARGT_IPV4; |
| 993 | break; |
| 994 | |
| 995 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 996 | if (argp[idx].type == ARGT_SINT) |
| 997 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 998 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 999 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1000 | msg = "invalid IPv4 mask"; |
| 1001 | goto error; |
| 1002 | } |
| 1003 | } |
| 1004 | else { |
| 1005 | msg = "integer or string expected"; |
| 1006 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1007 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1008 | argp[idx].type = ARGT_MSK4; |
| 1009 | break; |
| 1010 | |
| 1011 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1012 | if (argp[idx].type != ARGT_STR) { |
| 1013 | msg = "string expected"; |
| 1014 | goto error; |
| 1015 | } |
| 1016 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1017 | msg = "invalid IPv6 address"; |
| 1018 | goto error; |
| 1019 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1020 | argp[idx].type = ARGT_IPV6; |
| 1021 | break; |
| 1022 | |
| 1023 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1024 | if (argp[idx].type == ARGT_SINT) |
| 1025 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1026 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1027 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1028 | msg = "invalid IPv6 mask"; |
| 1029 | goto error; |
| 1030 | } |
| 1031 | } |
| 1032 | else { |
| 1033 | msg = "integer or string expected"; |
| 1034 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1035 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1036 | argp[idx].type = ARGT_MSK6; |
| 1037 | break; |
| 1038 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1039 | case ARGT_REG: |
| 1040 | if (argp[idx].type != ARGT_STR) { |
| 1041 | msg = "string expected"; |
| 1042 | goto error; |
| 1043 | } |
| 1044 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1045 | if (!reg) { |
| 1046 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1047 | argp[idx].data.str.area, err); |
| 1048 | free(err); |
| 1049 | goto error; |
| 1050 | } |
| 1051 | argp[idx].type = ARGT_REG; |
| 1052 | argp[idx].data.reg = reg; |
| 1053 | break; |
| 1054 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1055 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1056 | if (argp[idx].type != ARGT_STR) { |
| 1057 | msg = "string expected"; |
| 1058 | goto error; |
| 1059 | } |
| 1060 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1061 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1062 | ul = p->uri_auth->userlist; |
| 1063 | else |
| 1064 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1065 | |
| 1066 | if (!ul) { |
| 1067 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1068 | goto error; |
| 1069 | } |
| 1070 | argp[idx].type = ARGT_USR; |
| 1071 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1072 | break; |
| 1073 | |
| 1074 | case ARGT_STR: |
| 1075 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1076 | msg = "unable to duplicate string arg"; |
| 1077 | goto error; |
| 1078 | } |
| 1079 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1080 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1081 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1082 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1083 | msg = "type not yet supported"; |
| 1084 | goto error; |
| 1085 | break; |
| 1086 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1087 | } |
| 1088 | |
| 1089 | /* Check for type of argument. */ |
| 1090 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1091 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1092 | arg_type_names[(mask & ARGT_MASK)], |
| 1093 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1094 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1095 | } |
| 1096 | |
| 1097 | /* Next argument. */ |
| 1098 | mask >>= ARGT_BITS; |
| 1099 | idx++; |
| 1100 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1101 | return 0; |
| 1102 | |
| 1103 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1104 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1105 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1106 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1107 | } |
| 1108 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1109 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1110 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1111 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1112 | * |
| 1113 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1115 | */ |
| 1116 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1117 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1118 | struct hlua **hlua = lua_getextraspace(L); |
| 1119 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1120 | } |
| 1121 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1122 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1123 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1124 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1127 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1128 | * currently is executing from within a Lua function. One line per entry will |
| 1129 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1130 | * Lua function is not detected, NULL is returned. |
| 1131 | */ |
| 1132 | const char *hlua_show_current_location(const char *pfx) |
| 1133 | { |
| 1134 | lua_State *L; |
| 1135 | lua_Debug ar; |
| 1136 | |
| 1137 | /* global or per-thread stack initializing ? */ |
| 1138 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1139 | return hlua_traceback(L, pfx); |
| 1140 | |
| 1141 | /* per-thread stack running ? */ |
| 1142 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1143 | return hlua_traceback(L, pfx); |
| 1144 | |
| 1145 | /* global stack running ? */ |
| 1146 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1147 | return hlua_traceback(L, pfx); |
| 1148 | |
| 1149 | return NULL; |
| 1150 | } |
| 1151 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1152 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1153 | * and on the default syslog server. |
| 1154 | */ |
| 1155 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1156 | { |
| 1157 | struct tm tm; |
| 1158 | char *p; |
| 1159 | |
| 1160 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1161 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1162 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1163 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1164 | /* Break the message if exceed the buffer size. */ |
| 1165 | *(p-4) = ' '; |
| 1166 | *(p-3) = '.'; |
| 1167 | *(p-2) = '.'; |
| 1168 | *(p-1) = '.'; |
| 1169 | break; |
| 1170 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1171 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1172 | *p = *msg; |
| 1173 | else |
| 1174 | *p = '.'; |
| 1175 | } |
| 1176 | *p = '\0'; |
| 1177 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1178 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1179 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1180 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1181 | return; |
| 1182 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1183 | get_localtime(date.tv_sec, &tm); |
| 1184 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1185 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1186 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1187 | fflush(stderr); |
| 1188 | } |
| 1189 | } |
| 1190 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1191 | /* This function just ensure that the yield will be always |
| 1192 | * returned with a timeout and permit to set some flags |
| 1193 | */ |
| 1194 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1195 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1197 | struct hlua *hlua; |
| 1198 | |
| 1199 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1200 | hlua = hlua_gethlua(L); |
| 1201 | if (!hlua) { |
| 1202 | return; |
| 1203 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1204 | |
| 1205 | /* Set the wake timeout. If timeout is required, we set |
| 1206 | * the expiration time. |
| 1207 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1208 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1209 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1210 | hlua->flags |= flags; |
| 1211 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1212 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1213 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1214 | } |
| 1215 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1216 | /* This function initialises the Lua environment stored in the stream. |
| 1217 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1219 | * |
| 1220 | * This function is particular. it initialises a new Lua thread. If the |
| 1221 | * initialisation fails (example: out of memory error), the lua function |
| 1222 | * throws an error (longjmp). |
| 1223 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1224 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1225 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1226 | * threads appear, the safe environment set a lock to ensure only one |
| 1227 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1228 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1229 | * |
| 1230 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1231 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1232 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1233 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1234 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1235 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1236 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1237 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1238 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1239 | { |
| 1240 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1241 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1242 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1243 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1244 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1245 | LIST_INIT(&lua->com); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1246 | if (!already_safe) { |
| 1247 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1248 | lua->Tref = LUA_REFNIL; |
| 1249 | return 0; |
| 1250 | } |
| 1251 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1252 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1253 | if (!lua->T) { |
| 1254 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1255 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1256 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1257 | return 0; |
| 1258 | } |
| 1259 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1260 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1261 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1262 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1263 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1264 | return 1; |
| 1265 | } |
| 1266 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1267 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1268 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1269 | * is not freed. |
| 1270 | */ |
| 1271 | void hlua_ctx_destroy(struct hlua *lua) |
| 1272 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1273 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1274 | return; |
| 1275 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1276 | if (!lua->T) |
| 1277 | goto end; |
| 1278 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1279 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1280 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1281 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1282 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1283 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1284 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1285 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1286 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1287 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1288 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1289 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1290 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1291 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1292 | * without error, we run the GC on the thread pointer. Its freed all |
| 1293 | * the unused memory. |
| 1294 | * If the thread is finnish with an error or is currently yielded, |
| 1295 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1296 | * so e run the GC on the main thread. |
| 1297 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1298 | * the garbage collection. |
| 1299 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1300 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1301 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1302 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1303 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1304 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1305 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1306 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1307 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1308 | |
| 1309 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1310 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1311 | } |
| 1312 | |
| 1313 | /* This function is used to restore the Lua context when a coroutine |
| 1314 | * fails. This function copy the common memory between old coroutine |
| 1315 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1316 | * replaced by the new coroutine. |
| 1317 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1318 | * as string error message and it is copied in the new stack. |
| 1319 | */ |
| 1320 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1321 | { |
| 1322 | lua_State *T; |
| 1323 | int new_ref; |
| 1324 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1325 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1326 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1327 | if (!T) |
| 1328 | return 0; |
| 1329 | |
| 1330 | /* Copy last error message. */ |
| 1331 | if (keep_msg) |
| 1332 | lua_xmove(lua->T, T, 1); |
| 1333 | |
| 1334 | /* Copy data between the coroutines. */ |
| 1335 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1336 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1337 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1338 | |
| 1339 | /* Destroy old data. */ |
| 1340 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1341 | |
| 1342 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1343 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1344 | |
| 1345 | /* Fill the struct with the new coroutine values. */ |
| 1346 | lua->Mref = new_ref; |
| 1347 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1348 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | |
| 1350 | /* Set context. */ |
| 1351 | hlua_sethlua(lua); |
| 1352 | |
| 1353 | return 1; |
| 1354 | } |
| 1355 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1356 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1357 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1358 | struct hlua *hlua; |
| 1359 | |
| 1360 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1361 | hlua = hlua_gethlua(L); |
| 1362 | if (!hlua) |
| 1363 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1364 | |
| 1365 | /* Lua cannot yield when its returning from a function, |
| 1366 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1367 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1368 | */ |
| 1369 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1370 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1371 | return; |
| 1372 | } |
| 1373 | |
| 1374 | /* restore the interrupt condition. */ |
| 1375 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1376 | |
| 1377 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1378 | * If the state is not yieldable, trying yield causes an error. |
| 1379 | */ |
| 1380 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1381 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1382 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1383 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1384 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1385 | hlua->run_time += now_ms - hlua->start_time; |
| 1386 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1387 | lua_pushfstring(L, "execution timeout"); |
| 1388 | WILL_LJMP(lua_error(L)); |
| 1389 | } |
| 1390 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1391 | /* Update the start time. */ |
| 1392 | hlua->start_time = now_ms; |
| 1393 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1394 | /* Try to interrupt the process at the end of the current |
| 1395 | * unyieldable function. |
| 1396 | */ |
| 1397 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1398 | } |
| 1399 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1400 | /* This function start or resumes the Lua stack execution. If the flag |
| 1401 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1402 | * The function return an error. |
| 1403 | * |
| 1404 | * The function can returns 4 values: |
| 1405 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1406 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1407 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1408 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1409 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1410 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1411 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1412 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1413 | * LUA code. |
| 1414 | */ |
| 1415 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1416 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1417 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1418 | int nres; |
| 1419 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1420 | int ret; |
| 1421 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1422 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1424 | /* Initialise run time counter. */ |
| 1425 | if (!HLUA_IS_RUNNING(lua)) |
| 1426 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1427 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1428 | /* Lock the whole Lua execution. This lock must be before the |
| 1429 | * label "resume_execution". |
| 1430 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1431 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1432 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1433 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1434 | resume_execution: |
| 1435 | |
| 1436 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1437 | * instructions. it is used for preventing infinite loops. |
| 1438 | */ |
| 1439 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1440 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1441 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1442 | HLUA_SET_RUN(lua); |
| 1443 | HLUA_CLR_CTRLYIELD(lua); |
| 1444 | HLUA_CLR_WAKERESWR(lua); |
| 1445 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1446 | HLUA_CLR_NOYIELD(lua); |
| 1447 | if (!yield_allowed) |
| 1448 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1449 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1450 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1451 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1452 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1453 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1454 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1455 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1456 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1457 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1458 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1459 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1460 | switch (ret) { |
| 1461 | |
| 1462 | case LUA_OK: |
| 1463 | ret = HLUA_E_OK; |
| 1464 | break; |
| 1465 | |
| 1466 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1467 | /* Check if the execution timeout is expired. It it is the case, we |
| 1468 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1469 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1470 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1471 | lua->run_time += now_ms - lua->start_time; |
| 1472 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1473 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1474 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1475 | break; |
| 1476 | } |
| 1477 | /* Process the forced yield. if the general yield is not allowed or |
| 1478 | * if no task were associated this the current Lua execution |
| 1479 | * coroutine, we resume the execution. Else we want to return in the |
| 1480 | * scheduler and we want to be waked up again, to continue the |
| 1481 | * current Lua execution. So we schedule our own task. |
| 1482 | */ |
| 1483 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1484 | if (!yield_allowed || !lua->task) |
| 1485 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1486 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1487 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1488 | if (!yield_allowed) { |
| 1489 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1490 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1491 | break; |
| 1492 | } |
| 1493 | ret = HLUA_E_AGAIN; |
| 1494 | break; |
| 1495 | |
| 1496 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1497 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1498 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1499 | * because the errors ares the only one mean to return immediately |
| 1500 | * from and lua execution. |
| 1501 | */ |
| 1502 | if (lua->flags & HLUA_EXIT) { |
| 1503 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1504 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1505 | break; |
| 1506 | } |
| 1507 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1508 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1509 | if (!lua_checkstack(lua->T, 1)) { |
| 1510 | ret = HLUA_E_ERR; |
| 1511 | break; |
| 1512 | } |
| 1513 | msg = lua_tostring(lua->T, -1); |
| 1514 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1515 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1516 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1517 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1518 | lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1519 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1520 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1521 | ret = HLUA_E_ERRMSG; |
| 1522 | break; |
| 1523 | |
| 1524 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1525 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1526 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1527 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1528 | break; |
| 1529 | |
| 1530 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1531 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1532 | if (!lua_checkstack(lua->T, 1)) { |
| 1533 | ret = HLUA_E_ERR; |
| 1534 | break; |
| 1535 | } |
| 1536 | msg = lua_tostring(lua->T, -1); |
| 1537 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1538 | lua_pop(lua->T, 1); |
| 1539 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1540 | lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1541 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1542 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1543 | ret = HLUA_E_ERRMSG; |
| 1544 | break; |
| 1545 | |
| 1546 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1547 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1548 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1549 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1550 | break; |
| 1551 | } |
| 1552 | |
| 1553 | switch (ret) { |
| 1554 | case HLUA_E_AGAIN: |
| 1555 | break; |
| 1556 | |
| 1557 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1558 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1559 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1560 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1561 | break; |
| 1562 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1563 | case HLUA_E_ETMOUT: |
| 1564 | case HLUA_E_NOMEM: |
| 1565 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1566 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1567 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1568 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1569 | hlua_ctx_renew(lua, 0); |
| 1570 | break; |
| 1571 | |
| 1572 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1573 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1574 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1575 | break; |
| 1576 | } |
| 1577 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1578 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1579 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1580 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1581 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1582 | return ret; |
| 1583 | } |
| 1584 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1585 | /* This function exit the current code. */ |
| 1586 | __LJMP static int hlua_done(lua_State *L) |
| 1587 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1588 | struct hlua *hlua; |
| 1589 | |
| 1590 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1591 | hlua = hlua_gethlua(L); |
| 1592 | if (!hlua) |
| 1593 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1594 | |
| 1595 | hlua->flags |= HLUA_EXIT; |
| 1596 | WILL_LJMP(lua_error(L)); |
| 1597 | |
| 1598 | return 0; |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1601 | /* This function is an LUA binding. It provides a function |
| 1602 | * for deleting ACL from a referenced ACL file. |
| 1603 | */ |
| 1604 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1605 | { |
| 1606 | const char *name; |
| 1607 | const char *key; |
| 1608 | struct pat_ref *ref; |
| 1609 | |
| 1610 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1611 | |
| 1612 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1613 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1614 | |
| 1615 | ref = pat_ref_lookup(name); |
| 1616 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1617 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1618 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1619 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1620 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1621 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1622 | return 0; |
| 1623 | } |
| 1624 | |
| 1625 | /* This function is an LUA binding. It provides a function |
| 1626 | * for deleting map entry from a referenced map file. |
| 1627 | */ |
| 1628 | static int hlua_del_map(lua_State *L) |
| 1629 | { |
| 1630 | const char *name; |
| 1631 | const char *key; |
| 1632 | struct pat_ref *ref; |
| 1633 | |
| 1634 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1635 | |
| 1636 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1637 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1638 | |
| 1639 | ref = pat_ref_lookup(name); |
| 1640 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1641 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1642 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1643 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1644 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1645 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1646 | return 0; |
| 1647 | } |
| 1648 | |
| 1649 | /* This function is an LUA binding. It provides a function |
| 1650 | * for adding ACL pattern from a referenced ACL file. |
| 1651 | */ |
| 1652 | static int hlua_add_acl(lua_State *L) |
| 1653 | { |
| 1654 | const char *name; |
| 1655 | const char *key; |
| 1656 | struct pat_ref *ref; |
| 1657 | |
| 1658 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1659 | |
| 1660 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1661 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1662 | |
| 1663 | ref = pat_ref_lookup(name); |
| 1664 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1665 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1666 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1667 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1668 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1669 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1670 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | return 0; |
| 1672 | } |
| 1673 | |
| 1674 | /* This function is an LUA binding. It provides a function |
| 1675 | * for setting map pattern and sample from a referenced map |
| 1676 | * file. |
| 1677 | */ |
| 1678 | static int hlua_set_map(lua_State *L) |
| 1679 | { |
| 1680 | const char *name; |
| 1681 | const char *key; |
| 1682 | const char *value; |
| 1683 | struct pat_ref *ref; |
| 1684 | |
| 1685 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1686 | |
| 1687 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1688 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1689 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1690 | |
| 1691 | ref = pat_ref_lookup(name); |
| 1692 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1693 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1694 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1695 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1696 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1697 | pat_ref_set(ref, key, value, NULL); |
| 1698 | else |
| 1699 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1700 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1701 | return 0; |
| 1702 | } |
| 1703 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1704 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1705 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1706 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1707 | * the name of the object (_G[<name>] = <metable> ). |
| 1708 | * |
| 1709 | * A metable is a table that modify the standard behavior of a standard |
| 1710 | * access to the associated data. The entries of this new metatable are |
| 1711 | * defined as is: |
| 1712 | * |
| 1713 | * http://lua-users.org/wiki/MetatableEvents |
| 1714 | * |
| 1715 | * __index |
| 1716 | * |
| 1717 | * we access an absent field in a table, the result is nil. This is |
| 1718 | * true, but it is not the whole truth. Actually, such access triggers |
| 1719 | * the interpreter to look for an __index metamethod: If there is no |
| 1720 | * such method, as usually happens, then the access results in nil; |
| 1721 | * otherwise, the metamethod will provide the result. |
| 1722 | * |
| 1723 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1724 | * the key does not appear in the table, but the metatable has an __index |
| 1725 | * property: |
| 1726 | * |
| 1727 | * - if the value is a function, the function is called, passing in the |
| 1728 | * table and the key; the return value of that function is returned as |
| 1729 | * the result. |
| 1730 | * |
| 1731 | * - if the value is another table, the value of the key in that table is |
| 1732 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1733 | * table's metatable has an __index property, then it continues on up) |
| 1734 | * |
| 1735 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1736 | * |
| 1737 | * http://www.lua.org/pil/13.4.1.html |
| 1738 | * |
| 1739 | * __newindex |
| 1740 | * |
| 1741 | * Like __index, but control property assignment. |
| 1742 | * |
| 1743 | * __mode - Control weak references. A string value with one or both |
| 1744 | * of the characters 'k' and 'v' which specifies that the the |
| 1745 | * keys and/or values in the table are weak references. |
| 1746 | * |
| 1747 | * __call - Treat a table like a function. When a table is followed by |
| 1748 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1749 | * a __call key pointing to a function, that function is invoked |
| 1750 | * (passing any specified arguments) and the return value is |
| 1751 | * returned. |
| 1752 | * |
| 1753 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1754 | * called, if the metatable for myTable has a __metatable |
| 1755 | * key, the value of that key is returned instead of the |
| 1756 | * actual metatable. |
| 1757 | * |
| 1758 | * __tostring - Control string representation. When the builtin |
| 1759 | * "tostring( myTable )" function is called, if the metatable |
| 1760 | * for myTable has a __tostring property set to a function, |
| 1761 | * that function is invoked (passing myTable to it) and the |
| 1762 | * return value is used as the string representation. |
| 1763 | * |
| 1764 | * __len - Control table length. When the table length is requested using |
| 1765 | * the length operator ( '#' ), if the metatable for myTable has |
| 1766 | * a __len key pointing to a function, that function is invoked |
| 1767 | * (passing myTable to it) and the return value used as the value |
| 1768 | * of "#myTable". |
| 1769 | * |
| 1770 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1771 | * collected, if the metatable has a __gc field pointing to a |
| 1772 | * function, that function is first invoked, passing the userdata |
| 1773 | * to it. The __gc metamethod is not called for tables. |
| 1774 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1775 | * |
| 1776 | * Special metamethods for redefining standard operators: |
| 1777 | * http://www.lua.org/pil/13.1.html |
| 1778 | * |
| 1779 | * __add "+" |
| 1780 | * __sub "-" |
| 1781 | * __mul "*" |
| 1782 | * __div "/" |
| 1783 | * __unm "!" |
| 1784 | * __pow "^" |
| 1785 | * __concat ".." |
| 1786 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1787 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1788 | * http://www.lua.org/pil/13.2.html |
| 1789 | * |
| 1790 | * __eq "==" |
| 1791 | * __lt "<" |
| 1792 | * __le "<=" |
| 1793 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | |
| 1795 | /* |
| 1796 | * |
| 1797 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1798 | * Class Map |
| 1799 | * |
| 1800 | * |
| 1801 | */ |
| 1802 | |
| 1803 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1804 | * a class session, otherwise it throws an error. |
| 1805 | */ |
| 1806 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1807 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1808 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1809 | } |
| 1810 | |
| 1811 | /* This function is the map constructor. It don't need |
| 1812 | * the class Map object. It creates and return a new Map |
| 1813 | * object. It must be called only during "body" or "init" |
| 1814 | * context because it process some filesystem accesses. |
| 1815 | */ |
| 1816 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1817 | { |
| 1818 | const char *fn; |
| 1819 | int match = PAT_MATCH_STR; |
| 1820 | struct sample_conv conv; |
| 1821 | const char *file = ""; |
| 1822 | int line = 0; |
| 1823 | lua_Debug ar; |
| 1824 | char *err = NULL; |
| 1825 | struct arg args[2]; |
| 1826 | |
| 1827 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1828 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1829 | |
| 1830 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1831 | |
| 1832 | if (lua_gettop(L) >= 2) { |
| 1833 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1834 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1835 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1836 | } |
| 1837 | |
| 1838 | /* Get Lua filename and line number. */ |
| 1839 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1840 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1841 | if (ar.currentline > 0) { /* is there info? */ |
| 1842 | file = ar.short_src; |
| 1843 | line = ar.currentline; |
| 1844 | } |
| 1845 | } |
| 1846 | |
| 1847 | /* fill fake sample_conv struct. */ |
| 1848 | conv.kw = ""; /* unused. */ |
| 1849 | conv.process = NULL; /* unused. */ |
| 1850 | conv.arg_mask = 0; /* unused. */ |
| 1851 | conv.val_args = NULL; /* unused. */ |
| 1852 | conv.out_type = SMP_T_STR; |
| 1853 | conv.private = (void *)(long)match; |
| 1854 | switch (match) { |
| 1855 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1856 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1857 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1858 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1859 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1860 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1861 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1862 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1863 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1864 | default: |
| 1865 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1866 | } |
| 1867 | |
| 1868 | /* fill fake args. */ |
| 1869 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1870 | args[0].data.str.area = strdup(fn); |
| 1871 | args[0].data.str.data = strlen(fn); |
| 1872 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1873 | args[1].type = ARGT_STOP; |
| 1874 | |
| 1875 | /* load the map. */ |
| 1876 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1877 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1878 | * free the err variable. |
| 1879 | */ |
| 1880 | luaL_where(L, 1); |
| 1881 | lua_pushfstring(L, "'new': %s.", err); |
| 1882 | lua_concat(L, 2); |
| 1883 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1884 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1885 | WILL_LJMP(lua_error(L)); |
| 1886 | } |
| 1887 | |
| 1888 | /* create the lua object. */ |
| 1889 | lua_newtable(L); |
| 1890 | lua_pushlightuserdata(L, args[0].data.map); |
| 1891 | lua_rawseti(L, -2, 0); |
| 1892 | |
| 1893 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1894 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1895 | lua_setmetatable(L, -2); |
| 1896 | |
| 1897 | |
| 1898 | return 1; |
| 1899 | } |
| 1900 | |
| 1901 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1902 | { |
| 1903 | struct map_descriptor *desc; |
| 1904 | struct pattern *pat; |
| 1905 | struct sample smp; |
| 1906 | |
| 1907 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1908 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1909 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1910 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1911 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1912 | } |
| 1913 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1914 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1915 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1916 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 91dc0c0 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1917 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1918 | } |
| 1919 | |
| 1920 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1921 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1922 | if (str) |
| 1923 | lua_pushstring(L, ""); |
| 1924 | else |
| 1925 | lua_pushnil(L); |
| 1926 | return 1; |
| 1927 | } |
| 1928 | |
| 1929 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1930 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1931 | return 1; |
| 1932 | } |
| 1933 | |
| 1934 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1935 | { |
| 1936 | return _hlua_map_lookup(L, 0); |
| 1937 | } |
| 1938 | |
| 1939 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1940 | { |
| 1941 | return _hlua_map_lookup(L, 1); |
| 1942 | } |
| 1943 | |
| 1944 | /* |
| 1945 | * |
| 1946 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1947 | * Class Socket |
| 1948 | * |
| 1949 | * |
| 1950 | */ |
| 1951 | |
| 1952 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1953 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1954 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | } |
| 1956 | |
| 1957 | /* This function is the handler called for each I/O on the established |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1958 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | * received. |
| 1960 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1961 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1963 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1964 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1966 | if (ctx->die) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1967 | sc_shutw(sc); |
| 1968 | sc_shutr(sc); |
| 1969 | sc_ic(sc)->flags |= CF_READ_NULL; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1970 | notification_wake(&ctx->wake_on_read); |
| 1971 | notification_wake(&ctx->wake_on_write); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1972 | stream_shutdown(__sc_strm(sc), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1973 | } |
| 1974 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1975 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1976 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1977 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1978 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1979 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1980 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1981 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1982 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1983 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1984 | * to be notified whenever the connection completes. |
| 1985 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1986 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 1987 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 1988 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 1989 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1990 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1991 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1992 | |
| 1993 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1994 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1996 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1997 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1998 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1999 | |
| 2000 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2001 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2002 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2003 | |
| 2004 | /* Some data were injected in the buffer, notify the stream |
| 2005 | * interface. |
| 2006 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2007 | if (!channel_is_empty(sc_ic(sc))) |
| 2008 | sc_update(sc); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2009 | |
| 2010 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2011 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2012 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2013 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2014 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | } |
| 2016 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2017 | static int hlua_socket_init(struct appctx *appctx) |
| 2018 | { |
| 2019 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2020 | struct stream *s; |
| 2021 | |
| 2022 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2023 | goto error; |
| 2024 | |
| 2025 | s = appctx_strm(appctx); |
| 2026 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2027 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2028 | * and retrieve data from the server. The connection is initialized |
| 2029 | * with the "struct server". |
| 2030 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2031 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2032 | |
| 2033 | /* Force destination server. */ |
| 2034 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2035 | s->target = &socket_tcp->obj_type; |
| 2036 | |
| 2037 | ctx->appctx = appctx; |
| 2038 | return 0; |
| 2039 | |
| 2040 | error: |
| 2041 | return -1; |
| 2042 | } |
| 2043 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2044 | /* This function is called when the "struct stream" is destroyed. |
| 2045 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2046 | * Wake all the pending signals. |
| 2047 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2048 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2049 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2050 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2051 | struct xref *peer; |
| 2052 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2053 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2054 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2055 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2056 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2057 | |
| 2058 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2059 | notification_wake(&ctx->wake_on_read); |
| 2060 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | } |
| 2062 | |
| 2063 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2064 | * uses this object. If the stream does not exists, just quit. |
| 2065 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | * pending signal can rest in the read and write lists. destroy |
| 2067 | * it. |
| 2068 | */ |
| 2069 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2070 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2071 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2072 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2073 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2075 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2076 | |
| 2077 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2079 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2081 | |
| 2082 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2083 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2084 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2085 | ctx->die = 1; |
| 2086 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2088 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2094 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2096 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2098 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2099 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2100 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2101 | struct hlua *hlua; |
| 2102 | |
| 2103 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2104 | hlua = hlua_gethlua(L); |
| 2105 | if (!hlua) |
| 2106 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2107 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2108 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2109 | |
| 2110 | /* Check if we run on the same thread than the xreator thread. |
| 2111 | * We cannot access to the socket if the thread is different. |
| 2112 | */ |
| 2113 | if (socket->tid != tid) |
| 2114 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2115 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2116 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2117 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2119 | |
| 2120 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2121 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2122 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2124 | ctx->die = 1; |
| 2125 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2126 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2127 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2128 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2129 | return 0; |
| 2130 | } |
| 2131 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2132 | /* The close function calls close_helper. |
| 2133 | */ |
| 2134 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2135 | { |
| 2136 | MAY_LJMP(check_args(L, 1, "close")); |
| 2137 | return hlua_socket_close_helper(L); |
| 2138 | } |
| 2139 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2140 | /* This Lua function assumes that the stack contain three parameters. |
| 2141 | * 1 - USERDATA containing a struct socket |
| 2142 | * 2 - INTEGER with values of the macro defined below |
| 2143 | * If the integer is -1, we must read at most one line. |
| 2144 | * If the integer is -2, we ust read all the data until the |
| 2145 | * end of the stream. |
| 2146 | * If the integer is positive value, we must read a number of |
| 2147 | * bytes corresponding to this value. |
| 2148 | */ |
| 2149 | #define HLSR_READ_LINE (-1) |
| 2150 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2151 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | { |
| 2153 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2154 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2155 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2156 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2157 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2158 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2159 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2160 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2161 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2162 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2163 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2164 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2165 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2166 | struct stream *s; |
| 2167 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2168 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2170 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2171 | hlua = hlua_gethlua(L); |
| 2172 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2173 | /* Check if this lua stack is schedulable. */ |
| 2174 | if (!hlua || !hlua->task) |
| 2175 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2176 | "'frontend', 'backend' or 'task'")); |
| 2177 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2178 | /* Check if we run on the same thread than the xreator thread. |
| 2179 | * We cannot access to the socket if the thread is different. |
| 2180 | */ |
| 2181 | if (socket->tid != tid) |
| 2182 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2183 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2184 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2185 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2186 | if (!peer) |
| 2187 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2188 | |
| 2189 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2190 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2191 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2193 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2194 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2195 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2196 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | if (nblk < 0) /* Connection close. */ |
| 2198 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2199 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2201 | |
| 2202 | /* remove final \r\n. */ |
| 2203 | if (nblk == 1) { |
| 2204 | if (blk1[len1-1] == '\n') { |
| 2205 | len1--; |
| 2206 | skip_at_end++; |
| 2207 | if (blk1[len1-1] == '\r') { |
| 2208 | len1--; |
| 2209 | skip_at_end++; |
| 2210 | } |
| 2211 | } |
| 2212 | } |
| 2213 | else { |
| 2214 | if (blk2[len2-1] == '\n') { |
| 2215 | len2--; |
| 2216 | skip_at_end++; |
| 2217 | if (blk2[len2-1] == '\r') { |
| 2218 | len2--; |
| 2219 | skip_at_end++; |
| 2220 | } |
| 2221 | } |
| 2222 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | } |
| 2224 | |
| 2225 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2227 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | if (nblk < 0) /* Connection close. */ |
| 2229 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2230 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | goto connection_empty; |
| 2232 | } |
| 2233 | |
| 2234 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2235 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2236 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | if (nblk < 0) /* Connection close. */ |
| 2238 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2239 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | goto connection_empty; |
| 2241 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2242 | missing_bytes = wanted - socket->b.n; |
| 2243 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2245 | len1 = missing_bytes; |
| 2246 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2247 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2248 | } |
| 2249 | |
| 2250 | len = len1; |
| 2251 | |
| 2252 | luaL_addlstring(&socket->b, blk1, len1); |
| 2253 | if (nblk == 2) { |
| 2254 | len += len2; |
| 2255 | luaL_addlstring(&socket->b, blk2, len2); |
| 2256 | } |
| 2257 | |
| 2258 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2259 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | |
| 2261 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2262 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | |
| 2264 | /* If the pattern reclaim to read all the data |
| 2265 | * in the connection, got out. |
| 2266 | */ |
| 2267 | if (wanted == HLSR_READ_ALL) |
| 2268 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2269 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | goto connection_empty; |
| 2271 | |
| 2272 | /* Return result. */ |
| 2273 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2274 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | return 1; |
| 2276 | |
| 2277 | connection_closed: |
| 2278 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | xref_unlock(&socket->xref, peer); |
| 2280 | |
| 2281 | no_peer: |
| 2282 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | /* If the buffer containds data. */ |
| 2284 | if (socket->b.n > 0) { |
| 2285 | luaL_pushresult(&socket->b); |
| 2286 | return 1; |
| 2287 | } |
| 2288 | lua_pushnil(L); |
| 2289 | lua_pushstring(L, "connection closed."); |
| 2290 | return 2; |
| 2291 | |
| 2292 | connection_empty: |
| 2293 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2294 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | } |
| 2298 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2299 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | return 0; |
| 2301 | } |
| 2302 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2303 | /* This Lua function gets two parameters. The first one can be string |
| 2304 | * or a number. If the string is "*l", the user requires one line. If |
| 2305 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | * If the value is a number, the user require a number of bytes equal |
| 2307 | * to the value. The default value is "*l" (a line). |
| 2308 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2309 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2310 | * integer takes this values: |
| 2311 | * -1 : read a line |
| 2312 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2313 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2315 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2316 | * concatenated with the read data. |
| 2317 | */ |
| 2318 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2319 | { |
| 2320 | int wanted = HLSR_READ_LINE; |
| 2321 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2322 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | char *error; |
| 2324 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2325 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2326 | |
| 2327 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2328 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2329 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2330 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2332 | /* Check if we run on the same thread than the xreator thread. |
| 2333 | * We cannot access to the socket if the thread is different. |
| 2334 | */ |
| 2335 | if (socket->tid != tid) |
| 2336 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2337 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2338 | /* check for pattern. */ |
| 2339 | if (lua_gettop(L) >= 2) { |
| 2340 | type = lua_type(L, 2); |
| 2341 | if (type == LUA_TSTRING) { |
| 2342 | pattern = lua_tostring(L, 2); |
| 2343 | if (strcmp(pattern, "*a") == 0) |
| 2344 | wanted = HLSR_READ_ALL; |
| 2345 | else if (strcmp(pattern, "*l") == 0) |
| 2346 | wanted = HLSR_READ_LINE; |
| 2347 | else { |
| 2348 | wanted = strtoll(pattern, &error, 10); |
| 2349 | if (*error != '\0') |
| 2350 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2351 | } |
| 2352 | } |
| 2353 | else if (type == LUA_TNUMBER) { |
| 2354 | wanted = lua_tointeger(L, 2); |
| 2355 | if (wanted < 0) |
| 2356 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2357 | } |
| 2358 | } |
| 2359 | |
| 2360 | /* Set pattern. */ |
| 2361 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2362 | |
| 2363 | /* Check if we would replace the top by itself. */ |
| 2364 | if (lua_gettop(L) != 2) |
| 2365 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2367 | /* Save index of the top of the stack because since buffers are used, it |
| 2368 | * may change |
| 2369 | */ |
| 2370 | lastarg = lua_gettop(L); |
| 2371 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2372 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | luaL_buffinit(L, &socket->b); |
| 2374 | |
| 2375 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2376 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2377 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2378 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2379 | pattern = lua_tolstring(L, 3, &len); |
| 2380 | luaL_addlstring(&socket->b, pattern, len); |
| 2381 | } |
| 2382 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2383 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | } |
| 2385 | |
| 2386 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2387 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2388 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2389 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2390 | { |
| 2391 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2392 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2393 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | struct appctx *appctx; |
| 2395 | size_t buf_len; |
| 2396 | const char *buf; |
| 2397 | int len; |
| 2398 | int send_len; |
| 2399 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2401 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2402 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2404 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2405 | hlua = hlua_gethlua(L); |
| 2406 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | /* Check if this lua stack is schedulable. */ |
| 2408 | if (!hlua || !hlua->task) |
| 2409 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2410 | "'frontend', 'backend' or 'task'")); |
| 2411 | |
| 2412 | /* Get object */ |
| 2413 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2414 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2415 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2417 | /* Check if we run on the same thread than the xreator thread. |
| 2418 | * We cannot access to the socket if the thread is different. |
| 2419 | */ |
| 2420 | if (socket->tid != tid) |
| 2421 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2422 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2424 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2425 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2426 | lua_pushinteger(L, -1); |
| 2427 | return 1; |
| 2428 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2429 | |
| 2430 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2431 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2432 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2433 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2434 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2435 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2436 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2438 | lua_pushinteger(L, -1); |
| 2439 | return 1; |
| 2440 | } |
| 2441 | |
| 2442 | /* Update the input buffer data. */ |
| 2443 | buf += sent; |
| 2444 | send_len = buf_len - sent; |
| 2445 | |
| 2446 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | if (sent >= buf_len) { |
| 2448 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2450 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2451 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2452 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2453 | * the request buffer if its not required. |
| 2454 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2455 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2456 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2457 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2458 | } |
| 2459 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2460 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2461 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2462 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2463 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2464 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2465 | |
| 2466 | /* send data */ |
| 2467 | if (len < send_len) |
| 2468 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2469 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2472 | * the data" (-2) are not expected because the available length |
| 2473 | * is tested. |
| 2474 | * Other unknown error are also not expected. |
| 2475 | */ |
| 2476 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2477 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2478 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2479 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2480 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2482 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2484 | return 1; |
| 2485 | } |
| 2486 | |
| 2487 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2488 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2489 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2490 | s->req.rex = TICK_ETERNITY; |
| 2491 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | |
| 2493 | /* Update length sent. */ |
| 2494 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2495 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2496 | |
| 2497 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2498 | if (sent + len >= buf_len) { |
| 2499 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2501 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2502 | |
| 2503 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2504 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2505 | xref_unlock(&socket->xref, peer); |
| 2506 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2507 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2508 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2509 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2510 | return 0; |
| 2511 | } |
| 2512 | |
| 2513 | /* This function initiate the send of data. It just check the input |
| 2514 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2515 | * amount of data written to the buffer. This is used by the function |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | * "hlua_socket_write_yield" that can yield. |
| 2517 | * |
| 2518 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2519 | * the associated object. The second is a string buffer. The third is |
| 2520 | * a facultative integer that represents where is the buffer position |
| 2521 | * of the start of the data that can send. The first byte is the |
| 2522 | * position "1". The default value is "1". The fourth argument is a |
| 2523 | * facultative integer that represents where is the buffer position |
| 2524 | * of the end of the data that can send. The default is the last byte. |
| 2525 | */ |
| 2526 | static int hlua_socket_send(struct lua_State *L) |
| 2527 | { |
| 2528 | int i; |
| 2529 | int j; |
| 2530 | const char *buf; |
| 2531 | size_t buf_len; |
| 2532 | |
| 2533 | /* Check number of arguments. */ |
| 2534 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2535 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2536 | |
| 2537 | /* Get the string. */ |
| 2538 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2539 | |
| 2540 | /* Get and check j. */ |
| 2541 | if (lua_gettop(L) == 4) { |
| 2542 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2543 | if (j < 0) |
| 2544 | j = buf_len + j + 1; |
| 2545 | if (j > buf_len) |
| 2546 | j = buf_len + 1; |
| 2547 | lua_pop(L, 1); |
| 2548 | } |
| 2549 | else |
| 2550 | j = buf_len; |
| 2551 | |
| 2552 | /* Get and check i. */ |
| 2553 | if (lua_gettop(L) == 3) { |
| 2554 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2555 | if (i < 0) |
| 2556 | i = buf_len + i + 1; |
| 2557 | if (i > buf_len) |
| 2558 | i = buf_len + 1; |
| 2559 | lua_pop(L, 1); |
| 2560 | } else |
| 2561 | i = 1; |
| 2562 | |
| 2563 | /* Check bth i and j. */ |
| 2564 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2565 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2566 | return 1; |
| 2567 | } |
| 2568 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2569 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2570 | return 1; |
| 2571 | } |
| 2572 | if (i == 0) |
| 2573 | i = 1; |
| 2574 | if (j == 0) |
| 2575 | j = 1; |
| 2576 | |
| 2577 | /* Pop the string. */ |
| 2578 | lua_pop(L, 1); |
| 2579 | |
| 2580 | /* Update the buffer length. */ |
| 2581 | buf += i - 1; |
| 2582 | buf_len = j - i + 1; |
| 2583 | lua_pushlstring(L, buf, buf_len); |
| 2584 | |
| 2585 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2586 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | } |
| 2590 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2591 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2592 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | { |
| 2594 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2595 | int ret; |
| 2596 | int len; |
| 2597 | char *p; |
| 2598 | |
| 2599 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2600 | if (ret <= 0) { |
| 2601 | lua_pushnil(L); |
| 2602 | return 1; |
| 2603 | } |
| 2604 | |
| 2605 | if (ret == AF_UNIX) { |
| 2606 | lua_pushstring(L, buffer+1); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | else if (ret == AF_INET6) { |
| 2610 | buffer[0] = '['; |
| 2611 | len = strlen(buffer); |
| 2612 | buffer[len] = ']'; |
| 2613 | len++; |
| 2614 | buffer[len] = ':'; |
| 2615 | len++; |
| 2616 | p = buffer; |
| 2617 | } |
| 2618 | else if (ret == AF_INET) { |
| 2619 | p = buffer + 1; |
| 2620 | len = strlen(p); |
| 2621 | p[len] = ':'; |
| 2622 | len++; |
| 2623 | } |
| 2624 | else { |
| 2625 | lua_pushnil(L); |
| 2626 | return 1; |
| 2627 | } |
| 2628 | |
| 2629 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2630 | lua_pushnil(L); |
| 2631 | return 1; |
| 2632 | } |
| 2633 | |
| 2634 | lua_pushstring(L, p); |
| 2635 | return 1; |
| 2636 | } |
| 2637 | |
| 2638 | /* Returns information about the peer of the connection. */ |
| 2639 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2640 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2641 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2642 | struct xref *peer; |
| 2643 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2644 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2645 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2646 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2647 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2649 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2650 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2651 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2652 | /* Check if we run on the same thread than the xreator thread. |
| 2653 | * We cannot access to the socket if the thread is different. |
| 2654 | */ |
| 2655 | if (socket->tid != tid) |
| 2656 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2657 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2658 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2659 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2661 | lua_pushnil(L); |
| 2662 | return 1; |
| 2663 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2664 | |
| 2665 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2666 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2667 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2668 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2670 | lua_pushnil(L); |
| 2671 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | } |
| 2673 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2674 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2675 | xref_unlock(&socket->xref, peer); |
| 2676 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
| 2679 | /* Returns information about my connection side. */ |
| 2680 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2681 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2682 | struct hlua_socket *socket; |
| 2683 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2684 | struct appctx *appctx; |
| 2685 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2686 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2687 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2688 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2689 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2690 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2691 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2692 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2693 | /* Check if we run on the same thread than the xreator thread. |
| 2694 | * We cannot access to the socket if the thread is different. |
| 2695 | */ |
| 2696 | if (socket->tid != tid) |
| 2697 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2698 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2699 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2700 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2701 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2702 | lua_pushnil(L); |
| 2703 | return 1; |
| 2704 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2705 | |
| 2706 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2707 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2708 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2709 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2710 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2711 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | lua_pushnil(L); |
| 2713 | return 1; |
| 2714 | } |
| 2715 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2716 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2717 | xref_unlock(&socket->xref, peer); |
| 2718 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
| 2721 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2722 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2723 | .obj_type = OBJ_TYPE_APPLET, |
| 2724 | .name = "<LUA_TCP>", |
| 2725 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2726 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2727 | .release = hlua_socket_release, |
| 2728 | }; |
| 2729 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2730 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | { |
| 2732 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2733 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2734 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2735 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2736 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2737 | struct stream *s; |
| 2738 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2739 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2740 | hlua = hlua_gethlua(L); |
| 2741 | if (!hlua) |
| 2742 | return 0; |
| 2743 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2744 | /* Check if we run on the same thread than the xreator thread. |
| 2745 | * We cannot access to the socket if the thread is different. |
| 2746 | */ |
| 2747 | if (socket->tid != tid) |
| 2748 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2749 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2750 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2751 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2752 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2753 | lua_pushnil(L); |
| 2754 | lua_pushstring(L, "Can't connect"); |
| 2755 | return 2; |
| 2756 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2757 | |
| 2758 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2759 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2760 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2761 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2762 | /* Check if we run on the same thread than the xreator thread. |
| 2763 | * We cannot access to the socket if the thread is different. |
| 2764 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2765 | if (socket->tid != tid) { |
| 2766 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2767 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2768 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2769 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2770 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2771 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2772 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2773 | lua_pushnil(L); |
| 2774 | lua_pushstring(L, "Can't connect"); |
| 2775 | return 2; |
| 2776 | } |
| 2777 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2778 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2779 | |
| 2780 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2781 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2782 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2783 | lua_pushinteger(L, 1); |
| 2784 | return 1; |
| 2785 | } |
| 2786 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2787 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2788 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2789 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2790 | } |
| 2791 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2792 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2793 | return 0; |
| 2794 | } |
| 2795 | |
| 2796 | /* This function fail or initite the connection. */ |
| 2797 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2798 | { |
| 2799 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2800 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2801 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2802 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2803 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2804 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2805 | int low, high; |
| 2806 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2807 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2808 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2809 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2810 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2811 | if (lua_gettop(L) < 2) |
| 2812 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2813 | |
| 2814 | /* Get args. */ |
| 2815 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2816 | |
| 2817 | /* Check if we run on the same thread than the xreator thread. |
| 2818 | * We cannot access to the socket if the thread is different. |
| 2819 | */ |
| 2820 | if (socket->tid != tid) |
| 2821 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2822 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2823 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2824 | if (lua_gettop(L) >= 3) { |
| 2825 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2826 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2827 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2828 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2829 | * that are not enclosed within square brackets. |
| 2830 | */ |
| 2831 | if (port > 0) { |
| 2832 | luaL_buffinit(L, &b); |
| 2833 | luaL_addstring(&b, ip); |
| 2834 | luaL_addchar(&b, ':'); |
| 2835 | luaL_pushresult(&b); |
| 2836 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2837 | } |
| 2838 | } |
| 2839 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2840 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2841 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2842 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2843 | lua_pushnil(L); |
| 2844 | return 1; |
| 2845 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | |
| 2847 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2848 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2849 | if (!addr) { |
| 2850 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2851 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2852 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2853 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2854 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2855 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2856 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2857 | if (port == -1) { |
| 2858 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2859 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2860 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2861 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2862 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2863 | if (port == -1) { |
| 2864 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2865 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2866 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2867 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2868 | } |
| 2869 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2870 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2871 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2872 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2873 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2874 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2876 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2877 | xref_unlock(&socket->xref, peer); |
| 2878 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2879 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2880 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2881 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2882 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2883 | if (!hlua) |
| 2884 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2885 | |
| 2886 | /* inform the stream that we want to be notified whenever the |
| 2887 | * connection completes. |
| 2888 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2889 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2890 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2891 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2892 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2893 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2894 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2895 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2896 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2897 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2898 | } |
| 2899 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2900 | |
| 2901 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2902 | /* Return yield waiting for connection. */ |
| 2903 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2904 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2905 | |
| 2906 | return 0; |
| 2907 | } |
| 2908 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2909 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2910 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2911 | { |
| 2912 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2913 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2914 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | |
| 2916 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2917 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2918 | |
| 2919 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2920 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2921 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2922 | lua_pushnil(L); |
| 2923 | return 1; |
| 2924 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2925 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2926 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2927 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2928 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2929 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2930 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2931 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2932 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | |
| 2934 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2935 | { |
| 2936 | return 0; |
| 2937 | } |
| 2938 | |
| 2939 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2940 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2941 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2942 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2943 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2944 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2945 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2946 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2947 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2948 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2949 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2950 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2951 | /* convert the timeout to millis */ |
| 2952 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2953 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2954 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2955 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2956 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2957 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2958 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2959 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2960 | |
| 2961 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2962 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2963 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2964 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2965 | /* Check if we run on the same thread than the xreator thread. |
| 2966 | * We cannot access to the socket if the thread is different. |
| 2967 | */ |
| 2968 | if (socket->tid != tid) |
| 2969 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2970 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2971 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2972 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2973 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2974 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2975 | WILL_LJMP(lua_error(L)); |
| 2976 | return 0; |
| 2977 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2978 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2979 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2980 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2981 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2982 | s->req.rto = tmout; |
| 2983 | s->req.wto = tmout; |
| 2984 | s->res.rto = tmout; |
| 2985 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2986 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2987 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2988 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2989 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2990 | |
| 2991 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2992 | task_queue(s->task); |
| 2993 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2994 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2995 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2996 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2997 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2998 | } |
| 2999 | |
| 3000 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3001 | { |
| 3002 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3003 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3004 | struct appctx *appctx; |
| 3005 | |
| 3006 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3007 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3008 | hlua_pusherror(L, "socket: full stack"); |
| 3009 | goto out_fail_conf; |
| 3010 | } |
| 3011 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3012 | /* Create the object: obj[0] = userdata. */ |
| 3013 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3014 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3015 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3016 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3017 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3018 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3019 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3020 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3021 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3022 | goto out_fail_conf; |
| 3023 | } |
| 3024 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3025 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3026 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3027 | lua_setmetatable(L, -2); |
| 3028 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3029 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3030 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3031 | if (!appctx) { |
| 3032 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3033 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3034 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3035 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3036 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3037 | ctx->die = 0; |
| 3038 | LIST_INIT(&ctx->wake_on_write); |
| 3039 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3040 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3041 | if (appctx_init(appctx) == -1) { |
| 3042 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3043 | goto out_fail_appctx; |
| 3044 | } |
| 3045 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3046 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3047 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3048 | return 1; |
| 3049 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3050 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3051 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3052 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3053 | WILL_LJMP(lua_error(L)); |
| 3054 | return 0; |
| 3055 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3056 | |
| 3057 | /* |
| 3058 | * |
| 3059 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | * Class Channel |
| 3061 | * |
| 3062 | * |
| 3063 | */ |
| 3064 | |
| 3065 | /* Returns the struct hlua_channel join to the class channel in the |
| 3066 | * stack entry "ud" or throws an argument error. |
| 3067 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3068 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3070 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | } |
| 3072 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3074 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3075 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3076 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3077 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3079 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | return 0; |
| 3081 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3082 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3083 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3084 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | |
| 3086 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3087 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3088 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3089 | return 1; |
| 3090 | } |
| 3091 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3092 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3093 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3094 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3095 | * initialized. |
| 3096 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3097 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3098 | { |
| 3099 | struct filter *filter = NULL; |
| 3100 | |
| 3101 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3102 | struct hlua_flt_ctx *flt_ctx; |
| 3103 | |
| 3104 | filter = lua_touserdata (L, -1); |
| 3105 | flt_ctx = filter->ctx; |
| 3106 | if (hlua_filter_from_payload(filter)) { |
| 3107 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3108 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3109 | } |
| 3110 | } |
| 3111 | |
| 3112 | lua_pop(L, 1); |
| 3113 | return filter; |
| 3114 | } |
| 3115 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3116 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3117 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3118 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3119 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3120 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3121 | */ |
| 3122 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3124 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3125 | luaL_Buffer b; |
| 3126 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3127 | block1 = len; |
| 3128 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3129 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3130 | block2 = len - block1; |
| 3131 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3132 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3133 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3134 | if (block2) |
| 3135 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3136 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3137 | return len; |
| 3138 | } |
| 3139 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3140 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3141 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3142 | * number of bytes copied. |
| 3143 | */ |
| 3144 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3145 | { |
| 3146 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3147 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3148 | /* Nothing to do, just return */ |
| 3149 | if (unlikely(istlen(str) == 0)) |
| 3150 | goto end; |
| 3151 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3152 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3153 | ret = -1; |
| 3154 | goto end; |
| 3155 | } |
| 3156 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3157 | |
| 3158 | end: |
| 3159 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3160 | } |
| 3161 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3162 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3163 | */ |
| 3164 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3165 | { |
| 3166 | size_t end = offset + len; |
| 3167 | |
| 3168 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3169 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3170 | b_data(&chn->buf) - end, -len); |
| 3171 | b_sub(&chn->buf, len); |
| 3172 | } |
| 3173 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3174 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3175 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3176 | * offset by default). If there is not enough input data and more data can be |
| 3177 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3178 | * |
| 3179 | * From an action, All input data are considered. For a filter, the offset and |
| 3180 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3181 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3182 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3184 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3185 | struct filter *filter; |
| 3186 | size_t input, output; |
| 3187 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3188 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3189 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3190 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3191 | output = co_data(chn); |
| 3192 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3193 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3194 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3195 | if (filter && !hlua_filter_from_payload(filter)) |
| 3196 | WILL_LJMP(lua_error(L)); |
| 3197 | |
| 3198 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3199 | if (lua_gettop(L) > 1) { |
| 3200 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3201 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3202 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3203 | offset += output; |
| 3204 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | lua_pushfstring(L, "offset out of range."); |
| 3206 | WILL_LJMP(lua_error(L)); |
| 3207 | } |
| 3208 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3209 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | if (lua_gettop(L) == 3) { |
| 3211 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3212 | if (!len) |
| 3213 | goto dup; |
| 3214 | if (len == -1) |
| 3215 | len = global.tune.bufsize; |
| 3216 | if (len < 0) { |
| 3217 | lua_pushfstring(L, "length out of range."); |
| 3218 | WILL_LJMP(lua_error(L)); |
| 3219 | } |
| 3220 | } |
| 3221 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3222 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3223 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3224 | /* Yield waiting for more data, as requested */ |
| 3225 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3226 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | } |
| 3229 | |
| 3230 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3231 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3232 | return 1; |
| 3233 | } |
| 3234 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3235 | /* Copies the first line (including the trailing LF) of input data in the |
| 3236 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3237 | * a length (all remaining input data starting for the offset by default). If |
| 3238 | * there is not enough input data and more data can be received, the function |
| 3239 | * yields. If a length is explicitly specified, no more data are |
| 3240 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3241 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3242 | * |
| 3243 | * From an action, All input data are considered. For a filter, the offset and |
| 3244 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3245 | */ |
| 3246 | __LJMP static int hlua_channel_get_line_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3247 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3248 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3249 | struct filter *filter; |
| 3250 | size_t l, input, output; |
| 3251 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3252 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3253 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3254 | output = co_data(chn); |
| 3255 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3256 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3257 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3258 | if (filter && !hlua_filter_from_payload(filter)) |
| 3259 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3260 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3261 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3262 | if (lua_gettop(L) > 1) { |
| 3263 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3264 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3265 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3266 | offset += output; |
| 3267 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3268 | lua_pushfstring(L, "offset out of range."); |
| 3269 | WILL_LJMP(lua_error(L)); |
| 3270 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3271 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3272 | |
| 3273 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3274 | if (lua_gettop(L) == 3) { |
| 3275 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3276 | if (!len) |
| 3277 | goto dup; |
| 3278 | if (len == -1) |
| 3279 | len = global.tune.bufsize; |
| 3280 | if (len < 0) { |
| 3281 | lua_pushfstring(L, "length out of range."); |
| 3282 | WILL_LJMP(lua_error(L)); |
| 3283 | } |
| 3284 | } |
| 3285 | |
| 3286 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3287 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3288 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3289 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3290 | len = l+1; |
| 3291 | goto dup; |
| 3292 | } |
| 3293 | } |
| 3294 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3296 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3297 | /* Yield waiting for more data */ |
| 3298 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3299 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3301 | } |
| 3302 | |
| 3303 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3304 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3305 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3306 | } |
| 3307 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3308 | /* [ DEPRECATED ] |
| 3309 | * |
| 3310 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3311 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3312 | * |
| 3313 | * From an action, All input data are considered. For a filter, the offset and |
| 3314 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3315 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3316 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3317 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3318 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3319 | struct filter *filter; |
| 3320 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3321 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3322 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3323 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3324 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3325 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3326 | WILL_LJMP(lua_error(L)); |
| 3327 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3328 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3329 | offset = co_data(chn); |
| 3330 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3331 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3332 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3333 | if (filter && !hlua_filter_from_payload(filter)) |
| 3334 | WILL_LJMP(lua_error(L)); |
| 3335 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3336 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3337 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3338 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3339 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3340 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3342 | return 1; |
| 3343 | } |
| 3344 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | /* [ DEPRECATED ] |
| 3346 | * |
| 3347 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3348 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3349 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3350 | * |
| 3351 | * From an action, All input data are considered. For a filter, the offset and |
| 3352 | * the length of input data to consider are retrieved from the filter context. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3353 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3354 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3355 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3356 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3357 | struct filter *filter; |
| 3358 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3359 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3360 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3361 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3362 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3363 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3364 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3365 | WILL_LJMP(lua_error(L)); |
| 3366 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3367 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3368 | offset = co_data(chn); |
| 3369 | len = ci_data(chn); |
| 3370 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3371 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3372 | if (filter && !hlua_filter_from_payload(filter)) |
| 3373 | WILL_LJMP(lua_error(L)); |
| 3374 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3375 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3376 | lua_pushnil(L); |
| 3377 | return 1; |
| 3378 | } |
| 3379 | |
| 3380 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3381 | _hlua_channel_delete(chn, offset, ret); |
| 3382 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3383 | } |
| 3384 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3385 | /* This functions consumes and returns one line. If the channel is closed, |
| 3386 | * and the last data does not contains a final '\n', the data are returned |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3387 | * without the final '\n'. When no more data are available, it returns nil |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3388 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3389 | * |
| 3390 | * From an action, All input data are considered. For a filter, the offset and |
| 3391 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3392 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3393 | __LJMP static int hlua_channel_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3394 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3395 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3396 | struct filter *filter; |
| 3397 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3398 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3399 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3400 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3401 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3402 | offset = co_data(chn); |
| 3403 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3404 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3405 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3406 | if (filter && !hlua_filter_from_payload(filter)) |
| 3407 | WILL_LJMP(lua_error(L)); |
| 3408 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3409 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3410 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3411 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | for (l = 0; l < len; l++) { |
| 3415 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3416 | len = l+1; |
| 3417 | goto dup; |
| 3418 | } |
| 3419 | } |
| 3420 | |
| 3421 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3422 | /* Yield waiting for more data */ |
| 3423 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3424 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3425 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3426 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3427 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3428 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3429 | return 1; |
| 3430 | } |
| 3431 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3432 | /* [ DEPRECATED ] |
| 3433 | * |
| 3434 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3435 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3436 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3437 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3438 | struct channel *chn; |
| 3439 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3440 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3441 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3442 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3443 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3444 | WILL_LJMP(lua_error(L)); |
| 3445 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3446 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3447 | } |
| 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3450 | * available input data are returned. The offset may be negactive to start from |
| 3451 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3452 | * size. |
| 3453 | */ |
| 3454 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3455 | { |
| 3456 | struct channel *chn; |
| 3457 | |
| 3458 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3459 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3460 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3461 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3462 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3463 | WILL_LJMP(lua_error(L)); |
| 3464 | } |
| 3465 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3466 | } |
| 3467 | |
| 3468 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3469 | * available input data are returned. The offset may be negactive to start from |
| 3470 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3471 | * size. |
| 3472 | */ |
| 3473 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3474 | { |
| 3475 | struct channel *chn; |
| 3476 | |
| 3477 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3478 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3479 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3480 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3481 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3482 | WILL_LJMP(lua_error(L)); |
| 3483 | } |
| 3484 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3485 | } |
| 3486 | |
| 3487 | /* Appends a string into the input side of channel. It returns the length of the |
| 3488 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3489 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3490 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3491 | * |
| 3492 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3493 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3494 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3495 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3496 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3497 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3498 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3499 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3500 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3501 | |
| 3502 | MAY_LJMP(check_args(L, 2, "append")); |
| 3503 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3504 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3505 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3506 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3507 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3508 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3509 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3510 | offset = co_data(chn); |
| 3511 | len = ci_data(chn); |
| 3512 | |
| 3513 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3514 | if (filter && !hlua_filter_from_payload(filter)) |
| 3515 | WILL_LJMP(lua_error(L)); |
| 3516 | |
| 3517 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3518 | if (ret > 0 && filter) { |
| 3519 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3520 | |
| 3521 | flt_update_offsets(filter, chn, ret); |
| 3522 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3523 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3524 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3525 | return 1; |
| 3526 | } |
| 3527 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3528 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3529 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3530 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3531 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3532 | * |
| 3533 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3534 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3535 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3536 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3537 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3538 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3539 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3540 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3541 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3542 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3543 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3544 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3545 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3546 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3547 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3548 | WILL_LJMP(lua_error(L)); |
| 3549 | } |
| 3550 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3551 | offset = co_data(chn); |
| 3552 | len = ci_data(chn); |
| 3553 | |
| 3554 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3555 | if (filter && !hlua_filter_from_payload(filter)) |
| 3556 | WILL_LJMP(lua_error(L)); |
| 3557 | |
| 3558 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3559 | if (ret > 0 && filter) { |
| 3560 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3561 | |
| 3562 | flt_update_offsets(filter, chn, ret); |
| 3563 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3564 | } |
| 3565 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | lua_pushinteger(L, ret); |
| 3567 | return 1; |
| 3568 | } |
| 3569 | |
| 3570 | /* Inserts a given amount of input data at the given offset by a string |
| 3571 | * content. By default the string is appended at the end of input data. It |
| 3572 | * returns the length of the written string, or -1 if the channel is closed or |
| 3573 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | * |
| 3575 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3576 | */ |
| 3577 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3578 | { |
| 3579 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3580 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3581 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3582 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3583 | int ret, offset; |
| 3584 | |
| 3585 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3586 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3587 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3588 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3589 | |
| 3590 | output = co_data(chn); |
| 3591 | input = ci_data(chn); |
| 3592 | |
| 3593 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3594 | if (filter && !hlua_filter_from_payload(filter)) |
| 3595 | WILL_LJMP(lua_error(L)); |
| 3596 | |
| 3597 | offset = input + output; |
| 3598 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3599 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3600 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3601 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3602 | offset += output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3603 | if (offset < output || offset > output + input) { |
| 3604 | lua_pushfstring(L, "offset out of range."); |
| 3605 | WILL_LJMP(lua_error(L)); |
| 3606 | } |
| 3607 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3608 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3609 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3610 | WILL_LJMP(lua_error(L)); |
| 3611 | } |
| 3612 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3613 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3614 | if (ret > 0 && filter) { |
| 3615 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3616 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3617 | flt_update_offsets(filter, chn, ret); |
| 3618 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3619 | } |
| 3620 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | lua_pushinteger(L, ret); |
| 3622 | return 1; |
| 3623 | } |
| 3624 | /* Replaces a given amount of input data at the given offset by a string |
| 3625 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3626 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3627 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3628 | * |
| 3629 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3630 | */ |
| 3631 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3632 | { |
| 3633 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3634 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3635 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3636 | size_t sz, input, output; |
| 3637 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3638 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3640 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3641 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3642 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3643 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3644 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3645 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3646 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3647 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3648 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3649 | output = co_data(chn); |
| 3650 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3651 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3652 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3653 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3654 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3655 | |
| 3656 | offset = output; |
| 3657 | if (lua_gettop(L) > 2) { |
| 3658 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3659 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3660 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3661 | offset += output; |
| 3662 | if (offset < output || offset > input + output) { |
| 3663 | lua_pushfstring(L, "offset out of range."); |
| 3664 | WILL_LJMP(lua_error(L)); |
| 3665 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3666 | } |
| 3667 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3668 | len = output + input - offset; |
| 3669 | if (lua_gettop(L) == 4) { |
| 3670 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3671 | if (!len) |
| 3672 | goto set; |
| 3673 | if (len == -1) |
| 3674 | len = output + input - offset; |
| 3675 | if (len < 0 || offset + len > output + input) { |
| 3676 | lua_pushfstring(L, "length out of range."); |
| 3677 | WILL_LJMP(lua_error(L)); |
| 3678 | } |
| 3679 | } |
| 3680 | |
| 3681 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3682 | /* Be sure we can copied the string once input data will be removed. */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3683 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3684 | lua_pushinteger(L, -1); |
| 3685 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3686 | _hlua_channel_delete(chn, offset, len); |
| 3687 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3688 | if (filter) { |
| 3689 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3690 | |
| 3691 | len -= (ret > 0 ? ret : 0); |
| 3692 | flt_update_offsets(filter, chn, -len); |
| 3693 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3694 | } |
| 3695 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3696 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3697 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3698 | return 1; |
| 3699 | } |
| 3700 | |
| 3701 | /* Removes a given amount of input data at the given offset. By default all |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3702 | * input data are removed (offset = 0 and len = -1). It returns the amount of |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3703 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3704 | * |
| 3705 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3706 | */ |
| 3707 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3708 | { |
| 3709 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3710 | struct filter *filter; |
| 3711 | size_t input, output; |
| 3712 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3713 | |
| 3714 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3715 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3716 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3717 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3718 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3719 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3720 | WILL_LJMP(lua_error(L)); |
| 3721 | } |
| 3722 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3723 | output = co_data(chn); |
| 3724 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3725 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3726 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3727 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3728 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3729 | |
| 3730 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3731 | if (lua_gettop(L) > 1) { |
| 3732 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3733 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3734 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3735 | offset += output; |
| 3736 | if (offset < output || offset > input + output) { |
| 3737 | lua_pushfstring(L, "offset out of range."); |
| 3738 | WILL_LJMP(lua_error(L)); |
| 3739 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3740 | } |
| 3741 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3742 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3743 | if (lua_gettop(L) == 3) { |
| 3744 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3745 | if (!len) |
| 3746 | goto end; |
| 3747 | if (len == -1) |
| 3748 | len = output + input - offset; |
| 3749 | if (len < 0 || offset + len > output + input) { |
| 3750 | lua_pushfstring(L, "length out of range."); |
| 3751 | WILL_LJMP(lua_error(L)); |
| 3752 | } |
| 3753 | } |
| 3754 | |
| 3755 | _hlua_channel_delete(chn, offset, len); |
| 3756 | if (filter) { |
| 3757 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3758 | |
| 3759 | flt_update_offsets(filter, chn, -len); |
| 3760 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3761 | } |
| 3762 | |
| 3763 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3764 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3765 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3766 | } |
| 3767 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3768 | /* Append data in the output side of the buffer. This data is immediately |
| 3769 | * sent. The function returns the amount of data written. If the buffer |
| 3770 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3771 | * if the channel is closed. |
| 3772 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3773 | __LJMP static int hlua_channel_send_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3774 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3775 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3776 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3777 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3778 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3779 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3780 | struct hlua *hlua; |
| 3781 | |
| 3782 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3783 | hlua = hlua_gethlua(L); |
| 3784 | if (!hlua) { |
| 3785 | lua_pushnil(L); |
| 3786 | return 1; |
| 3787 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3788 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3789 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3790 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3791 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3792 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3793 | offset = co_data(chn); |
| 3794 | len = ci_data(chn); |
| 3795 | |
| 3796 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3797 | if (filter && !hlua_filter_from_payload(filter)) |
| 3798 | WILL_LJMP(lua_error(L)); |
| 3799 | |
| 3800 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3801 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3802 | lua_pushinteger(L, -1); |
| 3803 | return 1; |
| 3804 | } |
| 3805 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3806 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3807 | if (len > sz -l) { |
| 3808 | if (filter) { |
| 3809 | lua_pushinteger(L, -1); |
| 3810 | return 1; |
| 3811 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3812 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3813 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3814 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3815 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3816 | if (ret == -1) { |
| 3817 | lua_pop(L, 1); |
| 3818 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3819 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3820 | } |
| 3821 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3822 | if (filter) { |
| 3823 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3824 | |
| 3825 | |
| 3826 | flt_update_offsets(filter, chn, ret); |
| 3827 | FLT_OFF(filter, chn) += ret; |
| 3828 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3829 | } |
| 3830 | else |
| 3831 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3832 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3833 | l += ret; |
| 3834 | lua_pop(L, 1); |
| 3835 | lua_pushinteger(L, l); |
| 3836 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3837 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3838 | if (l < sz) { |
| 3839 | /* Yield only if the channel's output is not empty. |
| 3840 | * Otherwise it means we cannot add more data. */ |
| 3841 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3842 | return 1; |
| 3843 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3844 | /* If we are waiting for space in the response buffer, we |
| 3845 | * must set the flag WAKERESWR. This flag required the task |
| 3846 | * wake up if any activity is detected on the response buffer. |
| 3847 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3848 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3849 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3850 | else |
| 3851 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3852 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3853 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3854 | |
| 3855 | return 1; |
| 3856 | } |
| 3857 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3858 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3859 | * yield the LUA process, and resume it without checking the |
| 3860 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3861 | * |
| 3862 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3863 | */ |
| 3864 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3865 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3866 | struct channel *chn; |
| 3867 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3868 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3869 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3870 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3871 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3872 | WILL_LJMP(lua_error(L)); |
| 3873 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3874 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3875 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3876 | } |
| 3877 | |
| 3878 | /* This function forward and amount of butes. The data pass from |
| 3879 | * the input side of the buffer to the output side, and can be |
| 3880 | * forwarded. This function never fails. |
| 3881 | * |
| 3882 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3883 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3884 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3885 | __LJMP static int hlua_channel_forward_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3886 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3887 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3888 | struct filter *filter; |
| 3889 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3890 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3891 | struct hlua *hlua; |
| 3892 | |
| 3893 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3894 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3895 | if (!hlua) { |
| 3896 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3897 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3898 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3899 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3900 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3901 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3902 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3903 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3904 | offset = co_data(chn); |
| 3905 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3906 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3907 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3908 | if (filter && !hlua_filter_from_payload(filter)) |
| 3909 | WILL_LJMP(lua_error(L)); |
| 3910 | |
| 3911 | max = fwd - l; |
| 3912 | if (max > len) |
| 3913 | max = len; |
| 3914 | |
| 3915 | if (filter) { |
| 3916 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3917 | |
| 3918 | FLT_OFF(filter, chn) += max; |
| 3919 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3920 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3921 | } |
| 3922 | else |
| 3923 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3924 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3925 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3926 | lua_pop(L, 1); |
| 3927 | lua_pushinteger(L, l); |
| 3928 | |
| 3929 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3930 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3931 | /* The the input channel or the output channel are closed, we |
| 3932 | * must return the amount of data forwarded. |
| 3933 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3934 | if (channel_input_closed(chn) || channel_output_closed(chn) || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3935 | return 1; |
| 3936 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3937 | /* If we are waiting for space data in the response buffer, we |
| 3938 | * must set the flag WAKERESWR. This flag required the task |
| 3939 | * wake up if any activity is detected on the response buffer. |
| 3940 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3941 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3942 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3943 | else |
| 3944 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3945 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3946 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3947 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_forward_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3948 | } |
| 3949 | |
| 3950 | return 1; |
| 3951 | } |
| 3952 | |
| 3953 | /* Just check the input and prepare the stack for the previous |
| 3954 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3955 | * |
| 3956 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3957 | */ |
| 3958 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3959 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3960 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3961 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3962 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3963 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3964 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3965 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3966 | WILL_LJMP(lua_error(L)); |
| 3967 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3968 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3969 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3970 | } |
| 3971 | |
| 3972 | /* Just returns the number of bytes available in the input |
| 3973 | * side of the buffer. This function never fails. |
| 3974 | */ |
| 3975 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3976 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3977 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3978 | struct filter *filter; |
| 3979 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3980 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3981 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3982 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3983 | |
| 3984 | output = co_data(chn); |
| 3985 | input = ci_data(chn); |
| 3986 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3987 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3988 | lua_pushinteger(L, input); |
| 3989 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3990 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3991 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3992 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3993 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3994 | return 1; |
| 3995 | } |
| 3996 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3997 | /* Returns true if the channel is full. */ |
| 3998 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3999 | { |
| 4000 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4001 | |
| 4002 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4003 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4004 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4005 | * applet). |
| 4006 | */ |
| 4007 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4008 | return 1; |
| 4009 | } |
| 4010 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4011 | /* Returns true if the channel may still receive data. */ |
| 4012 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4013 | { |
| 4014 | struct channel *chn; |
| 4015 | |
| 4016 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4017 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4018 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4019 | return 1; |
| 4020 | } |
| 4021 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4022 | /* Returns true if the channel is the response channel. */ |
| 4023 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4024 | { |
| 4025 | struct channel *chn; |
| 4026 | |
| 4027 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4028 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4029 | |
| 4030 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4031 | return 1; |
| 4032 | } |
| 4033 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4034 | /* Just returns the number of bytes available in the output |
| 4035 | * side of the buffer. This function never fails. |
| 4036 | */ |
| 4037 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4038 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4039 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4040 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4041 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4042 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4043 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4044 | |
| 4045 | output = co_data(chn); |
| 4046 | input = ci_data(chn); |
| 4047 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4048 | |
| 4049 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4050 | return 1; |
| 4051 | } |
| 4052 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4053 | /* |
| 4054 | * |
| 4055 | * |
| 4056 | * Class Fetches |
| 4057 | * |
| 4058 | * |
| 4059 | */ |
| 4060 | |
| 4061 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4062 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4063 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4064 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4065 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4066 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4067 | } |
| 4068 | |
| 4069 | /* This function creates and push in the stack a fetch object according |
| 4070 | * with a current TXN. |
| 4071 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4072 | static int hlua_fetches_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4073 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4074 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4075 | |
| 4076 | /* Check stack size. */ |
| 4077 | if (!lua_checkstack(L, 3)) |
| 4078 | return 0; |
| 4079 | |
| 4080 | /* Create the object: obj[0] = userdata. |
| 4081 | * Note that the base of the Fetches object is the |
| 4082 | * transaction object. |
| 4083 | */ |
| 4084 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4085 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4086 | lua_rawseti(L, -2, 0); |
| 4087 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4088 | hsmp->s = txn->s; |
| 4089 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4090 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4091 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4092 | |
| 4093 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4094 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4095 | lua_setmetatable(L, -2); |
| 4096 | |
| 4097 | return 1; |
| 4098 | } |
| 4099 | |
| 4100 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4101 | * It uses closure argument to store the associated sample-fetch. It |
| 4102 | * returns only one argument or throws an error. An error is thrown |
| 4103 | * only if an error is encountered during the argument parsing. If |
| 4104 | * the "sample-fetch" function fails, nil is returned. |
| 4105 | */ |
| 4106 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4107 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4108 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4109 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4110 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4111 | int i; |
| 4112 | struct sample smp; |
| 4113 | |
| 4114 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4115 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4116 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4117 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4118 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4119 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4120 | /* Check execution authorization. */ |
| 4121 | if (f->use & SMP_USE_HTTP_ANY && |
| 4122 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4123 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4124 | "is not available in Lua services", f->kw); |
| 4125 | WILL_LJMP(lua_error(L)); |
| 4126 | } |
| 4127 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4128 | /* Get extra arguments. */ |
| 4129 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4130 | if (i >= ARGM_NBARGS) |
| 4131 | break; |
| 4132 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4133 | } |
| 4134 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4135 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4136 | |
| 4137 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4138 | MAY_LJMP(hlua_lua2arg_check(L, 2, args, f->arg_mask, hsmp->p)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4139 | |
| 4140 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4141 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4142 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4143 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4144 | } |
| 4145 | |
| 4146 | /* Initialise the sample. */ |
| 4147 | memset(&smp, 0, sizeof(smp)); |
| 4148 | |
| 4149 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4150 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 4151 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4152 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4153 | lua_pushstring(L, ""); |
| 4154 | else |
| 4155 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4156 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4157 | } |
| 4158 | |
| 4159 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4160 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4161 | hlua_smp2lua_str(L, &smp); |
| 4162 | else |
| 4163 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4164 | |
| 4165 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4166 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4167 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4168 | |
| 4169 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4170 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4171 | WILL_LJMP(lua_error(L)); |
| 4172 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4173 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4174 | |
| 4175 | /* |
| 4176 | * |
| 4177 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4178 | * Class Converters |
| 4179 | * |
| 4180 | * |
| 4181 | */ |
| 4182 | |
| 4183 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4184 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4185 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4186 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4187 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4188 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4189 | } |
| 4190 | |
| 4191 | /* This function creates and push in the stack a Converters object |
| 4192 | * according with a current TXN. |
| 4193 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4194 | static int hlua_converters_new(lua_State *L, struct hlua_txn *txn, unsigned int flags) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4195 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4196 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4197 | |
| 4198 | /* Check stack size. */ |
| 4199 | if (!lua_checkstack(L, 3)) |
| 4200 | return 0; |
| 4201 | |
| 4202 | /* Create the object: obj[0] = userdata. |
| 4203 | * Note that the base of the Converters object is the |
| 4204 | * same than the TXN object. |
| 4205 | */ |
| 4206 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4207 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4208 | lua_rawseti(L, -2, 0); |
| 4209 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4210 | hsmp->s = txn->s; |
| 4211 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4212 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4213 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4214 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4215 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4216 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4217 | lua_setmetatable(L, -2); |
| 4218 | |
| 4219 | return 1; |
| 4220 | } |
| 4221 | |
| 4222 | /* This function is an LUA binding. It is called with each converter. |
| 4223 | * It uses closure argument to store the associated converter. It |
| 4224 | * returns only one argument or throws an error. An error is thrown |
| 4225 | * only if an error is encountered during the argument parsing. If |
| 4226 | * the converter function function fails, nil is returned. |
| 4227 | */ |
| 4228 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4229 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4230 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4231 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4232 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4233 | int i; |
| 4234 | struct sample smp; |
| 4235 | |
| 4236 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4237 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4238 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4239 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4240 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4241 | |
| 4242 | /* Get extra arguments. */ |
| 4243 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4244 | if (i >= ARGM_NBARGS) |
| 4245 | break; |
| 4246 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4247 | } |
| 4248 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4249 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4250 | |
| 4251 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4252 | MAY_LJMP(hlua_lua2arg_check(L, 3, args, conv->arg_mask, hsmp->p)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4253 | |
| 4254 | /* Run the special args checker. */ |
| 4255 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4256 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4257 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4261 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4262 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4263 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4264 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4265 | } |
| 4266 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4267 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4268 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4269 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4270 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4271 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4272 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4273 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4274 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4275 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4276 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4277 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4278 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4279 | } |
| 4280 | |
| 4281 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4282 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4283 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4284 | lua_pushstring(L, ""); |
| 4285 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4286 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4287 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
| 4290 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4291 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4292 | hlua_smp2lua_str(L, &smp); |
| 4293 | else |
| 4294 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4295 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4296 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4297 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4298 | |
| 4299 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4300 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4301 | WILL_LJMP(lua_error(L)); |
| 4302 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4303 | } |
| 4304 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4305 | /* |
| 4306 | * |
| 4307 | * |
| 4308 | * Class AppletTCP |
| 4309 | * |
| 4310 | * |
| 4311 | */ |
| 4312 | |
| 4313 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4314 | * a class stream, otherwise it throws an error. |
| 4315 | */ |
| 4316 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4317 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4318 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4319 | } |
| 4320 | |
| 4321 | /* This function creates and push in the stack an Applet object |
| 4322 | * according with a current TXN. |
| 4323 | */ |
| 4324 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4325 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4326 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4327 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4328 | struct proxy *p; |
| 4329 | |
| 4330 | ALREADY_CHECKED(s); |
| 4331 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4332 | |
| 4333 | /* Check stack size. */ |
| 4334 | if (!lua_checkstack(L, 3)) |
| 4335 | return 0; |
| 4336 | |
| 4337 | /* Create the object: obj[0] = userdata. |
| 4338 | * Note that the base of the Converters object is the |
| 4339 | * same than the TXN object. |
| 4340 | */ |
| 4341 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4342 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4343 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4344 | luactx->appctx = ctx; |
| 4345 | luactx->htxn.s = s; |
| 4346 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4347 | |
| 4348 | /* Create the "f" field that contains a list of fetches. */ |
| 4349 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4350 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4351 | return 0; |
| 4352 | lua_settable(L, -3); |
| 4353 | |
| 4354 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4355 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4356 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4357 | return 0; |
| 4358 | lua_settable(L, -3); |
| 4359 | |
| 4360 | /* Create the "c" field that contains a list of converters. */ |
| 4361 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4362 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4363 | return 0; |
| 4364 | lua_settable(L, -3); |
| 4365 | |
| 4366 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4367 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4368 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4369 | return 0; |
| 4370 | lua_settable(L, -3); |
| 4371 | |
| 4372 | /* Pop a class stream metatable and affect it to the table. */ |
| 4373 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4374 | lua_setmetatable(L, -2); |
| 4375 | |
| 4376 | return 1; |
| 4377 | } |
| 4378 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4379 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4380 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4381 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4382 | struct stream *s; |
| 4383 | const char *name; |
| 4384 | size_t len; |
| 4385 | struct sample smp; |
| 4386 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4387 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4388 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4389 | |
| 4390 | /* It is useles to retrieve the stream, but this function |
| 4391 | * runs only in a stream context. |
| 4392 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4393 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4394 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4395 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4396 | |
| 4397 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4398 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4399 | hlua_lua2smp(L, 3, &smp); |
| 4400 | |
| 4401 | /* Store the sample in a variable. */ |
| 4402 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4403 | |
| 4404 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4405 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4406 | else |
| 4407 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4408 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4409 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4410 | } |
| 4411 | |
| 4412 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4413 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4414 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4415 | struct stream *s; |
| 4416 | const char *name; |
| 4417 | size_t len; |
| 4418 | struct sample smp; |
| 4419 | |
| 4420 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4421 | |
| 4422 | /* It is useles to retrieve the stream, but this function |
| 4423 | * runs only in a stream context. |
| 4424 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4425 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4426 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4427 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4428 | |
| 4429 | /* Unset the variable. */ |
| 4430 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4431 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4432 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4433 | } |
| 4434 | |
| 4435 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4436 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4437 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4438 | struct stream *s; |
| 4439 | const char *name; |
| 4440 | size_t len; |
| 4441 | struct sample smp; |
| 4442 | |
| 4443 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4444 | |
| 4445 | /* It is useles to retrieve the stream, but this function |
| 4446 | * runs only in a stream context. |
| 4447 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4448 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4449 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4450 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4451 | |
| 4452 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4453 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4454 | lua_pushnil(L); |
| 4455 | return 1; |
| 4456 | } |
| 4457 | |
| 4458 | return hlua_smp2lua(L, &smp); |
| 4459 | } |
| 4460 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4461 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4462 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4463 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4464 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4465 | struct hlua *hlua; |
| 4466 | |
| 4467 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4468 | if (!s->hlua) |
| 4469 | return 0; |
| 4470 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4471 | |
| 4472 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4473 | |
| 4474 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4475 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4476 | |
| 4477 | /* Get and store new value. */ |
| 4478 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4479 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4480 | |
| 4481 | return 0; |
| 4482 | } |
| 4483 | |
| 4484 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4485 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4486 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4487 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4488 | struct hlua *hlua; |
| 4489 | |
| 4490 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4491 | if (!s->hlua) { |
| 4492 | lua_pushnil(L); |
| 4493 | return 1; |
| 4494 | } |
| 4495 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4496 | |
| 4497 | /* Push configuration index in the stack. */ |
| 4498 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4499 | |
| 4500 | return 1; |
| 4501 | } |
| 4502 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4503 | /* If expected data not yet available, it returns a yield. This function |
| 4504 | * consumes the data in the buffer. It returns a string containing the |
| 4505 | * data. This string can be empty. |
| 4506 | */ |
| 4507 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4508 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4509 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4510 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4511 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4512 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4513 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4514 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4515 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4516 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4517 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4518 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4519 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4520 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4521 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4522 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4523 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_getline_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4524 | } |
| 4525 | |
| 4526 | /* End of data: commit the total strings and return. */ |
| 4527 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4528 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4529 | return 1; |
| 4530 | } |
| 4531 | |
| 4532 | /* Ensure that the block 2 length is usable. */ |
| 4533 | if (ret == 1) |
| 4534 | len2 = 0; |
| 4535 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4536 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4537 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4538 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4539 | |
| 4540 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4541 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4542 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4543 | return 1; |
| 4544 | } |
| 4545 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4546 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4547 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4548 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4549 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4550 | |
| 4551 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4552 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4553 | |
| 4554 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4555 | } |
| 4556 | |
| 4557 | /* If expected data not yet available, it returns a yield. This function |
| 4558 | * consumes the data in the buffer. It returns a string containing the |
| 4559 | * data. This string can be empty. |
| 4560 | */ |
| 4561 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4562 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4563 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4564 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4565 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4567 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4568 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4569 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4570 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4571 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4572 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4573 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4574 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4575 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4577 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4578 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4579 | } |
| 4580 | |
| 4581 | /* End of data: commit the total strings and return. */ |
| 4582 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4583 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4584 | return 1; |
| 4585 | } |
| 4586 | |
| 4587 | /* Ensure that the block 2 length is usable. */ |
| 4588 | if (ret == 1) |
| 4589 | len2 = 0; |
| 4590 | |
| 4591 | if (len == -1) { |
| 4592 | |
| 4593 | /* If len == -1, catenate all the data avalaile and |
| 4594 | * yield because we want to get all the data until |
| 4595 | * the end of data stream. |
| 4596 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4597 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4598 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4599 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4600 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4601 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4602 | |
| 4603 | } else { |
| 4604 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4605 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4606 | if (len1 > len) |
| 4607 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4608 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4609 | len -= len1; |
| 4610 | |
| 4611 | /* Copy the second block. */ |
| 4612 | if (len2 > len) |
| 4613 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4614 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4615 | len -= len2; |
| 4616 | |
| 4617 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4618 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4619 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4620 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4621 | if (len > 0) { |
| 4622 | lua_pushinteger(L, len); |
| 4623 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4624 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4625 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4626 | } |
| 4627 | |
| 4628 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4629 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4630 | return 1; |
| 4631 | } |
| 4632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4633 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4634 | hlua_pusherror(L, "Lua: internal error"); |
| 4635 | WILL_LJMP(lua_error(L)); |
| 4636 | return 0; |
| 4637 | } |
| 4638 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4639 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4640 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4641 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4642 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4643 | int len = -1; |
| 4644 | |
| 4645 | if (lua_gettop(L) > 2) |
| 4646 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4647 | if (lua_gettop(L) >= 2) { |
| 4648 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4649 | lua_pop(L, 1); |
| 4650 | } |
| 4651 | |
| 4652 | /* Confirm or set the required length */ |
| 4653 | lua_pushinteger(L, len); |
| 4654 | |
| 4655 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4656 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4657 | |
| 4658 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4659 | } |
| 4660 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4661 | /* Append data in the output side of the buffer. This data is immediately |
| 4662 | * sent. The function returns the amount of data written. If the buffer |
| 4663 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4664 | * if the channel is closed. |
| 4665 | */ |
| 4666 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4667 | { |
| 4668 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4669 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4670 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4671 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4672 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4673 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4674 | int max; |
| 4675 | |
| 4676 | /* Get the max amount of data which can write as input in the channel. */ |
| 4677 | max = channel_recv_max(chn); |
| 4678 | if (max > (len - l)) |
| 4679 | max = len - l; |
| 4680 | |
| 4681 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4682 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4683 | |
| 4684 | /* update counters. */ |
| 4685 | l += max; |
| 4686 | lua_pop(L, 1); |
| 4687 | lua_pushinteger(L, l); |
| 4688 | |
| 4689 | /* If some data is not send, declares the situation to the |
| 4690 | * applet, and returns a yield. |
| 4691 | */ |
| 4692 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4693 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4694 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_tcp_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4695 | } |
| 4696 | |
| 4697 | return 1; |
| 4698 | } |
| 4699 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4700 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4701 | * yield the LUA process, and resume it without checking the |
| 4702 | * input arguments. |
| 4703 | */ |
| 4704 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4705 | { |
| 4706 | MAY_LJMP(check_args(L, 2, "send")); |
| 4707 | lua_pushinteger(L, 0); |
| 4708 | |
| 4709 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4710 | } |
| 4711 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4712 | /* |
| 4713 | * |
| 4714 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4716 | * |
| 4717 | * |
| 4718 | */ |
| 4719 | |
| 4720 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4721 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4722 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4723 | __LJMP static struct hlua_appctx *hlua_checkapplet_http(lua_State *L, int ud) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4724 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4725 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4726 | } |
| 4727 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4728 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4729 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4730 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4731 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4732 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4733 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4735 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4736 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4737 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4738 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4740 | struct htx *htx; |
| 4741 | struct htx_blk *blk; |
| 4742 | struct htx_sl *sl; |
| 4743 | struct ist path; |
| 4744 | unsigned long long len = 0; |
| 4745 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4746 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4747 | |
| 4748 | /* Check stack size. */ |
| 4749 | if (!lua_checkstack(L, 3)) |
| 4750 | return 0; |
| 4751 | |
| 4752 | /* Create the object: obj[0] = userdata. |
| 4753 | * Note that the base of the Converters object is the |
| 4754 | * same than the TXN object. |
| 4755 | */ |
| 4756 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4757 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4758 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4759 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4760 | http_ctx->status = 200; /* Default status code returned. */ |
| 4761 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luactx->htxn.s = s; |
| 4763 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4764 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | /* Create the "f" field that contains a list of fetches. */ |
| 4766 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | return 0; |
| 4769 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4770 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4771 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4772 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4773 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4774 | return 0; |
| 4775 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4776 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4777 | /* Create the "c" field that contains a list of converters. */ |
| 4778 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4779 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | return 0; |
| 4781 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4782 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4783 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4784 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4785 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4786 | return 0; |
| 4787 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4788 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4789 | htx = htxbuf(&s->req.buf); |
| 4790 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4791 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4792 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4793 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | /* Stores the request method. */ |
| 4795 | lua_pushstring(L, "method"); |
| 4796 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4797 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4798 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4799 | /* Stores the http version. */ |
| 4800 | lua_pushstring(L, "version"); |
| 4801 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4802 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4803 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4805 | * the array on the top of the stack. |
| 4806 | */ |
| 4807 | lua_pushstring(L, "headers"); |
| 4808 | htxn.s = s; |
| 4809 | htxn.p = px; |
| 4810 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4811 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4812 | return 0; |
| 4813 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4814 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4815 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4816 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4817 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4818 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4819 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4820 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4821 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4822 | q = p; |
| 4823 | while (q < end && *q != '?') |
| 4824 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4825 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4826 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4827 | lua_pushstring(L, "path"); |
| 4828 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4829 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4830 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4831 | /* Stores the query string. */ |
| 4832 | lua_pushstring(L, "qs"); |
| 4833 | if (*q == '?') |
| 4834 | q++; |
| 4835 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4836 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4837 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4838 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4839 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4840 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4841 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4842 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4843 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | break; |
| 4845 | if (type == HTX_BLK_DATA) |
| 4846 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4847 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4848 | if (htx->extra != ULLONG_MAX) |
| 4849 | len += htx->extra; |
| 4850 | |
| 4851 | /* Stores the request path. */ |
| 4852 | lua_pushstring(L, "length"); |
| 4853 | lua_pushinteger(L, len); |
| 4854 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4855 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4856 | /* Create an empty array of HTTP request headers. */ |
| 4857 | lua_pushstring(L, "response"); |
| 4858 | lua_newtable(L); |
| 4859 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4861 | /* Pop a class stream metatable and affect it to the table. */ |
| 4862 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4863 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4864 | |
| 4865 | return 1; |
| 4866 | } |
| 4867 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4868 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4869 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4870 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4871 | struct stream *s; |
| 4872 | const char *name; |
| 4873 | size_t len; |
| 4874 | struct sample smp; |
| 4875 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4876 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4877 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4878 | |
| 4879 | /* It is useles to retrieve the stream, but this function |
| 4880 | * runs only in a stream context. |
| 4881 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4882 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4883 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4884 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4885 | |
| 4886 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4887 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4888 | hlua_lua2smp(L, 3, &smp); |
| 4889 | |
| 4890 | /* Store the sample in a variable. */ |
| 4891 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4892 | |
| 4893 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4894 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4895 | else |
| 4896 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4897 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4898 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4899 | } |
| 4900 | |
| 4901 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4902 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4903 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4904 | struct stream *s; |
| 4905 | const char *name; |
| 4906 | size_t len; |
| 4907 | struct sample smp; |
| 4908 | |
| 4909 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4910 | |
| 4911 | /* It is useles to retrieve the stream, but this function |
| 4912 | * runs only in a stream context. |
| 4913 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4914 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4915 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4916 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4917 | |
| 4918 | /* Unset the variable. */ |
| 4919 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4920 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4921 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4922 | } |
| 4923 | |
| 4924 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4925 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4926 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4927 | struct stream *s; |
| 4928 | const char *name; |
| 4929 | size_t len; |
| 4930 | struct sample smp; |
| 4931 | |
| 4932 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4933 | |
| 4934 | /* It is useles to retrieve the stream, but this function |
| 4935 | * runs only in a stream context. |
| 4936 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4937 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4938 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4939 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4940 | |
| 4941 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4942 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4943 | lua_pushnil(L); |
| 4944 | return 1; |
| 4945 | } |
| 4946 | |
| 4947 | return hlua_smp2lua(L, &smp); |
| 4948 | } |
| 4949 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4950 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4951 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4952 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4953 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4954 | struct hlua *hlua; |
| 4955 | |
| 4956 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4957 | if (!s->hlua) |
| 4958 | return 0; |
| 4959 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4960 | |
| 4961 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4962 | |
| 4963 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4964 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4965 | |
| 4966 | /* Get and store new value. */ |
| 4967 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4968 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4969 | |
| 4970 | return 0; |
| 4971 | } |
| 4972 | |
| 4973 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4974 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4975 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4976 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4977 | struct hlua *hlua; |
| 4978 | |
| 4979 | /* Note that this hlua struct is from the session and not from the applet. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 4980 | if (!s->hlua) { |
| 4981 | lua_pushnil(L); |
| 4982 | return 1; |
| 4983 | } |
| 4984 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4985 | |
| 4986 | /* Push configuration index in the stack. */ |
| 4987 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4988 | |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4992 | /* If expected data not yet available, it returns a yield. This function |
| 4993 | * consumes the data in the buffer. It returns a string containing the |
| 4994 | * data. This string can be empty. |
| 4995 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4996 | __LJMP static int hlua_applet_http_getline_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4997 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4998 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4999 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5000 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5001 | struct htx *htx; |
| 5002 | struct htx_blk *blk; |
| 5003 | size_t count; |
| 5004 | int stop = 0; |
| 5005 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5006 | htx = htx_from_buf(&req->buf); |
| 5007 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5008 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5009 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5010 | while (count && !stop && blk) { |
| 5011 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5012 | uint32_t sz = htx_get_blksz(blk); |
| 5013 | struct ist v; |
| 5014 | uint32_t vlen; |
| 5015 | char *nl; |
| 5016 | |
| 5017 | vlen = sz; |
| 5018 | if (vlen > count) { |
| 5019 | if (type != HTX_BLK_DATA) |
| 5020 | break; |
| 5021 | vlen = count; |
| 5022 | } |
| 5023 | |
| 5024 | switch (type) { |
| 5025 | case HTX_BLK_UNUSED: |
| 5026 | break; |
| 5027 | |
| 5028 | case HTX_BLK_DATA: |
| 5029 | v = htx_get_blk_value(htx, blk); |
| 5030 | v.len = vlen; |
| 5031 | nl = istchr(v, '\n'); |
| 5032 | if (nl != NULL) { |
| 5033 | stop = 1; |
| 5034 | vlen = nl - v.ptr + 1; |
| 5035 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5036 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5037 | break; |
| 5038 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5039 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5040 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5041 | stop = 1; |
| 5042 | break; |
| 5043 | |
| 5044 | default: |
| 5045 | break; |
| 5046 | } |
| 5047 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5048 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5049 | count -= vlen; |
| 5050 | if (sz == vlen) |
| 5051 | blk = htx_remove_blk(htx, blk); |
| 5052 | else { |
| 5053 | htx_cut_data_blk(htx, blk, vlen); |
| 5054 | break; |
| 5055 | } |
| 5056 | } |
| 5057 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5058 | /* The message was fully consumed and no more data are expected |
| 5059 | * (EOM flag set). |
| 5060 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5061 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5062 | stop = 1; |
| 5063 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5064 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5065 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5066 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5067 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_getline_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5068 | } |
| 5069 | |
| 5070 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5071 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5072 | return 1; |
| 5073 | } |
| 5074 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5075 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5076 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5077 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5078 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5079 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5080 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5081 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5082 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5083 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5084 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | } |
| 5086 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5087 | /* If expected data not yet available, it returns a yield. This function |
| 5088 | * consumes the data in the buffer. It returns a string containing the |
| 5089 | * data. This string can be empty. |
| 5090 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5091 | __LJMP static int hlua_applet_http_recv_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5092 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5093 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5094 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5095 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5096 | struct htx *htx; |
| 5097 | struct htx_blk *blk; |
| 5098 | size_t count; |
| 5099 | int len; |
| 5100 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5101 | htx = htx_from_buf(&req->buf); |
| 5102 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5103 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5104 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5105 | while (count && len && blk) { |
| 5106 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5107 | uint32_t sz = htx_get_blksz(blk); |
| 5108 | struct ist v; |
| 5109 | uint32_t vlen; |
| 5110 | |
| 5111 | vlen = sz; |
| 5112 | if (len > 0 && vlen > len) |
| 5113 | vlen = len; |
| 5114 | if (vlen > count) { |
| 5115 | if (type != HTX_BLK_DATA) |
| 5116 | break; |
| 5117 | vlen = count; |
| 5118 | } |
| 5119 | |
| 5120 | switch (type) { |
| 5121 | case HTX_BLK_UNUSED: |
| 5122 | break; |
| 5123 | |
| 5124 | case HTX_BLK_DATA: |
| 5125 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5126 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5127 | break; |
| 5128 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5129 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5130 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5131 | len = 0; |
| 5132 | break; |
| 5133 | |
| 5134 | default: |
| 5135 | break; |
| 5136 | } |
| 5137 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5138 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5139 | count -= vlen; |
| 5140 | if (len > 0) |
| 5141 | len -= vlen; |
| 5142 | if (sz == vlen) |
| 5143 | blk = htx_remove_blk(htx, blk); |
| 5144 | else { |
| 5145 | htx_cut_data_blk(htx, blk, vlen); |
| 5146 | break; |
| 5147 | } |
| 5148 | } |
| 5149 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5150 | /* The message was fully consumed and no more data are expected |
| 5151 | * (EOM flag set). |
| 5152 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5153 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5154 | len = 0; |
| 5155 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5156 | htx_to_buf(htx, &req->buf); |
| 5157 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5158 | /* If we are no other data available, yield waiting for new data. */ |
| 5159 | if (len) { |
| 5160 | if (len > 0) { |
| 5161 | lua_pushinteger(L, len); |
| 5162 | lua_replace(L, 2); |
| 5163 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5164 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5165 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_recv_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5166 | } |
| 5167 | |
| 5168 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5169 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5170 | return 1; |
| 5171 | } |
| 5172 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5173 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5174 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5175 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5176 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5177 | int len = -1; |
| 5178 | |
| 5179 | /* Check arguments. */ |
| 5180 | if (lua_gettop(L) > 2) |
| 5181 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5182 | if (lua_gettop(L) >= 2) { |
| 5183 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5184 | lua_pop(L, 1); |
| 5185 | } |
| 5186 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5187 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5188 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5189 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5190 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5191 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5192 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5193 | } |
| 5194 | |
| 5195 | /* Append data in the output side of the buffer. This data is immediately |
| 5196 | * sent. The function returns the amount of data written. If the buffer |
| 5197 | * cannot contain the data, the function yields. The function returns -1 |
| 5198 | * if the channel is closed. |
| 5199 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5200 | __LJMP static int hlua_applet_http_send_yield(lua_State *L, int status, lua_KContext ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5201 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5202 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5203 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5204 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5205 | struct htx *htx = htx_from_buf(&res->buf); |
| 5206 | const char *data; |
| 5207 | size_t len; |
| 5208 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5209 | int max; |
| 5210 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5211 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5212 | if (!max) |
| 5213 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5214 | |
| 5215 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5216 | |
| 5217 | /* Get the max amount of data which can write as input in the channel. */ |
| 5218 | if (max > (len - l)) |
| 5219 | max = len - l; |
| 5220 | |
| 5221 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5222 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5223 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5224 | |
| 5225 | /* update counters. */ |
| 5226 | l += max; |
| 5227 | lua_pop(L, 1); |
| 5228 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5229 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5230 | /* If some data is not send, declares the situation to the |
| 5231 | * applet, and returns a yield. |
| 5232 | */ |
| 5233 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5234 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5235 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5236 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5237 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_applet_http_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5238 | } |
| 5239 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5240 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5241 | return 1; |
| 5242 | } |
| 5243 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5244 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5245 | * yield the LUA process, and resume it without checking the |
| 5246 | * input arguments. |
| 5247 | */ |
| 5248 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5249 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5250 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5251 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | |
| 5253 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5254 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5255 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5256 | WILL_LJMP(lua_error(L)); |
| 5257 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5258 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5259 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5260 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5261 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5262 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5263 | } |
| 5264 | |
| 5265 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5266 | { |
| 5267 | const char *name; |
| 5268 | int ret; |
| 5269 | |
| 5270 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5271 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5272 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5273 | |
| 5274 | /* Push in the stack the "response" entry. */ |
| 5275 | ret = lua_getfield(L, 1, "response"); |
| 5276 | if (ret != LUA_TTABLE) { |
| 5277 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5278 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5279 | WILL_LJMP(lua_error(L)); |
| 5280 | } |
| 5281 | |
| 5282 | /* check if the header is already registered if it is not |
| 5283 | * the case, register it. |
| 5284 | */ |
| 5285 | ret = lua_getfield(L, -1, name); |
| 5286 | if (ret == LUA_TNIL) { |
| 5287 | |
| 5288 | /* Entry not found. */ |
| 5289 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5290 | |
| 5291 | /* Insert the new header name in the array in the top of the stack. |
| 5292 | * It left the new array in the top of the stack. |
| 5293 | */ |
| 5294 | lua_newtable(L); |
| 5295 | lua_pushvalue(L, 2); |
| 5296 | lua_pushvalue(L, -2); |
| 5297 | lua_settable(L, -4); |
| 5298 | |
| 5299 | } else if (ret != LUA_TTABLE) { |
| 5300 | |
| 5301 | /* corruption error. */ |
| 5302 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5303 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5304 | WILL_LJMP(lua_error(L)); |
| 5305 | } |
| 5306 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5307 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5308 | * the header value as new entry. |
| 5309 | */ |
| 5310 | lua_pushvalue(L, 3); |
| 5311 | ret = lua_rawlen(L, -2); |
| 5312 | lua_rawseti(L, -2, ret + 1); |
| 5313 | lua_pushboolean(L, 1); |
| 5314 | return 1; |
| 5315 | } |
| 5316 | |
| 5317 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5318 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5319 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5320 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5321 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5322 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5323 | |
| 5324 | if (status < 100 || status > 599) { |
| 5325 | lua_pushboolean(L, 0); |
| 5326 | return 1; |
| 5327 | } |
| 5328 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5329 | http_ctx->status = status; |
| 5330 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5331 | lua_pushboolean(L, 1); |
| 5332 | return 1; |
| 5333 | } |
| 5334 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5335 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5336 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5337 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5338 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5339 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5340 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5341 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5342 | struct htx *htx; |
| 5343 | struct htx_sl *sl; |
| 5344 | struct h1m h1m; |
| 5345 | const char *status, *reason; |
| 5346 | const char *name, *value; |
| 5347 | size_t nlen, vlen; |
| 5348 | unsigned int flags; |
| 5349 | |
| 5350 | /* Send the message at once. */ |
| 5351 | htx = htx_from_buf(&res->buf); |
| 5352 | h1m_init_res(&h1m); |
| 5353 | |
| 5354 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5355 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5356 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5357 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5358 | reason = http_get_reason(http_ctx->status); |
| 5359 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5360 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5361 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5362 | } |
| 5363 | else { |
| 5364 | flags = HTX_SL_F_IS_RESP; |
| 5365 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5366 | } |
| 5367 | if (!sl) { |
| 5368 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5369 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5370 | WILL_LJMP(lua_error(L)); |
| 5371 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5372 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5373 | |
| 5374 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Aurelien DARRAGON | cd341d5 | 2022-09-29 12:00:04 +0200 | [diff] [blame] | 5375 | /* FIXME: according to Lua doc, 0 is considered to be invalid index? */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5376 | lua_pushvalue(L, 0); |
| 5377 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5378 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5379 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5380 | WILL_LJMP(lua_error(L)); |
| 5381 | } |
| 5382 | |
| 5383 | /* Browse the list of headers. */ |
| 5384 | lua_pushnil(L); |
| 5385 | while(lua_next(L, -2) != 0) { |
| 5386 | /* We expect a string as -2. */ |
| 5387 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5388 | 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] | 5389 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5390 | lua_typename(L, lua_type(L, -2))); |
| 5391 | WILL_LJMP(lua_error(L)); |
| 5392 | } |
| 5393 | name = lua_tolstring(L, -2, &nlen); |
| 5394 | |
| 5395 | /* We expect an array as -1. */ |
| 5396 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5397 | 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] | 5398 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5399 | name, |
| 5400 | lua_typename(L, lua_type(L, -1))); |
| 5401 | WILL_LJMP(lua_error(L)); |
| 5402 | } |
| 5403 | |
| 5404 | /* Browse the table who is on the top of the stack. */ |
| 5405 | lua_pushnil(L); |
| 5406 | while(lua_next(L, -2) != 0) { |
| 5407 | int id; |
| 5408 | |
| 5409 | /* We expect a number as -2. */ |
| 5410 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5411 | 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] | 5412 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5413 | name, |
| 5414 | lua_typename(L, lua_type(L, -2))); |
| 5415 | WILL_LJMP(lua_error(L)); |
| 5416 | } |
| 5417 | id = lua_tointeger(L, -2); |
| 5418 | |
| 5419 | /* We expect a string as -2. */ |
| 5420 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5421 | 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] | 5422 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5423 | name, id, |
| 5424 | lua_typename(L, lua_type(L, -1))); |
| 5425 | WILL_LJMP(lua_error(L)); |
| 5426 | } |
| 5427 | value = lua_tolstring(L, -1, &vlen); |
| 5428 | |
| 5429 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5430 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5431 | int ret; |
| 5432 | |
| 5433 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5434 | if (ret < 0) { |
| 5435 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5436 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5437 | name); |
| 5438 | WILL_LJMP(lua_error(L)); |
| 5439 | } |
| 5440 | else if (ret == 0) |
| 5441 | goto next; /* Skip it */ |
| 5442 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5443 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5444 | struct ist v = ist2(value, vlen); |
| 5445 | int ret; |
| 5446 | |
| 5447 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5448 | if (ret < 0) { |
| 5449 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5450 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5451 | name); |
| 5452 | WILL_LJMP(lua_error(L)); |
| 5453 | } |
| 5454 | else if (ret == 0) |
| 5455 | goto next; /* Skip it */ |
| 5456 | } |
| 5457 | |
| 5458 | /* Add a new header */ |
| 5459 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5460 | 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] | 5461 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5462 | name); |
| 5463 | WILL_LJMP(lua_error(L)); |
| 5464 | } |
| 5465 | next: |
| 5466 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5467 | lua_pop(L, 1); |
| 5468 | } |
| 5469 | |
| 5470 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5471 | lua_pop(L, 1); |
| 5472 | } |
| 5473 | |
| 5474 | if (h1m.flags & H1_MF_CHNK) |
| 5475 | h1m.flags &= ~H1_MF_CLEN; |
| 5476 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5477 | h1m.flags |= H1_MF_XFER_LEN; |
| 5478 | |
| 5479 | /* Uset HTX start-line flags */ |
| 5480 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5481 | flags |= HTX_SL_F_XFER_ENC; |
| 5482 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5483 | flags |= HTX_SL_F_XFER_LEN; |
| 5484 | if (h1m.flags & H1_MF_CHNK) |
| 5485 | flags |= HTX_SL_F_CHNK; |
| 5486 | else if (h1m.flags & H1_MF_CLEN) |
| 5487 | flags |= HTX_SL_F_CLEN; |
| 5488 | if (h1m.body_len == 0) |
| 5489 | flags |= HTX_SL_F_BODYLESS; |
| 5490 | } |
| 5491 | sl->flags |= flags; |
| 5492 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5493 | /* 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] | 5494 | * and the status code implies the presence of a message body, we must |
| 5495 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5496 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5497 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5498 | */ |
| 5499 | 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] | 5500 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5501 | /* Add a new header */ |
| 5502 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5503 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5504 | 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] | 5505 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5506 | WILL_LJMP(lua_error(L)); |
| 5507 | } |
| 5508 | } |
| 5509 | |
| 5510 | /* Finalize headers. */ |
| 5511 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5512 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5513 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5514 | WILL_LJMP(lua_error(L)); |
| 5515 | } |
| 5516 | |
| 5517 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5518 | b_reset(&res->buf); |
| 5519 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5520 | WILL_LJMP(lua_error(L)); |
| 5521 | } |
| 5522 | |
| 5523 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5524 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5525 | |
| 5526 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5527 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5528 | return 0; |
| 5529 | |
| 5530 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5531 | /* We will build the status line and the headers of the HTTP response. |
| 5532 | * We will try send at once if its not possible, we give back the hand |
| 5533 | * waiting for more room. |
| 5534 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5535 | __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] | 5536 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5537 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5538 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5539 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5540 | |
| 5541 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5542 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5543 | 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] | 5544 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5545 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5546 | } |
| 5547 | |
| 5548 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5549 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5550 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5551 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5552 | } |
| 5553 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5554 | /* |
| 5555 | * |
| 5556 | * |
| 5557 | * Class HTTP |
| 5558 | * |
| 5559 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5560 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5561 | |
| 5562 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5563 | * a class stream, otherwise it throws an error. |
| 5564 | */ |
| 5565 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5566 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5567 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5568 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5569 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5570 | /* This function creates and push in the stack a HTTP object |
| 5571 | * according with a current TXN. |
| 5572 | */ |
| 5573 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5574 | { |
| 5575 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5576 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5577 | /* Check stack size. */ |
| 5578 | if (!lua_checkstack(L, 3)) |
| 5579 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5580 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5581 | /* Create the object: obj[0] = userdata. |
| 5582 | * Note that the base of the Converters object is the |
| 5583 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5584 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5585 | lua_newtable(L); |
| 5586 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5587 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5588 | |
| 5589 | htxn->s = txn->s; |
| 5590 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5591 | htxn->dir = txn->dir; |
| 5592 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5593 | |
| 5594 | /* Pop a class stream metatable and affect it to the table. */ |
| 5595 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5596 | lua_setmetatable(L, -2); |
| 5597 | |
| 5598 | return 1; |
| 5599 | } |
| 5600 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5601 | /* This function creates and returns an array containing the status-line |
| 5602 | * elements. This function does not fails. |
| 5603 | */ |
| 5604 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5605 | { |
| 5606 | /* Create the table. */ |
| 5607 | lua_newtable(L); |
| 5608 | |
| 5609 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5610 | lua_pushstring(L, "version"); |
| 5611 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5612 | lua_settable(L, -3); |
| 5613 | lua_pushstring(L, "code"); |
| 5614 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5615 | lua_settable(L, -3); |
| 5616 | lua_pushstring(L, "reason"); |
| 5617 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5618 | lua_settable(L, -3); |
| 5619 | } |
| 5620 | else { |
| 5621 | lua_pushstring(L, "method"); |
| 5622 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5623 | lua_settable(L, -3); |
| 5624 | lua_pushstring(L, "uri"); |
| 5625 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5626 | lua_settable(L, -3); |
| 5627 | lua_pushstring(L, "version"); |
| 5628 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5629 | lua_settable(L, -3); |
| 5630 | } |
| 5631 | return 1; |
| 5632 | } |
| 5633 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5634 | /* This function creates ans returns an array of HTTP headers. |
| 5635 | * This function does not fails. It is used as wrapper with the |
| 5636 | * 2 following functions. |
| 5637 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5638 | __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] | 5639 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5640 | struct htx *htx; |
| 5641 | int32_t pos; |
| 5642 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5643 | /* Create the table. */ |
| 5644 | lua_newtable(L); |
| 5645 | |
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 | htx = htxbuf(&msg->chn->buf); |
| 5648 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5649 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5650 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5651 | struct ist n, v; |
| 5652 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5653 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5654 | if (type == HTX_BLK_HDR) { |
| 5655 | n = htx_get_blk_name(htx,blk); |
| 5656 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5657 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5658 | else if (type == HTX_BLK_EOH) |
| 5659 | break; |
| 5660 | else |
| 5661 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5662 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5663 | /* Check for existing entry: |
| 5664 | * assume that the table is on the top of the stack, and |
| 5665 | * push the key in the stack, the function lua_gettable() |
| 5666 | * perform the lookup. |
| 5667 | */ |
| 5668 | lua_pushlstring(L, n.ptr, n.len); |
| 5669 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5670 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5671 | switch (lua_type(L, -1)) { |
| 5672 | case LUA_TNIL: |
| 5673 | /* Table not found, create it. */ |
| 5674 | lua_pop(L, 1); /* remove the nil value. */ |
| 5675 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5676 | lua_newtable(L); /* create and push empty table. */ |
| 5677 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5678 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5679 | 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] | 5680 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5681 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5682 | case LUA_TTABLE: |
| 5683 | /* Entry found: push the value in the table. */ |
| 5684 | len = lua_rawlen(L, -1); |
| 5685 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5686 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5687 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5688 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5689 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5690 | default: |
| 5691 | /* Other cases are errors. */ |
| 5692 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5693 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5694 | } |
| 5695 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5696 | return 1; |
| 5697 | } |
| 5698 | |
| 5699 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5700 | { |
| 5701 | struct hlua_txn *htxn; |
| 5702 | |
| 5703 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5704 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5705 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5706 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5707 | WILL_LJMP(lua_error(L)); |
| 5708 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5709 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5710 | } |
| 5711 | |
| 5712 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5713 | { |
| 5714 | struct hlua_txn *htxn; |
| 5715 | |
| 5716 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5717 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5718 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5719 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5720 | WILL_LJMP(lua_error(L)); |
| 5721 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5722 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5723 | } |
| 5724 | |
| 5725 | /* This function replace full header, or just a value in |
| 5726 | * the request or in the response. It is a wrapper fir the |
| 5727 | * 4 following functions. |
| 5728 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5729 | __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] | 5730 | { |
| 5731 | size_t name_len; |
| 5732 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5733 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5734 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5735 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5736 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5737 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5738 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5739 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5740 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5741 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5742 | 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] | 5743 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5744 | return 0; |
| 5745 | } |
| 5746 | |
| 5747 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5748 | { |
| 5749 | struct hlua_txn *htxn; |
| 5750 | |
| 5751 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5752 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5753 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5754 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5755 | WILL_LJMP(lua_error(L)); |
| 5756 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5757 | 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] | 5758 | } |
| 5759 | |
| 5760 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5761 | { |
| 5762 | struct hlua_txn *htxn; |
| 5763 | |
| 5764 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5765 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5766 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5767 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5768 | WILL_LJMP(lua_error(L)); |
| 5769 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5770 | 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] | 5771 | } |
| 5772 | |
| 5773 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5774 | { |
| 5775 | struct hlua_txn *htxn; |
| 5776 | |
| 5777 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5778 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5779 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5780 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5781 | WILL_LJMP(lua_error(L)); |
| 5782 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5783 | 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] | 5784 | } |
| 5785 | |
| 5786 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5787 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5788 | struct hlua_txn *htxn; |
| 5789 | |
| 5790 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5791 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5792 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5793 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5794 | WILL_LJMP(lua_error(L)); |
| 5795 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5796 | 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] | 5797 | } |
| 5798 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5799 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5800 | * It is a wrapper for the 2 following functions. |
| 5801 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5802 | __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] | 5803 | { |
| 5804 | size_t len; |
| 5805 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5806 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5807 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5808 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5809 | ctx.blk = NULL; |
| 5810 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5811 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5812 | return 0; |
| 5813 | } |
| 5814 | |
| 5815 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5816 | { |
| 5817 | struct hlua_txn *htxn; |
| 5818 | |
| 5819 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5820 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5821 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5822 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5823 | WILL_LJMP(lua_error(L)); |
| 5824 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5825 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5826 | } |
| 5827 | |
| 5828 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5829 | { |
| 5830 | struct hlua_txn *htxn; |
| 5831 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5832 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5833 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5834 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5835 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5836 | WILL_LJMP(lua_error(L)); |
| 5837 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5838 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5839 | } |
| 5840 | |
| 5841 | /* This function adds an header. It is a wrapper used by |
| 5842 | * the 2 following functions. |
| 5843 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5844 | __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] | 5845 | { |
| 5846 | size_t name_len; |
| 5847 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5848 | size_t value_len; |
| 5849 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5850 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5851 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5852 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5853 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5854 | return 0; |
| 5855 | } |
| 5856 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5857 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5858 | { |
| 5859 | struct hlua_txn *htxn; |
| 5860 | |
| 5861 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5862 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5863 | |
| 5864 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5865 | WILL_LJMP(lua_error(L)); |
| 5866 | |
| 5867 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5868 | } |
| 5869 | |
| 5870 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5871 | { |
| 5872 | struct hlua_txn *htxn; |
| 5873 | |
| 5874 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5875 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5876 | |
| 5877 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5878 | WILL_LJMP(lua_error(L)); |
| 5879 | |
| 5880 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5881 | } |
| 5882 | |
| 5883 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5884 | { |
| 5885 | struct hlua_txn *htxn; |
| 5886 | |
| 5887 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5888 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5889 | |
| 5890 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5891 | WILL_LJMP(lua_error(L)); |
| 5892 | |
| 5893 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5894 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5895 | } |
| 5896 | |
| 5897 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5898 | { |
| 5899 | struct hlua_txn *htxn; |
| 5900 | |
| 5901 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5902 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5903 | |
| 5904 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5905 | WILL_LJMP(lua_error(L)); |
| 5906 | |
| 5907 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5908 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5909 | } |
| 5910 | |
| 5911 | /* This function set the method. */ |
| 5912 | static int hlua_http_req_set_meth(lua_State *L) |
| 5913 | { |
| 5914 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5915 | size_t name_len; |
| 5916 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5917 | |
| 5918 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5919 | WILL_LJMP(lua_error(L)); |
| 5920 | |
| 5921 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5922 | return 1; |
| 5923 | } |
| 5924 | |
| 5925 | /* This function set the method. */ |
| 5926 | static int hlua_http_req_set_path(lua_State *L) |
| 5927 | { |
| 5928 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5929 | size_t name_len; |
| 5930 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5931 | |
| 5932 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5933 | WILL_LJMP(lua_error(L)); |
| 5934 | |
| 5935 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5936 | return 1; |
| 5937 | } |
| 5938 | |
| 5939 | /* This function set the query-string. */ |
| 5940 | static int hlua_http_req_set_query(lua_State *L) |
| 5941 | { |
| 5942 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5943 | size_t name_len; |
| 5944 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5945 | |
| 5946 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5947 | WILL_LJMP(lua_error(L)); |
| 5948 | |
| 5949 | /* Check length. */ |
| 5950 | if (name_len > trash.size - 1) { |
| 5951 | lua_pushboolean(L, 0); |
| 5952 | return 1; |
| 5953 | } |
| 5954 | |
| 5955 | /* Add the mark question as prefix. */ |
| 5956 | chunk_reset(&trash); |
| 5957 | trash.area[trash.data++] = '?'; |
| 5958 | memcpy(trash.area + trash.data, name, name_len); |
| 5959 | trash.data += name_len; |
| 5960 | |
| 5961 | lua_pushboolean(L, |
| 5962 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5963 | return 1; |
| 5964 | } |
| 5965 | |
| 5966 | /* This function set the uri. */ |
| 5967 | static int hlua_http_req_set_uri(lua_State *L) |
| 5968 | { |
| 5969 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5970 | size_t name_len; |
| 5971 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5972 | |
| 5973 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5974 | WILL_LJMP(lua_error(L)); |
| 5975 | |
| 5976 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5977 | return 1; |
| 5978 | } |
| 5979 | |
| 5980 | /* This function set the response code & optionally reason. */ |
| 5981 | static int hlua_http_res_set_status(lua_State *L) |
| 5982 | { |
| 5983 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5984 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5985 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5986 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5987 | |
| 5988 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5989 | WILL_LJMP(lua_error(L)); |
| 5990 | |
| 5991 | http_res_set_status(code, reason, htxn->s); |
| 5992 | return 0; |
| 5993 | } |
| 5994 | |
| 5995 | /* |
| 5996 | * |
| 5997 | * |
| 5998 | * Class HTTPMessage |
| 5999 | * |
| 6000 | * |
| 6001 | */ |
| 6002 | |
| 6003 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6004 | * otherwise it throws an error. |
| 6005 | */ |
| 6006 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6007 | { |
| 6008 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6009 | } |
| 6010 | |
| 6011 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6012 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6013 | 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] | 6014 | { |
| 6015 | /* Check stack size. */ |
| 6016 | if (!lua_checkstack(L, 3)) |
| 6017 | return 0; |
| 6018 | |
| 6019 | lua_newtable(L); |
| 6020 | lua_pushlightuserdata(L, msg); |
| 6021 | lua_rawseti(L, -2, 0); |
| 6022 | |
| 6023 | /* Create the "channel" field that contains the request channel object. */ |
| 6024 | lua_pushstring(L, "channel"); |
| 6025 | if (!hlua_channel_new(L, msg->chn)) |
| 6026 | return 0; |
| 6027 | lua_rawset(L, -3); |
| 6028 | |
| 6029 | /* Pop a class stream metatable and affect it to the table. */ |
| 6030 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6031 | lua_setmetatable(L, -2); |
| 6032 | |
| 6033 | return 1; |
| 6034 | } |
| 6035 | |
| 6036 | /* Helper function returning a filter attached to the HTTP message at the |
| 6037 | * 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] | 6038 | * 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] | 6039 | * filled with output and input length respectively. |
| 6040 | */ |
| 6041 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6042 | { |
| 6043 | struct channel *chn = msg->chn; |
| 6044 | struct htx *htx = htxbuf(&chn->buf); |
| 6045 | struct filter *filter = NULL; |
| 6046 | |
| 6047 | *offset = co_data(msg->chn); |
| 6048 | *len = htx->data - co_data(msg->chn); |
| 6049 | |
| 6050 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6051 | filter = lua_touserdata (L, -1); |
| 6052 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6053 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6054 | |
| 6055 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6056 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6057 | } |
| 6058 | } |
| 6059 | |
| 6060 | lua_pop(L, 1); |
| 6061 | return filter; |
| 6062 | } |
| 6063 | |
| 6064 | /* Returns true if the channel attached to the HTTP message is the response |
| 6065 | * channel. |
| 6066 | */ |
| 6067 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6068 | { |
| 6069 | struct http_msg *msg; |
| 6070 | |
| 6071 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6072 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6073 | |
| 6074 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6075 | return 1; |
| 6076 | } |
| 6077 | |
| 6078 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6079 | * on hlua_http_get_stline(). |
| 6080 | */ |
| 6081 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6082 | { |
| 6083 | struct http_msg *msg; |
| 6084 | struct htx *htx; |
| 6085 | struct htx_sl *sl; |
| 6086 | |
| 6087 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6088 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6089 | |
| 6090 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6091 | WILL_LJMP(lua_error(L)); |
| 6092 | |
| 6093 | htx = htxbuf(&msg->chn->buf); |
| 6094 | sl = http_get_stline(htx); |
| 6095 | if (!sl) |
| 6096 | return 0; |
| 6097 | return hlua_http_get_stline(L, sl); |
| 6098 | } |
| 6099 | |
| 6100 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6101 | * hlua_http_get_headers(). |
| 6102 | */ |
| 6103 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6104 | { |
| 6105 | struct http_msg *msg; |
| 6106 | |
| 6107 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6108 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6109 | |
| 6110 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6111 | WILL_LJMP(lua_error(L)); |
| 6112 | |
| 6113 | return hlua_http_get_headers(L, msg); |
| 6114 | } |
| 6115 | |
| 6116 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6117 | * name. It relies on hlua_http_del_hdr(). |
| 6118 | */ |
| 6119 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6120 | { |
| 6121 | struct http_msg *msg; |
| 6122 | |
| 6123 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6124 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6125 | |
| 6126 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6127 | WILL_LJMP(lua_error(L)); |
| 6128 | |
| 6129 | return hlua_http_del_hdr(L, msg); |
| 6130 | } |
| 6131 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6132 | /* 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] | 6133 | * message given its name against a regex and replaces it if it matches. It |
| 6134 | * relies on hlua_http_rep_hdr(). |
| 6135 | */ |
| 6136 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6137 | { |
| 6138 | struct http_msg *msg; |
| 6139 | |
| 6140 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6141 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6142 | |
| 6143 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6144 | WILL_LJMP(lua_error(L)); |
| 6145 | |
| 6146 | return hlua_http_rep_hdr(L, msg, 1); |
| 6147 | } |
| 6148 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6149 | /* 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] | 6150 | * message given its name against a regex and replaces it if it matches. It |
| 6151 | * relies on hlua_http_rep_hdr(). |
| 6152 | */ |
| 6153 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6154 | { |
| 6155 | struct http_msg *msg; |
| 6156 | |
| 6157 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6158 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6159 | |
| 6160 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6161 | WILL_LJMP(lua_error(L)); |
| 6162 | |
| 6163 | return hlua_http_rep_hdr(L, msg, 0); |
| 6164 | } |
| 6165 | |
| 6166 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6167 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6168 | { |
| 6169 | struct http_msg *msg; |
| 6170 | |
| 6171 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6172 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6173 | |
| 6174 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6175 | WILL_LJMP(lua_error(L)); |
| 6176 | |
| 6177 | return hlua_http_add_hdr(L, msg); |
| 6178 | } |
| 6179 | |
| 6180 | /* Add an header in the HTTP message removing existing headers with the same |
| 6181 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6182 | */ |
| 6183 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6184 | { |
| 6185 | struct http_msg *msg; |
| 6186 | |
| 6187 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6188 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6189 | |
| 6190 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6191 | WILL_LJMP(lua_error(L)); |
| 6192 | |
| 6193 | hlua_http_del_hdr(L, msg); |
| 6194 | return hlua_http_add_hdr(L, msg); |
| 6195 | } |
| 6196 | |
| 6197 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6198 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6199 | { |
| 6200 | struct stream *s; |
| 6201 | struct http_msg *msg; |
| 6202 | const char *name; |
| 6203 | size_t name_len; |
| 6204 | |
| 6205 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6206 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6207 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6208 | |
| 6209 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6210 | WILL_LJMP(lua_error(L)); |
| 6211 | |
| 6212 | s = chn_strm(msg->chn); |
| 6213 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6214 | return 1; |
| 6215 | } |
| 6216 | |
| 6217 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6218 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6219 | { |
| 6220 | struct stream *s; |
| 6221 | struct http_msg *msg; |
| 6222 | const char *name; |
| 6223 | size_t name_len; |
| 6224 | |
| 6225 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6226 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6227 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6228 | |
| 6229 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6230 | WILL_LJMP(lua_error(L)); |
| 6231 | |
| 6232 | s = chn_strm(msg->chn); |
| 6233 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6234 | return 1; |
| 6235 | } |
| 6236 | |
| 6237 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6238 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6239 | { |
| 6240 | struct stream *s; |
| 6241 | struct http_msg *msg; |
| 6242 | const char *name; |
| 6243 | size_t name_len; |
| 6244 | |
| 6245 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6246 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6247 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6248 | |
| 6249 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6250 | WILL_LJMP(lua_error(L)); |
| 6251 | |
| 6252 | /* Check length. */ |
| 6253 | if (name_len > trash.size - 1) { |
| 6254 | lua_pushboolean(L, 0); |
| 6255 | return 1; |
| 6256 | } |
| 6257 | |
| 6258 | /* Add the mark question as prefix. */ |
| 6259 | chunk_reset(&trash); |
| 6260 | trash.area[trash.data++] = '?'; |
| 6261 | memcpy(trash.area + trash.data, name, name_len); |
| 6262 | trash.data += name_len; |
| 6263 | |
| 6264 | s = chn_strm(msg->chn); |
| 6265 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6266 | return 1; |
| 6267 | } |
| 6268 | |
| 6269 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6270 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6271 | { |
| 6272 | struct stream *s; |
| 6273 | struct http_msg *msg; |
| 6274 | const char *name; |
| 6275 | size_t name_len; |
| 6276 | |
| 6277 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6278 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6279 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6280 | |
| 6281 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6282 | WILL_LJMP(lua_error(L)); |
| 6283 | |
| 6284 | s = chn_strm(msg->chn); |
| 6285 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6286 | return 1; |
| 6287 | } |
| 6288 | |
| 6289 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6290 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6291 | { |
| 6292 | struct http_msg *msg; |
| 6293 | unsigned int code; |
| 6294 | const char *reason; |
| 6295 | size_t reason_len; |
| 6296 | |
| 6297 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6298 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6299 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6300 | |
| 6301 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6302 | WILL_LJMP(lua_error(L)); |
| 6303 | |
| 6304 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6305 | return 1; |
| 6306 | } |
| 6307 | |
| 6308 | /* Returns true if the HTTP message is full. */ |
| 6309 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6310 | { |
| 6311 | struct http_msg *msg; |
| 6312 | |
| 6313 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6314 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6315 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6316 | return 1; |
| 6317 | } |
| 6318 | |
| 6319 | /* Returns true if the HTTP message may still receive data. */ |
| 6320 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6321 | { |
| 6322 | struct http_msg *msg; |
| 6323 | struct htx *htx; |
| 6324 | |
| 6325 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6326 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6327 | htx = htxbuf(&msg->chn->buf); |
| 6328 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6329 | return 1; |
| 6330 | } |
| 6331 | |
| 6332 | /* Returns true if the HTTP message EOM was received */ |
| 6333 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6334 | { |
| 6335 | struct http_msg *msg; |
| 6336 | struct htx *htx; |
| 6337 | |
| 6338 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6339 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6340 | htx = htxbuf(&msg->chn->buf); |
| 6341 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6342 | return 1; |
| 6343 | } |
| 6344 | |
| 6345 | /* Returns the number of bytes available in the input side of the HTTP |
| 6346 | * message. This function never fails. |
| 6347 | */ |
| 6348 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6349 | { |
| 6350 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6351 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6352 | |
| 6353 | MAY_LJMP(check_args(L, 1, "input")); |
| 6354 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6355 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6356 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6357 | return 1; |
| 6358 | } |
| 6359 | |
| 6360 | /* Returns the number of bytes available in the output side of the HTTP |
| 6361 | * message. This function never fails. |
| 6362 | */ |
| 6363 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6364 | { |
| 6365 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6366 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6367 | |
| 6368 | MAY_LJMP(check_args(L, 1, "output")); |
| 6369 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6370 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6371 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6372 | return 1; |
| 6373 | } |
| 6374 | |
| 6375 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6376 | * 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] | 6377 | * 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] | 6378 | * block. This function is called during the payload filtering, so the headers |
| 6379 | * are already scheduled for output (from the filter point of view). |
| 6380 | */ |
| 6381 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6382 | { |
| 6383 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6384 | struct htx_blk *blk; |
| 6385 | struct htx_ret htxret; |
| 6386 | luaL_Buffer b; |
| 6387 | int ret = 0; |
| 6388 | |
| 6389 | luaL_buffinit(L, &b); |
| 6390 | htxret = htx_find_offset(htx, offset); |
| 6391 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6392 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6393 | struct ist v; |
| 6394 | |
| 6395 | switch (type) { |
| 6396 | case HTX_BLK_UNUSED: |
| 6397 | break; |
| 6398 | |
| 6399 | case HTX_BLK_DATA: |
| 6400 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6401 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6402 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6403 | |
| 6404 | luaL_addlstring(&b, v.ptr, v.len); |
| 6405 | ret += v.len; |
| 6406 | break; |
| 6407 | |
| 6408 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6409 | if (!ret) |
| 6410 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6411 | goto end; |
| 6412 | } |
| 6413 | offset = 0; |
| 6414 | } |
| 6415 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6416 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6417 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6418 | goto no_data; |
| 6419 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6420 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6421 | |
| 6422 | no_data: |
| 6423 | /* Remove the empty string and push nil on the stack */ |
| 6424 | lua_pop(L, 1); |
| 6425 | lua_pushnil(L); |
| 6426 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6427 | } |
| 6428 | |
| 6429 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6430 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6431 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6432 | * the filter context. |
| 6433 | */ |
| 6434 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6435 | { |
| 6436 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6437 | struct htx_ret htxret; |
| 6438 | int /*max, */ret = 0; |
| 6439 | |
| 6440 | /* Nothing to do, just return */ |
| 6441 | if (unlikely(istlen(str) == 0)) |
| 6442 | goto end; |
| 6443 | |
| 6444 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6445 | ret = -1; |
| 6446 | goto end; |
| 6447 | } |
| 6448 | |
| 6449 | htxret = htx_find_offset(htx, offset); |
| 6450 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6451 | if (!htx_add_last_data(htx, str)) |
| 6452 | goto end; |
| 6453 | } |
| 6454 | else { |
| 6455 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6456 | v.ptr += htxret.ret; |
| 6457 | v.len = 0; |
| 6458 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6459 | goto end; |
| 6460 | } |
| 6461 | ret = str.len; |
| 6462 | if (ret) { |
| 6463 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6464 | flt_update_offsets(filter, msg->chn, ret); |
| 6465 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6466 | } |
| 6467 | |
| 6468 | end: |
| 6469 | htx_to_buf(htx, &msg->chn->buf); |
| 6470 | return ret; |
| 6471 | } |
| 6472 | |
| 6473 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6474 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6475 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6476 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6477 | * update the filter context. |
| 6478 | */ |
| 6479 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6480 | { |
| 6481 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6482 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6483 | struct htx_blk *blk; |
| 6484 | struct htx_ret htxret; |
| 6485 | size_t ret = 0; |
| 6486 | |
| 6487 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6488 | 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] | 6489 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6490 | * of special blocks like HTX_BLK_EOT. |
| 6491 | * We simply truncate after offset |
| 6492 | * (truncate targeted blk and discard the following ones) |
| 6493 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6494 | htx_truncate(htx, offset); |
| 6495 | ret = len; |
| 6496 | goto end; |
| 6497 | } |
| 6498 | |
| 6499 | htxret = htx_find_offset(htx, offset); |
| 6500 | blk = htxret.blk; |
| 6501 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6502 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6503 | struct ist v; |
| 6504 | |
| 6505 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6506 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6507 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6508 | v = htx_get_blk_value(htx, blk); |
| 6509 | v.ptr += htxret.ret; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6510 | v.len -= htxret.ret; |
| 6511 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6512 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6513 | /* trimming data in blk: discard everything after the offset |
| 6514 | * (replace 'v' with 'IST_NULL') |
| 6515 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6516 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6517 | if (blk && v.len < len) { |
| 6518 | /* In this case, caller wants to keep removing data, |
| 6519 | * but we need to spare current blk |
| 6520 | * because it was already trimmed |
| 6521 | */ |
| 6522 | blk = htx_get_next_blk(htx, blk); |
| 6523 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6524 | len -= v.len; |
| 6525 | ret += v.len; |
| 6526 | } |
| 6527 | |
| 6528 | |
| 6529 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6530 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6531 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6532 | uint32_t sz = htx_get_blksz(blk); |
| 6533 | |
| 6534 | switch (type) { |
| 6535 | case HTX_BLK_UNUSED: |
| 6536 | break; |
| 6537 | |
| 6538 | case HTX_BLK_DATA: |
| 6539 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6540 | /* don't discard whole blk, only part of it |
| 6541 | * (from the beginning) |
| 6542 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6543 | htx_cut_data_blk(htx, blk, len); |
| 6544 | ret += len; |
| 6545 | goto end; |
| 6546 | } |
| 6547 | break; |
| 6548 | |
| 6549 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6550 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6551 | goto end; |
| 6552 | } |
| 6553 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6554 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6555 | len -= sz; |
| 6556 | ret += sz; |
| 6557 | blk = htx_remove_blk(htx, blk); |
| 6558 | } |
| 6559 | |
| 6560 | end: |
| 6561 | flt_update_offsets(filter, msg->chn, -ret); |
| 6562 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6563 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6564 | * to loose the EOM flag if the message is empty. |
| 6565 | */ |
| 6566 | } |
| 6567 | |
| 6568 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6569 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6570 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6571 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6572 | * the stack. |
| 6573 | */ |
| 6574 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6575 | { |
| 6576 | struct http_msg *msg; |
| 6577 | struct filter *filter; |
| 6578 | size_t output, input; |
| 6579 | int offset, len; |
| 6580 | |
| 6581 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6582 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6583 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6584 | |
| 6585 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6586 | WILL_LJMP(lua_error(L)); |
| 6587 | |
| 6588 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6589 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6590 | WILL_LJMP(lua_error(L)); |
| 6591 | |
| 6592 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6593 | lua_pushnil(L); |
| 6594 | return 1; |
| 6595 | } |
| 6596 | |
| 6597 | offset = output; |
| 6598 | if (lua_gettop(L) > 1) { |
| 6599 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6600 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6601 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6602 | offset += output; |
| 6603 | if (offset < output || offset > input + output) { |
| 6604 | lua_pushfstring(L, "offset out of range."); |
| 6605 | WILL_LJMP(lua_error(L)); |
| 6606 | } |
| 6607 | } |
| 6608 | len = output + input - offset; |
| 6609 | if (lua_gettop(L) == 3) { |
| 6610 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6611 | if (!len) |
| 6612 | goto dup; |
| 6613 | if (len == -1) |
| 6614 | len = global.tune.bufsize; |
| 6615 | if (len < 0) { |
| 6616 | lua_pushfstring(L, "length out of range."); |
| 6617 | WILL_LJMP(lua_error(L)); |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | dup: |
| 6622 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6623 | return 1; |
| 6624 | } |
| 6625 | |
| 6626 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6627 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6628 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6629 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6630 | * yield. An exception is returned if it is called from another callback. |
| 6631 | */ |
| 6632 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6633 | { |
| 6634 | struct http_msg *msg; |
| 6635 | struct filter *filter; |
| 6636 | const char *str; |
| 6637 | size_t offset, len, sz; |
| 6638 | int ret; |
| 6639 | |
| 6640 | MAY_LJMP(check_args(L, 2, "append")); |
| 6641 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6642 | |
| 6643 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6644 | WILL_LJMP(lua_error(L)); |
| 6645 | |
| 6646 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6647 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6648 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6649 | WILL_LJMP(lua_error(L)); |
| 6650 | |
| 6651 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6652 | lua_pushinteger(L, ret); |
| 6653 | return 1; |
| 6654 | } |
| 6655 | |
| 6656 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6657 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6658 | * channel function used to prepend data, this one can only be called inside a |
| 6659 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6660 | * returned if it is called from another callback. |
| 6661 | */ |
| 6662 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6663 | { |
| 6664 | struct http_msg *msg; |
| 6665 | struct filter *filter; |
| 6666 | const char *str; |
| 6667 | size_t offset, len, sz; |
| 6668 | int ret; |
| 6669 | |
| 6670 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6671 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6672 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6673 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6674 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6675 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6676 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6677 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6678 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6679 | WILL_LJMP(lua_error(L)); |
| 6680 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6681 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6682 | lua_pushinteger(L, ret); |
| 6683 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6684 | } |
| 6685 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6686 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6687 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6688 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6689 | * this one can only be called inside a filter, from http_payload callback. So |
| 6690 | * it cannot yield. An exception is returned if it is called from another |
| 6691 | * callback. |
| 6692 | */ |
| 6693 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6694 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6695 | struct http_msg *msg; |
| 6696 | struct filter *filter; |
| 6697 | const char *str; |
| 6698 | size_t input, output, sz; |
| 6699 | int offset; |
| 6700 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6701 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6702 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6703 | 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] | 6704 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6705 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6706 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6707 | WILL_LJMP(lua_error(L)); |
| 6708 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6709 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6710 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6711 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6712 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6713 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6714 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6715 | if (lua_gettop(L) > 2) { |
| 6716 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6717 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6718 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6719 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6720 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6721 | lua_pushfstring(L, "offset out of range."); |
| 6722 | WILL_LJMP(lua_error(L)); |
| 6723 | } |
| 6724 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6725 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6726 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6727 | lua_pushinteger(L, ret); |
| 6728 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6729 | } |
| 6730 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6732 | * default all DATA blocks are removed. It returns the amount of data |
| 6733 | * removed. Unlike the channel function used to remove data, this one can only |
| 6734 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6735 | * exception is returned if it is called from another callback. |
| 6736 | */ |
| 6737 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6738 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6739 | struct http_msg *msg; |
| 6740 | struct filter *filter; |
| 6741 | size_t input, output; |
| 6742 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6743 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6744 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6745 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6747 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6748 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6749 | WILL_LJMP(lua_error(L)); |
| 6750 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6751 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6752 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6753 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6754 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6755 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6756 | if (lua_gettop(L) > 1) { |
| 6757 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6758 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6759 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6760 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6761 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6762 | lua_pushfstring(L, "offset out of range."); |
| 6763 | WILL_LJMP(lua_error(L)); |
| 6764 | } |
| 6765 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6766 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6768 | if (lua_gettop(L) == 3) { |
| 6769 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6770 | if (!len) |
| 6771 | goto end; |
| 6772 | if (len == -1) |
| 6773 | len = output + input - offset; |
| 6774 | if (len < 0 || offset + len > output + input) { |
| 6775 | lua_pushfstring(L, "length out of range."); |
| 6776 | WILL_LJMP(lua_error(L)); |
| 6777 | } |
| 6778 | } |
| 6779 | |
| 6780 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6781 | |
| 6782 | end: |
| 6783 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6784 | return 1; |
| 6785 | } |
| 6786 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6787 | /* 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] | 6788 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6789 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6790 | * function used to replace data, this one can only be called inside a filter, |
| 6791 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6792 | * it is called from another callback. |
| 6793 | */ |
| 6794 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6795 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6796 | struct http_msg *msg; |
| 6797 | struct filter *filter; |
| 6798 | struct htx *htx; |
| 6799 | const char *str; |
| 6800 | size_t input, output, sz; |
| 6801 | int offset, len; |
| 6802 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6803 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6804 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6805 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6806 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6807 | |
| 6808 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6809 | WILL_LJMP(lua_error(L)); |
| 6810 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6811 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6812 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6813 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6814 | WILL_LJMP(lua_error(L)); |
| 6815 | |
| 6816 | offset = output; |
| 6817 | if (lua_gettop(L) > 2) { |
| 6818 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 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; |
| 6822 | if (offset < output || offset > input + output) { |
| 6823 | lua_pushfstring(L, "offset out of range."); |
| 6824 | WILL_LJMP(lua_error(L)); |
| 6825 | } |
| 6826 | } |
| 6827 | |
| 6828 | len = output + input - offset; |
| 6829 | if (lua_gettop(L) == 4) { |
| 6830 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6831 | if (!len) |
| 6832 | goto set; |
| 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 | set: |
| 6842 | /* Be sure we can copied the string once input data will be removed. */ |
| 6843 | htx = htx_from_buf(&msg->chn->buf); |
| 6844 | if (sz > htx_free_data_space(htx) + len) |
| 6845 | lua_pushinteger(L, -1); |
| 6846 | else { |
| 6847 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6848 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6849 | lua_pushinteger(L, ret); |
| 6850 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6851 | return 1; |
| 6852 | } |
| 6853 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6854 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6855 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6856 | * the channel function used to send data, this one can only be called inside a |
| 6857 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6858 | * returned if it is called from another callback. |
| 6859 | */ |
| 6860 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6861 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6862 | struct http_msg *msg; |
| 6863 | struct filter *filter; |
| 6864 | struct htx *htx; |
| 6865 | const char *str; |
| 6866 | size_t offset, len, sz; |
| 6867 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6868 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6869 | MAY_LJMP(check_args(L, 2, "send")); |
| 6870 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6871 | |
| 6872 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6873 | WILL_LJMP(lua_error(L)); |
| 6874 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6875 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6876 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6877 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6878 | WILL_LJMP(lua_error(L)); |
| 6879 | |
| 6880 | /* Return an error if the channel's output is closed */ |
| 6881 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6882 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6883 | return 1; |
| 6884 | } |
| 6885 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6886 | htx = htx_from_buf(&msg->chn->buf); |
| 6887 | if (sz > htx_free_data_space(htx)) { |
| 6888 | lua_pushinteger(L, -1); |
| 6889 | return 1; |
| 6890 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6891 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6892 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6893 | if (ret > 0) { |
| 6894 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6895 | |
| 6896 | FLT_OFF(filter, msg->chn) += ret; |
| 6897 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6898 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6899 | } |
| 6900 | |
| 6901 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6902 | return 1; |
| 6903 | } |
| 6904 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6905 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6906 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6907 | * channel function used to forward data, this one can only be called inside a |
| 6908 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6909 | * returned if it is called from another callback. All other functions deal with |
| 6910 | * DATA block, this one not. |
| 6911 | */ |
| 6912 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6913 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6914 | struct http_msg *msg; |
| 6915 | struct filter *filter; |
| 6916 | size_t offset, len; |
| 6917 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6918 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6919 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6920 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6921 | |
| 6922 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6923 | WILL_LJMP(lua_error(L)); |
| 6924 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6925 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6926 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6927 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6928 | WILL_LJMP(lua_error(L)); |
| 6929 | |
| 6930 | /* Nothing to do, just return */ |
| 6931 | if (!fwd) |
| 6932 | goto end; |
| 6933 | |
| 6934 | /* Return an error if the channel's output is closed */ |
| 6935 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6936 | ret = -1; |
| 6937 | goto end; |
| 6938 | } |
| 6939 | |
| 6940 | ret = fwd; |
| 6941 | if (ret > len) |
| 6942 | ret = len; |
| 6943 | |
| 6944 | if (ret) { |
| 6945 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6946 | |
| 6947 | FLT_OFF(filter, msg->chn) += ret; |
| 6948 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6949 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6950 | } |
| 6951 | |
| 6952 | end: |
| 6953 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6954 | return 1; |
| 6955 | } |
| 6956 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6957 | /* Set EOM flag on the HTX message. |
| 6958 | * |
| 6959 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6960 | * really know how to do without this feature. |
| 6961 | */ |
| 6962 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6963 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6964 | struct http_msg *msg; |
| 6965 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6966 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6967 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6968 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6969 | htx = htxbuf(&msg->chn->buf); |
| 6970 | htx->flags |= HTX_FL_EOM; |
| 6971 | return 0; |
| 6972 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6973 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6974 | /* Unset EOM flag on the HTX message. |
| 6975 | * |
| 6976 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6977 | * really know how to do without this feature. |
| 6978 | */ |
| 6979 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6980 | { |
| 6981 | struct http_msg *msg; |
| 6982 | struct htx *htx; |
| 6983 | |
| 6984 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6985 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6986 | htx = htxbuf(&msg->chn->buf); |
| 6987 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6988 | return 0; |
| 6989 | } |
| 6990 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6991 | /* |
| 6992 | * |
| 6993 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6994 | * Class HTTPClient |
| 6995 | * |
| 6996 | * |
| 6997 | */ |
| 6998 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 6999 | { |
| 7000 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7001 | } |
| 7002 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7003 | |
| 7004 | /* stops the httpclient and ask it to kill itself */ |
| 7005 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7006 | { |
| 7007 | struct hlua_httpclient *hlua_hc; |
| 7008 | |
| 7009 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7010 | |
| 7011 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7012 | |
| 7013 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 7014 | |
| 7015 | hlua_hc->hc = NULL; |
| 7016 | |
| 7017 | |
| 7018 | return 0; |
| 7019 | } |
| 7020 | |
| 7021 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7022 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7023 | { |
| 7024 | struct hlua_httpclient *hlua_hc; |
| 7025 | struct hlua *hlua; |
| 7026 | |
| 7027 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7028 | hlua = hlua_gethlua(L); |
| 7029 | if (!hlua) |
| 7030 | return 0; |
| 7031 | |
| 7032 | /* Check stack size. */ |
| 7033 | if (!lua_checkstack(L, 3)) { |
| 7034 | hlua_pusherror(L, "httpclient: full stack"); |
| 7035 | goto err; |
| 7036 | } |
| 7037 | /* Create the object: obj[0] = userdata. */ |
| 7038 | lua_newtable(L); |
| 7039 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7040 | lua_rawseti(L, -2, 0); |
| 7041 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7042 | |
| 7043 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7044 | if (!hlua_hc->hc) |
| 7045 | goto err; |
| 7046 | |
| 7047 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7048 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7049 | lua_setmetatable(L, -2); |
| 7050 | |
| 7051 | return 1; |
| 7052 | |
| 7053 | err: |
| 7054 | WILL_LJMP(lua_error(L)); |
| 7055 | return 0; |
| 7056 | } |
| 7057 | |
| 7058 | |
| 7059 | /* |
| 7060 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7061 | * httpclient receives some data |
| 7062 | * |
| 7063 | */ |
| 7064 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7065 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7066 | { |
| 7067 | struct hlua *hlua = hc->caller; |
| 7068 | |
| 7069 | if (!hlua || !hlua->task) |
| 7070 | return; |
| 7071 | |
| 7072 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7073 | } |
| 7074 | |
| 7075 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7076 | * Fill the lua stack with headers from the httpclient response |
| 7077 | * This works the same way as the hlua_http_get_headers() function |
| 7078 | */ |
| 7079 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7080 | { |
| 7081 | struct http_hdr *hdr; |
| 7082 | |
| 7083 | lua_newtable(L); |
| 7084 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7085 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7086 | struct ist n, v; |
| 7087 | int len; |
| 7088 | |
| 7089 | n = hdr->n; |
| 7090 | v = hdr->v; |
| 7091 | |
| 7092 | /* Check for existing entry: |
| 7093 | * assume that the table is on the top of the stack, and |
| 7094 | * push the key in the stack, the function lua_gettable() |
| 7095 | * perform the lookup. |
| 7096 | */ |
| 7097 | |
| 7098 | lua_pushlstring(L, n.ptr, n.len); |
| 7099 | lua_gettable(L, -2); |
| 7100 | |
| 7101 | switch (lua_type(L, -1)) { |
| 7102 | case LUA_TNIL: |
| 7103 | /* Table not found, create it. */ |
| 7104 | lua_pop(L, 1); /* remove the nil value. */ |
| 7105 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7106 | lua_newtable(L); /* create and push empty table. */ |
| 7107 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7108 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7109 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7110 | break; |
| 7111 | |
| 7112 | case LUA_TTABLE: |
| 7113 | /* Entry found: push the value in the table. */ |
| 7114 | len = lua_rawlen(L, -1); |
| 7115 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7116 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7117 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7118 | break; |
| 7119 | |
| 7120 | default: |
| 7121 | /* Other cases are errors. */ |
| 7122 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7123 | WILL_LJMP(lua_error(L)); |
| 7124 | } |
| 7125 | } |
| 7126 | return 1; |
| 7127 | } |
| 7128 | |
| 7129 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7130 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7131 | * |
| 7132 | * Caller must free the result |
| 7133 | */ |
| 7134 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7135 | { |
| 7136 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7137 | struct http_hdr *result = NULL; |
| 7138 | uint32_t hdr_num = 0; |
| 7139 | |
| 7140 | lua_pushnil(L); |
| 7141 | while (lua_next(L, -2) != 0) { |
| 7142 | struct ist name, value; |
| 7143 | const char *n, *v; |
| 7144 | size_t nlen, vlen; |
| 7145 | |
| 7146 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7147 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7148 | goto next_hdr; |
| 7149 | } |
| 7150 | |
| 7151 | n = lua_tolstring(L, -2, &nlen); |
| 7152 | name = ist2(n, nlen); |
| 7153 | |
| 7154 | /* Loop on header's values */ |
| 7155 | lua_pushnil(L); |
| 7156 | while (lua_next(L, -2)) { |
| 7157 | if (!lua_isstring(L, -1)) { |
| 7158 | /* Skip the value if it is not a string */ |
| 7159 | goto next_value; |
| 7160 | } |
| 7161 | |
| 7162 | v = lua_tolstring(L, -1, &vlen); |
| 7163 | value = ist2(v, vlen); |
| 7164 | name = ist2(n, nlen); |
| 7165 | |
| 7166 | hdrs[hdr_num].n = istdup(name); |
| 7167 | hdrs[hdr_num].v = istdup(value); |
| 7168 | |
| 7169 | hdr_num++; |
| 7170 | |
| 7171 | next_value: |
| 7172 | lua_pop(L, 1); |
| 7173 | } |
| 7174 | |
| 7175 | next_hdr: |
| 7176 | lua_pop(L, 1); |
| 7177 | |
| 7178 | } |
| 7179 | |
| 7180 | if (hdr_num) { |
| 7181 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7182 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7183 | if (!result) |
| 7184 | goto skip_headers; |
| 7185 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7186 | |
| 7187 | result[hdr_num].n = IST_NULL; |
| 7188 | result[hdr_num].v = IST_NULL; |
| 7189 | } |
| 7190 | |
| 7191 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7192 | |
| 7193 | return result; |
| 7194 | } |
| 7195 | |
| 7196 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7197 | /* |
| 7198 | * For each yield, checks if there is some data in the httpclient and push them |
| 7199 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7200 | * the stack |
| 7201 | */ |
| 7202 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7203 | { |
| 7204 | struct buffer *tr; |
| 7205 | int res; |
| 7206 | struct hlua *hlua = hlua_gethlua(L); |
| 7207 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7208 | |
| 7209 | |
| 7210 | tr = get_trash_chunk(); |
| 7211 | |
| 7212 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7213 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7214 | |
| 7215 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7216 | |
| 7217 | luaL_pushresult(&hlua_hc->b); |
| 7218 | lua_settable(L, -3); |
| 7219 | |
| 7220 | lua_pushstring(L, "status"); |
| 7221 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7222 | lua_settable(L, -3); |
| 7223 | |
| 7224 | |
| 7225 | lua_pushstring(L, "reason"); |
| 7226 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7227 | lua_settable(L, -3); |
| 7228 | |
| 7229 | lua_pushstring(L, "headers"); |
| 7230 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7231 | lua_settable(L, -3); |
| 7232 | |
| 7233 | return 1; |
| 7234 | } |
| 7235 | |
| 7236 | if (httpclient_data(hlua_hc->hc)) |
| 7237 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7238 | |
| 7239 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7240 | |
| 7241 | return 0; |
| 7242 | } |
| 7243 | |
| 7244 | /* |
| 7245 | * Call this when trying to stream a body during a request |
| 7246 | */ |
| 7247 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7248 | { |
| 7249 | struct hlua *hlua; |
| 7250 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7251 | const char *body_str = NULL; |
| 7252 | int ret; |
| 7253 | int end = 0; |
| 7254 | size_t buf_len; |
| 7255 | size_t to_send = 0; |
| 7256 | |
| 7257 | hlua = hlua_gethlua(L); |
| 7258 | |
| 7259 | if (!hlua || !hlua->task) |
| 7260 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7261 | "'frontend', 'backend' or 'task'")); |
| 7262 | |
| 7263 | ret = lua_getfield(L, -1, "body"); |
| 7264 | if (ret != LUA_TSTRING) |
| 7265 | goto rcv; |
| 7266 | |
| 7267 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7268 | lua_pop(L, 1); |
| 7269 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7270 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7271 | |
| 7272 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7273 | end = 1; |
| 7274 | |
| 7275 | /* the end flag is always set since we are using the whole remaining size */ |
| 7276 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7277 | |
| 7278 | if (buf_len > hlua_hc->sent) { |
| 7279 | /* still need to process the buffer */ |
| 7280 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7281 | } else { |
| 7282 | goto rcv; |
| 7283 | /* we sent the whole request buffer we can recv */ |
| 7284 | } |
| 7285 | return 0; |
| 7286 | |
| 7287 | rcv: |
| 7288 | |
| 7289 | /* we return a "res" object */ |
| 7290 | lua_newtable(L); |
| 7291 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7292 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7293 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7294 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7295 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7296 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7297 | |
| 7298 | return 1; |
| 7299 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7300 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7301 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7302 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7303 | */ |
| 7304 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7305 | __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] | 7306 | { |
| 7307 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7308 | struct http_hdr *hdrs = NULL; |
| 7309 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7310 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7311 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7312 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7313 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7314 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7315 | |
| 7316 | hlua = hlua_gethlua(L); |
| 7317 | |
| 7318 | if (!hlua || !hlua->task) |
| 7319 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7320 | "'frontend', 'backend' or 'task'")); |
| 7321 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7322 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7323 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7324 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7325 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7326 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7327 | lua_pushnil(L); /* first key */ |
| 7328 | while (lua_next(L, 2)) { |
| 7329 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7330 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7331 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7332 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7333 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7334 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7335 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7336 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7337 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7338 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7339 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7340 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7341 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7342 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7343 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7344 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7345 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7346 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7347 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7348 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7349 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7350 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7351 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7352 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7353 | } else { |
| 7354 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7355 | } |
| 7356 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7357 | lua_pop(L, 1); |
| 7358 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7359 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7360 | if (!url_str) { |
| 7361 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7362 | return 0; |
| 7363 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7364 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7365 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7366 | |
| 7367 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7368 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7369 | |
| 7370 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7371 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7372 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7373 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7374 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7375 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7376 | /* a body is available, it will use the request callback */ |
| 7377 | if (body_str) { |
| 7378 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7379 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7380 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7381 | 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] | 7382 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7383 | /* free the temporary headers array */ |
| 7384 | hdrs_i = hdrs; |
| 7385 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7386 | istfree(&hdrs_i->n); |
| 7387 | istfree(&hdrs_i->v); |
| 7388 | hdrs_i++; |
| 7389 | } |
| 7390 | ha_free(&hdrs); |
| 7391 | |
| 7392 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7393 | if (ret != ERR_NONE) { |
| 7394 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7395 | return 0; |
| 7396 | } |
| 7397 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7398 | if (!httpclient_start(hlua_hc->hc)) |
| 7399 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7400 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7401 | 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] | 7402 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7403 | return 0; |
| 7404 | } |
| 7405 | |
| 7406 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7407 | * Sends an HTTP HEAD request and wait for a response |
| 7408 | * |
| 7409 | * httpclient:head(url, headers, payload) |
| 7410 | */ |
| 7411 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7412 | { |
| 7413 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7414 | } |
| 7415 | |
| 7416 | /* |
| 7417 | * Send an HTTP GET request and wait for a response |
| 7418 | * |
| 7419 | * httpclient:get(url, headers, payload) |
| 7420 | */ |
| 7421 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7422 | { |
| 7423 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7424 | |
| 7425 | } |
| 7426 | |
| 7427 | /* |
| 7428 | * Sends an HTTP PUT request and wait for a response |
| 7429 | * |
| 7430 | * httpclient:put(url, headers, payload) |
| 7431 | */ |
| 7432 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7433 | { |
| 7434 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7435 | } |
| 7436 | |
| 7437 | /* |
| 7438 | * Send an HTTP POST request and wait for a response |
| 7439 | * |
| 7440 | * httpclient:post(url, headers, payload) |
| 7441 | */ |
| 7442 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7443 | { |
| 7444 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7445 | } |
| 7446 | |
| 7447 | |
| 7448 | /* |
| 7449 | * Sends an HTTP DELETE request and wait for a response |
| 7450 | * |
| 7451 | * httpclient:delete(url, headers, payload) |
| 7452 | */ |
| 7453 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7454 | { |
| 7455 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7456 | } |
| 7457 | |
| 7458 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7459 | * |
| 7460 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7461 | * Class TXN |
| 7462 | * |
| 7463 | * |
| 7464 | */ |
| 7465 | |
| 7466 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7467 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7468 | */ |
| 7469 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7470 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7471 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7472 | } |
| 7473 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7474 | __LJMP static int hlua_set_var(lua_State *L) |
| 7475 | { |
| 7476 | struct hlua_txn *htxn; |
| 7477 | const char *name; |
| 7478 | size_t len; |
| 7479 | struct sample smp; |
| 7480 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7481 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7482 | 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] | 7483 | |
| 7484 | /* It is useles to retrieve the stream, but this function |
| 7485 | * runs only in a stream context. |
| 7486 | */ |
| 7487 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7488 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7489 | |
| 7490 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7491 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7492 | hlua_lua2smp(L, 3, &smp); |
| 7493 | |
| 7494 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7495 | 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] | 7496 | |
| 7497 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7498 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7499 | else |
| 7500 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7501 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7502 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7503 | } |
| 7504 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7505 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7506 | { |
| 7507 | struct hlua_txn *htxn; |
| 7508 | const char *name; |
| 7509 | size_t len; |
| 7510 | struct sample smp; |
| 7511 | |
| 7512 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7513 | |
| 7514 | /* It is useles to retrieve the stream, but this function |
| 7515 | * runs only in a stream context. |
| 7516 | */ |
| 7517 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7518 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7519 | |
| 7520 | /* Unset the variable. */ |
| 7521 | 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] | 7522 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7523 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7524 | } |
| 7525 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7526 | __LJMP static int hlua_get_var(lua_State *L) |
| 7527 | { |
| 7528 | struct hlua_txn *htxn; |
| 7529 | const char *name; |
| 7530 | size_t len; |
| 7531 | struct sample smp; |
| 7532 | |
| 7533 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7534 | |
| 7535 | /* It is useles to retrieve the stream, but this function |
| 7536 | * runs only in a stream context. |
| 7537 | */ |
| 7538 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7539 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7540 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7541 | 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] | 7542 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7543 | lua_pushnil(L); |
| 7544 | return 1; |
| 7545 | } |
| 7546 | |
| 7547 | return hlua_smp2lua(L, &smp); |
| 7548 | } |
| 7549 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7550 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7551 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7552 | struct hlua *hlua; |
| 7553 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7554 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7555 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7556 | /* It is useles to retrieve the stream, but this function |
| 7557 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7558 | */ |
| 7559 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7560 | |
| 7561 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7562 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7563 | if (!hlua) |
| 7564 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7565 | |
| 7566 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7567 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7568 | |
| 7569 | /* Get and store new value. */ |
| 7570 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7571 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7572 | |
| 7573 | return 0; |
| 7574 | } |
| 7575 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7576 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7577 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7578 | struct hlua *hlua; |
| 7579 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7580 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7581 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7582 | /* It is useles to retrieve the stream, but this function |
| 7583 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7584 | */ |
| 7585 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7586 | |
| 7587 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7588 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7589 | if (!hlua) { |
| 7590 | lua_pushnil(L); |
| 7591 | return 1; |
| 7592 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7593 | |
| 7594 | /* Push configuration index in the stack. */ |
| 7595 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7596 | |
| 7597 | return 1; |
| 7598 | } |
| 7599 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7600 | /* Create stack entry containing a class TXN. This function |
| 7601 | * return 0 if the stack does not contains free slots, |
| 7602 | * otherwise it returns 1. |
| 7603 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7604 | 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] | 7605 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7606 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7607 | |
| 7608 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7609 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7610 | return 0; |
| 7611 | |
| 7612 | /* NOTE: The allocation never fails. The failure |
| 7613 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7614 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7615 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7616 | /* Create the object: obj[0] = userdata. */ |
| 7617 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7618 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7619 | lua_rawseti(L, -2, 0); |
| 7620 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7621 | htxn->s = s; |
| 7622 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7623 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7624 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7625 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7626 | /* Create the "f" field that contains a list of fetches. */ |
| 7627 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7628 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7629 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7630 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7631 | |
| 7632 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7633 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7634 | 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] | 7635 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7636 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7637 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7638 | /* Create the "c" field that contains a list of converters. */ |
| 7639 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7640 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7641 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7642 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7643 | |
| 7644 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7645 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7646 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7647 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7648 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7649 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7650 | /* Create the "req" field that contains the request channel object. */ |
| 7651 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7652 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7653 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7654 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7655 | |
| 7656 | /* Create the "res" field that contains the response channel object. */ |
| 7657 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7658 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7659 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7660 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7661 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7662 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7663 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7664 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7665 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7666 | return 0; |
| 7667 | } |
| 7668 | else |
| 7669 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7670 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7671 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7672 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7673 | /* HTTPMessage object are created when a lua TXN is created from |
| 7674 | * a filter context only |
| 7675 | */ |
| 7676 | |
| 7677 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7678 | lua_pushstring(L, "http_req"); |
| 7679 | if (p->mode == PR_MODE_HTTP) { |
| 7680 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7681 | return 0; |
| 7682 | } |
| 7683 | else |
| 7684 | lua_pushnil(L); |
| 7685 | lua_rawset(L, -3); |
| 7686 | |
| 7687 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7688 | lua_pushstring(L, "http_res"); |
| 7689 | if (p->mode == PR_MODE_HTTP) { |
| 7690 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7691 | return 0; |
| 7692 | } |
| 7693 | else |
| 7694 | lua_pushnil(L); |
| 7695 | lua_rawset(L, -3); |
| 7696 | } |
| 7697 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7698 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7699 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7700 | lua_setmetatable(L, -2); |
| 7701 | |
| 7702 | return 1; |
| 7703 | } |
| 7704 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7705 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7706 | { |
| 7707 | const char *msg; |
| 7708 | struct hlua_txn *htxn; |
| 7709 | |
| 7710 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7711 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7712 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7713 | |
| 7714 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7715 | return 0; |
| 7716 | } |
| 7717 | |
| 7718 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7719 | { |
| 7720 | int level; |
| 7721 | const char *msg; |
| 7722 | struct hlua_txn *htxn; |
| 7723 | |
| 7724 | MAY_LJMP(check_args(L, 3, "log")); |
| 7725 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7726 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7727 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7728 | |
| 7729 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7730 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7731 | |
| 7732 | hlua_sendlog(htxn->s->be, level, msg); |
| 7733 | return 0; |
| 7734 | } |
| 7735 | |
| 7736 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7737 | { |
| 7738 | const char *msg; |
| 7739 | struct hlua_txn *htxn; |
| 7740 | |
| 7741 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7742 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7743 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7744 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7745 | return 0; |
| 7746 | } |
| 7747 | |
| 7748 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7749 | { |
| 7750 | const char *msg; |
| 7751 | struct hlua_txn *htxn; |
| 7752 | |
| 7753 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7754 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7755 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7756 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7757 | return 0; |
| 7758 | } |
| 7759 | |
| 7760 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7761 | { |
| 7762 | const char *msg; |
| 7763 | struct hlua_txn *htxn; |
| 7764 | |
| 7765 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7766 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7767 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7768 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7769 | return 0; |
| 7770 | } |
| 7771 | |
| 7772 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7773 | { |
| 7774 | const char *msg; |
| 7775 | struct hlua_txn *htxn; |
| 7776 | |
| 7777 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7778 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7779 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7780 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7781 | return 0; |
| 7782 | } |
| 7783 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7784 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7785 | { |
| 7786 | struct hlua_txn *htxn; |
| 7787 | int ll; |
| 7788 | |
| 7789 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7790 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7791 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7792 | |
| 7793 | if (ll < 0 || ll > 7) |
| 7794 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7795 | |
| 7796 | htxn->s->logs.level = ll; |
| 7797 | return 0; |
| 7798 | } |
| 7799 | |
| 7800 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7801 | { |
| 7802 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7803 | int tos; |
| 7804 | |
| 7805 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7806 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7807 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7808 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7809 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7810 | return 0; |
| 7811 | } |
| 7812 | |
| 7813 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7814 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7815 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7816 | int mark; |
| 7817 | |
| 7818 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7819 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7820 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7821 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7822 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7823 | return 0; |
| 7824 | } |
| 7825 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7826 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7827 | { |
| 7828 | struct hlua_txn *htxn; |
| 7829 | |
| 7830 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7831 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7832 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7833 | return 0; |
| 7834 | } |
| 7835 | |
| 7836 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7837 | { |
| 7838 | struct hlua_txn *htxn; |
| 7839 | |
| 7840 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7841 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7842 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7843 | return 0; |
| 7844 | } |
| 7845 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7846 | /* 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] | 7847 | * 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] | 7848 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7849 | * error. The Reply must be on top of the stack. |
| 7850 | */ |
| 7851 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7852 | { |
| 7853 | struct htx *htx; |
| 7854 | struct htx_sl *sl; |
| 7855 | struct h1m h1m; |
| 7856 | const char *status, *reason, *body; |
| 7857 | size_t status_len, reason_len, body_len; |
| 7858 | int ret, code, flags; |
| 7859 | |
| 7860 | code = 200; |
| 7861 | status = "200"; |
| 7862 | status_len = 3; |
| 7863 | ret = lua_getfield(L, -1, "status"); |
| 7864 | if (ret == LUA_TNUMBER) { |
| 7865 | code = lua_tointeger(L, -1); |
| 7866 | status = lua_tolstring(L, -1, &status_len); |
| 7867 | } |
| 7868 | lua_pop(L, 1); |
| 7869 | |
| 7870 | reason = http_get_reason(code); |
| 7871 | reason_len = strlen(reason); |
| 7872 | ret = lua_getfield(L, -1, "reason"); |
| 7873 | if (ret == LUA_TSTRING) |
| 7874 | reason = lua_tolstring(L, -1, &reason_len); |
| 7875 | lua_pop(L, 1); |
| 7876 | |
| 7877 | body = NULL; |
| 7878 | body_len = 0; |
| 7879 | ret = lua_getfield(L, -1, "body"); |
| 7880 | if (ret == LUA_TSTRING) |
| 7881 | body = lua_tolstring(L, -1, &body_len); |
| 7882 | lua_pop(L, 1); |
| 7883 | |
| 7884 | /* Prepare the response before inserting the headers */ |
| 7885 | h1m_init_res(&h1m); |
| 7886 | htx = htx_from_buf(&s->res.buf); |
| 7887 | channel_htx_truncate(&s->res, htx); |
| 7888 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7889 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7890 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7891 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7892 | } |
| 7893 | else { |
| 7894 | flags = HTX_SL_F_IS_RESP; |
| 7895 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7896 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7897 | } |
| 7898 | if (!sl) |
| 7899 | goto fail; |
| 7900 | sl->info.res.status = code; |
| 7901 | |
| 7902 | /* Push in the stack the "headers" entry. */ |
| 7903 | ret = lua_getfield(L, -1, "headers"); |
| 7904 | if (ret != LUA_TTABLE) |
| 7905 | goto skip_headers; |
| 7906 | |
| 7907 | lua_pushnil(L); |
| 7908 | while (lua_next(L, -2) != 0) { |
| 7909 | struct ist name, value; |
| 7910 | const char *n, *v; |
| 7911 | size_t nlen, vlen; |
| 7912 | |
| 7913 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7914 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7915 | goto next_hdr; |
| 7916 | } |
| 7917 | |
| 7918 | n = lua_tolstring(L, -2, &nlen); |
| 7919 | name = ist2(n, nlen); |
| 7920 | if (isteqi(name, ist("content-length"))) { |
| 7921 | /* Always skip content-length header. It will be added |
| 7922 | * later with the correct len |
| 7923 | */ |
| 7924 | goto next_hdr; |
| 7925 | } |
| 7926 | |
| 7927 | /* Loop on header's values */ |
| 7928 | lua_pushnil(L); |
| 7929 | while (lua_next(L, -2)) { |
| 7930 | if (!lua_isstring(L, -1)) { |
| 7931 | /* Skip the value if it is not a string */ |
| 7932 | goto next_value; |
| 7933 | } |
| 7934 | |
| 7935 | v = lua_tolstring(L, -1, &vlen); |
| 7936 | value = ist2(v, vlen); |
| 7937 | |
| 7938 | if (isteqi(name, ist("transfer-encoding"))) |
| 7939 | h1_parse_xfer_enc_header(&h1m, value); |
| 7940 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7941 | goto fail; |
| 7942 | |
| 7943 | next_value: |
| 7944 | lua_pop(L, 1); |
| 7945 | } |
| 7946 | |
| 7947 | next_hdr: |
| 7948 | lua_pop(L, 1); |
| 7949 | } |
| 7950 | skip_headers: |
| 7951 | lua_pop(L, 1); |
| 7952 | |
| 7953 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7954 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7955 | const char *clen = ultoa(body_len); |
| 7956 | |
| 7957 | h1m.flags |= H1_MF_CLEN; |
| 7958 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7959 | goto fail; |
| 7960 | } |
| 7961 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7962 | h1m.flags |= H1_MF_XFER_LEN; |
| 7963 | |
| 7964 | /* Update HTX start-line flags */ |
| 7965 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7966 | flags |= HTX_SL_F_XFER_ENC; |
| 7967 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7968 | flags |= HTX_SL_F_XFER_LEN; |
| 7969 | if (h1m.flags & H1_MF_CHNK) |
| 7970 | flags |= HTX_SL_F_CHNK; |
| 7971 | else if (h1m.flags & H1_MF_CLEN) |
| 7972 | flags |= HTX_SL_F_CLEN; |
| 7973 | if (h1m.body_len == 0) |
| 7974 | flags |= HTX_SL_F_BODYLESS; |
| 7975 | } |
| 7976 | sl->flags |= flags; |
| 7977 | |
| 7978 | |
| 7979 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7980 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7981 | goto fail; |
| 7982 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7983 | htx->flags |= HTX_FL_EOM; |
| 7984 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7985 | /* Now, forward the response and terminate the transaction */ |
| 7986 | s->txn->status = code; |
| 7987 | htx_to_buf(htx, &s->res.buf); |
| 7988 | if (!http_forward_proxy_resp(s, 1)) |
| 7989 | goto fail; |
| 7990 | |
| 7991 | return 1; |
| 7992 | |
| 7993 | fail: |
| 7994 | channel_htx_truncate(&s->res, htx); |
| 7995 | return 0; |
| 7996 | } |
| 7997 | |
| 7998 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 7999 | * processing is just aborted. Nothing is returned to the client and all |
| 8000 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8001 | * is forwarded to the client before terminating the transaction. On success, |
| 8002 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8003 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8004 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8005 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8006 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8007 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8008 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8009 | struct stream *s; |
| 8010 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8011 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8012 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8013 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8014 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8015 | * just end the execution of the current lua code. */ |
| 8016 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8017 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8018 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8019 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8020 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8021 | struct channel *req = &s->req; |
| 8022 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8023 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8024 | channel_auto_read(req); |
| 8025 | channel_abort(req); |
| 8026 | channel_auto_close(req); |
| 8027 | channel_erase(req); |
| 8028 | |
| 8029 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 8030 | channel_auto_read(res); |
| 8031 | channel_auto_close(res); |
| 8032 | channel_shutr_now(res); |
| 8033 | |
| 8034 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8035 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8036 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8037 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8038 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8039 | /* No reply or invalid reply */ |
| 8040 | s->txn->status = 0; |
| 8041 | http_reply_and_close(s, 0, NULL); |
| 8042 | } |
| 8043 | else { |
| 8044 | /* Remove extra args to have the reply on top of the stack */ |
| 8045 | if (lua_gettop(L) > 2) |
| 8046 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8047 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8048 | if (!hlua_txn_forward_reply(L, s)) { |
| 8049 | if (!(s->flags & SF_ERR_MASK)) |
| 8050 | s->flags |= SF_ERR_PRXCOND; |
| 8051 | lua_pushinteger(L, ACT_RET_ERR); |
| 8052 | WILL_LJMP(hlua_done(L)); |
| 8053 | return 0; /* Never reached */ |
| 8054 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8055 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8056 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8057 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8058 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8059 | /* let's log the request time */ |
| 8060 | s->logs.tv_request = now; |
| 8061 | 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] | 8062 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8063 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8064 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8065 | done: |
| 8066 | if (!(s->flags & SF_ERR_MASK)) |
| 8067 | s->flags |= SF_ERR_LOCAL; |
| 8068 | if (!(s->flags & SF_FINST_MASK)) |
| 8069 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8070 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8071 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8072 | lua_pushinteger(L, -1); |
| 8073 | else |
| 8074 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8075 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8076 | return 0; |
| 8077 | } |
| 8078 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8079 | /* |
| 8080 | * |
| 8081 | * |
| 8082 | * Class REPLY |
| 8083 | * |
| 8084 | * |
| 8085 | */ |
| 8086 | |
| 8087 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8088 | * free slots, the function fails and returns 0; |
| 8089 | */ |
| 8090 | static int hlua_txn_reply_new(lua_State *L) |
| 8091 | { |
| 8092 | struct hlua_txn *htxn; |
| 8093 | const char *reason, *body = NULL; |
| 8094 | int ret, status; |
| 8095 | |
| 8096 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8097 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8098 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8099 | WILL_LJMP(lua_error(L)); |
| 8100 | } |
| 8101 | |
| 8102 | /* Default value */ |
| 8103 | status = 200; |
| 8104 | reason = http_get_reason(status); |
| 8105 | |
| 8106 | if (lua_istable(L, 2)) { |
| 8107 | /* load status and reason from the table argument at index 2 */ |
| 8108 | ret = lua_getfield(L, 2, "status"); |
| 8109 | if (ret == LUA_TNIL) |
| 8110 | goto reason; |
| 8111 | else if (ret != LUA_TNUMBER) { |
| 8112 | /* invalid status: ignore the reason */ |
| 8113 | goto body; |
| 8114 | } |
| 8115 | status = lua_tointeger(L, -1); |
| 8116 | |
| 8117 | reason: |
| 8118 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8119 | ret = lua_getfield(L, 2, "reason"); |
| 8120 | if (ret == LUA_TSTRING) |
| 8121 | reason = lua_tostring(L, -1); |
| 8122 | |
| 8123 | body: |
| 8124 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8125 | ret = lua_getfield(L, 2, "body"); |
| 8126 | if (ret == LUA_TSTRING) |
| 8127 | body = lua_tostring(L, -1); |
| 8128 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8129 | } |
| 8130 | |
| 8131 | /* Create the Reply table */ |
| 8132 | lua_newtable(L); |
| 8133 | |
| 8134 | /* Add status element */ |
| 8135 | lua_pushstring(L, "status"); |
| 8136 | lua_pushinteger(L, status); |
| 8137 | lua_settable(L, -3); |
| 8138 | |
| 8139 | /* Add reason element */ |
| 8140 | reason = http_get_reason(status); |
| 8141 | lua_pushstring(L, "reason"); |
| 8142 | lua_pushstring(L, reason); |
| 8143 | lua_settable(L, -3); |
| 8144 | |
| 8145 | /* Add body element, nil if undefined */ |
| 8146 | lua_pushstring(L, "body"); |
| 8147 | if (body) |
| 8148 | lua_pushstring(L, body); |
| 8149 | else |
| 8150 | lua_pushnil(L); |
| 8151 | lua_settable(L, -3); |
| 8152 | |
| 8153 | /* Add headers element */ |
| 8154 | lua_pushstring(L, "headers"); |
| 8155 | lua_newtable(L); |
| 8156 | |
| 8157 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8158 | if (lua_istable(L, 2)) { |
| 8159 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8160 | ret = lua_getfield(L, 2, "headers"); |
| 8161 | if (ret == LUA_TTABLE) { |
| 8162 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8163 | lua_pushnil(L); |
| 8164 | while (lua_next(L, -2) != 0) { |
| 8165 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8166 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8167 | /* invalid value type, skip it */ |
| 8168 | lua_pop(L, 1); |
| 8169 | continue; |
| 8170 | } |
| 8171 | |
| 8172 | |
| 8173 | /* Duplicate the key and swap it with the value. */ |
| 8174 | lua_pushvalue(L, -2); |
| 8175 | lua_insert(L, -2); |
| 8176 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8177 | |
| 8178 | lua_newtable(L); |
| 8179 | lua_insert(L, -2); |
| 8180 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8181 | |
| 8182 | if (lua_isstring(L, -1)) { |
| 8183 | /* push the value in the inner table */ |
| 8184 | lua_rawseti(L, -2, 1); |
| 8185 | } |
| 8186 | else { /* table */ |
| 8187 | lua_pushnil(L); |
| 8188 | while (lua_next(L, -2) != 0) { |
| 8189 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8190 | if (!lua_isstring(L, -1)) { |
| 8191 | /* invalid value type, skip it*/ |
| 8192 | lua_pop(L, 1); |
| 8193 | continue; |
| 8194 | } |
| 8195 | /* push the value in the inner table */ |
| 8196 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8197 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8198 | } |
| 8199 | lua_pop(L, 1); |
| 8200 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8201 | } |
| 8202 | |
| 8203 | /* push (k,v) on the stack in the headers table: |
| 8204 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8205 | */ |
| 8206 | lua_settable(L, -5); |
| 8207 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8208 | } |
| 8209 | } |
| 8210 | lua_pop(L, 1); |
| 8211 | } |
| 8212 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8213 | lua_settable(L, -3); |
| 8214 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8215 | |
| 8216 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8217 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8218 | lua_setmetatable(L, -2); |
| 8219 | return 1; |
| 8220 | } |
| 8221 | |
| 8222 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8223 | * provided, the default one corresponding to the status code is used. |
| 8224 | */ |
| 8225 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8226 | { |
| 8227 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8228 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8229 | |
| 8230 | /* First argument (self) must be a table */ |
| 8231 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8232 | |
| 8233 | if (status < 100 || status > 599) { |
| 8234 | lua_pushboolean(L, 0); |
| 8235 | return 1; |
| 8236 | } |
| 8237 | if (!reason) |
| 8238 | reason = http_get_reason(status); |
| 8239 | |
| 8240 | lua_pushinteger(L, status); |
| 8241 | lua_setfield(L, 1, "status"); |
| 8242 | |
| 8243 | lua_pushstring(L, reason); |
| 8244 | lua_setfield(L, 1, "reason"); |
| 8245 | |
| 8246 | lua_pushboolean(L, 1); |
| 8247 | return 1; |
| 8248 | } |
| 8249 | |
| 8250 | /* Add a header into the reply object. Each header name is associated to an |
| 8251 | * array of values in the "headers" table. If the header name is not found, a |
| 8252 | * new entry is created. |
| 8253 | */ |
| 8254 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8255 | { |
| 8256 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8257 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8258 | int ret; |
| 8259 | |
| 8260 | /* First argument (self) must be a table */ |
| 8261 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8262 | |
| 8263 | /* Push in the stack the "headers" entry. */ |
| 8264 | ret = lua_getfield(L, 1, "headers"); |
| 8265 | if (ret != LUA_TTABLE) { |
| 8266 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8267 | WILL_LJMP(lua_error(L)); |
| 8268 | } |
| 8269 | |
| 8270 | /* check if the header is already registered. If not, register it. */ |
| 8271 | ret = lua_getfield(L, -1, name); |
| 8272 | if (ret == LUA_TNIL) { |
| 8273 | /* Entry not found. */ |
| 8274 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8275 | |
| 8276 | /* Insert the new header name in the array in the top of the stack. |
| 8277 | * It left the new array in the top of the stack. |
| 8278 | */ |
| 8279 | lua_newtable(L); |
| 8280 | lua_pushstring(L, name); |
| 8281 | lua_pushvalue(L, -2); |
| 8282 | lua_settable(L, -4); |
| 8283 | } |
| 8284 | else if (ret != LUA_TTABLE) { |
| 8285 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8286 | WILL_LJMP(lua_error(L)); |
| 8287 | } |
| 8288 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8289 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8290 | * the header value as new entry. |
| 8291 | */ |
| 8292 | lua_pushstring(L, value); |
| 8293 | ret = lua_rawlen(L, -2); |
| 8294 | lua_rawseti(L, -2, ret + 1); |
| 8295 | |
| 8296 | lua_pushboolean(L, 1); |
| 8297 | return 1; |
| 8298 | } |
| 8299 | |
| 8300 | /* Remove all occurrences of a given header name. */ |
| 8301 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8302 | { |
| 8303 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8304 | int ret; |
| 8305 | |
| 8306 | /* First argument (self) must be a table */ |
| 8307 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8308 | |
| 8309 | /* Push in the stack the "headers" entry. */ |
| 8310 | ret = lua_getfield(L, 1, "headers"); |
| 8311 | if (ret != LUA_TTABLE) { |
| 8312 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8313 | WILL_LJMP(lua_error(L)); |
| 8314 | } |
| 8315 | |
| 8316 | lua_pushstring(L, name); |
| 8317 | lua_pushnil(L); |
| 8318 | lua_settable(L, -3); |
| 8319 | |
| 8320 | lua_pushboolean(L, 1); |
| 8321 | return 1; |
| 8322 | } |
| 8323 | |
| 8324 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8325 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8326 | { |
| 8327 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8328 | |
| 8329 | /* First argument (self) must be a table */ |
| 8330 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8331 | |
| 8332 | lua_pushstring(L, payload); |
| 8333 | lua_setfield(L, 1, "body"); |
| 8334 | |
| 8335 | lua_pushboolean(L, 1); |
| 8336 | return 1; |
| 8337 | } |
| 8338 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8339 | __LJMP static int hlua_log(lua_State *L) |
| 8340 | { |
| 8341 | int level; |
| 8342 | const char *msg; |
| 8343 | |
| 8344 | MAY_LJMP(check_args(L, 2, "log")); |
| 8345 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8346 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8347 | |
| 8348 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8349 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8350 | |
| 8351 | hlua_sendlog(NULL, level, msg); |
| 8352 | return 0; |
| 8353 | } |
| 8354 | |
| 8355 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8356 | { |
| 8357 | const char *msg; |
| 8358 | |
| 8359 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8360 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8361 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8362 | return 0; |
| 8363 | } |
| 8364 | |
| 8365 | __LJMP static int hlua_log_info(lua_State *L) |
| 8366 | { |
| 8367 | const char *msg; |
| 8368 | |
| 8369 | MAY_LJMP(check_args(L, 1, "info")); |
| 8370 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8371 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8372 | return 0; |
| 8373 | } |
| 8374 | |
| 8375 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8376 | { |
| 8377 | const char *msg; |
| 8378 | |
| 8379 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8380 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8381 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8382 | return 0; |
| 8383 | } |
| 8384 | |
| 8385 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8386 | { |
| 8387 | const char *msg; |
| 8388 | |
| 8389 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8390 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8391 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8392 | return 0; |
| 8393 | } |
| 8394 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8395 | __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] | 8396 | { |
| 8397 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8398 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8399 | 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] | 8400 | return 0; |
| 8401 | } |
| 8402 | |
| 8403 | __LJMP static int hlua_sleep(lua_State *L) |
| 8404 | { |
| 8405 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8406 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8407 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8408 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8409 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8410 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8411 | wakeup_ms = tick_add(now_ms, delay); |
| 8412 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8413 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8414 | 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] | 8415 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8416 | } |
| 8417 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8418 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8419 | { |
| 8420 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8421 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8422 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8423 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8424 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8425 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8426 | wakeup_ms = tick_add(now_ms, delay); |
| 8427 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8428 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8429 | 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] | 8430 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8431 | } |
| 8432 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8433 | /* This functionis an LUA binding. it permits to give back |
| 8434 | * the hand at the HAProxy scheduler. It is used when the |
| 8435 | * LUA processing consumes a lot of time. |
| 8436 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8437 | __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] | 8438 | { |
| 8439 | return 0; |
| 8440 | } |
| 8441 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8442 | __LJMP static int hlua_yield(lua_State *L) |
| 8443 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8444 | 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] | 8445 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8446 | } |
| 8447 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8448 | /* This function change the nice of the currently executed |
| 8449 | * task. It is used set low or high priority at the current |
| 8450 | * task. |
| 8451 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8452 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8453 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8454 | struct hlua *hlua; |
| 8455 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8456 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8457 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8458 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8459 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8460 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8461 | hlua = hlua_gethlua(L); |
| 8462 | |
| 8463 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8464 | if (!hlua || !hlua->task) |
| 8465 | return 0; |
| 8466 | |
| 8467 | if (nice < -1024) |
| 8468 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8469 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8470 | nice = 1024; |
| 8471 | |
| 8472 | hlua->task->nice = nice; |
| 8473 | return 0; |
| 8474 | } |
| 8475 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8476 | /* 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] | 8477 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8478 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8479 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8480 | * |
| 8481 | * Task wrapper are longjmp safe because the only one Lua code |
| 8482 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8483 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8484 | 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] | 8485 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8486 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8487 | enum hlua_exec status; |
| 8488 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8489 | if (task->tid < 0) |
| 8490 | task->tid = tid; |
| 8491 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8492 | /* If it is the first call to the task, we must initialize the |
| 8493 | * execution timeouts. |
| 8494 | */ |
| 8495 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8496 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8497 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8498 | /* Execute the Lua code. */ |
| 8499 | status = hlua_ctx_resume(hlua, 1); |
| 8500 | |
| 8501 | switch (status) { |
| 8502 | /* finished or yield */ |
| 8503 | case HLUA_E_OK: |
| 8504 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8505 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8506 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8507 | break; |
| 8508 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8509 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8510 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8511 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8512 | break; |
| 8513 | |
| 8514 | /* finished with error. */ |
| 8515 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8516 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8517 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8518 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8519 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8520 | break; |
| 8521 | |
| 8522 | case HLUA_E_ERR: |
| 8523 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8524 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8525 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8526 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8527 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8528 | break; |
| 8529 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8530 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8531 | } |
| 8532 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8533 | /* This function is an LUA binding that register LUA function to be |
| 8534 | * executed after the HAProxy configuration parsing and before the |
| 8535 | * HAProxy scheduler starts. This function expect only one LUA |
| 8536 | * argument that is a function. This function returns nothing, but |
| 8537 | * throws if an error is encountered. |
| 8538 | */ |
| 8539 | __LJMP static int hlua_register_init(lua_State *L) |
| 8540 | { |
| 8541 | struct hlua_init_function *init; |
| 8542 | int ref; |
| 8543 | |
| 8544 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8545 | |
| 8546 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8547 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8548 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8549 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8550 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8551 | |
| 8552 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8553 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8554 | return 0; |
| 8555 | } |
| 8556 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8557 | /* This functio is an LUA binding. It permits to register a task |
| 8558 | * executed in parallel of the main HAroxy activity. The task is |
| 8559 | * created and it is set in the HAProxy scheduler. It can be called |
| 8560 | * from the "init" section, "post init" or during the runtime. |
| 8561 | * |
| 8562 | * Lua prototype: |
| 8563 | * |
| 8564 | * <none> core.register_task(<function>) |
| 8565 | */ |
| 8566 | static int hlua_register_task(lua_State *L) |
| 8567 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8568 | struct hlua *hlua = NULL; |
| 8569 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8570 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8571 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8572 | |
| 8573 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8574 | |
| 8575 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8576 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8577 | /* Get the reference state. If the reference is NULL, L is the master |
| 8578 | * state, otherwise hlua->T is. |
| 8579 | */ |
| 8580 | hlua = hlua_gethlua(L); |
| 8581 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8582 | /* we are in runtime processing */ |
| 8583 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8584 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8585 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8586 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8587 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8588 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8589 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8590 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8591 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8592 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8593 | /* We are in the common lua state, execute the task anywhere, |
| 8594 | * otherwise, inherit the current thread identifier |
| 8595 | */ |
| 8596 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8597 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8598 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8599 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8600 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8601 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8602 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8603 | task->context = hlua; |
| 8604 | task->process = hlua_process_task; |
| 8605 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8606 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8607 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8608 | |
| 8609 | /* Restore the function in the stack. */ |
| 8610 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8611 | hlua->nargs = 0; |
| 8612 | |
| 8613 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8614 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8615 | |
| 8616 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8617 | |
| 8618 | alloc_error: |
| 8619 | task_destroy(task); |
| 8620 | hlua_ctx_destroy(hlua); |
| 8621 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8622 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8623 | } |
| 8624 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8625 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8626 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8627 | * resume converters. |
| 8628 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8629 | 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] | 8630 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8631 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8632 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8633 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8634 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8635 | if (!stream) |
| 8636 | return 0; |
| 8637 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8638 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8639 | * Lua context can be not initialized. This behavior |
| 8640 | * permits to save performances because a systematic |
| 8641 | * Lua initialization cause 5% performances loss. |
| 8642 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8643 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8644 | struct hlua *hlua; |
| 8645 | |
| 8646 | hlua = pool_alloc(pool_head_hlua); |
| 8647 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8648 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8649 | return 0; |
| 8650 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8651 | HLUA_INIT(hlua); |
| 8652 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8653 | 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] | 8654 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8655 | return 0; |
| 8656 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8657 | } |
| 8658 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8659 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8660 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8661 | |
| 8662 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8663 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8664 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8665 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8666 | else |
| 8667 | error = "critical error"; |
| 8668 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8669 | return 0; |
| 8670 | } |
| 8671 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8672 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8673 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8674 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8675 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8676 | return 0; |
| 8677 | } |
| 8678 | |
| 8679 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8680 | 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] | 8681 | |
| 8682 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8683 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8684 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8685 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8686 | return 0; |
| 8687 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8688 | hlua_smp2lua(stream->hlua->T, smp); |
| 8689 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8690 | |
| 8691 | /* push keywords in the stack. */ |
| 8692 | if (arg_p) { |
| 8693 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8694 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8695 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8696 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8697 | return 0; |
| 8698 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8699 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8700 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8701 | } |
| 8702 | } |
| 8703 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8704 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8705 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8706 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8707 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8708 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8709 | } |
| 8710 | |
| 8711 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8712 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8713 | /* finished. */ |
| 8714 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8715 | /* If the stack is empty, the function fails. */ |
| 8716 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8717 | return 0; |
| 8718 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8719 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8720 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8721 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8722 | return 1; |
| 8723 | |
| 8724 | /* yield. */ |
| 8725 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8726 | 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] | 8727 | return 0; |
| 8728 | |
| 8729 | /* finished with error. */ |
| 8730 | case HLUA_E_ERRMSG: |
| 8731 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8732 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8733 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8734 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8735 | return 0; |
| 8736 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8737 | case HLUA_E_ETMOUT: |
| 8738 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8739 | return 0; |
| 8740 | |
| 8741 | case HLUA_E_NOMEM: |
| 8742 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8743 | return 0; |
| 8744 | |
| 8745 | case HLUA_E_YIELD: |
| 8746 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8747 | return 0; |
| 8748 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8749 | case HLUA_E_ERR: |
| 8750 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8751 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 8752 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8753 | |
| 8754 | default: |
| 8755 | return 0; |
| 8756 | } |
| 8757 | } |
| 8758 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8759 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8760 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8761 | * resume sample-fetches. This function will be called by the sample |
| 8762 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8763 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8764 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8765 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8766 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8767 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8768 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8769 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8770 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8771 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8772 | if (!stream) |
| 8773 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8774 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8775 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8776 | * Lua context can be not initialized. This behavior |
| 8777 | * permits to save performances because a systematic |
| 8778 | * Lua initialization cause 5% performances loss. |
| 8779 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8780 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8781 | struct hlua *hlua; |
| 8782 | |
| 8783 | hlua = pool_alloc(pool_head_hlua); |
| 8784 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8785 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8786 | return 0; |
| 8787 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8788 | hlua->T = NULL; |
| 8789 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8790 | 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] | 8791 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8792 | return 0; |
| 8793 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8794 | } |
| 8795 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8796 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8797 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8798 | |
| 8799 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8800 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8801 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8802 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8803 | else |
| 8804 | error = "critical error"; |
| 8805 | 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] | 8806 | return 0; |
| 8807 | } |
| 8808 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8809 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8810 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8811 | 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] | 8812 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8813 | return 0; |
| 8814 | } |
| 8815 | |
| 8816 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8817 | 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] | 8818 | |
| 8819 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8820 | 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] | 8821 | 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] | 8822 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8823 | return 0; |
| 8824 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8825 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8826 | |
| 8827 | /* push keywords in the stack. */ |
| 8828 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8829 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8830 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8831 | 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] | 8832 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8833 | return 0; |
| 8834 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8835 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8836 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8837 | } |
| 8838 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8839 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8840 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8841 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8842 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8843 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8844 | } |
| 8845 | |
| 8846 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8847 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8848 | /* finished. */ |
| 8849 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8850 | /* If the stack is empty, the function fails. */ |
| 8851 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8852 | return 0; |
| 8853 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8854 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8855 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8856 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8857 | |
| 8858 | /* Set the end of execution flag. */ |
| 8859 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8860 | return 1; |
| 8861 | |
| 8862 | /* yield. */ |
| 8863 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8864 | 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] | 8865 | return 0; |
| 8866 | |
| 8867 | /* finished with error. */ |
| 8868 | case HLUA_E_ERRMSG: |
| 8869 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8870 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8871 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8872 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8873 | return 0; |
| 8874 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8875 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8876 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8877 | return 0; |
| 8878 | |
| 8879 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8880 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8881 | return 0; |
| 8882 | |
| 8883 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8884 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8885 | return 0; |
| 8886 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8887 | case HLUA_E_ERR: |
| 8888 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8889 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Tim Duesterhus | 588b314 | 2020-05-29 14:35:51 +0200 | [diff] [blame] | 8890 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8891 | |
| 8892 | default: |
| 8893 | return 0; |
| 8894 | } |
| 8895 | } |
| 8896 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8897 | /* This function is an LUA binding used for registering |
| 8898 | * "sample-conv" functions. It expects a converter name used |
| 8899 | * in the haproxy configuration file, and an LUA function. |
| 8900 | */ |
| 8901 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8902 | { |
| 8903 | struct sample_conv_kw_list *sck; |
| 8904 | const char *name; |
| 8905 | int ref; |
| 8906 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8907 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8908 | struct sample_conv *sc; |
| 8909 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8910 | |
| 8911 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8912 | |
| 8913 | /* First argument : converter name. */ |
| 8914 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8915 | |
| 8916 | /* Second argument : lua function. */ |
| 8917 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8918 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8919 | /* Check if the converter is already registered */ |
| 8920 | trash = get_trash_chunk(); |
| 8921 | chunk_printf(trash, "lua.%s", name); |
| 8922 | sc = find_sample_conv(trash->area, trash->data); |
| 8923 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8924 | fcn = sc->private; |
| 8925 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8926 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8927 | "This will become a hard error in version 2.5.\n", name); |
| 8928 | } |
| 8929 | fcn->function_ref[hlua_state_id] = ref; |
| 8930 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8931 | } |
| 8932 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8933 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8934 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8935 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8936 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8937 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8938 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8939 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8940 | |
| 8941 | /* Fill fcn. */ |
| 8942 | fcn->name = strdup(name); |
| 8943 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8944 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8945 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8946 | |
| 8947 | /* List head */ |
| 8948 | sck->list.n = sck->list.p = NULL; |
| 8949 | |
| 8950 | /* converter keyword. */ |
| 8951 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8952 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8953 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8954 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8955 | |
| 8956 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8957 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8958 | 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] | 8959 | sck->kw[0].val_args = NULL; |
| 8960 | sck->kw[0].in_type = SMP_T_STR; |
| 8961 | sck->kw[0].out_type = SMP_T_STR; |
| 8962 | sck->kw[0].private = fcn; |
| 8963 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8964 | /* Register this new converter */ |
| 8965 | sample_register_convs(sck); |
| 8966 | |
| 8967 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8968 | |
| 8969 | alloc_error: |
| 8970 | release_hlua_function(fcn); |
| 8971 | ha_free(&sck); |
| 8972 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8973 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8974 | } |
| 8975 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8976 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8977 | * "sample-fetch" functions. It expects a converter name used |
| 8978 | * in the haproxy configuration file, and an LUA function. |
| 8979 | */ |
| 8980 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8981 | { |
| 8982 | const char *name; |
| 8983 | int ref; |
| 8984 | int len; |
| 8985 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8986 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8987 | struct sample_fetch *sf; |
| 8988 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8989 | |
| 8990 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8991 | |
| 8992 | /* First argument : sample-fetch name. */ |
| 8993 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8994 | |
| 8995 | /* Second argument : lua function. */ |
| 8996 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8997 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8998 | /* Check if the sample-fetch is already registered */ |
| 8999 | trash = get_trash_chunk(); |
| 9000 | chunk_printf(trash, "lua.%s", name); |
| 9001 | sf = find_sample_fetch(trash->area, trash->data); |
| 9002 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9003 | fcn = sf->private; |
| 9004 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9005 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9006 | "This will become a hard error in version 2.5.\n", name); |
| 9007 | } |
| 9008 | fcn->function_ref[hlua_state_id] = ref; |
| 9009 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9010 | } |
| 9011 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9012 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9013 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9014 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9015 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9016 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9017 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9018 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9019 | |
| 9020 | /* Fill fcn. */ |
| 9021 | fcn->name = strdup(name); |
| 9022 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9023 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9024 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9025 | |
| 9026 | /* List head */ |
| 9027 | sfk->list.n = sfk->list.p = NULL; |
| 9028 | |
| 9029 | /* sample-fetch keyword. */ |
| 9030 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9031 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9032 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9033 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9034 | |
| 9035 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9036 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9037 | 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] | 9038 | sfk->kw[0].val_args = NULL; |
| 9039 | sfk->kw[0].out_type = SMP_T_STR; |
| 9040 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9041 | sfk->kw[0].val = 0; |
| 9042 | sfk->kw[0].private = fcn; |
| 9043 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9044 | /* Register this new fetch. */ |
| 9045 | sample_register_fetches(sfk); |
| 9046 | |
| 9047 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9048 | |
| 9049 | alloc_error: |
| 9050 | release_hlua_function(fcn); |
| 9051 | ha_free(&sfk); |
| 9052 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9053 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9054 | } |
| 9055 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9056 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9057 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9058 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9059 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9060 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9061 | unsigned int delay; |
| 9062 | unsigned int wakeup_ms; |
| 9063 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9064 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9065 | hlua = hlua_gethlua(L); |
| 9066 | if (!hlua) { |
| 9067 | return 0; |
| 9068 | } |
| 9069 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9070 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9071 | |
| 9072 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9073 | wakeup_ms = tick_add(now_ms, delay); |
| 9074 | hlua->wake_time = wakeup_ms; |
| 9075 | return 0; |
| 9076 | } |
| 9077 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9078 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9079 | * wrapper during the initialisation period. This function may return any |
| 9080 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9081 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9082 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9083 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9084 | 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] | 9085 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9086 | { |
| 9087 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9088 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9089 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9090 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9091 | |
| 9092 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9093 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9094 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9095 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9096 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9097 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9098 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9099 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9100 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9101 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9102 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9103 | * Lua context can be not initialized. This behavior |
| 9104 | * permits to save performances because a systematic |
| 9105 | * Lua initialization cause 5% performances loss. |
| 9106 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9107 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9108 | struct hlua *hlua; |
| 9109 | |
| 9110 | hlua = pool_alloc(pool_head_hlua); |
| 9111 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9112 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9113 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9114 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9115 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9116 | HLUA_INIT(hlua); |
| 9117 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9118 | 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] | 9119 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9120 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9121 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9122 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9123 | } |
| 9124 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9125 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9126 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9127 | |
| 9128 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9129 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9130 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9131 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9132 | else |
| 9133 | error = "critical error"; |
| 9134 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9135 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9136 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9137 | } |
| 9138 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9139 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9140 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9141 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9142 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9143 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9144 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9145 | } |
| 9146 | |
| 9147 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9148 | 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] | 9149 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9150 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9151 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9152 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9153 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9154 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9155 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9156 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9157 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9158 | |
| 9159 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9160 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9161 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9162 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9163 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9164 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9165 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9166 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9167 | lua_pushstring(s->hlua->T, *arg); |
| 9168 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9169 | } |
| 9170 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9171 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9172 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9173 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9174 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9175 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9176 | } |
| 9177 | |
| 9178 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9179 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9180 | /* finished. */ |
| 9181 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9182 | /* Catch the return value */ |
| 9183 | if (lua_gettop(s->hlua->T) > 0) |
| 9184 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9185 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9186 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9187 | if (act_ret == ACT_RET_YIELD) { |
| 9188 | if (flags & ACT_OPT_FINAL) |
| 9189 | goto err_yield; |
| 9190 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9191 | if (dir == SMP_OPT_DIR_REQ) |
| 9192 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9193 | s->hlua->wake_time); |
| 9194 | else |
| 9195 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9196 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9197 | } |
| 9198 | goto end; |
| 9199 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9200 | /* yield. */ |
| 9201 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9202 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9203 | if (dir == SMP_OPT_DIR_REQ) |
| 9204 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9205 | s->hlua->wake_time); |
| 9206 | else |
| 9207 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9208 | s->hlua->wake_time); |
| 9209 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9210 | /* Some actions can be wake up when a "write" event |
| 9211 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9212 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9213 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9214 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9215 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9216 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9217 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9218 | act_ret = ACT_RET_YIELD; |
| 9219 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9220 | |
| 9221 | /* finished with error. */ |
| 9222 | case HLUA_E_ERRMSG: |
| 9223 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9224 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9225 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9226 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9227 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9228 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9229 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9230 | 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] | 9231 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9232 | |
| 9233 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9234 | 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] | 9235 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9236 | |
| 9237 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9238 | err_yield: |
| 9239 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9240 | 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] | 9241 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9242 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9243 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9244 | case HLUA_E_ERR: |
| 9245 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9246 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9247 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9248 | |
| 9249 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9250 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9251 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9252 | |
| 9253 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9254 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9255 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9256 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9257 | } |
| 9258 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9259 | 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] | 9260 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9261 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9262 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9263 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9264 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9265 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9266 | } |
| 9267 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9268 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9269 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9270 | 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] | 9271 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9272 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9273 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9274 | struct task *task; |
| 9275 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9276 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9277 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9278 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9279 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9280 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9281 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9282 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9283 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9284 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9285 | tcp_ctx->hlua = hlua; |
| 9286 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9287 | |
| 9288 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9289 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9290 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9291 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9292 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9293 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9294 | } |
| 9295 | task->nice = 0; |
| 9296 | task->context = ctx; |
| 9297 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9298 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | |
| 9300 | /* In the execution wrappers linked with a stream, the |
| 9301 | * Lua context can be not initialized. This behavior |
| 9302 | * permits to save performances because a systematic |
| 9303 | * Lua initialization cause 5% performances loss. |
| 9304 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9305 | 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] | 9306 | 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] | 9307 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9308 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9309 | } |
| 9310 | |
| 9311 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9312 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9313 | |
| 9314 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9315 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9316 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9317 | error = lua_tostring(hlua->T, -1); |
| 9318 | else |
| 9319 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9320 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9321 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9322 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9323 | } |
| 9324 | |
| 9325 | /* Check stack available size. */ |
| 9326 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9327 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9328 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9329 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9330 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9331 | } |
| 9332 | |
| 9333 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9334 | 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] | 9335 | |
| 9336 | /* Create and and push object stream in the stack. */ |
| 9337 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9338 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9339 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9340 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9341 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9342 | } |
| 9343 | hlua->nargs = 1; |
| 9344 | |
| 9345 | /* push keywords in the stack. */ |
| 9346 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9347 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9348 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9349 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9350 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9351 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9352 | } |
| 9353 | lua_pushstring(hlua->T, *arg); |
| 9354 | hlua->nargs++; |
| 9355 | } |
| 9356 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9357 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9358 | |
| 9359 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9360 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9361 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9362 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9363 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9364 | } |
| 9365 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9366 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9367 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9368 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9369 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9370 | struct stream *strm = __sc_strm(sc); |
| 9371 | struct channel *res = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9372 | struct act_rule *rule = ctx->rule; |
| 9373 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9374 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9375 | |
| 9376 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9377 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9378 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9379 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9380 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9381 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9382 | |
| 9383 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9384 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9385 | return; |
| 9386 | |
| 9387 | /* Execute the function. */ |
| 9388 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9389 | /* finished. */ |
| 9390 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9391 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9392 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9393 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9394 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9395 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9396 | sc_shutr(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9397 | return; |
| 9398 | |
| 9399 | /* yield. */ |
| 9400 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9401 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9402 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9403 | return; |
| 9404 | |
| 9405 | /* finished with error. */ |
| 9406 | case HLUA_E_ERRMSG: |
| 9407 | /* Display log. */ |
| 9408 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9409 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9410 | lua_pop(hlua->T, 1); |
| 9411 | goto error; |
| 9412 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9413 | case HLUA_E_ETMOUT: |
| 9414 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9415 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9416 | goto error; |
| 9417 | |
| 9418 | case HLUA_E_NOMEM: |
| 9419 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9420 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9421 | goto error; |
| 9422 | |
| 9423 | case HLUA_E_YIELD: /* unexpected */ |
| 9424 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9425 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9426 | goto error; |
| 9427 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9428 | case HLUA_E_ERR: |
| 9429 | /* Display log. */ |
| 9430 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9431 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9432 | goto error; |
| 9433 | |
| 9434 | default: |
| 9435 | goto error; |
| 9436 | } |
| 9437 | |
| 9438 | error: |
| 9439 | |
| 9440 | /* For all other cases, just close the stream. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9441 | sc_shutw(sc); |
| 9442 | sc_shutr(sc); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9443 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9444 | } |
| 9445 | |
| 9446 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9447 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9448 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9449 | |
| 9450 | task_destroy(tcp_ctx->task); |
| 9451 | tcp_ctx->task = NULL; |
| 9452 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9453 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9454 | } |
| 9455 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9456 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9457 | * 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] | 9458 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9459 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9460 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9461 | 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] | 9462 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9463 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9464 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9465 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9466 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9467 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9468 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9469 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9470 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9471 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9472 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9473 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9474 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9475 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9476 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9477 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9478 | http_ctx->hlua = hlua; |
| 9479 | http_ctx->left_bytes = -1; |
| 9480 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9481 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9482 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9483 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9484 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9485 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9486 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9487 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9488 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9489 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9490 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9491 | } |
| 9492 | task->nice = 0; |
| 9493 | task->context = ctx; |
| 9494 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9495 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9496 | |
| 9497 | /* In the execution wrappers linked with a stream, the |
| 9498 | * Lua context can be not initialized. This behavior |
| 9499 | * permits to save performances because a systematic |
| 9500 | * Lua initialization cause 5% performances loss. |
| 9501 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9502 | 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] | 9503 | 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] | 9504 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9505 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9506 | } |
| 9507 | |
| 9508 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9509 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9510 | |
| 9511 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9512 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9513 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9514 | error = lua_tostring(hlua->T, -1); |
| 9515 | else |
| 9516 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9517 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9518 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9519 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9520 | } |
| 9521 | |
| 9522 | /* Check stack available size. */ |
| 9523 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9524 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9525 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9526 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9527 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9528 | } |
| 9529 | |
| 9530 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9531 | 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] | 9532 | |
| 9533 | /* Create and and push object stream in the stack. */ |
| 9534 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9535 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9536 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9537 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9538 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9539 | } |
| 9540 | hlua->nargs = 1; |
| 9541 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9542 | /* push keywords in the stack. */ |
| 9543 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9544 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9545 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9546 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9547 | RESET_SAFE_LJMP(hlua); |
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 | lua_pushstring(hlua->T, *arg); |
| 9551 | hlua->nargs++; |
| 9552 | } |
| 9553 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9554 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9555 | |
| 9556 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9557 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9558 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9559 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9560 | } |
| 9561 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9562 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9563 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9564 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9565 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9566 | struct stream *strm = __sc_strm(sc); |
| 9567 | struct channel *req = sc_oc(sc); |
| 9568 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9569 | struct act_rule *rule = ctx->rule; |
| 9570 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9571 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9572 | struct htx *req_htx, *res_htx; |
| 9573 | |
| 9574 | res_htx = htx_from_buf(&res->buf); |
| 9575 | |
| 9576 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9577 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9578 | goto out; |
| 9579 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9580 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9581 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9582 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9583 | goto out; |
| 9584 | } |
| 9585 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9586 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9587 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9588 | |
| 9589 | /* Set the currently running flag. */ |
| 9590 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9591 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9592 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9593 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9594 | goto out; |
| 9595 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9596 | } |
| 9597 | |
| 9598 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9599 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9600 | |
| 9601 | /* Execute the function. */ |
| 9602 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9603 | /* finished. */ |
| 9604 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9605 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9606 | break; |
| 9607 | |
| 9608 | /* yield. */ |
| 9609 | case HLUA_E_AGAIN: |
| 9610 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9611 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9612 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9613 | |
| 9614 | /* finished with error. */ |
| 9615 | case HLUA_E_ERRMSG: |
| 9616 | /* Display log. */ |
| 9617 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9618 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9619 | lua_pop(hlua->T, 1); |
| 9620 | goto error; |
| 9621 | |
| 9622 | case HLUA_E_ETMOUT: |
| 9623 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9624 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9625 | goto error; |
| 9626 | |
| 9627 | case HLUA_E_NOMEM: |
| 9628 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9629 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9630 | goto error; |
| 9631 | |
| 9632 | case HLUA_E_YIELD: /* unexpected */ |
| 9633 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9634 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9635 | goto error; |
| 9636 | |
| 9637 | case HLUA_E_ERR: |
| 9638 | /* Display log. */ |
| 9639 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9640 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9641 | goto error; |
| 9642 | |
| 9643 | default: |
| 9644 | goto error; |
| 9645 | } |
| 9646 | } |
| 9647 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9648 | if (http_ctx->flags & APPLET_DONE) { |
| 9649 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9650 | goto done; |
| 9651 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9652 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9653 | goto error; |
| 9654 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9655 | /* no more data are expected. If the response buffer is empty |
| 9656 | * for a chunked message, be sure to add something (EOT block in |
| 9657 | * this case) to have something to send. It is important to be |
| 9658 | * sure the EOM flags will be handled by the endpoint. |
| 9659 | */ |
| 9660 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9661 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9662 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9663 | goto out; |
| 9664 | } |
| 9665 | channel_add_input(res, 1); |
| 9666 | } |
| 9667 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9668 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9669 | res->flags |= CF_EOI; |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 9670 | se_fl_set(ctx->sedesc, SE_FL_EOI); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9671 | strm->txn->status = http_ctx->status; |
| 9672 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9673 | } |
| 9674 | |
| 9675 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9676 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9677 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9678 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9679 | sc_shutr(sc); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9680 | } |
| 9681 | |
| 9682 | /* eat the whole request */ |
| 9683 | if (co_data(req)) { |
| 9684 | req_htx = htx_from_buf(&req->buf); |
| 9685 | co_htx_skip(req, req_htx, co_data(req)); |
| 9686 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9687 | } |
| 9688 | } |
| 9689 | |
| 9690 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9691 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9692 | return; |
| 9693 | |
| 9694 | error: |
| 9695 | |
| 9696 | /* If we are in HTTP mode, and we are not send any |
| 9697 | * data, return a 500 server error in best effort: |
| 9698 | * if there is no room available in the buffer, |
| 9699 | * just close the connection. |
| 9700 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9701 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9702 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9703 | |
| 9704 | channel_erase(res); |
| 9705 | res->buf.data = b_data(err); |
| 9706 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9707 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9708 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9709 | } |
| 9710 | if (!(strm->flags & SF_ERR_MASK)) |
| 9711 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9712 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9713 | goto done; |
| 9714 | } |
| 9715 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9716 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9717 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9718 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9719 | |
| 9720 | task_destroy(http_ctx->task); |
| 9721 | http_ctx->task = NULL; |
| 9722 | hlua_ctx_destroy(http_ctx->hlua); |
| 9723 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9724 | } |
| 9725 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9726 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9727 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9728 | * |
| 9729 | * This function can fail with an abort() due to an Lua critical error. |
| 9730 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9731 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9732 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9733 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9734 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9735 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9736 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9737 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9738 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9739 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9740 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9741 | if (!rule->arg.hlua_rule) { |
| 9742 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9743 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9744 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9745 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9746 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9747 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9748 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9749 | if (!rule->arg.hlua_rule->args) { |
| 9750 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9751 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9752 | } |
| 9753 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9754 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9755 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9756 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9757 | /* Expect some arguments */ |
| 9758 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9759 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9760 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9761 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9762 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9763 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9764 | if (!rule->arg.hlua_rule->args[i]) { |
| 9765 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9766 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9767 | } |
| 9768 | (*cur_arg)++; |
| 9769 | } |
| 9770 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9771 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9772 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9773 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9774 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9775 | |
| 9776 | error: |
| 9777 | if (rule->arg.hlua_rule) { |
| 9778 | if (rule->arg.hlua_rule->args) { |
| 9779 | for (i = 0; i < fcn->nargs; i++) |
| 9780 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9781 | ha_free(&rule->arg.hlua_rule->args); |
| 9782 | } |
| 9783 | ha_free(&rule->arg.hlua_rule); |
| 9784 | } |
| 9785 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9786 | } |
| 9787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9788 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9789 | struct act_rule *rule, char **err) |
| 9790 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9791 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9792 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9793 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9794 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9795 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9796 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9797 | * the call of this analyzer. |
| 9798 | */ |
| 9799 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9800 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9801 | return ACT_RET_PRS_ERR; |
| 9802 | } |
| 9803 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9804 | /* Memory for the rule. */ |
| 9805 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9806 | if (!rule->arg.hlua_rule) { |
| 9807 | memprintf(err, "out of memory error"); |
| 9808 | return ACT_RET_PRS_ERR; |
| 9809 | } |
| 9810 | |
| 9811 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9812 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9813 | |
| 9814 | /* TODO: later accept arguments. */ |
| 9815 | rule->arg.hlua_rule->args = NULL; |
| 9816 | |
| 9817 | /* Add applet pointer in the rule. */ |
| 9818 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9819 | rule->applet.name = fcn->name; |
| 9820 | rule->applet.init = hlua_applet_http_init; |
| 9821 | rule->applet.fct = hlua_applet_http_fct; |
| 9822 | rule->applet.release = hlua_applet_http_release; |
| 9823 | rule->applet.timeout = hlua_timeout_applet; |
| 9824 | |
| 9825 | return ACT_RET_PRS_OK; |
| 9826 | } |
| 9827 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9828 | /* This function is an LUA binding used for registering |
| 9829 | * "sample-conv" functions. It expects a converter name used |
| 9830 | * in the haproxy configuration file, and an LUA function. |
| 9831 | */ |
| 9832 | __LJMP static int hlua_register_action(lua_State *L) |
| 9833 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9834 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9835 | const char *name; |
| 9836 | int ref; |
| 9837 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9838 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9839 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9840 | struct buffer *trash; |
| 9841 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9842 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9843 | /* Initialise the number of expected arguments at 0. */ |
| 9844 | nargs = 0; |
| 9845 | |
| 9846 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9847 | 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] | 9848 | |
| 9849 | /* First argument : converter name. */ |
| 9850 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9851 | |
| 9852 | /* Second argument : environment. */ |
| 9853 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9854 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9855 | |
| 9856 | /* Third argument : lua function. */ |
| 9857 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9858 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9859 | /* 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] | 9860 | if (lua_gettop(L) >= 4) |
| 9861 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9862 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9863 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9864 | lua_pushnil(L); |
| 9865 | while (lua_next(L, 2) != 0) { |
| 9866 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9867 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9868 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9869 | /* Check if action exists */ |
| 9870 | trash = get_trash_chunk(); |
| 9871 | chunk_printf(trash, "lua.%s", name); |
| 9872 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9873 | akw = tcp_req_cont_action(trash->area); |
| 9874 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9875 | akw = tcp_res_cont_action(trash->area); |
| 9876 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9877 | akw = action_http_req_custom(trash->area); |
| 9878 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9879 | akw = action_http_res_custom(trash->area); |
| 9880 | } else { |
| 9881 | akw = NULL; |
| 9882 | } |
| 9883 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9884 | fcn = akw->private; |
| 9885 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9886 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9887 | "This will become a hard error in version 2.5.\n", name); |
| 9888 | } |
| 9889 | fcn->function_ref[hlua_state_id] = ref; |
| 9890 | |
| 9891 | /* pop the environment string. */ |
| 9892 | lua_pop(L, 1); |
| 9893 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9894 | } |
| 9895 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9896 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9897 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9898 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9899 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9900 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9901 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9902 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9903 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9904 | |
| 9905 | /* Fill fcn. */ |
| 9906 | fcn->name = strdup(name); |
| 9907 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9908 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9909 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9910 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9911 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9912 | fcn->nargs = nargs; |
| 9913 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9914 | /* List head */ |
| 9915 | akl->list.n = akl->list.p = NULL; |
| 9916 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9917 | /* action keyword. */ |
| 9918 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9919 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9920 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9921 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9922 | |
| 9923 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9924 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9925 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9926 | akl->kw[0].private = fcn; |
| 9927 | akl->kw[0].parse = action_register_lua; |
| 9928 | |
| 9929 | /* select the action registering point. */ |
| 9930 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9931 | tcp_req_cont_keywords_register(akl); |
| 9932 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9933 | tcp_res_cont_keywords_register(akl); |
| 9934 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9935 | http_req_keywords_register(akl); |
| 9936 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9937 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9938 | else { |
| 9939 | release_hlua_function(fcn); |
| 9940 | if (akl) |
| 9941 | ha_free((char **)&(akl->kw[0].kw)); |
| 9942 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9943 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9944 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9945 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9946 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9947 | |
| 9948 | /* pop the environment string. */ |
| 9949 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9950 | |
| 9951 | /* reset for next loop */ |
| 9952 | akl = NULL; |
| 9953 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9954 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9955 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9956 | |
| 9957 | alloc_error: |
| 9958 | release_hlua_function(fcn); |
| 9959 | ha_free(&akl); |
| 9960 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9961 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9962 | } |
| 9963 | |
| 9964 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9965 | struct act_rule *rule, char **err) |
| 9966 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9967 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9968 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9969 | if (px->mode == PR_MODE_HTTP) { |
| 9970 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9971 | return ACT_RET_PRS_ERR; |
| 9972 | } |
| 9973 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9974 | /* Memory for the rule. */ |
| 9975 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9976 | if (!rule->arg.hlua_rule) { |
| 9977 | memprintf(err, "out of memory error"); |
| 9978 | return ACT_RET_PRS_ERR; |
| 9979 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9980 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9981 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9982 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9983 | |
| 9984 | /* TODO: later accept arguments. */ |
| 9985 | rule->arg.hlua_rule->args = NULL; |
| 9986 | |
| 9987 | /* Add applet pointer in the rule. */ |
| 9988 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9989 | rule->applet.name = fcn->name; |
| 9990 | rule->applet.init = hlua_applet_tcp_init; |
| 9991 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9992 | rule->applet.release = hlua_applet_tcp_release; |
| 9993 | rule->applet.timeout = hlua_timeout_applet; |
| 9994 | |
| 9995 | return 0; |
| 9996 | } |
| 9997 | |
| 9998 | /* This function is an LUA binding used for registering |
| 9999 | * "sample-conv" functions. It expects a converter name used |
| 10000 | * in the haproxy configuration file, and an LUA function. |
| 10001 | */ |
| 10002 | __LJMP static int hlua_register_service(lua_State *L) |
| 10003 | { |
| 10004 | struct action_kw_list *akl; |
| 10005 | const char *name; |
| 10006 | const char *env; |
| 10007 | int ref; |
| 10008 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10009 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10010 | struct buffer *trash; |
| 10011 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10012 | |
| 10013 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10014 | |
| 10015 | /* First argument : converter name. */ |
| 10016 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10017 | |
| 10018 | /* Second argument : environment. */ |
| 10019 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10020 | |
| 10021 | /* Third argument : lua function. */ |
| 10022 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10023 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10024 | /* Check for service already registered */ |
| 10025 | trash = get_trash_chunk(); |
| 10026 | chunk_printf(trash, "lua.%s", name); |
| 10027 | akw = service_find(trash->area); |
| 10028 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10029 | fcn = akw->private; |
| 10030 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10031 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10032 | "This will become a hard error in version 2.5.\n", name); |
| 10033 | } |
| 10034 | fcn->function_ref[hlua_state_id] = ref; |
| 10035 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10036 | } |
| 10037 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10038 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10039 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10040 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10041 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10042 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10043 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10044 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10045 | |
| 10046 | /* Fill fcn. */ |
| 10047 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10048 | fcn->name = calloc(1, len); |
| 10049 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10050 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10051 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10052 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10053 | |
| 10054 | /* List head */ |
| 10055 | akl->list.n = akl->list.p = NULL; |
| 10056 | |
| 10057 | /* converter keyword. */ |
| 10058 | len = strlen("lua.") + strlen(name) + 1; |
| 10059 | akl->kw[0].kw = calloc(1, len); |
| 10060 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10061 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10062 | |
| 10063 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10064 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10065 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10066 | if (strcmp(env, "tcp") == 0) |
| 10067 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10068 | else if (strcmp(env, "http") == 0) |
| 10069 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10070 | else { |
| 10071 | release_hlua_function(fcn); |
| 10072 | if (akl) |
| 10073 | ha_free((char **)&(akl->kw[0].kw)); |
| 10074 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10075 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10076 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10077 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10078 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10079 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10080 | akl->kw[0].private = fcn; |
| 10081 | |
| 10082 | /* End of array. */ |
| 10083 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10084 | |
| 10085 | /* Register this new converter */ |
| 10086 | service_keywords_register(akl); |
| 10087 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10088 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10089 | |
| 10090 | alloc_error: |
| 10091 | release_hlua_function(fcn); |
| 10092 | ha_free(&akl); |
| 10093 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10094 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10095 | } |
| 10096 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10097 | /* This function initialises Lua cli handler. It copies the |
| 10098 | * arguments in the Lua stack and create channel IO objects. |
| 10099 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10100 | 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] | 10101 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10102 | 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] | 10103 | struct hlua *hlua; |
| 10104 | struct hlua_function *fcn; |
| 10105 | int i; |
| 10106 | const char *error; |
| 10107 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10108 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10109 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10110 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10111 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10112 | if (!hlua) { |
| 10113 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10114 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10115 | } |
| 10116 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10117 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10118 | |
| 10119 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10120 | * 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] | 10121 | * applet_http. It is absolutely compatible. |
| 10122 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10123 | ctx->task = task_new_here(); |
| 10124 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10125 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10126 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10127 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10128 | ctx->task->nice = 0; |
| 10129 | ctx->task->context = appctx; |
| 10130 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10131 | |
| 10132 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10133 | 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] | 10134 | 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] | 10135 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10136 | } |
| 10137 | |
| 10138 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10139 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10140 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10141 | error = lua_tostring(hlua->T, -1); |
| 10142 | else |
| 10143 | error = "critical error"; |
| 10144 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10145 | goto error; |
| 10146 | } |
| 10147 | |
| 10148 | /* Check stack available size. */ |
| 10149 | if (!lua_checkstack(hlua->T, 2)) { |
| 10150 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10151 | goto error; |
| 10152 | } |
| 10153 | |
| 10154 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10155 | 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] | 10156 | |
| 10157 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10158 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10159 | */ |
| 10160 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10161 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10162 | goto error; |
| 10163 | } |
| 10164 | hlua->nargs = 1; |
| 10165 | |
| 10166 | /* push keywords in the stack. */ |
| 10167 | for (i = 0; *args[i]; i++) { |
| 10168 | /* Check stack available size. */ |
| 10169 | if (!lua_checkstack(hlua->T, 1)) { |
| 10170 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10171 | goto error; |
| 10172 | } |
| 10173 | lua_pushstring(hlua->T, args[i]); |
| 10174 | hlua->nargs++; |
| 10175 | } |
| 10176 | |
| 10177 | /* We must initialize the execution timeouts. */ |
| 10178 | hlua->max_time = hlua_timeout_session; |
| 10179 | |
| 10180 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10181 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10182 | |
| 10183 | /* It's ok */ |
| 10184 | return 0; |
| 10185 | |
| 10186 | /* It's not ok. */ |
| 10187 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10188 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10189 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10190 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10191 | return 1; |
| 10192 | } |
| 10193 | |
| 10194 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10195 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10196 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10197 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10198 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10199 | struct hlua_function *fcn; |
| 10200 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10201 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10202 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10203 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10204 | |
| 10205 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10206 | 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] | 10207 | return 1; |
| 10208 | |
| 10209 | /* Execute the function. */ |
| 10210 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10211 | |
| 10212 | /* finished. */ |
| 10213 | case HLUA_E_OK: |
| 10214 | return 1; |
| 10215 | |
| 10216 | /* yield. */ |
| 10217 | case HLUA_E_AGAIN: |
| 10218 | /* We want write. */ |
| 10219 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10220 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10221 | /* Set the timeout. */ |
| 10222 | if (hlua->wake_time != TICK_ETERNITY) |
| 10223 | task_schedule(hlua->task, hlua->wake_time); |
| 10224 | return 0; |
| 10225 | |
| 10226 | /* finished with error. */ |
| 10227 | case HLUA_E_ERRMSG: |
| 10228 | /* Display log. */ |
| 10229 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10230 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10231 | lua_pop(hlua->T, 1); |
| 10232 | return 1; |
| 10233 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10234 | case HLUA_E_ETMOUT: |
| 10235 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10236 | fcn->name); |
| 10237 | return 1; |
| 10238 | |
| 10239 | case HLUA_E_NOMEM: |
| 10240 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10241 | fcn->name); |
| 10242 | return 1; |
| 10243 | |
| 10244 | case HLUA_E_YIELD: /* unexpected */ |
| 10245 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10246 | fcn->name); |
| 10247 | return 1; |
| 10248 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10249 | case HLUA_E_ERR: |
| 10250 | /* Display log. */ |
| 10251 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10252 | fcn->name); |
| 10253 | return 1; |
| 10254 | |
| 10255 | default: |
| 10256 | return 1; |
| 10257 | } |
| 10258 | |
| 10259 | return 1; |
| 10260 | } |
| 10261 | |
| 10262 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10263 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10264 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10265 | |
| 10266 | hlua_ctx_destroy(ctx->hlua); |
| 10267 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10268 | } |
| 10269 | |
| 10270 | /* This function is an LUA binding used for registering |
| 10271 | * new keywords in the cli. It expects a list of keywords |
| 10272 | * which are the "path". It is limited to 5 keywords. A |
| 10273 | * description of the command, a function to be executed |
| 10274 | * for the parsing and a function for io handlers. |
| 10275 | */ |
| 10276 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10277 | { |
| 10278 | struct cli_kw_list *cli_kws; |
| 10279 | const char *message; |
| 10280 | int ref_io; |
| 10281 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10282 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10283 | int index; |
| 10284 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10285 | struct buffer *trash; |
| 10286 | const char *kw[5]; |
| 10287 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10288 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10289 | |
| 10290 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10291 | |
| 10292 | /* First argument : an array of maximum 5 keywords. */ |
| 10293 | if (!lua_istable(L, 1)) |
| 10294 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10295 | |
| 10296 | /* Second argument : string with contextual message. */ |
| 10297 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10298 | |
| 10299 | /* Third and fourth argument : lua function. */ |
| 10300 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10301 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10302 | /* Check for CLI service already registered */ |
| 10303 | trash = get_trash_chunk(); |
| 10304 | index = 0; |
| 10305 | lua_pushnil(L); |
| 10306 | memset(kw, 0, sizeof(kw)); |
| 10307 | while (lua_next(L, 1) != 0) { |
| 10308 | if (index >= CLI_PREFIX_KW_NB) |
| 10309 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10310 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10311 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10312 | kw[index] = lua_tostring(L, -1); |
| 10313 | if (index == 0) |
| 10314 | chunk_printf(trash, "%s", kw[index]); |
| 10315 | else |
| 10316 | chunk_appendf(trash, " %s", kw[index]); |
| 10317 | index++; |
| 10318 | lua_pop(L, 1); |
| 10319 | } |
| 10320 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10321 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10322 | fcn = cli_kw->private; |
| 10323 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10324 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10325 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10326 | } |
| 10327 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10328 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10329 | } |
| 10330 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10331 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10332 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10333 | if (!cli_kws) { |
| 10334 | errmsg = "Lua out of memory error."; |
| 10335 | goto error; |
| 10336 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10337 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10338 | if (!fcn) { |
| 10339 | errmsg = "Lua out of memory error."; |
| 10340 | goto error; |
| 10341 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10342 | |
| 10343 | /* Fill path. */ |
| 10344 | index = 0; |
| 10345 | lua_pushnil(L); |
| 10346 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10347 | if (index >= 5) { |
| 10348 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10349 | goto error; |
| 10350 | } |
| 10351 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10352 | errmsg = "1st argument must be a table filled with strings"; |
| 10353 | goto error; |
| 10354 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10355 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10356 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10357 | errmsg = "Lua out of memory error."; |
| 10358 | goto error; |
| 10359 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10360 | index++; |
| 10361 | lua_pop(L, 1); |
| 10362 | } |
| 10363 | |
| 10364 | /* Copy help message. */ |
| 10365 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10366 | if (!cli_kws->kw[0].usage) { |
| 10367 | errmsg = "Lua out of memory error."; |
| 10368 | goto error; |
| 10369 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10370 | |
| 10371 | /* Fill fcn io handler. */ |
| 10372 | len = strlen("<lua.cli>") + 1; |
| 10373 | for (i = 0; i < index; i++) |
| 10374 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10375 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10376 | if (!fcn->name) { |
| 10377 | errmsg = "Lua out of memory error."; |
| 10378 | goto error; |
| 10379 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10380 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10381 | for (i = 0; i < index; i++) { |
| 10382 | strncat((char *)fcn->name, ".", len); |
| 10383 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10384 | } |
| 10385 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10386 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10387 | |
| 10388 | /* Fill last entries. */ |
| 10389 | cli_kws->kw[0].private = fcn; |
| 10390 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10391 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10392 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10393 | |
| 10394 | /* Register this new converter */ |
| 10395 | cli_register_kw(cli_kws); |
| 10396 | |
| 10397 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10398 | |
| 10399 | error: |
| 10400 | release_hlua_function(fcn); |
| 10401 | if (cli_kws) { |
| 10402 | for (i = 0; i < index; i++) |
| 10403 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10404 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10405 | } |
| 10406 | ha_free(&cli_kws); |
| 10407 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10408 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10409 | } |
| 10410 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10411 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10412 | { |
| 10413 | struct hlua_flt_config *conf = fconf->conf; |
| 10414 | lua_State *L; |
| 10415 | int error, pos, state_id, flt_ref; |
| 10416 | |
| 10417 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10418 | L = hlua_states[state_id]; |
| 10419 | pos = lua_gettop(L); |
| 10420 | |
| 10421 | /* The filter parsing function */ |
| 10422 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10423 | |
| 10424 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10425 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10426 | |
| 10427 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10428 | lua_newtable(L); |
| 10429 | lua_pushnil(L); |
| 10430 | |
| 10431 | while (lua_next(L, pos+2)) { |
| 10432 | lua_pushvalue(L, -2); |
| 10433 | lua_insert(L, -2); |
| 10434 | lua_settable(L, -4); |
| 10435 | } |
| 10436 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10437 | |
| 10438 | /* Remove the original lua filter class from the stack */ |
| 10439 | lua_pop(L, 1); |
| 10440 | |
| 10441 | /* Push the copy on the stack */ |
| 10442 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10443 | |
| 10444 | /* extra args are pushed in a table */ |
| 10445 | lua_newtable(L); |
| 10446 | for (pos = 0; conf->args[pos]; pos++) { |
| 10447 | /* Check stack available size. */ |
| 10448 | if (!lua_checkstack(L, 1)) { |
| 10449 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10450 | goto error; |
| 10451 | } |
| 10452 | lua_pushstring(L, conf->args[pos]); |
| 10453 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10454 | } |
| 10455 | |
| 10456 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10457 | switch (error) { |
| 10458 | case LUA_OK: |
| 10459 | /* replace the filter ref */ |
| 10460 | conf->ref[state_id] = flt_ref; |
| 10461 | break; |
| 10462 | case LUA_ERRRUN: |
| 10463 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10464 | goto error; |
| 10465 | case LUA_ERRMEM: |
| 10466 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10467 | goto error; |
| 10468 | case LUA_ERRERR: |
| 10469 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10470 | goto error; |
| 10471 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10472 | case LUA_ERRGCMM: |
| 10473 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10474 | goto error; |
| 10475 | #endif |
| 10476 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10477 | 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] | 10478 | goto error; |
| 10479 | } |
| 10480 | |
| 10481 | lua_settop(L, 0); |
| 10482 | return 0; |
| 10483 | |
| 10484 | error: |
| 10485 | lua_settop(L, 0); |
| 10486 | return -1; |
| 10487 | } |
| 10488 | |
| 10489 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10490 | { |
| 10491 | struct hlua_flt_config *conf = fconf->conf; |
| 10492 | lua_State *L; |
| 10493 | int state_id; |
| 10494 | |
| 10495 | if (!conf) |
| 10496 | return; |
| 10497 | |
| 10498 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10499 | L = hlua_states[state_id]; |
| 10500 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10501 | } |
| 10502 | |
| 10503 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10504 | { |
| 10505 | struct hlua_flt_config *conf = fconf->conf; |
| 10506 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10507 | |
| 10508 | /* Rely on per-thread init for global scripts */ |
| 10509 | if (!state_id) |
| 10510 | return hlua_filter_init_per_thread(px, fconf); |
| 10511 | return 0; |
| 10512 | } |
| 10513 | |
| 10514 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10515 | { |
| 10516 | |
| 10517 | if (fconf->conf) { |
| 10518 | struct hlua_flt_config *conf = fconf->conf; |
| 10519 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10520 | int pos; |
| 10521 | |
| 10522 | /* Rely on per-thread deinit for global scripts */ |
| 10523 | if (!state_id) |
| 10524 | hlua_filter_deinit_per_thread(px, fconf); |
| 10525 | |
| 10526 | for (pos = 0; conf->args[pos]; pos++) |
| 10527 | free(conf->args[pos]); |
| 10528 | free(conf->args); |
| 10529 | } |
| 10530 | ha_free(&fconf->conf); |
| 10531 | ha_free((char **)&fconf->id); |
| 10532 | ha_free(&fconf->ops); |
| 10533 | } |
| 10534 | |
| 10535 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10536 | { |
| 10537 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10538 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10539 | int ret = 1; |
| 10540 | |
| 10541 | /* In the execution wrappers linked with a stream, the |
| 10542 | * Lua context can be not initialized. This behavior |
| 10543 | * permits to save performances because a systematic |
| 10544 | * Lua initialization cause 5% performances loss. |
| 10545 | */ |
| 10546 | if (!s->hlua) { |
| 10547 | struct hlua *hlua; |
| 10548 | |
| 10549 | hlua = pool_alloc(pool_head_hlua); |
| 10550 | if (!hlua) { |
| 10551 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10552 | conf->reg->name); |
| 10553 | ret = 0; |
| 10554 | goto end; |
| 10555 | } |
| 10556 | HLUA_INIT(hlua); |
| 10557 | s->hlua = hlua; |
| 10558 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10559 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10560 | conf->reg->name); |
| 10561 | ret = 0; |
| 10562 | goto end; |
| 10563 | } |
| 10564 | } |
| 10565 | |
| 10566 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10567 | if (!flt_ctx) { |
| 10568 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10569 | conf->reg->name); |
| 10570 | ret = 0; |
| 10571 | goto end; |
| 10572 | } |
| 10573 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10574 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10575 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10576 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10577 | conf->reg->name); |
| 10578 | ret = 0; |
| 10579 | goto end; |
| 10580 | } |
| 10581 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10582 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10583 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10584 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10585 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10586 | conf->reg->name); |
| 10587 | ret = 0; |
| 10588 | goto end; |
| 10589 | } |
| 10590 | |
| 10591 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10592 | /* The following Lua calls can fail. */ |
| 10593 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10594 | const char *error; |
| 10595 | |
| 10596 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10597 | error = lua_tostring(s->hlua->T, -1); |
| 10598 | else |
| 10599 | error = "critical error"; |
| 10600 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10601 | ret = 0; |
| 10602 | goto end; |
| 10603 | } |
| 10604 | |
| 10605 | /* Check stack size. */ |
| 10606 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10607 | 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] | 10608 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10609 | ret = 0; |
| 10610 | goto end; |
| 10611 | } |
| 10612 | |
| 10613 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10614 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10615 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10616 | conf->reg->name); |
| 10617 | RESET_SAFE_LJMP(s->hlua); |
| 10618 | ret = 0; |
| 10619 | goto end; |
| 10620 | } |
| 10621 | lua_insert(s->hlua->T, -2); |
| 10622 | |
| 10623 | /* Push the copy on the stack */ |
| 10624 | s->hlua->nargs = 1; |
| 10625 | |
| 10626 | /* We must initialize the execution timeouts. */ |
| 10627 | s->hlua->max_time = hlua_timeout_session; |
| 10628 | |
| 10629 | /* At this point the execution is safe. */ |
| 10630 | RESET_SAFE_LJMP(s->hlua); |
| 10631 | } |
| 10632 | |
| 10633 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10634 | case HLUA_E_OK: |
| 10635 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10636 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10637 | ret = 0; |
| 10638 | goto end; |
| 10639 | } |
| 10640 | |
| 10641 | /* Attached the filter pointer to the ctx */ |
| 10642 | lua_pushstring(s->hlua->T, "__filter"); |
| 10643 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10644 | lua_settable(s->hlua->T, -3); |
| 10645 | |
| 10646 | /* Save a ref on the filter ctx */ |
| 10647 | lua_pushvalue(s->hlua->T, 1); |
| 10648 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10649 | filter->ctx = flt_ctx; |
| 10650 | break; |
| 10651 | case HLUA_E_ERRMSG: |
| 10652 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10653 | ret = -1; |
| 10654 | goto end; |
| 10655 | case HLUA_E_ETMOUT: |
| 10656 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10657 | ret = 0; |
| 10658 | goto end; |
| 10659 | case HLUA_E_NOMEM: |
| 10660 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10661 | ret = 0; |
| 10662 | goto end; |
| 10663 | case HLUA_E_AGAIN: |
| 10664 | case HLUA_E_YIELD: |
| 10665 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10666 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10667 | ret = 0; |
| 10668 | goto end; |
| 10669 | case HLUA_E_ERR: |
| 10670 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10671 | ret = 0; |
| 10672 | goto end; |
| 10673 | default: |
| 10674 | ret = 0; |
| 10675 | goto end; |
| 10676 | } |
| 10677 | |
| 10678 | end: |
| 10679 | if (s->hlua) |
| 10680 | lua_settop(s->hlua->T, 0); |
| 10681 | if (ret <= 0) { |
| 10682 | if (flt_ctx) { |
| 10683 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10684 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10685 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10686 | } |
| 10687 | } |
| 10688 | return ret; |
| 10689 | } |
| 10690 | |
| 10691 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10692 | { |
| 10693 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10694 | |
| 10695 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10696 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10697 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10698 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10699 | filter->ctx = NULL; |
| 10700 | } |
| 10701 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10702 | static int hlua_filter_from_payload(struct filter *filter) |
| 10703 | { |
| 10704 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10705 | |
| 10706 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10707 | } |
| 10708 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10709 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10710 | int dir, unsigned int flags) |
| 10711 | { |
| 10712 | struct hlua *flt_hlua; |
| 10713 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10714 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10715 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10716 | int ret = 1; |
| 10717 | |
| 10718 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10719 | if (!flt_hlua) |
| 10720 | goto end; |
| 10721 | |
| 10722 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10723 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10724 | |
| 10725 | /* The following Lua calls can fail. */ |
| 10726 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10727 | const char *error; |
| 10728 | |
| 10729 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10730 | error = lua_tostring(flt_hlua->T, -1); |
| 10731 | else |
| 10732 | error = "critical error"; |
| 10733 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10734 | goto end; |
| 10735 | } |
| 10736 | |
| 10737 | /* Check stack size. */ |
| 10738 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10739 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10740 | RESET_SAFE_LJMP(flt_hlua); |
| 10741 | goto end; |
| 10742 | } |
| 10743 | |
| 10744 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10745 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10746 | RESET_SAFE_LJMP(flt_hlua); |
| 10747 | goto end; |
| 10748 | } |
| 10749 | lua_insert(flt_hlua->T, -2); |
| 10750 | |
| 10751 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10752 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10753 | RESET_SAFE_LJMP(flt_hlua); |
| 10754 | goto end; |
| 10755 | } |
| 10756 | flt_hlua->nargs = 2; |
| 10757 | |
| 10758 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10759 | if (dir == SMP_OPT_DIR_REQ) |
| 10760 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10761 | else |
| 10762 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10763 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10764 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10765 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10766 | lua_settable(flt_hlua->T, -3); |
| 10767 | } |
| 10768 | flt_hlua->nargs++; |
| 10769 | } |
| 10770 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10771 | if (dir == SMP_OPT_DIR_REQ) |
| 10772 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10773 | else |
| 10774 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10775 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10776 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10777 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10778 | lua_settable(flt_hlua->T, -3); |
| 10779 | } |
| 10780 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10781 | } |
| 10782 | |
| 10783 | /* Check stack size. */ |
| 10784 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10785 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10786 | RESET_SAFE_LJMP(flt_hlua); |
| 10787 | goto end; |
| 10788 | } |
| 10789 | |
| 10790 | while (extra_idx--) { |
| 10791 | lua_pushvalue(flt_hlua->T, 1); |
| 10792 | lua_remove(flt_hlua->T, 1); |
| 10793 | flt_hlua->nargs++; |
| 10794 | } |
| 10795 | |
| 10796 | /* We must initialize the execution timeouts. */ |
| 10797 | flt_hlua->max_time = hlua_timeout_session; |
| 10798 | |
| 10799 | /* At this point the execution is safe. */ |
| 10800 | RESET_SAFE_LJMP(flt_hlua); |
| 10801 | } |
| 10802 | |
| 10803 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10804 | case HLUA_E_OK: |
| 10805 | /* Catch the return value if it required */ |
| 10806 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10807 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10808 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10809 | } |
| 10810 | |
| 10811 | /* Set timeout in the required channel. */ |
| 10812 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10813 | if (dir == SMP_OPT_DIR_REQ) |
| 10814 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10815 | else |
| 10816 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10817 | } |
| 10818 | break; |
| 10819 | case HLUA_E_AGAIN: |
| 10820 | /* Set timeout in the required channel. */ |
| 10821 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10822 | if (dir == SMP_OPT_DIR_REQ) |
| 10823 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10824 | else |
| 10825 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10826 | } |
| 10827 | /* Some actions can be wake up when a "write" event |
| 10828 | * is detected on a response channel. This is useful |
| 10829 | * only for actions targeted on the requests. |
| 10830 | */ |
| 10831 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10832 | s->res.flags |= CF_WAKE_WRITE; |
| 10833 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10834 | s->req.flags |= CF_WAKE_WRITE; |
| 10835 | ret = 0; |
| 10836 | goto end; |
| 10837 | case HLUA_E_ERRMSG: |
| 10838 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10839 | ret = -1; |
| 10840 | goto end; |
| 10841 | case HLUA_E_ETMOUT: |
| 10842 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10843 | goto end; |
| 10844 | case HLUA_E_NOMEM: |
| 10845 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10846 | goto end; |
| 10847 | case HLUA_E_YIELD: |
| 10848 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10849 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10850 | goto end; |
| 10851 | case HLUA_E_ERR: |
| 10852 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10853 | goto end; |
| 10854 | default: |
| 10855 | goto end; |
| 10856 | } |
| 10857 | |
| 10858 | |
| 10859 | end: |
| 10860 | return ret; |
| 10861 | } |
| 10862 | |
| 10863 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10864 | { |
| 10865 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10866 | |
| 10867 | flt_ctx->flags = 0; |
| 10868 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10869 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10870 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10871 | } |
| 10872 | |
| 10873 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10874 | { |
| 10875 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10876 | |
| 10877 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10878 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10879 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10880 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10881 | } |
| 10882 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10883 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10884 | { |
| 10885 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10886 | |
| 10887 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10888 | return hlua_filter_callback(s, filter, "http_headers", |
| 10889 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10890 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10891 | } |
| 10892 | |
| 10893 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10894 | unsigned int offset, unsigned int len) |
| 10895 | { |
| 10896 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10897 | struct hlua *flt_hlua; |
| 10898 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10899 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10900 | int ret; |
| 10901 | |
| 10902 | flt_hlua = flt_ctx->hlua[idx]; |
| 10903 | flt_ctx->cur_off[idx] = offset; |
| 10904 | flt_ctx->cur_len[idx] = len; |
| 10905 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10906 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10907 | if (ret != -1) { |
| 10908 | ret = flt_ctx->cur_len[idx]; |
| 10909 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10910 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10911 | if (ret > flt_ctx->cur_len[idx]) |
| 10912 | ret = flt_ctx->cur_len[idx]; |
| 10913 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10914 | } |
| 10915 | } |
| 10916 | return ret; |
| 10917 | } |
| 10918 | |
| 10919 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10920 | { |
| 10921 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10922 | |
| 10923 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10924 | return hlua_filter_callback(s, filter, "http_end", |
| 10925 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10926 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10927 | } |
| 10928 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10929 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10930 | unsigned int offset, unsigned int len) |
| 10931 | { |
| 10932 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10933 | struct hlua *flt_hlua; |
| 10934 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10935 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10936 | int ret; |
| 10937 | |
| 10938 | flt_hlua = flt_ctx->hlua[idx]; |
| 10939 | flt_ctx->cur_off[idx] = offset; |
| 10940 | flt_ctx->cur_len[idx] = len; |
| 10941 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10942 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10943 | if (ret != -1) { |
| 10944 | ret = flt_ctx->cur_len[idx]; |
| 10945 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10946 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10947 | if (ret > flt_ctx->cur_len[idx]) |
| 10948 | ret = flt_ctx->cur_len[idx]; |
| 10949 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10950 | } |
| 10951 | } |
| 10952 | return ret; |
| 10953 | } |
| 10954 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10955 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10956 | struct flt_conf *fconf, char **err, void *private) |
| 10957 | { |
| 10958 | struct hlua_reg_filter *reg_flt = private; |
| 10959 | lua_State *L; |
| 10960 | struct hlua_flt_config *conf = NULL; |
| 10961 | const char *flt_id = NULL; |
| 10962 | int state_id, pos, flt_flags = 0; |
| 10963 | struct flt_ops *hlua_flt_ops = NULL; |
| 10964 | |
| 10965 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10966 | L = hlua_states[state_id]; |
| 10967 | |
| 10968 | /* Initialize the filter ops with default callbacks */ |
| 10969 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10970 | if (!hlua_flt_ops) |
| 10971 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10972 | hlua_flt_ops->init = hlua_filter_init; |
| 10973 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10974 | if (state_id) { |
| 10975 | /* Set per-thread callback if script is loaded per-thread */ |
| 10976 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10977 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10978 | } |
| 10979 | hlua_flt_ops->attach = hlua_filter_new; |
| 10980 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10981 | |
| 10982 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10983 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10984 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10985 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10986 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10987 | lua_pop(L, 1); |
| 10988 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10989 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10990 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10991 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10992 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10993 | lua_pop(L, 1); |
| 10994 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 10995 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 10996 | lua_pop(L, 1); |
| 10997 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 10998 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 10999 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11000 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11001 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11002 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11003 | |
| 11004 | /* Get id and flags of the filter class */ |
| 11005 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11006 | flt_id = lua_tostring(L, -1); |
| 11007 | lua_pop(L, 1); |
| 11008 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11009 | flt_flags = lua_tointeger(L, -1); |
| 11010 | lua_pop(L, 1); |
| 11011 | |
| 11012 | /* Create the filter config */ |
| 11013 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11014 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11015 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11016 | conf->reg = reg_flt; |
| 11017 | |
| 11018 | /* duplicate args */ |
| 11019 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11020 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11021 | if (!conf->args) |
| 11022 | goto error; |
| 11023 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11024 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11025 | if (!conf->args[pos]) |
| 11026 | goto error; |
| 11027 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11028 | conf->args[pos] = NULL; |
| 11029 | *cur_arg += pos + 1; |
| 11030 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11031 | if (flt_id) { |
| 11032 | fconf->id = strdup(flt_id); |
| 11033 | if (!fconf->id) |
| 11034 | goto error; |
| 11035 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11036 | fconf->flags = flt_flags; |
| 11037 | fconf->conf = conf; |
| 11038 | fconf->ops = hlua_flt_ops; |
| 11039 | |
| 11040 | lua_settop(L, 0); |
| 11041 | return 0; |
| 11042 | |
| 11043 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11044 | 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] | 11045 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11046 | if (conf && conf->args) { |
| 11047 | for (pos = 0; conf->args[pos]; pos++) |
| 11048 | free(conf->args[pos]); |
| 11049 | free(conf->args); |
| 11050 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11051 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11052 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11053 | lua_settop(L, 0); |
| 11054 | return -1; |
| 11055 | } |
| 11056 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11057 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11058 | { |
| 11059 | struct filter *filter; |
| 11060 | struct channel *chn; |
| 11061 | |
| 11062 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11063 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11064 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11065 | |
| 11066 | lua_getfield(L, 1, "__filter"); |
| 11067 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11068 | filter = lua_touserdata (L, -1); |
| 11069 | lua_pop(L, 1); |
| 11070 | |
| 11071 | register_data_filter(chn_strm(chn), chn, filter); |
| 11072 | return 1; |
| 11073 | } |
| 11074 | |
| 11075 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11076 | { |
| 11077 | struct filter *filter; |
| 11078 | struct channel *chn; |
| 11079 | |
| 11080 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11081 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11082 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11083 | |
| 11084 | lua_getfield(L, 1, "__filter"); |
| 11085 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11086 | filter = lua_touserdata (L, -1); |
| 11087 | lua_pop(L, 1); |
| 11088 | |
| 11089 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11090 | return 1; |
| 11091 | } |
| 11092 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11093 | /* 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] | 11094 | * 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] | 11095 | * parse configuration arguments. |
| 11096 | */ |
| 11097 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11098 | { |
| 11099 | struct buffer *trash; |
| 11100 | struct flt_kw_list *fkl; |
| 11101 | struct flt_kw *fkw; |
| 11102 | const char *name; |
| 11103 | struct hlua_reg_filter *reg_flt= NULL; |
| 11104 | int flt_ref, fun_ref; |
| 11105 | int len; |
| 11106 | |
| 11107 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11108 | |
| 11109 | /* First argument : filter name. */ |
| 11110 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11111 | |
| 11112 | /* Second argument : The filter class */ |
| 11113 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11114 | |
| 11115 | /* Third argument : lua function. */ |
| 11116 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11117 | |
| 11118 | trash = get_trash_chunk(); |
| 11119 | chunk_printf(trash, "lua.%s", name); |
| 11120 | fkw = flt_find_kw(trash->area); |
| 11121 | if (fkw != NULL) { |
| 11122 | reg_flt = fkw->private; |
| 11123 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11124 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11125 | "This will become a hard error in version 2.5.\n", name); |
| 11126 | } |
| 11127 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11128 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11129 | return 0; |
| 11130 | } |
| 11131 | |
| 11132 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11133 | if (!fkl) |
| 11134 | goto alloc_error; |
| 11135 | fkl->scope = "HLUA"; |
| 11136 | |
| 11137 | reg_flt = new_hlua_reg_filter(name); |
| 11138 | if (!reg_flt) |
| 11139 | goto alloc_error; |
| 11140 | |
| 11141 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11142 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11143 | |
| 11144 | /* The filter keyword */ |
| 11145 | len = strlen("lua.") + strlen(name) + 1; |
| 11146 | fkl->kw[0].kw = calloc(1, len); |
| 11147 | if (!fkl->kw[0].kw) |
| 11148 | goto alloc_error; |
| 11149 | |
| 11150 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11151 | |
| 11152 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11153 | fkl->kw[0].private = reg_flt; |
| 11154 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11155 | |
| 11156 | /* Register this new filter */ |
| 11157 | flt_register_keywords(fkl); |
| 11158 | |
| 11159 | return 0; |
| 11160 | |
| 11161 | alloc_error: |
| 11162 | release_hlua_reg_filter(reg_flt); |
| 11163 | ha_free(&fkl); |
| 11164 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11165 | return 0; /* Never reached */ |
| 11166 | } |
| 11167 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11168 | 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] | 11169 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11170 | char **err, unsigned int *timeout) |
| 11171 | { |
| 11172 | const char *error; |
| 11173 | |
| 11174 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11175 | if (error == PARSE_TIME_OVER) { |
| 11176 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11177 | args[1], args[0]); |
| 11178 | return -1; |
| 11179 | } |
| 11180 | else if (error == PARSE_TIME_UNDER) { |
| 11181 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11182 | args[1], args[0]); |
| 11183 | return -1; |
| 11184 | } |
| 11185 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11186 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11187 | return -1; |
| 11188 | } |
| 11189 | return 0; |
| 11190 | } |
| 11191 | |
| 11192 | 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] | 11193 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11194 | char **err) |
| 11195 | { |
| 11196 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11197 | file, line, err, &hlua_timeout_session); |
| 11198 | } |
| 11199 | |
| 11200 | 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] | 11201 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11202 | char **err) |
| 11203 | { |
| 11204 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11205 | file, line, err, &hlua_timeout_task); |
| 11206 | } |
| 11207 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11208 | 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] | 11209 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11210 | char **err) |
| 11211 | { |
| 11212 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11213 | file, line, err, &hlua_timeout_applet); |
| 11214 | } |
| 11215 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11216 | 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] | 11217 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11218 | char **err) |
| 11219 | { |
| 11220 | char *error; |
| 11221 | |
| 11222 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11223 | if (*error != '\0') { |
| 11224 | memprintf(err, "%s: invalid number", args[0]); |
| 11225 | return -1; |
| 11226 | } |
| 11227 | return 0; |
| 11228 | } |
| 11229 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11230 | 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] | 11231 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11232 | char **err) |
| 11233 | { |
| 11234 | char *error; |
| 11235 | |
| 11236 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11237 | 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] | 11238 | return -1; |
| 11239 | } |
| 11240 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11241 | if (*error != '\0') { |
| 11242 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11243 | return -1; |
| 11244 | } |
| 11245 | return 0; |
| 11246 | } |
| 11247 | |
| 11248 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11249 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11250 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11251 | * the main lua entry point. |
| 11252 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11253 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11254 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11255 | * |
| 11256 | * In some error case, LUA set an error message in top of the stack. This function |
| 11257 | * 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] | 11258 | * |
| 11259 | * This function can fail with an abort() due to an Lua critical error. |
| 11260 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11261 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11262 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11263 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11264 | { |
| 11265 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11266 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11267 | |
| 11268 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11269 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11270 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11271 | 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] | 11272 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11273 | return -1; |
| 11274 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11275 | |
| 11276 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11277 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11278 | /* Check stack size. */ |
| 11279 | if (!lua_checkstack(L, 1)) { |
| 11280 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11281 | return -1; |
| 11282 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11283 | lua_pushstring(L, args[nargs]); |
| 11284 | } |
| 11285 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11286 | |
| 11287 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11288 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11289 | switch (error) { |
| 11290 | case LUA_OK: |
| 11291 | break; |
| 11292 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11293 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11294 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11295 | return -1; |
| 11296 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11297 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11298 | return -1; |
| 11299 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11300 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11301 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11302 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11303 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11304 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11305 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11306 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11307 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11308 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11309 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11310 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11311 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11312 | return -1; |
| 11313 | } |
| 11314 | |
| 11315 | return 0; |
| 11316 | } |
| 11317 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11318 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11319 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11320 | char **err) |
| 11321 | { |
| 11322 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11323 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11324 | return -1; |
| 11325 | } |
| 11326 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11327 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11328 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11329 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11330 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11331 | } |
| 11332 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11333 | 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] | 11334 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11335 | char **err) |
| 11336 | { |
| 11337 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11338 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11339 | |
| 11340 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11341 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11342 | return -1; |
| 11343 | } |
| 11344 | |
| 11345 | if (per_thread_load == NULL) { |
| 11346 | /* allocate the first entry large enough to store the final NULL */ |
| 11347 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11348 | if (per_thread_load == NULL) { |
| 11349 | memprintf(err, "out of memory error"); |
| 11350 | return -1; |
| 11351 | } |
| 11352 | } |
| 11353 | |
| 11354 | /* count used entries */ |
| 11355 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11356 | ; |
| 11357 | |
| 11358 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11359 | if (per_thread_load == NULL) { |
| 11360 | memprintf(err, "out of memory error"); |
| 11361 | return -1; |
| 11362 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11363 | per_thread_load[len + 1] = NULL; |
| 11364 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11365 | /* count args excepting the first, allocate array and copy args */ |
| 11366 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11367 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11368 | if (per_thread_load[len] == NULL) { |
| 11369 | memprintf(err, "out of memory error"); |
| 11370 | return -1; |
| 11371 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11372 | for (i = 1; *(args[i]) != 0; i++) { |
| 11373 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11374 | if (per_thread_load[len][i - 1] == NULL) { |
| 11375 | memprintf(err, "out of memory error"); |
| 11376 | return -1; |
| 11377 | } |
| 11378 | } |
| 11379 | per_thread_load[len][i - 1] = strdup(""); |
| 11380 | if (per_thread_load[len][i - 1] == NULL) { |
| 11381 | memprintf(err, "out of memory error"); |
| 11382 | return -1; |
| 11383 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11384 | |
| 11385 | /* loading for thread 1 only */ |
| 11386 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11387 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11388 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11389 | } |
| 11390 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11391 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11392 | * in the given <ctx>. |
| 11393 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11394 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11395 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11396 | lua_getglobal(L, "package"); /* push package variable */ |
| 11397 | lua_pushstring(L, path); /* push given path */ |
| 11398 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11399 | lua_getfield(L, -3, type); /* push old path */ |
| 11400 | lua_concat(L, 3); /* concatenate to new path */ |
| 11401 | lua_setfield(L, -2, type); /* store new path */ |
| 11402 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11403 | |
| 11404 | return 0; |
| 11405 | } |
| 11406 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11407 | 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] | 11408 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11409 | char **err) |
| 11410 | { |
| 11411 | char *path; |
| 11412 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11413 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11414 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11415 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11416 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11417 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11418 | } |
| 11419 | |
| 11420 | if (!(*args[1])) { |
| 11421 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11422 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11423 | } |
| 11424 | path = args[1]; |
| 11425 | |
| 11426 | if (*args[2]) { |
| 11427 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11428 | 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] | 11429 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11430 | } |
| 11431 | type = args[2]; |
| 11432 | } |
| 11433 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11434 | p = calloc(1, sizeof(*p)); |
| 11435 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11436 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11437 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11438 | } |
| 11439 | p->path = strdup(path); |
| 11440 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11441 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11442 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11443 | } |
| 11444 | p->type = strdup(type); |
| 11445 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11446 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11447 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11448 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11449 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11450 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11451 | /* Handle the global state and the per-thread state for the first |
| 11452 | * thread. The remaining threads will be initialized based on |
| 11453 | * prepend_path_list. |
| 11454 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11455 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11456 | lua_State *L = hlua_states[i]; |
| 11457 | const char *error; |
| 11458 | |
| 11459 | if (setjmp(safe_ljmp_env) != 0) { |
| 11460 | lua_atpanic(L, hlua_panic_safe); |
| 11461 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11462 | error = lua_tostring(L, -1); |
| 11463 | else |
| 11464 | error = "critical error"; |
| 11465 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11466 | exit(1); |
| 11467 | } else { |
| 11468 | lua_atpanic(L, hlua_panic_ljmp); |
| 11469 | } |
| 11470 | |
| 11471 | hlua_prepend_path(L, type, path); |
| 11472 | |
| 11473 | lua_atpanic(L, hlua_panic_safe); |
| 11474 | } |
| 11475 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11476 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11477 | |
| 11478 | err2: |
| 11479 | free(p->type); |
| 11480 | free(p->path); |
| 11481 | err: |
| 11482 | free(p); |
| 11483 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11484 | } |
| 11485 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11486 | /* configuration keywords declaration */ |
| 11487 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11488 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11489 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11490 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11491 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11492 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11493 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11494 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11495 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11496 | { 0, NULL, NULL }, |
| 11497 | }}; |
| 11498 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11499 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11500 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11501 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11502 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11503 | /* |
| 11504 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11505 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11506 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11507 | * way. |
| 11508 | */ |
| 11509 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11510 | { |
| 11511 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11512 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11513 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11514 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11515 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11516 | struct hlua *hlua; |
| 11517 | char *err = NULL; |
| 11518 | int y = 1; |
| 11519 | |
| 11520 | hlua = hlua_gethlua(L); |
| 11521 | |
| 11522 | /* get the first ckchi to copy */ |
| 11523 | if (ckchi == NULL) |
| 11524 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11525 | |
| 11526 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11527 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11528 | struct ckch_inst *new_inst; |
| 11529 | |
| 11530 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11531 | if (y % 10 == 0) { |
| 11532 | |
| 11533 | *lua_ckchi = ckchi; |
| 11534 | |
| 11535 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11536 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11537 | } |
| 11538 | |
| 11539 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11540 | goto error; |
| 11541 | |
| 11542 | /* link the new ckch_inst to the duplicate */ |
| 11543 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11544 | y++; |
| 11545 | } |
| 11546 | |
| 11547 | /* The generation is finished, we can insert everything */ |
| 11548 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11549 | |
| 11550 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11551 | |
| 11552 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11553 | |
| 11554 | return 0; |
| 11555 | |
| 11556 | error: |
| 11557 | ckch_store_free(new_ckchs); |
| 11558 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11559 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11560 | free(err); |
| 11561 | |
| 11562 | return 0; |
| 11563 | } |
| 11564 | |
| 11565 | /* |
| 11566 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11567 | * from the table in parameter. |
| 11568 | * |
| 11569 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11570 | * means it does not need to have a transaction since everything is done in the |
| 11571 | * same function. |
| 11572 | * |
| 11573 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11574 | * |
| 11575 | */ |
| 11576 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11577 | { |
| 11578 | struct hlua *hlua; |
| 11579 | struct ckch_inst **lua_ckchi; |
| 11580 | struct ckch_store **lua_ckchs; |
| 11581 | struct ckch_store *old_ckchs = NULL; |
| 11582 | struct ckch_store *new_ckchs = NULL; |
| 11583 | int errcode = 0; |
| 11584 | char *err = NULL; |
| 11585 | struct cert_exts *cert_ext = NULL; |
| 11586 | char *filename; |
| 11587 | struct cert_key_and_chain *ckch; |
| 11588 | int ret; |
| 11589 | |
| 11590 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11591 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11592 | |
| 11593 | hlua = hlua_gethlua(L); |
| 11594 | |
| 11595 | /* FIXME: this should not return an error but should come back later */ |
| 11596 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11597 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11598 | |
| 11599 | ret = lua_getfield(L, -1, "filename"); |
| 11600 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11601 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11602 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11603 | goto end; |
| 11604 | } |
| 11605 | filename = (char *)lua_tostring(L, -1); |
| 11606 | |
| 11607 | |
| 11608 | /* look for the filename in the tree */ |
| 11609 | old_ckchs = ckchs_lookup(filename); |
| 11610 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11611 | 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] | 11612 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11613 | goto end; |
| 11614 | } |
| 11615 | /* TODO: handle extra_files_noext */ |
| 11616 | |
| 11617 | new_ckchs = ckchs_dup(old_ckchs); |
| 11618 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11619 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11620 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11621 | goto end; |
| 11622 | } |
| 11623 | |
| 11624 | ckch = new_ckchs->ckch; |
| 11625 | |
| 11626 | /* loop on the field in the table, which have the same name as the |
| 11627 | * possible extensions of files */ |
| 11628 | lua_pushnil(L); |
| 11629 | while (lua_next(L, 1)) { |
| 11630 | int i; |
| 11631 | const char *field = lua_tostring(L, -2); |
| 11632 | char *payload = (char *)lua_tostring(L, -1); |
| 11633 | |
| 11634 | if (!field || strcmp(field, "filename") == 0) { |
| 11635 | lua_pop(L, 1); |
| 11636 | continue; |
| 11637 | } |
| 11638 | |
| 11639 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11640 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11641 | cert_ext = &cert_exts[i]; |
| 11642 | break; |
| 11643 | } |
| 11644 | } |
| 11645 | |
| 11646 | /* this is the default type, the field is not supported */ |
| 11647 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11648 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11649 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11650 | goto end; |
| 11651 | } |
| 11652 | |
| 11653 | /* appply the change on the duplicate */ |
William Lallemand | d8c195a | 2022-05-26 11:20:13 +0200 | [diff] [blame] | 11654 | if (cert_ext->load(filename, payload, ckch, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11655 | 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] | 11656 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11657 | goto end; |
| 11658 | } |
| 11659 | lua_pop(L, 1); |
| 11660 | } |
| 11661 | |
| 11662 | /* store the pointers on the lua stack */ |
| 11663 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11664 | lua_ckchs[0] = old_ckchs; |
| 11665 | lua_ckchs[1] = new_ckchs; |
| 11666 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11667 | *lua_ckchi = NULL; |
| 11668 | |
| 11669 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11670 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11671 | |
| 11672 | end: |
| 11673 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11674 | |
| 11675 | if (errcode & ERR_CODE) { |
| 11676 | ckch_store_free(new_ckchs); |
| 11677 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11678 | } |
| 11679 | free(err); |
| 11680 | |
| 11681 | return 0; |
| 11682 | } |
| 11683 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11684 | #else |
| 11685 | |
| 11686 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11687 | { |
| 11688 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11689 | |
| 11690 | return 0; |
| 11691 | } |
| 11692 | #endif /* ! USE_OPENSSL */ |
| 11693 | |
| 11694 | |
| 11695 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11696 | /* This function can fail with an abort() due to an Lua critical error. |
| 11697 | * We are in the initialisation process of HAProxy, this abort() is |
| 11698 | * tolerated. |
| 11699 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11700 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11701 | { |
| 11702 | struct hlua_init_function *init; |
| 11703 | const char *msg; |
| 11704 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11705 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11706 | const char *kind; |
| 11707 | const char *trace; |
| 11708 | int return_status = 1; |
| 11709 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11710 | int nres; |
| 11711 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11712 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11713 | /* disable memory limit checks if limit is not set */ |
| 11714 | if (!hlua_global_allocator.limit) |
| 11715 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11716 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11717 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11718 | if (setjmp(safe_ljmp_env) != 0) { |
| 11719 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11720 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11721 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11722 | else |
| 11723 | error = "critical error"; |
| 11724 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11725 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11726 | } else { |
| 11727 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11728 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11729 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11730 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11731 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11732 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11733 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11734 | |
| 11735 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11736 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11737 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11738 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11739 | #endif |
| 11740 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11741 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11742 | |
| 11743 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11744 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11745 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11746 | |
| 11747 | case LUA_ERRERR: |
| 11748 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11749 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11750 | case LUA_ERRRUN: |
| 11751 | if (!kind) |
| 11752 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11753 | msg = lua_tostring(L, -1); |
| 11754 | lua_settop(L, 0); /* Empty the stack. */ |
| 11755 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11756 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11757 | if (msg) |
| 11758 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11759 | else |
| 11760 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11761 | return_status = 0; |
| 11762 | break; |
| 11763 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11764 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11765 | /* Unknown error */ |
| 11766 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11767 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11768 | case LUA_YIELD: |
| 11769 | /* yield is not configured at this step, this state doesn't happen */ |
| 11770 | if (!kind) |
| 11771 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11772 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11773 | case LUA_ERRMEM: |
| 11774 | if (!kind) |
| 11775 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11776 | lua_settop(L, 0); |
| 11777 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11778 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11779 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11780 | return_status = 0; |
| 11781 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11782 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11783 | if (!return_status) |
| 11784 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11785 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11786 | |
| 11787 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11788 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11789 | } |
| 11790 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11791 | int hlua_post_init() |
| 11792 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11793 | int ret; |
| 11794 | int i; |
| 11795 | int errors; |
| 11796 | char *err = NULL; |
| 11797 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11798 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11799 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11800 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11801 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11802 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11803 | int saved_used_backed = global.ssl_used_backend; |
| 11804 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11805 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11806 | global.ssl_used_backend = saved_used_backed; |
| 11807 | } |
| 11808 | #endif |
| 11809 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11810 | /* Perform post init of common thread */ |
| 11811 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11812 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11813 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11814 | if (ret == 0) |
| 11815 | return 0; |
| 11816 | |
| 11817 | /* init remaining lua states and load files */ |
| 11818 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11819 | |
| 11820 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11821 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11822 | |
| 11823 | /* Init lua state */ |
| 11824 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11825 | |
| 11826 | /* Load lua files */ |
| 11827 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11828 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11829 | if (ret != 0) { |
| 11830 | ha_alert("Lua init: %s\n", err); |
| 11831 | return 0; |
| 11832 | } |
| 11833 | } |
| 11834 | } |
| 11835 | |
| 11836 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11837 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11838 | |
| 11839 | /* Execute post init for all states */ |
| 11840 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11841 | |
| 11842 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11843 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11844 | |
| 11845 | /* run post init */ |
| 11846 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11847 | if (ret == 0) |
| 11848 | return 0; |
| 11849 | } |
| 11850 | |
| 11851 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11852 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11853 | |
| 11854 | /* control functions registering. Each function must have: |
| 11855 | * - only the function_ref[0] set positive and all other to -1 |
| 11856 | * - only the function_ref[0] set to -1 and all other positive |
| 11857 | * This ensure a same reference is not used both in shared |
| 11858 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11859 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11860 | * complicated to found for the end user. |
| 11861 | */ |
| 11862 | errors = 0; |
| 11863 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11864 | ret = 0; |
| 11865 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11866 | if (fcn->function_ref[i] == -1) |
| 11867 | ret--; |
| 11868 | else |
| 11869 | ret++; |
| 11870 | } |
| 11871 | if (abs(ret) != global.nbthread) { |
| 11872 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11873 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11874 | errors++; |
| 11875 | continue; |
| 11876 | } |
| 11877 | |
| 11878 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11879 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11880 | "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] | 11881 | "exclusive.\n", fcn->name); |
| 11882 | errors++; |
| 11883 | } |
| 11884 | } |
| 11885 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11886 | /* Do the same with registered filters */ |
| 11887 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11888 | ret = 0; |
| 11889 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11890 | if (reg_flt->flt_ref[i] == -1) |
| 11891 | ret--; |
| 11892 | else |
| 11893 | ret++; |
| 11894 | } |
| 11895 | if (abs(ret) != global.nbthread) { |
| 11896 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11897 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11898 | errors++; |
| 11899 | continue; |
| 11900 | } |
| 11901 | |
| 11902 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11903 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11904 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11905 | "exclusive.\n", fcn->name); |
| 11906 | errors++; |
| 11907 | } |
| 11908 | } |
| 11909 | |
| 11910 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11911 | if (errors > 0) |
| 11912 | return 0; |
| 11913 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11914 | /* 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] | 11915 | * -1 in order to have probably a segfault if someone use it |
| 11916 | */ |
| 11917 | hlua_state_id = -1; |
| 11918 | |
| 11919 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11920 | } |
| 11921 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11922 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11923 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11924 | * 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] | 11925 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11926 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11927 | * zero. This one verifies that the limits are respected but is optimized |
| 11928 | * 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] | 11929 | * |
| 11930 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11931 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11932 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11933 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11934 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11935 | */ |
| 11936 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11937 | { |
| 11938 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11939 | size_t limit, old, new; |
| 11940 | |
| 11941 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11942 | * for accounting anymore. |
| 11943 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11944 | if (likely(~zone->limit == 0)) { |
| 11945 | if (!nsize) |
| 11946 | ha_free(&ptr); |
| 11947 | else |
| 11948 | ptr = realloc(ptr, nsize); |
| 11949 | return ptr; |
| 11950 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11951 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11952 | if (!ptr) |
| 11953 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11954 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11955 | /* enforce strict limits across all threads */ |
| 11956 | limit = zone->limit; |
| 11957 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11958 | do { |
| 11959 | new = old + nsize - osize; |
| 11960 | if (unlikely(nsize && limit && new > limit)) |
| 11961 | return NULL; |
| 11962 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11963 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11964 | if (!nsize) |
| 11965 | ha_free(&ptr); |
| 11966 | else |
| 11967 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11968 | |
| 11969 | if (unlikely(!ptr && nsize)) // failed |
| 11970 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11971 | |
| 11972 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11973 | return ptr; |
| 11974 | } |
| 11975 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11976 | /* 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] | 11977 | * We are in the initialisation process of HAProxy, this abort() is |
| 11978 | * tolerated. |
| 11979 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11980 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11981 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11982 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11983 | int idx; |
| 11984 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11985 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11986 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11987 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11988 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11989 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11990 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11991 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11992 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11993 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11994 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11995 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11996 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11997 | *context = NULL; |
| 11998 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11999 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12000 | * the Lua function can fail with an abort. We are in the initialisation |
| 12001 | * process of HAProxy, this abort() is tolerated. |
| 12002 | */ |
| 12003 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12004 | /* Call post initialisation function in safe environment. */ |
| 12005 | if (setjmp(safe_ljmp_env) != 0) { |
| 12006 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12007 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12008 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12009 | else |
| 12010 | error_msg = "critical error"; |
| 12011 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12012 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12013 | } else { |
| 12014 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12015 | } |
| 12016 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12017 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12018 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12019 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12020 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12021 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12022 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12023 | #endif |
| 12024 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12025 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12026 | #endif |
| 12027 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12028 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12029 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12030 | /* Apply configured prepend path */ |
| 12031 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12032 | hlua_prepend_path(L, pp->type, pp->path); |
| 12033 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12034 | /* |
| 12035 | * |
| 12036 | * Create "core" object. |
| 12037 | * |
| 12038 | */ |
| 12039 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12040 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12041 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12042 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12043 | /* set the thread id */ |
| 12044 | hlua_class_const_int(L, "thread", thread_num); |
| 12045 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12046 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12047 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12048 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12049 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12050 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12051 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12052 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12053 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12054 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12055 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12056 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12057 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12058 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12059 | hlua_class_function(L, "yield", hlua_yield); |
| 12060 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12061 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12062 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12063 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12064 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12065 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12066 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12067 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12068 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12069 | hlua_class_function(L, "log", hlua_log); |
| 12070 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12071 | hlua_class_function(L, "Info", hlua_log_info); |
| 12072 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12073 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12074 | hlua_class_function(L, "done", hlua_done); |
| 12075 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12076 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12077 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12078 | |
| 12079 | /* |
| 12080 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12081 | * Create "act" object. |
| 12082 | * |
| 12083 | */ |
| 12084 | |
| 12085 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12086 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12087 | |
| 12088 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12089 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12090 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12091 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12092 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12093 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12094 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12095 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12096 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12097 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12098 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12099 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12100 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12101 | |
| 12102 | /* |
| 12103 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12104 | * Create "Filter" object. |
| 12105 | * |
| 12106 | */ |
| 12107 | |
| 12108 | /* This table entry is the object "filter" base. */ |
| 12109 | lua_newtable(L); |
| 12110 | |
| 12111 | /* push flags and constants */ |
| 12112 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12113 | hlua_class_const_int(L, "WAIT", 0); |
| 12114 | hlua_class_const_int(L, "ERROR", -1); |
| 12115 | |
| 12116 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12117 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12118 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12119 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12120 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12121 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12122 | lua_setglobal(L, "filter"); |
| 12123 | |
| 12124 | /* |
| 12125 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12126 | * Register class Map |
| 12127 | * |
| 12128 | */ |
| 12129 | |
| 12130 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12131 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12132 | |
| 12133 | /* register pattern types. */ |
| 12134 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12135 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12136 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12137 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12138 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12139 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12140 | |
| 12141 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12142 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12143 | |
| 12144 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12145 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12146 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12147 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12148 | lua_pushstring(L, "__index"); |
| 12149 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12150 | |
| 12151 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12152 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12153 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12154 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12155 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12156 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12157 | /* Register previous table in the registry with reference and named entry. |
| 12158 | * The function hlua_register_metatable() pops the stack, so we |
| 12159 | * previously create a copy of the table. |
| 12160 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12161 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12162 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12163 | |
| 12164 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12165 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12166 | |
| 12167 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12168 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12169 | |
| 12170 | /* |
| 12171 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12172 | * Register "CertCache" class |
| 12173 | * |
| 12174 | */ |
| 12175 | |
| 12176 | /* Create and fill the metatable. */ |
| 12177 | lua_newtable(L); |
| 12178 | /* Register */ |
| 12179 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12180 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12181 | |
| 12182 | /* |
| 12183 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12184 | * Register class Channel |
| 12185 | * |
| 12186 | */ |
| 12187 | |
| 12188 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12189 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12190 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12191 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12192 | lua_pushstring(L, "__index"); |
| 12193 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12194 | |
| 12195 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12196 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12197 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12198 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12199 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12200 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12201 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12202 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12203 | hlua_class_function(L, "send", hlua_channel_send); |
| 12204 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12205 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12206 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12207 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12208 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12209 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12210 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12211 | /* Deprecated API */ |
| 12212 | hlua_class_function(L, "get", hlua_channel_get); |
| 12213 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12214 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12215 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12216 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12217 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12218 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12219 | |
| 12220 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12221 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12222 | |
| 12223 | /* |
| 12224 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12225 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12226 | * |
| 12227 | */ |
| 12228 | |
| 12229 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12230 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12231 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12232 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12233 | lua_pushstring(L, "__index"); |
| 12234 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12235 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12236 | /* Browse existing fetches and create the associated |
| 12237 | * object method. |
| 12238 | */ |
| 12239 | sf = NULL; |
| 12240 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12241 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12242 | * by an underscore. |
| 12243 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12244 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12245 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12246 | if (*p == '.' || *p == '-' || *p == '+') |
| 12247 | *p = '_'; |
| 12248 | |
| 12249 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12250 | lua_pushstring(L, trash.area); |
| 12251 | lua_pushlightuserdata(L, sf); |
| 12252 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12253 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12254 | } |
| 12255 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12256 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12257 | |
| 12258 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12259 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12260 | |
| 12261 | /* |
| 12262 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12263 | * Register class Converters |
| 12264 | * |
| 12265 | */ |
| 12266 | |
| 12267 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12268 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12269 | |
| 12270 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12271 | lua_pushstring(L, "__index"); |
| 12272 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12273 | |
| 12274 | /* Browse existing converters and create the associated |
| 12275 | * object method. |
| 12276 | */ |
| 12277 | sc = NULL; |
| 12278 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12279 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12280 | * by an underscore. |
| 12281 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12282 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12283 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12284 | if (*p == '.' || *p == '-' || *p == '+') |
| 12285 | *p = '_'; |
| 12286 | |
| 12287 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12288 | lua_pushstring(L, trash.area); |
| 12289 | lua_pushlightuserdata(L, sc); |
| 12290 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12291 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12292 | } |
| 12293 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12294 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12295 | |
| 12296 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12297 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12298 | |
| 12299 | /* |
| 12300 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12301 | * Register class HTTP |
| 12302 | * |
| 12303 | */ |
| 12304 | |
| 12305 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12306 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12307 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12308 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12309 | lua_pushstring(L, "__index"); |
| 12310 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12311 | |
| 12312 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12313 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12314 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12315 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12316 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12317 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12318 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12319 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12320 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12321 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12322 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12323 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12324 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12325 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12326 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12327 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12328 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12329 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12330 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12331 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12332 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12333 | |
| 12334 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12335 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12336 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12337 | /* |
| 12338 | * |
| 12339 | * Register class HTTPMessage |
| 12340 | * |
| 12341 | */ |
| 12342 | |
| 12343 | /* Create and fill the metatable. */ |
| 12344 | lua_newtable(L); |
| 12345 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12346 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12347 | lua_pushstring(L, "__index"); |
| 12348 | lua_newtable(L); |
| 12349 | |
| 12350 | /* Register Lua functions. */ |
| 12351 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12352 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12353 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12354 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12355 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12356 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12357 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12358 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12359 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12360 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12361 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12362 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12363 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12364 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12365 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12366 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12367 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12368 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12369 | |
| 12370 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12371 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12372 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12373 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12374 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12375 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12376 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12377 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12378 | |
| 12379 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12380 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12381 | |
| 12382 | lua_rawset(L, -3); |
| 12383 | |
| 12384 | /* Register previous table in the registry with reference and named entry. */ |
| 12385 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12386 | |
| 12387 | /* |
| 12388 | * |
| 12389 | * Register class HTTPClient |
| 12390 | * |
| 12391 | */ |
| 12392 | |
| 12393 | /* Create and fill the metatable. */ |
| 12394 | lua_newtable(L); |
| 12395 | lua_pushstring(L, "__index"); |
| 12396 | lua_newtable(L); |
| 12397 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12398 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12399 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12400 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12401 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12402 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12403 | /* Register the garbage collector entry. */ |
| 12404 | lua_pushstring(L, "__gc"); |
| 12405 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12406 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12407 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12408 | |
| 12409 | |
| 12410 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12411 | /* |
| 12412 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12413 | * Register class AppletTCP |
| 12414 | * |
| 12415 | */ |
| 12416 | |
| 12417 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12418 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12419 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12420 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12421 | lua_pushstring(L, "__index"); |
| 12422 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12423 | |
| 12424 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12425 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12426 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12427 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12428 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12429 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12430 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12431 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12432 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12433 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12434 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12435 | |
| 12436 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12437 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12438 | |
| 12439 | /* |
| 12440 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12441 | * Register class AppletHTTP |
| 12442 | * |
| 12443 | */ |
| 12444 | |
| 12445 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12446 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12447 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12448 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12449 | lua_pushstring(L, "__index"); |
| 12450 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12451 | |
| 12452 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12453 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12454 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12455 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12456 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12457 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12458 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12459 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12460 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12461 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12462 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12463 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12464 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12465 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12466 | |
| 12467 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12468 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12469 | |
| 12470 | /* |
| 12471 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12472 | * Register class TXN |
| 12473 | * |
| 12474 | */ |
| 12475 | |
| 12476 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12477 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12478 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12479 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12480 | lua_pushstring(L, "__index"); |
| 12481 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12482 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12483 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12484 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12485 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12486 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12487 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12488 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12489 | hlua_class_function(L, "done", hlua_txn_done); |
| 12490 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12491 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12492 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12493 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12494 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12495 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12496 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12497 | hlua_class_function(L, "log", hlua_txn_log); |
| 12498 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12499 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12500 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12501 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12502 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12503 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12504 | |
| 12505 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12506 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12507 | |
| 12508 | /* |
| 12509 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12510 | * Register class reply |
| 12511 | * |
| 12512 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12513 | lua_newtable(L); |
| 12514 | lua_pushstring(L, "__index"); |
| 12515 | lua_newtable(L); |
| 12516 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12517 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12518 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12519 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12520 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12521 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12522 | |
| 12523 | |
| 12524 | /* |
| 12525 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12526 | * Register class Socket |
| 12527 | * |
| 12528 | */ |
| 12529 | |
| 12530 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12531 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12532 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12533 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12534 | lua_pushstring(L, "__index"); |
| 12535 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12536 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12537 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12538 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12539 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12540 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12541 | hlua_class_function(L, "send", hlua_socket_send); |
| 12542 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12543 | hlua_class_function(L, "close", hlua_socket_close); |
| 12544 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12545 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12546 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12547 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12548 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12549 | 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] | 12550 | |
| 12551 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12552 | lua_pushstring(L, "__gc"); |
| 12553 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12554 | 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] | 12555 | |
| 12556 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12557 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12558 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12559 | lua_atpanic(L, hlua_panic_safe); |
| 12560 | |
| 12561 | return L; |
| 12562 | } |
| 12563 | |
| 12564 | void hlua_init(void) { |
| 12565 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12566 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12567 | #ifdef USE_OPENSSL |
| 12568 | struct srv_kw *kw; |
| 12569 | int tmp_error; |
| 12570 | char *error; |
| 12571 | char *args[] = { /* SSL client configuration. */ |
| 12572 | "ssl", |
| 12573 | "verify", |
| 12574 | "none", |
| 12575 | NULL |
| 12576 | }; |
| 12577 | #endif |
| 12578 | |
| 12579 | /* Init post init function list head */ |
| 12580 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12581 | LIST_INIT(&hlua_init_functions[i]); |
| 12582 | |
| 12583 | /* Init state for common/shared lua parts */ |
| 12584 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12585 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12586 | hlua_states[0] = hlua_init_state(0); |
| 12587 | |
| 12588 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12589 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12590 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12591 | hlua_states[1] = hlua_init_state(1); |
| 12592 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12593 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12594 | 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] | 12595 | if (!socket_proxy) { |
| 12596 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12597 | exit(1); |
| 12598 | } |
| 12599 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12600 | |
| 12601 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12602 | socket_tcp = new_server(socket_proxy); |
| 12603 | if (!socket_tcp) { |
| 12604 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12605 | exit(1); |
| 12606 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12607 | |
| 12608 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12609 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12610 | socket_ssl = new_server(socket_proxy); |
| 12611 | if (!socket_ssl) { |
| 12612 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12613 | exit(1); |
| 12614 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12615 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12616 | socket_ssl->use_ssl = 1; |
| 12617 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12618 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12619 | for (i = 0; args[i] != NULL; i++) { |
| 12620 | 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] | 12621 | /* |
| 12622 | * |
| 12623 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12624 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12625 | * features like client certificates and ssl_verify. |
| 12626 | * |
| 12627 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12628 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12629 | if (tmp_error != 0) { |
| 12630 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12631 | abort(); /* This must be never arrives because the command line |
| 12632 | not editable by the user. */ |
| 12633 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12634 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12635 | } |
| 12636 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12637 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12638 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12639 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12640 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12641 | static void hlua_deinit() |
| 12642 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12643 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12644 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12645 | |
| 12646 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12647 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12648 | |
| 12649 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12650 | if (hlua_states[thr]) |
| 12651 | lua_close(hlua_states[thr]); |
| 12652 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12653 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12654 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12655 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12656 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12657 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12658 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12659 | |
| 12660 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12661 | } |
| 12662 | |
| 12663 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12664 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12665 | static void hlua_register_build_options(void) |
| 12666 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12667 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12668 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12669 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12670 | hap_register_build_opts(ptr, 1); |
| 12671 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12672 | |
| 12673 | INITCALL0(STG_REGISTER, hlua_register_build_options); |