Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Bertrand Jacquin | f4c12d4 | 2021-01-21 21:14:07 +0000 | [diff] [blame] | 13 | #define _GNU_SOURCE |
| 14 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | #include <ctype.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 16 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 17 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 18 | #include <lauxlib.h> |
| 19 | #include <lua.h> |
| 20 | #include <lualib.h> |
| 21 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 22 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 23 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 24 | #endif |
| 25 | |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 26 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 27 | |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 28 | #include <haproxy/api.h> |
| 29 | #include <haproxy/applet.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 30 | #include <haproxy/arg.h> |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 31 | #include <haproxy/auth.h> |
Willy Tarreau | 6be7849 | 2020-06-05 00:00:29 +0200 | [diff] [blame] | 32 | #include <haproxy/cfgparse.h> |
Willy Tarreau | f1d32c4 | 2020-06-04 21:07:02 +0200 | [diff] [blame] | 33 | #include <haproxy/channel.h> |
Willy Tarreau | 83487a8 | 2020-06-04 20:19:54 +0200 | [diff] [blame] | 34 | #include <haproxy/cli.h> |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 35 | #include <haproxy/clock.h> |
Willy Tarreau | 7ea393d | 2020-06-04 18:02:10 +0200 | [diff] [blame] | 36 | #include <haproxy/connection.h> |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 37 | #include <haproxy/filters.h> |
Willy Tarreau | 5413a87 | 2020-06-02 19:33:08 +0200 | [diff] [blame] | 38 | #include <haproxy/h1.h> |
Willy Tarreau | 8641605 | 2020-06-04 09:20:54 +0200 | [diff] [blame] | 39 | #include <haproxy/hlua.h> |
Willy Tarreau | 8c79400 | 2020-06-04 10:05:25 +0200 | [diff] [blame] | 40 | #include <haproxy/hlua_fcn.h> |
Willy Tarreau | c2b1ff0 | 2020-06-04 21:21:03 +0200 | [diff] [blame] | 41 | #include <haproxy/http_ana.h> |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 42 | #include <haproxy/http_client.h> |
Willy Tarreau | 126ba3a | 2020-06-04 18:26:43 +0200 | [diff] [blame] | 43 | #include <haproxy/http_fetch.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 44 | #include <haproxy/http_htx.h> |
Willy Tarreau | c761f84 | 2020-06-04 11:40:28 +0200 | [diff] [blame] | 45 | #include <haproxy/http_rules.h> |
Willy Tarreau | 36979d9 | 2020-06-05 17:27:29 +0200 | [diff] [blame] | 46 | #include <haproxy/log.h> |
Willy Tarreau | 2cd5809 | 2020-06-04 15:10:43 +0200 | [diff] [blame] | 47 | #include <haproxy/map.h> |
Willy Tarreau | 8efbdfb | 2020-06-04 11:29:21 +0200 | [diff] [blame] | 48 | #include <haproxy/obj_type.h> |
Willy Tarreau | 225a90a | 2020-06-04 15:06:28 +0200 | [diff] [blame] | 49 | #include <haproxy/pattern.h> |
Willy Tarreau | 469509b | 2020-06-04 15:13:30 +0200 | [diff] [blame] | 50 | #include <haproxy/payload.h> |
Willy Tarreau | 3d6ee40 | 2021-05-08 20:28:07 +0200 | [diff] [blame] | 51 | #include <haproxy/proxy.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 52 | #include <haproxy/regex.h> |
Willy Tarreau | e6ce10b | 2020-06-04 15:33:47 +0200 | [diff] [blame] | 53 | #include <haproxy/sample.h> |
Willy Tarreau | 5edca2f | 2022-05-27 09:25:10 +0200 | [diff] [blame] | 54 | #include <haproxy/sc_strm.h> |
Willy Tarreau | 198e92a | 2021-03-05 10:23:32 +0100 | [diff] [blame] | 55 | #include <haproxy/server.h> |
Willy Tarreau | 48d25b3 | 2020-06-04 18:58:52 +0200 | [diff] [blame] | 56 | #include <haproxy/session.h> |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 57 | #include <haproxy/ssl_ckch.h> |
| 58 | #include <haproxy/ssl_sock.h> |
Willy Tarreau | 2eec9b5 | 2020-06-04 19:58:55 +0200 | [diff] [blame] | 59 | #include <haproxy/stats-t.h> |
Willy Tarreau | cb086c6 | 2022-05-27 09:47:12 +0200 | [diff] [blame] | 60 | #include <haproxy/stconn.h> |
Willy Tarreau | dfd3de8 | 2020-06-04 23:46:14 +0200 | [diff] [blame] | 61 | #include <haproxy/stream.h> |
Willy Tarreau | cea0e1b | 2020-06-04 17:25:40 +0200 | [diff] [blame] | 62 | #include <haproxy/task.h> |
Willy Tarreau | 8b550af | 2020-06-04 17:42:48 +0200 | [diff] [blame] | 63 | #include <haproxy/tcp_rules.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 64 | #include <haproxy/thread.h> |
Willy Tarreau | 48fbcae | 2020-06-03 18:09:46 +0200 | [diff] [blame] | 65 | #include <haproxy/tools.h> |
Willy Tarreau | a171892 | 2020-06-04 16:25:31 +0200 | [diff] [blame] | 66 | #include <haproxy/vars.h> |
Willy Tarreau | b255105 | 2020-06-09 09:07:15 +0200 | [diff] [blame] | 67 | #include <haproxy/xref.h> |
| 68 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 69 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 70 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 71 | * macro is used only for identifying the function that can |
| 72 | * not return because a longjmp is executed. |
| 73 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 74 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 75 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 76 | */ |
| 77 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 78 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 79 | #define MAY_LJMP(func) func |
| 80 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 81 | /* This couple of function executes securely some Lua calls outside of |
| 82 | * the lua runtime environment. Each Lua call can return a longjmp |
| 83 | * if it encounter a memory error. |
| 84 | * |
| 85 | * Lua documentation extract: |
| 86 | * |
| 87 | * If an error happens outside any protected environment, Lua calls |
| 88 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 89 | * exiting the host application. Your panic function can avoid this |
| 90 | * exit by never returning (e.g., doing a long jump to your own |
| 91 | * recovery point outside Lua). |
| 92 | * |
| 93 | * The panic function runs as if it were a message handler (see |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 94 | * #2.3); in particular, the error message is at the top of the |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 95 | * stack. However, there is no guarantee about stack space. To push |
| 96 | * anything on the stack, the panic function must first check the |
Willy Tarreau | 3dfb7da | 2022-03-02 22:33:39 +0100 | [diff] [blame] | 97 | * available space (see #4.2). |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 98 | * |
| 99 | * We must check all the Lua entry point. This includes: |
| 100 | * - The include/proto/hlua.h exported functions |
| 101 | * - the task wrapper function |
| 102 | * - The action wrapper function |
| 103 | * - The converters wrapper function |
| 104 | * - The sample-fetch wrapper functions |
| 105 | * |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 106 | * It is tolerated that the initialisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 107 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 108 | * |
| 109 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 110 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 111 | * because they must be exists in the program stack when the longjmp |
| 112 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 113 | * |
| 114 | * Note that the Lua processing is not really thread safe. It provides |
| 115 | * heavy system which consists to add our own lock function in the Lua |
| 116 | * code and recompile the library. This system will probably not accepted |
| 117 | * by maintainers of various distribs. |
| 118 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 119 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 120 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 121 | * of function and a lua_unlock() at the end of the function. So I |
| 122 | * conclude that the Lua thread safe mode just perform a mutex around |
| 123 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 124 | * easier for distro maintainers. |
| 125 | * |
| 126 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 127 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 128 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 130 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 131 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 132 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 6a51090 | 2021-07-14 19:41:25 +0200 | [diff] [blame] | 133 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); return 0; } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 135 | /* This is the chained list of struct hlua_function referenced |
| 136 | * for haproxy action, sample-fetches, converters, cli and |
| 137 | * applet bindings. It is used for a post-initialisation control. |
| 138 | */ |
| 139 | static struct list referenced_functions = LIST_HEAD_INIT(referenced_functions); |
| 140 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 141 | /* This variable is used only during initialization to identify the Lua state |
| 142 | * currently being initialized. 0 is the common lua state, 1 to n are the Lua |
| 143 | * states dedicated to each thread (in this case hlua_state_id==tid+1). |
| 144 | */ |
| 145 | static int hlua_state_id; |
| 146 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 147 | /* This is a NULL-terminated list of lua file which are referenced to load per thread */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 148 | static char ***per_thread_load = NULL; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 149 | |
| 150 | lua_State *hlua_init_state(int thread_id); |
| 151 | |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 152 | /* This function takes the Lua global lock. Keep this function's visibility |
| 153 | * global so that it can appear in stack dumps and performance profiles! |
| 154 | */ |
| 155 | void lua_take_global_lock() |
| 156 | { |
| 157 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
| 158 | } |
| 159 | |
| 160 | static inline void lua_drop_global_lock() |
| 161 | { |
| 162 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
| 163 | } |
| 164 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 165 | #define SET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 166 | ({ \ |
| 167 | int ret; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 168 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 169 | lua_take_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 170 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 171 | lua_atpanic(__L, hlua_panic_safe); \ |
| 172 | ret = 0; \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 173 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 174 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 175 | } else { \ |
| 176 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 177 | ret = 1; \ |
| 178 | } \ |
| 179 | ret; \ |
| 180 | }) |
| 181 | |
| 182 | /* If we are the last function catching Lua errors, we |
| 183 | * must reset the panic function. |
| 184 | */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 185 | #define RESET_SAFE_LJMP_L(__L, __HLUA) \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 186 | do { \ |
| 187 | lua_atpanic(__L, hlua_panic_safe); \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 188 | if ((__HLUA)->state_id == 0) \ |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 189 | lua_drop_global_lock(); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 190 | } while(0) |
| 191 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 192 | #define SET_SAFE_LJMP(__HLUA) \ |
| 193 | SET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 194 | |
| 195 | #define RESET_SAFE_LJMP(__HLUA) \ |
| 196 | RESET_SAFE_LJMP_L((__HLUA)->T, __HLUA) |
| 197 | |
| 198 | #define SET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 199 | SET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 200 | |
| 201 | #define RESET_SAFE_LJMP_PARENT(__HLUA) \ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 202 | RESET_SAFE_LJMP_L(hlua_states[(__HLUA)->state_id], __HLUA) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 204 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 205 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 206 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 207 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 208 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 209 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 210 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 211 | |
Thierry Fournier | afc63e2 | 2020-11-28 17:06:51 +0100 | [diff] [blame] | 212 | /* The main Lua execution context. The 0 index is the |
| 213 | * common state shared by all threads. |
| 214 | */ |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 215 | static lua_State *hlua_states[MAX_THREADS + 1]; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 216 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 217 | #define HLUA_FLT_CB_FINAL 0x00000001 |
| 218 | #define HLUA_FLT_CB_RETVAL 0x00000002 |
| 219 | #define HLUA_FLT_CB_ARG_CHN 0x00000004 |
| 220 | #define HLUA_FLT_CB_ARG_HTTP_MSG 0x00000008 |
| 221 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 222 | #define HLUA_FLT_CTX_FL_PAYLOAD 0x00000001 |
| 223 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 224 | struct hlua_reg_filter { |
| 225 | char *name; |
| 226 | int flt_ref[MAX_THREADS + 1]; |
| 227 | int fun_ref[MAX_THREADS + 1]; |
| 228 | struct list l; |
| 229 | }; |
| 230 | |
| 231 | struct hlua_flt_config { |
| 232 | struct hlua_reg_filter *reg; |
| 233 | int ref[MAX_THREADS + 1]; |
| 234 | char **args; |
| 235 | }; |
| 236 | |
| 237 | struct hlua_flt_ctx { |
| 238 | int ref; /* ref to the filter lua object */ |
| 239 | struct hlua *hlua[2]; /* lua runtime context (0: request, 1: response) */ |
| 240 | unsigned int cur_off[2]; /* current offset (0: request, 1: response) */ |
| 241 | unsigned int cur_len[2]; /* current forwardable length (0: request, 1: response) */ |
| 242 | unsigned int flags; /* HLUA_FLT_CTX_FL_* */ |
| 243 | }; |
| 244 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 245 | /* appctx context used by the cosockets */ |
| 246 | struct hlua_csk_ctx { |
| 247 | int connected; |
| 248 | struct xref xref; /* cross reference with the Lua object owner. */ |
| 249 | struct list wake_on_read; |
| 250 | struct list wake_on_write; |
| 251 | struct appctx *appctx; |
| 252 | int die; |
| 253 | }; |
| 254 | |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 255 | /* appctx context used by TCP services */ |
| 256 | struct hlua_tcp_ctx { |
| 257 | struct hlua *hlua; |
| 258 | int flags; |
| 259 | struct task *task; |
| 260 | }; |
| 261 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 262 | /* appctx context used by HTTP services */ |
| 263 | struct hlua_http_ctx { |
| 264 | struct hlua *hlua; |
| 265 | int left_bytes; /* The max amount of bytes that we can read. */ |
| 266 | int flags; |
| 267 | int status; |
| 268 | const char *reason; |
| 269 | struct task *task; |
| 270 | }; |
| 271 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 272 | /* used by registered CLI keywords */ |
| 273 | struct hlua_cli_ctx { |
| 274 | struct hlua *hlua; |
| 275 | struct task *task; |
| 276 | struct hlua_function *fcn; |
| 277 | }; |
| 278 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 279 | DECLARE_STATIC_POOL(pool_head_hlua_flt_ctx, "hlua_flt_ctx", sizeof(struct hlua_flt_ctx)); |
| 280 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 281 | static int hlua_filter_from_payload(struct filter *filter); |
| 282 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 283 | /* This is the chained list of struct hlua_flt referenced |
| 284 | * for haproxy filters. It is used for a post-initialisation control. |
| 285 | */ |
| 286 | static struct list referenced_filters = LIST_HEAD_INIT(referenced_filters); |
| 287 | |
| 288 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 289 | /* This is the memory pool containing struct lua for applets |
| 290 | * (including cli). |
| 291 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 292 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 293 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 294 | /* Used for Socket connection. */ |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 295 | static struct proxy *socket_proxy; |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 296 | static struct server *socket_tcp; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 297 | #ifdef USE_OPENSSL |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 298 | static struct server *socket_ssl; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 299 | #endif |
| 300 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 301 | /* List head of the function called at the initialisation time. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 302 | struct list hlua_init_functions[MAX_THREADS + 1]; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 303 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 304 | /* The following variables contains the reference of the different |
| 305 | * Lua classes. These references are useful for identify metadata |
| 306 | * associated with an object. |
| 307 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 308 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 309 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 310 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 311 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 312 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 313 | static int class_http_ref; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 314 | static int class_http_msg_ref; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 315 | static int class_httpclient_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 316 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 317 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 318 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 319 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 320 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 321 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 322 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 323 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 324 | * because a task may remain alive during all the haproxy execution. |
| 325 | */ |
| 326 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 327 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 328 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 329 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 330 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 331 | * it is used for preventing infinite loops. |
| 332 | * |
| 333 | * I test the scheer with an infinite loop containing one incrementation |
| 334 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 335 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 336 | * to one interrupt each 10 000 instructions. |
| 337 | * |
| 338 | * configured | Number of |
| 339 | * instructions | loops executed |
| 340 | * between two | in milions |
| 341 | * forced yields | |
| 342 | * ---------------+--------------- |
| 343 | * 10 | 160 |
| 344 | * 500 | 670 |
| 345 | * 1000 | 680 |
| 346 | * 5000 | 700 |
| 347 | * 7000 | 700 |
| 348 | * 8000 | 700 |
| 349 | * 9000 | 710 <- ceil |
| 350 | * 10000 | 710 |
| 351 | * 100000 | 710 |
| 352 | * 1000000 | 710 |
| 353 | * |
| 354 | */ |
| 355 | static unsigned int hlua_nb_instruction = 10000; |
| 356 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 357 | /* Descriptor for the memory allocation state. The limit is pre-initialised to |
| 358 | * 0 until it is replaced by "tune.lua.maxmem" during the config parsing, or it |
| 359 | * is replaced with ~0 during post_init after everything was loaded. This way |
| 360 | * it is guaranteed that if limit is ~0 the boot is complete and that if it's |
| 361 | * zero it's not yet limited and proper accounting is required. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 362 | */ |
| 363 | struct hlua_mem_allocator { |
| 364 | size_t allocated; |
| 365 | size_t limit; |
| 366 | }; |
| 367 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 368 | static struct hlua_mem_allocator hlua_global_allocator THREAD_ALIGNED(64); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 369 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 370 | /* These functions converts types between HAProxy internal args or |
| 371 | * sample and LUA types. Another function permits to check if the |
| 372 | * LUA stack contains arguments according with an required ARG_T |
| 373 | * format. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 376 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 377 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 378 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 379 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 380 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 381 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 382 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 383 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 384 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 385 | struct prepend_path { |
| 386 | struct list l; |
| 387 | char *type; |
| 388 | char *path; |
| 389 | }; |
| 390 | |
| 391 | static struct list prepend_path_list = LIST_HEAD_INIT(prepend_path_list); |
| 392 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 393 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 394 | do { \ |
| 395 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 396 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 397 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 398 | } while (0) |
| 399 | |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 400 | static inline struct hlua_function *new_hlua_function() |
| 401 | { |
| 402 | struct hlua_function *fcn; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 403 | int i; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 404 | |
| 405 | fcn = calloc(1, sizeof(*fcn)); |
| 406 | if (!fcn) |
| 407 | return NULL; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 408 | LIST_APPEND(&referenced_functions, &fcn->l); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 409 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 410 | fcn->function_ref[i] = -1; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 411 | return fcn; |
| 412 | } |
| 413 | |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 414 | static inline void release_hlua_function(struct hlua_function *fcn) |
| 415 | { |
| 416 | if (!fcn) |
| 417 | return; |
| 418 | if (fcn->name) |
| 419 | ha_free(&fcn->name); |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 420 | LIST_DELETE(&fcn->l); |
Christopher Faulet | dda4444 | 2021-04-12 14:05:43 +0200 | [diff] [blame] | 421 | ha_free(&fcn); |
| 422 | } |
| 423 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 424 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 425 | static inline int fcn_ref_to_stack_id(struct hlua_function *fcn) |
| 426 | { |
| 427 | if (fcn->function_ref[0] == -1) |
| 428 | return tid + 1; |
| 429 | return 0; |
| 430 | } |
| 431 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 432 | /* Create a new registered filter. Only its name is filled */ |
| 433 | static inline struct hlua_reg_filter *new_hlua_reg_filter(const char *name) |
| 434 | { |
| 435 | struct hlua_reg_filter *reg_flt; |
| 436 | int i; |
| 437 | |
| 438 | reg_flt = calloc(1, sizeof(*reg_flt)); |
| 439 | if (!reg_flt) |
| 440 | return NULL; |
| 441 | reg_flt->name = strdup(name); |
| 442 | if (!reg_flt->name) { |
| 443 | free(reg_flt); |
| 444 | return NULL; |
| 445 | } |
| 446 | LIST_APPEND(&referenced_filters, ®_flt->l); |
| 447 | for (i = 0; i < MAX_THREADS + 1; i++) { |
| 448 | reg_flt->flt_ref[i] = -1; |
| 449 | reg_flt->fun_ref[i] = -1; |
| 450 | } |
| 451 | return reg_flt; |
| 452 | } |
| 453 | |
| 454 | /* Release a registered filter */ |
| 455 | static inline void release_hlua_reg_filter(struct hlua_reg_filter *reg_flt) |
| 456 | { |
| 457 | if (!reg_flt) |
| 458 | return; |
| 459 | if (reg_flt->name) |
| 460 | ha_free(®_flt->name); |
| 461 | LIST_DELETE(®_flt->l); |
| 462 | ha_free(®_flt); |
| 463 | } |
| 464 | |
| 465 | /* If the common state is set, the stack id is 0, otherwise it is the tid + 1 */ |
| 466 | static inline int reg_flt_to_stack_id(struct hlua_reg_filter *reg_flt) |
| 467 | { |
| 468 | if (reg_flt->fun_ref[0] == -1) |
| 469 | return tid + 1; |
| 470 | return 0; |
| 471 | } |
| 472 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 473 | /* Used to check an Lua function type in the stack. It creates and |
| 474 | * returns a reference of the function. This function throws an |
| 475 | * error if the rgument is not a "function". |
| 476 | */ |
| 477 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 478 | { |
| 479 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 480 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 481 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 482 | } |
| 483 | lua_pushvalue(L, argno); |
| 484 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 485 | } |
| 486 | |
Christopher Faulet | ba9e21d | 2020-02-25 10:20:04 +0100 | [diff] [blame] | 487 | /* Used to check an Lua table type in the stack. It creates and |
| 488 | * returns a reference of the table. This function throws an |
| 489 | * error if the rgument is not a "table". |
| 490 | */ |
| 491 | __LJMP unsigned int hlua_checktable(lua_State *L, int argno) |
| 492 | { |
| 493 | if (!lua_istable(L, argno)) { |
| 494 | const char *msg = lua_pushfstring(L, "table expected, got %s", luaL_typename(L, argno)); |
| 495 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 496 | } |
| 497 | lua_pushvalue(L, argno); |
| 498 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 499 | } |
| 500 | |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 501 | __LJMP const char *hlua_traceback(lua_State *L, const char* sep) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 502 | { |
| 503 | lua_Debug ar; |
| 504 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 505 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 506 | |
| 507 | while (lua_getstack(L, level++, &ar)) { |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 508 | /* Fill fields: |
| 509 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 510 | * 'l': fills in the field currentline; |
| 511 | * 'n': fills in the field name and namewhat; |
| 512 | * 't': fills in the field istailcall; |
| 513 | */ |
| 514 | lua_getinfo(L, "Slnt", &ar); |
| 515 | |
Willy Tarreau | 5c14340 | 2022-06-19 17:35:53 +0200 | [diff] [blame] | 516 | /* skip these empty entries, usually they come from deep C functions */ |
| 517 | if (ar.currentline < 0 && *ar.what == 'C' && !*ar.namewhat && !ar.name) |
| 518 | continue; |
| 519 | |
| 520 | /* Add separator */ |
| 521 | if (b_data(msg)) |
| 522 | chunk_appendf(msg, "%s", sep); |
| 523 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 524 | /* Append code localisation */ |
| 525 | if (ar.currentline > 0) |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 526 | chunk_appendf(msg, "%s:%d: ", ar.short_src, ar.currentline); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 527 | else |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 528 | chunk_appendf(msg, "%s: ", ar.short_src); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 529 | |
| 530 | /* |
| 531 | * Get function name |
| 532 | * |
| 533 | * if namewhat is no empty, name is defined. |
| 534 | * what contains "Lua" for Lua function, "C" for C function, |
| 535 | * or "main" for main code. |
| 536 | */ |
| 537 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 538 | chunk_appendf(msg, "in %s '%s'", ar.namewhat, ar.name); /* use it */ |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 539 | |
| 540 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 541 | chunk_appendf(msg, "in main chunk"); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 542 | |
| 543 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 544 | chunk_appendf(msg, "in function line %d", ar.linedefined); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 545 | |
| 546 | else /* nothing left... */ |
| 547 | chunk_appendf(msg, "?"); |
| 548 | |
| 549 | |
| 550 | /* Display tailed call */ |
| 551 | if (ar.istailcall) |
| 552 | chunk_appendf(msg, " ..."); |
| 553 | } |
| 554 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 555 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 559 | /* This function check the number of arguments available in the |
| 560 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 561 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 562 | */ |
| 563 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 564 | { |
| 565 | if (lua_gettop(L) == nb) |
| 566 | return; |
| 567 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 568 | } |
| 569 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 570 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 571 | * and the line number where the error is encountered. |
| 572 | */ |
| 573 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 574 | { |
| 575 | va_list argp; |
| 576 | va_start(argp, fmt); |
| 577 | luaL_where(L, 1); |
| 578 | lua_pushvfstring(L, fmt, argp); |
| 579 | va_end(argp); |
| 580 | lua_concat(L, 2); |
| 581 | return 1; |
| 582 | } |
| 583 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 584 | /* This functions is used with sample fetch and converters. It |
| 585 | * converts the HAProxy configuration argument in a lua stack |
| 586 | * values. |
| 587 | * |
| 588 | * It takes an array of "arg", and each entry of the array is |
| 589 | * converted and pushed in the LUA stack. |
| 590 | */ |
| 591 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 592 | { |
| 593 | switch (arg->type) { |
| 594 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | case ARGT_TIME: |
| 596 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 597 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 598 | break; |
| 599 | |
| 600 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 601 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 602 | break; |
| 603 | |
| 604 | case ARGT_IPV4: |
| 605 | case ARGT_IPV6: |
| 606 | case ARGT_MSK4: |
| 607 | case ARGT_MSK6: |
| 608 | case ARGT_FE: |
| 609 | case ARGT_BE: |
| 610 | case ARGT_TAB: |
| 611 | case ARGT_SRV: |
| 612 | case ARGT_USR: |
| 613 | case ARGT_MAP: |
| 614 | default: |
| 615 | lua_pushnil(L); |
| 616 | break; |
| 617 | } |
| 618 | return 1; |
| 619 | } |
| 620 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 621 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 622 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 623 | * with sample fetch wrappers. The input arguments are given to the |
| 624 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 625 | */ |
| 626 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 627 | { |
| 628 | switch (lua_type(L, ud)) { |
| 629 | |
| 630 | case LUA_TNUMBER: |
| 631 | case LUA_TBOOLEAN: |
| 632 | arg->type = ARGT_SINT; |
| 633 | arg->data.sint = lua_tointeger(L, ud); |
| 634 | break; |
| 635 | |
| 636 | case LUA_TSTRING: |
| 637 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 638 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 639 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 640 | arg->data.str.size = arg->data.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 641 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 642 | break; |
| 643 | |
| 644 | case LUA_TUSERDATA: |
| 645 | case LUA_TNIL: |
| 646 | case LUA_TTABLE: |
| 647 | case LUA_TFUNCTION: |
| 648 | case LUA_TTHREAD: |
| 649 | case LUA_TLIGHTUSERDATA: |
| 650 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 651 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | break; |
| 653 | } |
| 654 | return 1; |
| 655 | } |
| 656 | |
| 657 | /* the following functions are used to convert a struct sample |
| 658 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 659 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 660 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 661 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 663 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 664 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 665 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 666 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 667 | break; |
| 668 | |
| 669 | case SMP_T_BIN: |
| 670 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 671 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 675 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 676 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 677 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 678 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 679 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 680 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 681 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 682 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 683 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 684 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 685 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 686 | break; |
| 687 | default: |
| 688 | lua_pushnil(L); |
| 689 | break; |
| 690 | } |
| 691 | break; |
| 692 | |
| 693 | case SMP_T_IPV4: |
| 694 | case SMP_T_IPV6: |
| 695 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 696 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 697 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 698 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 699 | else |
| 700 | lua_pushnil(L); |
| 701 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 702 | default: |
| 703 | lua_pushnil(L); |
| 704 | break; |
| 705 | } |
| 706 | return 1; |
| 707 | } |
| 708 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 709 | /* the following functions are used to convert a struct sample |
| 710 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 711 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 712 | */ |
| 713 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 714 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 715 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 716 | |
| 717 | case SMP_T_BIN: |
| 718 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 719 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 720 | break; |
| 721 | |
| 722 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 723 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 724 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 725 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 726 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 727 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 728 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 729 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 730 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 731 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 732 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 733 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 734 | break; |
| 735 | default: |
| 736 | lua_pushstring(L, ""); |
| 737 | break; |
| 738 | } |
| 739 | break; |
| 740 | |
| 741 | case SMP_T_SINT: |
| 742 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 743 | case SMP_T_IPV4: |
| 744 | case SMP_T_IPV6: |
| 745 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 746 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 747 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 749 | else |
| 750 | lua_pushstring(L, ""); |
| 751 | break; |
| 752 | default: |
| 753 | lua_pushstring(L, ""); |
| 754 | break; |
| 755 | } |
| 756 | return 1; |
| 757 | } |
| 758 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 759 | /* the following functions are used to convert an Lua type in a |
| 760 | * struct sample. This is useful to provide data from a converter |
| 761 | * to the LUA code. |
| 762 | */ |
| 763 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 764 | { |
| 765 | switch (lua_type(L, ud)) { |
| 766 | |
| 767 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 768 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 769 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 770 | break; |
| 771 | |
| 772 | |
| 773 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 774 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 775 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 779 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 781 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 782 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 783 | smp->data.u.str.size = smp->data.u.str.data+1; /* count the terminating null byte */ |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 784 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 785 | break; |
| 786 | |
| 787 | case LUA_TUSERDATA: |
| 788 | case LUA_TNIL: |
| 789 | case LUA_TTABLE: |
| 790 | case LUA_TFUNCTION: |
| 791 | case LUA_TTHREAD: |
| 792 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 793 | case LUA_TNONE: |
| 794 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 795 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 796 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 797 | break; |
| 798 | } |
| 799 | return 1; |
| 800 | } |
| 801 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 802 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 803 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 804 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 805 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 806 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 807 | * entries and that there is at least one stop at the last position. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 808 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 809 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 810 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 811 | { |
| 812 | int min_arg; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 813 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 814 | struct proxy *px; |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 815 | struct userlist *ul; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 816 | struct my_regex *reg; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 817 | const char *msg = NULL; |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 818 | char *sname, *pname, *err = NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 819 | |
| 820 | idx = 0; |
| 821 | min_arg = ARGM(mask); |
| 822 | mask >>= ARGM_BITS; |
| 823 | |
| 824 | while (1) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 825 | struct buffer tmp = BUF_NULL; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 826 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 827 | /* Check for mandatory arguments. */ |
| 828 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 829 | if (idx < min_arg) { |
| 830 | |
| 831 | /* If miss other argument than the first one, we return an error. */ |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 832 | if (idx > 0) { |
| 833 | msg = "Mandatory argument expected"; |
| 834 | goto error; |
| 835 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 836 | |
| 837 | /* If first argument have a certain type, some default values |
| 838 | * may be used. See the function smp_resolve_args(). |
| 839 | */ |
| 840 | switch (mask & ARGT_MASK) { |
| 841 | |
| 842 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 843 | if (!(p->cap & PR_CAP_FE)) { |
| 844 | msg = "Mandatory argument expected"; |
| 845 | goto error; |
| 846 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 847 | argp[idx].data.prx = p; |
| 848 | argp[idx].type = ARGT_FE; |
| 849 | argp[idx+1].type = ARGT_STOP; |
| 850 | break; |
| 851 | |
| 852 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 853 | if (!(p->cap & PR_CAP_BE)) { |
| 854 | msg = "Mandatory argument expected"; |
| 855 | goto error; |
| 856 | } |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 857 | argp[idx].data.prx = p; |
| 858 | argp[idx].type = ARGT_BE; |
| 859 | argp[idx+1].type = ARGT_STOP; |
| 860 | break; |
| 861 | |
| 862 | case ARGT_TAB: |
Olivier Houchard | 14f6268 | 2022-09-13 00:35:53 +0200 | [diff] [blame] | 863 | if (!p->table) { |
| 864 | msg = "Mandatory argument expected"; |
| 865 | goto error; |
| 866 | } |
| 867 | argp[idx].data.t = p->table; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 868 | argp[idx].type = ARGT_TAB; |
| 869 | argp[idx+1].type = ARGT_STOP; |
| 870 | break; |
| 871 | |
| 872 | default: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 873 | msg = "Mandatory argument expected"; |
| 874 | goto error; |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 875 | break; |
| 876 | } |
| 877 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 878 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 879 | } |
| 880 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 881 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 882 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 883 | argp[idx].type != ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 884 | msg = "Last argument expected"; |
| 885 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 886 | } |
| 887 | |
| 888 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 889 | argp[idx].type == ARGT_STOP) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 890 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 891 | } |
| 892 | |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 893 | /* Convert some argument types. All string in argp[] are for not |
| 894 | * duplicated yet. |
| 895 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 896 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 897 | case ARGT_SINT: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 898 | if (argp[idx].type != ARGT_SINT) { |
| 899 | msg = "integer expected"; |
| 900 | goto error; |
| 901 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 902 | argp[idx].type = ARGT_SINT; |
| 903 | break; |
| 904 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 905 | case ARGT_TIME: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 906 | if (argp[idx].type != ARGT_SINT) { |
| 907 | msg = "integer expected"; |
| 908 | goto error; |
| 909 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 910 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 911 | break; |
| 912 | |
| 913 | case ARGT_SIZE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 914 | if (argp[idx].type != ARGT_SINT) { |
| 915 | msg = "integer expected"; |
| 916 | goto error; |
| 917 | } |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 918 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 919 | break; |
| 920 | |
| 921 | case ARGT_FE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 922 | if (argp[idx].type != ARGT_STR) { |
| 923 | msg = "string expected"; |
| 924 | goto error; |
| 925 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 926 | argp[idx].data.prx = proxy_fe_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 927 | if (!argp[idx].data.prx) { |
| 928 | msg = "frontend doesn't exist"; |
| 929 | goto error; |
| 930 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 931 | argp[idx].type = ARGT_FE; |
| 932 | break; |
| 933 | |
| 934 | case ARGT_BE: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 935 | if (argp[idx].type != ARGT_STR) { |
| 936 | msg = "string expected"; |
| 937 | goto error; |
| 938 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 939 | argp[idx].data.prx = proxy_be_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 940 | if (!argp[idx].data.prx) { |
| 941 | msg = "backend doesn't exist"; |
| 942 | goto error; |
| 943 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 944 | argp[idx].type = ARGT_BE; |
| 945 | break; |
| 946 | |
| 947 | case ARGT_TAB: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 948 | if (argp[idx].type != ARGT_STR) { |
| 949 | msg = "string expected"; |
| 950 | goto error; |
| 951 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 952 | argp[idx].data.t = stktable_find_by_name(argp[idx].data.str.area); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 953 | if (!argp[idx].data.t) { |
| 954 | msg = "table doesn't exist"; |
| 955 | goto error; |
| 956 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 957 | argp[idx].type = ARGT_TAB; |
| 958 | break; |
| 959 | |
| 960 | case ARGT_SRV: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 961 | if (argp[idx].type != ARGT_STR) { |
| 962 | msg = "string expected"; |
| 963 | goto error; |
| 964 | } |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 965 | sname = strrchr(argp[idx].data.str.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 966 | if (sname) { |
| 967 | *sname++ = '\0'; |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 968 | pname = argp[idx].data.str.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 969 | px = proxy_be_by_name(pname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 970 | if (!px) { |
| 971 | msg = "backend doesn't exist"; |
| 972 | goto error; |
| 973 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 974 | } |
| 975 | else { |
Christopher Faulet | fdea1b6 | 2020-08-06 08:29:18 +0200 | [diff] [blame] | 976 | sname = argp[idx].data.str.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 977 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 978 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 979 | argp[idx].data.srv = findserver(px, sname); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 980 | if (!argp[idx].data.srv) { |
| 981 | msg = "server doesn't exist"; |
| 982 | goto error; |
| 983 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 984 | argp[idx].type = ARGT_SRV; |
| 985 | break; |
| 986 | |
| 987 | case ARGT_IPV4: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 988 | if (argp[idx].type != ARGT_STR) { |
| 989 | msg = "string expected"; |
| 990 | goto error; |
| 991 | } |
| 992 | if (inet_pton(AF_INET, argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 993 | msg = "invalid IPv4 address"; |
| 994 | goto error; |
| 995 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 996 | argp[idx].type = ARGT_IPV4; |
| 997 | break; |
| 998 | |
| 999 | case ARGT_MSK4: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1000 | if (argp[idx].type == ARGT_SINT) |
| 1001 | len2mask4(argp[idx].data.sint, &argp[idx].data.ipv4); |
| 1002 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1003 | if (!str2mask(argp[idx].data.str.area, &argp[idx].data.ipv4)) { |
| 1004 | msg = "invalid IPv4 mask"; |
| 1005 | goto error; |
| 1006 | } |
| 1007 | } |
| 1008 | else { |
| 1009 | msg = "integer or string expected"; |
| 1010 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1011 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1012 | argp[idx].type = ARGT_MSK4; |
| 1013 | break; |
| 1014 | |
| 1015 | case ARGT_IPV6: |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1016 | if (argp[idx].type != ARGT_STR) { |
| 1017 | msg = "string expected"; |
| 1018 | goto error; |
| 1019 | } |
| 1020 | if (inet_pton(AF_INET6, argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1021 | msg = "invalid IPv6 address"; |
| 1022 | goto error; |
| 1023 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1024 | argp[idx].type = ARGT_IPV6; |
| 1025 | break; |
| 1026 | |
| 1027 | case ARGT_MSK6: |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1028 | if (argp[idx].type == ARGT_SINT) |
| 1029 | len2mask6(argp[idx].data.sint, &argp[idx].data.ipv6); |
| 1030 | else if (argp[idx].type == ARGT_STR) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1031 | if (!str2mask6(argp[idx].data.str.area, &argp[idx].data.ipv6)) { |
| 1032 | msg = "invalid IPv6 mask"; |
| 1033 | goto error; |
| 1034 | } |
| 1035 | } |
| 1036 | else { |
| 1037 | msg = "integer or string expected"; |
| 1038 | goto error; |
Christopher Faulet | e663a6e | 2020-08-07 09:11:22 +0200 | [diff] [blame] | 1039 | } |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 1040 | argp[idx].type = ARGT_MSK6; |
| 1041 | break; |
| 1042 | |
Christopher Faulet | fd2e906 | 2020-08-06 11:10:57 +0200 | [diff] [blame] | 1043 | case ARGT_REG: |
| 1044 | if (argp[idx].type != ARGT_STR) { |
| 1045 | msg = "string expected"; |
| 1046 | goto error; |
| 1047 | } |
| 1048 | reg = regex_comp(argp[idx].data.str.area, !(argp[idx].type_flags & ARGF_REG_ICASE), 1, &err); |
| 1049 | if (!reg) { |
| 1050 | msg = lua_pushfstring(L, "error compiling regex '%s' : '%s'", |
| 1051 | argp[idx].data.str.area, err); |
| 1052 | free(err); |
| 1053 | goto error; |
| 1054 | } |
| 1055 | argp[idx].type = ARGT_REG; |
| 1056 | argp[idx].data.reg = reg; |
| 1057 | break; |
| 1058 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 1059 | case ARGT_USR: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1060 | if (argp[idx].type != ARGT_STR) { |
| 1061 | msg = "string expected"; |
| 1062 | goto error; |
| 1063 | } |
| 1064 | if (p->uri_auth && p->uri_auth->userlist && |
Tim Duesterhus | e5ff141 | 2021-01-02 22:31:53 +0100 | [diff] [blame] | 1065 | strcmp(p->uri_auth->userlist->name, argp[idx].data.str.area) == 0) |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1066 | ul = p->uri_auth->userlist; |
| 1067 | else |
| 1068 | ul = auth_find_userlist(argp[idx].data.str.area); |
| 1069 | |
| 1070 | if (!ul) { |
| 1071 | msg = lua_pushfstring(L, "unable to find userlist '%s'", argp[idx].data.str.area); |
| 1072 | goto error; |
| 1073 | } |
| 1074 | argp[idx].type = ARGT_USR; |
| 1075 | argp[idx].data.usr = ul; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1076 | break; |
| 1077 | |
| 1078 | case ARGT_STR: |
| 1079 | if (!chunk_dup(&tmp, &argp[idx].data.str)) { |
| 1080 | msg = "unable to duplicate string arg"; |
| 1081 | goto error; |
| 1082 | } |
| 1083 | argp[idx].data.str = tmp; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1084 | break; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1085 | |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1086 | case ARGT_MAP: |
Christopher Faulet | d25d926 | 2020-08-06 11:04:46 +0200 | [diff] [blame] | 1087 | msg = "type not yet supported"; |
| 1088 | goto error; |
| 1089 | break; |
| 1090 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1091 | } |
| 1092 | |
| 1093 | /* Check for type of argument. */ |
| 1094 | if ((mask & ARGT_MASK) != argp[idx].type) { |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1095 | msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 1096 | arg_type_names[(mask & ARGT_MASK)], |
| 1097 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 1098 | goto error; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1099 | } |
| 1100 | |
| 1101 | /* Next argument. */ |
| 1102 | mask >>= ARGT_BITS; |
| 1103 | idx++; |
| 1104 | } |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1105 | return 0; |
| 1106 | |
| 1107 | error: |
Olivier Houchard | ca43161 | 2022-09-13 00:31:17 +0200 | [diff] [blame] | 1108 | argp[idx].type = ARGT_STOP; |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 1109 | free_args(argp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 1110 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 1111 | return 0; /* Never reached */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 1112 | } |
| 1113 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1114 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1115 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1116 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1117 | * |
| 1118 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1119 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 1120 | */ |
| 1121 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 1122 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1123 | struct hlua **hlua = lua_getextraspace(L); |
| 1124 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1125 | } |
| 1126 | static inline void hlua_sethlua(struct hlua *hlua) |
| 1127 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 1128 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 1129 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1130 | } |
| 1131 | |
Willy Tarreau | 0b7b639 | 2022-06-19 17:39:33 +0200 | [diff] [blame] | 1132 | /* Will return a non-NULL string indicating the Lua call trace if the caller |
| 1133 | * currently is executing from within a Lua function. One line per entry will |
| 1134 | * be emitted, and each extra line will be prefixed with <pfx>. If a current |
| 1135 | * Lua function is not detected, NULL is returned. |
| 1136 | */ |
| 1137 | const char *hlua_show_current_location(const char *pfx) |
| 1138 | { |
| 1139 | lua_State *L; |
| 1140 | lua_Debug ar; |
| 1141 | |
| 1142 | /* global or per-thread stack initializing ? */ |
| 1143 | if (hlua_state_id != -1 && (L = hlua_states[hlua_state_id]) && lua_getstack(L, 0, &ar)) |
| 1144 | return hlua_traceback(L, pfx); |
| 1145 | |
| 1146 | /* per-thread stack running ? */ |
| 1147 | if (hlua_states[tid + 1] && (L = hlua_states[tid + 1]) && lua_getstack(L, 0, &ar)) |
| 1148 | return hlua_traceback(L, pfx); |
| 1149 | |
| 1150 | /* global stack running ? */ |
| 1151 | if (hlua_states[0] && (L = hlua_states[0]) && lua_getstack(L, 0, &ar)) |
| 1152 | return hlua_traceback(L, pfx); |
| 1153 | |
| 1154 | return NULL; |
| 1155 | } |
| 1156 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1157 | /* This function is used to send logs. It try to send on screen (stderr) |
| 1158 | * and on the default syslog server. |
| 1159 | */ |
| 1160 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 1161 | { |
| 1162 | struct tm tm; |
| 1163 | char *p; |
| 1164 | |
| 1165 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1166 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1167 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1168 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 1169 | /* Break the message if exceed the buffer size. */ |
| 1170 | *(p-4) = ' '; |
| 1171 | *(p-3) = '.'; |
| 1172 | *(p-2) = '.'; |
| 1173 | *(p-1) = '.'; |
| 1174 | break; |
| 1175 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 1176 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1177 | *p = *msg; |
| 1178 | else |
| 1179 | *p = '.'; |
| 1180 | } |
| 1181 | *p = '\0'; |
| 1182 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1183 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1184 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Christopher Faulet | f98d821 | 2020-10-02 18:13:52 +0200 | [diff] [blame] | 1185 | if (level == LOG_DEBUG && !(global.mode & MODE_DEBUG)) |
| 1186 | return; |
| 1187 | |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 1188 | get_localtime(date.tv_sec, &tm); |
| 1189 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1190 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1191 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 1192 | fflush(stderr); |
| 1193 | } |
| 1194 | } |
| 1195 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1196 | /* This function just ensure that the yield will be always |
| 1197 | * returned with a timeout and permit to set some flags |
| 1198 | */ |
| 1199 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1200 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1201 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1202 | struct hlua *hlua; |
| 1203 | |
| 1204 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1205 | hlua = hlua_gethlua(L); |
| 1206 | if (!hlua) { |
| 1207 | return; |
| 1208 | } |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1209 | |
| 1210 | /* Set the wake timeout. If timeout is required, we set |
| 1211 | * the expiration time. |
| 1212 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1213 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1214 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 1215 | hlua->flags |= flags; |
| 1216 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1217 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1218 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1219 | } |
| 1220 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1221 | /* This function initialises the Lua environment stored in the stream. |
| 1222 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1224 | * |
| 1225 | * This function is particular. it initialises a new Lua thread. If the |
| 1226 | * initialisation fails (example: out of memory error), the lua function |
| 1227 | * throws an error (longjmp). |
| 1228 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1229 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1230 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1231 | * threads appear, the safe environment set a lock to ensure only one |
| 1232 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1233 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1234 | * |
| 1235 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1236 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1237 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1238 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 1239 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1240 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1241 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1242 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1243 | int hlua_ctx_init(struct hlua *lua, int state_id, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1244 | { |
| 1245 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1246 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1247 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1248 | lua->wake_time = TICK_ETERNITY; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1249 | lua->state_id = state_id; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1250 | LIST_INIT(&lua->com); |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1251 | LIST_INIT(&lua->hc_list); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1252 | if (!already_safe) { |
| 1253 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1254 | lua->Tref = LUA_REFNIL; |
| 1255 | return 0; |
| 1256 | } |
| 1257 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1258 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1259 | if (!lua->T) { |
| 1260 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1261 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1262 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1263 | return 0; |
| 1264 | } |
| 1265 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1266 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1267 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1268 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1269 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1270 | return 1; |
| 1271 | } |
| 1272 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1273 | /* kill all associated httpclient to this hlua task */ |
| 1274 | static void hlua_httpclient_destroy_all(struct hlua *hlua) |
| 1275 | { |
| 1276 | struct hlua_httpclient *hlua_hc, *back; |
| 1277 | |
| 1278 | list_for_each_entry_safe(hlua_hc, back, &hlua->hc_list, by_hlua) { |
| 1279 | if (hlua_hc->hc) |
| 1280 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 1281 | hlua_hc->hc = NULL; |
| 1282 | LIST_DELETE(&hlua_hc->by_hlua); |
| 1283 | } |
| 1284 | } |
| 1285 | |
| 1286 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1287 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1288 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1289 | * is not freed. |
| 1290 | */ |
| 1291 | void hlua_ctx_destroy(struct hlua *lua) |
| 1292 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1293 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1294 | return; |
| 1295 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1296 | if (!lua->T) |
| 1297 | goto end; |
| 1298 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 1299 | /* clean all running httpclient */ |
| 1300 | hlua_httpclient_destroy_all(lua); |
| 1301 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1302 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1303 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1304 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1305 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1306 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1307 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1308 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1309 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1310 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1311 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1312 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1313 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1314 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1315 | * without error, we run the GC on the thread pointer. Its freed all |
| 1316 | * the unused memory. |
| 1317 | * If the thread is finnish with an error or is currently yielded, |
| 1318 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1319 | * so e run the GC on the main thread. |
| 1320 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1321 | * the garbage collection. |
| 1322 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1323 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1324 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1325 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1326 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1327 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1328 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1329 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1330 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1331 | |
| 1332 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1333 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1334 | } |
| 1335 | |
| 1336 | /* This function is used to restore the Lua context when a coroutine |
| 1337 | * fails. This function copy the common memory between old coroutine |
| 1338 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1339 | * replaced by the new coroutine. |
| 1340 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1341 | * as string error message and it is copied in the new stack. |
| 1342 | */ |
| 1343 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1344 | { |
| 1345 | lua_State *T; |
| 1346 | int new_ref; |
| 1347 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1348 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1349 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1350 | if (!T) |
| 1351 | return 0; |
| 1352 | |
| 1353 | /* Copy last error message. */ |
| 1354 | if (keep_msg) |
| 1355 | lua_xmove(lua->T, T, 1); |
| 1356 | |
| 1357 | /* Copy data between the coroutines. */ |
| 1358 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1359 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1360 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1361 | |
| 1362 | /* Destroy old data. */ |
| 1363 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1364 | |
| 1365 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1366 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1367 | |
| 1368 | /* Fill the struct with the new coroutine values. */ |
| 1369 | lua->Mref = new_ref; |
| 1370 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1371 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1372 | |
| 1373 | /* Set context. */ |
| 1374 | hlua_sethlua(lua); |
| 1375 | |
| 1376 | return 1; |
| 1377 | } |
| 1378 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1379 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1380 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1381 | struct hlua *hlua; |
| 1382 | |
| 1383 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1384 | hlua = hlua_gethlua(L); |
| 1385 | if (!hlua) |
| 1386 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1387 | |
| 1388 | /* Lua cannot yield when its returning from a function, |
| 1389 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1390 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1391 | */ |
| 1392 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1393 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1394 | return; |
| 1395 | } |
| 1396 | |
| 1397 | /* restore the interrupt condition. */ |
| 1398 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1399 | |
| 1400 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1401 | * If the state is not yieldable, trying yield causes an error. |
| 1402 | */ |
| 1403 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1404 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1405 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1406 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1407 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1408 | hlua->run_time += now_ms - hlua->start_time; |
| 1409 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1410 | lua_pushfstring(L, "execution timeout"); |
| 1411 | WILL_LJMP(lua_error(L)); |
| 1412 | } |
| 1413 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1414 | /* Update the start time. */ |
| 1415 | hlua->start_time = now_ms; |
| 1416 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1417 | /* Try to interrupt the process at the end of the current |
| 1418 | * unyieldable function. |
| 1419 | */ |
| 1420 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1421 | } |
| 1422 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1423 | /* This function start or resumes the Lua stack execution. If the flag |
| 1424 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1425 | * The function return an error. |
| 1426 | * |
| 1427 | * The function can returns 4 values: |
| 1428 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1429 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1430 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1431 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1432 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1433 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1434 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1435 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1436 | * LUA code. |
| 1437 | */ |
| 1438 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1439 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1440 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1441 | int nres; |
| 1442 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1443 | int ret; |
| 1444 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1445 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1446 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1447 | /* Initialise run time counter. */ |
| 1448 | if (!HLUA_IS_RUNNING(lua)) |
| 1449 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1450 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1451 | /* Lock the whole Lua execution. This lock must be before the |
| 1452 | * label "resume_execution". |
| 1453 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1454 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1455 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1456 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1457 | resume_execution: |
| 1458 | |
| 1459 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1460 | * instructions. it is used for preventing infinite loops. |
| 1461 | */ |
| 1462 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1463 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1464 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1465 | HLUA_SET_RUN(lua); |
| 1466 | HLUA_CLR_CTRLYIELD(lua); |
| 1467 | HLUA_CLR_WAKERESWR(lua); |
| 1468 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1469 | HLUA_CLR_NOYIELD(lua); |
| 1470 | if (!yield_allowed) |
| 1471 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1472 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1473 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1474 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1475 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1476 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1477 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1478 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1479 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs, &nres); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1480 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1481 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1482 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1483 | switch (ret) { |
| 1484 | |
| 1485 | case LUA_OK: |
| 1486 | ret = HLUA_E_OK; |
| 1487 | break; |
| 1488 | |
| 1489 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1490 | /* Check if the execution timeout is expired. It it is the case, we |
| 1491 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1492 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1493 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1494 | lua->run_time += now_ms - lua->start_time; |
| 1495 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1496 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1497 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1498 | break; |
| 1499 | } |
| 1500 | /* Process the forced yield. if the general yield is not allowed or |
| 1501 | * if no task were associated this the current Lua execution |
| 1502 | * coroutine, we resume the execution. Else we want to return in the |
| 1503 | * scheduler and we want to be waked up again, to continue the |
| 1504 | * current Lua execution. So we schedule our own task. |
| 1505 | */ |
| 1506 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1507 | if (!yield_allowed || !lua->task) |
| 1508 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1509 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1510 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1511 | if (!yield_allowed) { |
| 1512 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1513 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | break; |
| 1515 | } |
| 1516 | ret = HLUA_E_AGAIN; |
| 1517 | break; |
| 1518 | |
| 1519 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1520 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1521 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1522 | * because the errors ares the only one mean to return immediately |
| 1523 | * from and lua execution. |
| 1524 | */ |
| 1525 | if (lua->flags & HLUA_EXIT) { |
| 1526 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1527 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1528 | break; |
| 1529 | } |
| 1530 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1531 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1532 | if (!lua_checkstack(lua->T, 1)) { |
| 1533 | ret = HLUA_E_ERR; |
| 1534 | break; |
| 1535 | } |
| 1536 | msg = lua_tostring(lua->T, -1); |
| 1537 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1538 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1539 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1540 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1541 | lua_pushfstring(lua->T, "[state-id %d] runtime error: %s from %s", lua->state_id, msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1542 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1543 | lua_pushfstring(lua->T, "[state-id %d] unknown runtime error from %s", lua->state_id, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1544 | ret = HLUA_E_ERRMSG; |
| 1545 | break; |
| 1546 | |
| 1547 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1548 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1549 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1550 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1551 | break; |
| 1552 | |
| 1553 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1554 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1555 | if (!lua_checkstack(lua->T, 1)) { |
| 1556 | ret = HLUA_E_ERR; |
| 1557 | break; |
| 1558 | } |
| 1559 | msg = lua_tostring(lua->T, -1); |
| 1560 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1561 | lua_pop(lua->T, 1); |
| 1562 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1563 | lua_pushfstring(lua->T, "[state-id %d] message handler error: %s", lua->state_id, msg); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1564 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1565 | lua_pushfstring(lua->T, "[state-id %d] message handler error", lua->state_id); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1566 | ret = HLUA_E_ERRMSG; |
| 1567 | break; |
| 1568 | |
| 1569 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1570 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1571 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1572 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | switch (ret) { |
| 1577 | case HLUA_E_AGAIN: |
| 1578 | break; |
| 1579 | |
| 1580 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1581 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1582 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1583 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1584 | break; |
| 1585 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1586 | case HLUA_E_ETMOUT: |
| 1587 | case HLUA_E_NOMEM: |
| 1588 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1589 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1590 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1591 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1592 | hlua_ctx_renew(lua, 0); |
| 1593 | break; |
| 1594 | |
| 1595 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1596 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1597 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1598 | break; |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1601 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1602 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1603 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1604 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1605 | return ret; |
| 1606 | } |
| 1607 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1608 | /* This function exit the current code. */ |
| 1609 | __LJMP static int hlua_done(lua_State *L) |
| 1610 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1611 | struct hlua *hlua; |
| 1612 | |
| 1613 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1614 | hlua = hlua_gethlua(L); |
| 1615 | if (!hlua) |
| 1616 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1617 | |
| 1618 | hlua->flags |= HLUA_EXIT; |
| 1619 | WILL_LJMP(lua_error(L)); |
| 1620 | |
| 1621 | return 0; |
| 1622 | } |
| 1623 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1624 | /* This function is an LUA binding. It provides a function |
| 1625 | * for deleting ACL from a referenced ACL file. |
| 1626 | */ |
| 1627 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1628 | { |
| 1629 | const char *name; |
| 1630 | const char *key; |
| 1631 | struct pat_ref *ref; |
| 1632 | |
| 1633 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1634 | |
| 1635 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1636 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1637 | |
| 1638 | ref = pat_ref_lookup(name); |
| 1639 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1640 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1641 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1642 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1643 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1644 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1645 | return 0; |
| 1646 | } |
| 1647 | |
| 1648 | /* This function is an LUA binding. It provides a function |
| 1649 | * for deleting map entry from a referenced map file. |
| 1650 | */ |
| 1651 | static int hlua_del_map(lua_State *L) |
| 1652 | { |
| 1653 | const char *name; |
| 1654 | const char *key; |
| 1655 | struct pat_ref *ref; |
| 1656 | |
| 1657 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1658 | |
| 1659 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1660 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1661 | |
| 1662 | ref = pat_ref_lookup(name); |
| 1663 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1664 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1665 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1666 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1667 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1668 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1669 | return 0; |
| 1670 | } |
| 1671 | |
| 1672 | /* This function is an LUA binding. It provides a function |
| 1673 | * for adding ACL pattern from a referenced ACL file. |
| 1674 | */ |
| 1675 | static int hlua_add_acl(lua_State *L) |
| 1676 | { |
| 1677 | const char *name; |
| 1678 | const char *key; |
| 1679 | struct pat_ref *ref; |
| 1680 | |
| 1681 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1682 | |
| 1683 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1684 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1685 | |
| 1686 | ref = pat_ref_lookup(name); |
| 1687 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1688 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1689 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1690 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1691 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1692 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1693 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1694 | return 0; |
| 1695 | } |
| 1696 | |
| 1697 | /* This function is an LUA binding. It provides a function |
| 1698 | * for setting map pattern and sample from a referenced map |
| 1699 | * file. |
| 1700 | */ |
| 1701 | static int hlua_set_map(lua_State *L) |
| 1702 | { |
| 1703 | const char *name; |
| 1704 | const char *key; |
| 1705 | const char *value; |
| 1706 | struct pat_ref *ref; |
| 1707 | |
| 1708 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1709 | |
| 1710 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1711 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1712 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1713 | |
| 1714 | ref = pat_ref_lookup(name); |
| 1715 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1716 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1717 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1718 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1719 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1720 | pat_ref_set(ref, key, value, NULL); |
| 1721 | else |
| 1722 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1723 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1727 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1728 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1729 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1730 | * the name of the object (_G[<name>] = <metable> ). |
| 1731 | * |
| 1732 | * A metable is a table that modify the standard behavior of a standard |
| 1733 | * access to the associated data. The entries of this new metatable are |
| 1734 | * defined as is: |
| 1735 | * |
| 1736 | * http://lua-users.org/wiki/MetatableEvents |
| 1737 | * |
| 1738 | * __index |
| 1739 | * |
| 1740 | * we access an absent field in a table, the result is nil. This is |
| 1741 | * true, but it is not the whole truth. Actually, such access triggers |
| 1742 | * the interpreter to look for an __index metamethod: If there is no |
| 1743 | * such method, as usually happens, then the access results in nil; |
| 1744 | * otherwise, the metamethod will provide the result. |
| 1745 | * |
| 1746 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1747 | * the key does not appear in the table, but the metatable has an __index |
| 1748 | * property: |
| 1749 | * |
| 1750 | * - if the value is a function, the function is called, passing in the |
| 1751 | * table and the key; the return value of that function is returned as |
| 1752 | * the result. |
| 1753 | * |
| 1754 | * - if the value is another table, the value of the key in that table is |
| 1755 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1756 | * table's metatable has an __index property, then it continues on up) |
| 1757 | * |
| 1758 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1759 | * |
| 1760 | * http://www.lua.org/pil/13.4.1.html |
| 1761 | * |
| 1762 | * __newindex |
| 1763 | * |
| 1764 | * Like __index, but control property assignment. |
| 1765 | * |
| 1766 | * __mode - Control weak references. A string value with one or both |
| 1767 | * of the characters 'k' and 'v' which specifies that the the |
| 1768 | * keys and/or values in the table are weak references. |
| 1769 | * |
| 1770 | * __call - Treat a table like a function. When a table is followed by |
| 1771 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1772 | * a __call key pointing to a function, that function is invoked |
| 1773 | * (passing any specified arguments) and the return value is |
| 1774 | * returned. |
| 1775 | * |
| 1776 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1777 | * called, if the metatable for myTable has a __metatable |
| 1778 | * key, the value of that key is returned instead of the |
| 1779 | * actual metatable. |
| 1780 | * |
| 1781 | * __tostring - Control string representation. When the builtin |
| 1782 | * "tostring( myTable )" function is called, if the metatable |
| 1783 | * for myTable has a __tostring property set to a function, |
| 1784 | * that function is invoked (passing myTable to it) and the |
| 1785 | * return value is used as the string representation. |
| 1786 | * |
| 1787 | * __len - Control table length. When the table length is requested using |
| 1788 | * the length operator ( '#' ), if the metatable for myTable has |
| 1789 | * a __len key pointing to a function, that function is invoked |
| 1790 | * (passing myTable to it) and the return value used as the value |
| 1791 | * of "#myTable". |
| 1792 | * |
| 1793 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1794 | * collected, if the metatable has a __gc field pointing to a |
| 1795 | * function, that function is first invoked, passing the userdata |
| 1796 | * to it. The __gc metamethod is not called for tables. |
| 1797 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1798 | * |
| 1799 | * Special metamethods for redefining standard operators: |
| 1800 | * http://www.lua.org/pil/13.1.html |
| 1801 | * |
| 1802 | * __add "+" |
| 1803 | * __sub "-" |
| 1804 | * __mul "*" |
| 1805 | * __div "/" |
| 1806 | * __unm "!" |
| 1807 | * __pow "^" |
| 1808 | * __concat ".." |
| 1809 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1810 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1811 | * http://www.lua.org/pil/13.2.html |
| 1812 | * |
| 1813 | * __eq "==" |
| 1814 | * __lt "<" |
| 1815 | * __le "<=" |
| 1816 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | |
| 1818 | /* |
| 1819 | * |
| 1820 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1821 | * Class Map |
| 1822 | * |
| 1823 | * |
| 1824 | */ |
| 1825 | |
| 1826 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1827 | * a class session, otherwise it throws an error. |
| 1828 | */ |
| 1829 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1830 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1831 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | /* This function is the map constructor. It don't need |
| 1835 | * the class Map object. It creates and return a new Map |
| 1836 | * object. It must be called only during "body" or "init" |
| 1837 | * context because it process some filesystem accesses. |
| 1838 | */ |
| 1839 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1840 | { |
| 1841 | const char *fn; |
| 1842 | int match = PAT_MATCH_STR; |
| 1843 | struct sample_conv conv; |
| 1844 | const char *file = ""; |
| 1845 | int line = 0; |
| 1846 | lua_Debug ar; |
| 1847 | char *err = NULL; |
| 1848 | struct arg args[2]; |
| 1849 | |
| 1850 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1851 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1852 | |
| 1853 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1854 | |
| 1855 | if (lua_gettop(L) >= 2) { |
| 1856 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1857 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1858 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1859 | } |
| 1860 | |
| 1861 | /* Get Lua filename and line number. */ |
| 1862 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1863 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1864 | if (ar.currentline > 0) { /* is there info? */ |
| 1865 | file = ar.short_src; |
| 1866 | line = ar.currentline; |
| 1867 | } |
| 1868 | } |
| 1869 | |
| 1870 | /* fill fake sample_conv struct. */ |
| 1871 | conv.kw = ""; /* unused. */ |
| 1872 | conv.process = NULL; /* unused. */ |
| 1873 | conv.arg_mask = 0; /* unused. */ |
| 1874 | conv.val_args = NULL; /* unused. */ |
| 1875 | conv.out_type = SMP_T_STR; |
| 1876 | conv.private = (void *)(long)match; |
| 1877 | switch (match) { |
| 1878 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1879 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1880 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1881 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1882 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1883 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1884 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1885 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1886 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1887 | default: |
| 1888 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1889 | } |
| 1890 | |
| 1891 | /* fill fake args. */ |
| 1892 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1893 | args[0].data.str.area = strdup(fn); |
| 1894 | args[0].data.str.data = strlen(fn); |
| 1895 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1896 | args[1].type = ARGT_STOP; |
| 1897 | |
| 1898 | /* load the map. */ |
| 1899 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1900 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1901 | * free the err variable. |
| 1902 | */ |
| 1903 | luaL_where(L, 1); |
| 1904 | lua_pushfstring(L, "'new': %s.", err); |
| 1905 | lua_concat(L, 2); |
| 1906 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1907 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1908 | WILL_LJMP(lua_error(L)); |
| 1909 | } |
| 1910 | |
| 1911 | /* create the lua object. */ |
| 1912 | lua_newtable(L); |
| 1913 | lua_pushlightuserdata(L, args[0].data.map); |
| 1914 | lua_rawseti(L, -2, 0); |
| 1915 | |
| 1916 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1917 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1918 | lua_setmetatable(L, -2); |
| 1919 | |
| 1920 | |
| 1921 | return 1; |
| 1922 | } |
| 1923 | |
| 1924 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1925 | { |
| 1926 | struct map_descriptor *desc; |
| 1927 | struct pattern *pat; |
| 1928 | struct sample smp; |
| 1929 | |
| 1930 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1931 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1932 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1933 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1934 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1935 | } |
| 1936 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1937 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1938 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1939 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry Fournier | 91dc0c0 | 2020-11-10 20:38:20 +0100 | [diff] [blame] | 1940 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1941 | } |
| 1942 | |
| 1943 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1944 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1945 | if (str) |
| 1946 | lua_pushstring(L, ""); |
| 1947 | else |
| 1948 | lua_pushnil(L); |
| 1949 | return 1; |
| 1950 | } |
| 1951 | |
| 1952 | /* The Lua pattern must return a string, so we can't check the returned type */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1953 | lua_pushlstring(L, pat->data->u.str.area, pat->data->u.str.data); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1954 | return 1; |
| 1955 | } |
| 1956 | |
| 1957 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1958 | { |
| 1959 | return _hlua_map_lookup(L, 0); |
| 1960 | } |
| 1961 | |
| 1962 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1963 | { |
| 1964 | return _hlua_map_lookup(L, 1); |
| 1965 | } |
| 1966 | |
| 1967 | /* |
| 1968 | * |
| 1969 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | * Class Socket |
| 1971 | * |
| 1972 | * |
| 1973 | */ |
| 1974 | |
| 1975 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1976 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1977 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /* This function is the handler called for each I/O on the established |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1981 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | * received. |
| 1983 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1984 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1986 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1987 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1989 | if (ctx->die) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1990 | sc_shutw(sc); |
| 1991 | sc_shutr(sc); |
| 1992 | sc_ic(sc)->flags |= CF_READ_NULL; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1993 | notification_wake(&ctx->wake_on_read); |
| 1994 | notification_wake(&ctx->wake_on_write); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1995 | stream_shutdown(__sc_strm(sc), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1996 | } |
| 1997 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1998 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1999 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2000 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2001 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 2002 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2003 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2004 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 2005 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2006 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2007 | * to be notified whenever the connection completes. |
| 2008 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2009 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2010 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 2011 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2012 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 2013 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 2014 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | |
| 2016 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2017 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2019 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2020 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2021 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2022 | |
| 2023 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2024 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2025 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2026 | |
| 2027 | /* Some data were injected in the buffer, notify the stream |
| 2028 | * interface. |
| 2029 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2030 | if (!channel_is_empty(sc_ic(sc))) |
| 2031 | sc_update(sc); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2032 | |
| 2033 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2034 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2035 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2036 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2037 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2040 | static int hlua_socket_init(struct appctx *appctx) |
| 2041 | { |
| 2042 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2043 | struct stream *s; |
| 2044 | |
| 2045 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2046 | goto error; |
| 2047 | |
| 2048 | s = appctx_strm(appctx); |
| 2049 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2050 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2051 | * and retrieve data from the server. The connection is initialized |
| 2052 | * with the "struct server". |
| 2053 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2054 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2055 | |
| 2056 | /* Force destination server. */ |
| 2057 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2058 | s->target = &socket_tcp->obj_type; |
| 2059 | |
| 2060 | ctx->appctx = appctx; |
| 2061 | return 0; |
| 2062 | |
| 2063 | error: |
| 2064 | return -1; |
| 2065 | } |
| 2066 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2067 | /* This function is called when the "struct stream" is destroyed. |
| 2068 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | * Wake all the pending signals. |
| 2070 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2071 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2073 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2074 | struct xref *peer; |
| 2075 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2076 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2077 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2079 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2082 | notification_wake(&ctx->wake_on_read); |
| 2083 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | } |
| 2085 | |
| 2086 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2087 | * uses this object. If the stream does not exists, just quit. |
| 2088 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2089 | * pending signal can rest in the read and write lists. destroy |
| 2090 | * it. |
| 2091 | */ |
| 2092 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2093 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2094 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2095 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2096 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2097 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2098 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2099 | |
| 2100 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2101 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2102 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2103 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2104 | |
| 2105 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2106 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2107 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2108 | ctx->die = 1; |
| 2109 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2110 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2111 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2112 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2113 | return 0; |
| 2114 | } |
| 2115 | |
| 2116 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2117 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2118 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2119 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2120 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2121 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2122 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2123 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2124 | struct hlua *hlua; |
| 2125 | |
| 2126 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2127 | hlua = hlua_gethlua(L); |
| 2128 | if (!hlua) |
| 2129 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2130 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2131 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2132 | |
| 2133 | /* Check if we run on the same thread than the xreator thread. |
| 2134 | * We cannot access to the socket if the thread is different. |
| 2135 | */ |
| 2136 | if (socket->tid != tid) |
| 2137 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2138 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2139 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2140 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2141 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2142 | |
| 2143 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2144 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2146 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2147 | ctx->die = 1; |
| 2148 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2149 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2150 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2151 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2152 | return 0; |
| 2153 | } |
| 2154 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2155 | /* The close function calls close_helper. |
| 2156 | */ |
| 2157 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2158 | { |
| 2159 | MAY_LJMP(check_args(L, 1, "close")); |
| 2160 | return hlua_socket_close_helper(L); |
| 2161 | } |
| 2162 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2163 | /* This Lua function assumes that the stack contain three parameters. |
| 2164 | * 1 - USERDATA containing a struct socket |
| 2165 | * 2 - INTEGER with values of the macro defined below |
| 2166 | * If the integer is -1, we must read at most one line. |
| 2167 | * If the integer is -2, we ust read all the data until the |
| 2168 | * end of the stream. |
| 2169 | * If the integer is positive value, we must read a number of |
| 2170 | * bytes corresponding to this value. |
| 2171 | */ |
| 2172 | #define HLSR_READ_LINE (-1) |
| 2173 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2174 | __LJMP static int hlua_socket_receive_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | { |
| 2176 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2177 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2178 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2179 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2180 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2181 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2182 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2183 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2184 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2185 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2186 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2187 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2188 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | struct stream *s; |
| 2190 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2191 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2192 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2193 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2194 | hlua = hlua_gethlua(L); |
| 2195 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2196 | /* Check if this lua stack is schedulable. */ |
| 2197 | if (!hlua || !hlua->task) |
| 2198 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2199 | "'frontend', 'backend' or 'task'")); |
| 2200 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2201 | /* Check if we run on the same thread than the xreator thread. |
| 2202 | * We cannot access to the socket if the thread is different. |
| 2203 | */ |
| 2204 | if (socket->tid != tid) |
| 2205 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2206 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2207 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2208 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2209 | if (!peer) |
| 2210 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2211 | |
| 2212 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2213 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2214 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2215 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2216 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2217 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2218 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2219 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2220 | if (nblk < 0) /* Connection close. */ |
| 2221 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2222 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2223 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2224 | |
| 2225 | /* remove final \r\n. */ |
| 2226 | if (nblk == 1) { |
| 2227 | if (blk1[len1-1] == '\n') { |
| 2228 | len1--; |
| 2229 | skip_at_end++; |
| 2230 | if (blk1[len1-1] == '\r') { |
| 2231 | len1--; |
| 2232 | skip_at_end++; |
| 2233 | } |
| 2234 | } |
| 2235 | } |
| 2236 | else { |
| 2237 | if (blk2[len2-1] == '\n') { |
| 2238 | len2--; |
| 2239 | skip_at_end++; |
| 2240 | if (blk2[len2-1] == '\r') { |
| 2241 | len2--; |
| 2242 | skip_at_end++; |
| 2243 | } |
| 2244 | } |
| 2245 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2246 | } |
| 2247 | |
| 2248 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2250 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2251 | if (nblk < 0) /* Connection close. */ |
| 2252 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2253 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | goto connection_empty; |
| 2255 | } |
| 2256 | |
| 2257 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2258 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2259 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | if (nblk < 0) /* Connection close. */ |
| 2261 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2262 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2263 | goto connection_empty; |
| 2264 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2265 | missing_bytes = wanted - socket->b.n; |
| 2266 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2268 | len1 = missing_bytes; |
| 2269 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2270 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | } |
| 2272 | |
| 2273 | len = len1; |
| 2274 | |
| 2275 | luaL_addlstring(&socket->b, blk1, len1); |
| 2276 | if (nblk == 2) { |
| 2277 | len += len2; |
| 2278 | luaL_addlstring(&socket->b, blk2, len2); |
| 2279 | } |
| 2280 | |
| 2281 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2282 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | |
| 2284 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2285 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
| 2287 | /* If the pattern reclaim to read all the data |
| 2288 | * in the connection, got out. |
| 2289 | */ |
| 2290 | if (wanted == HLSR_READ_ALL) |
| 2291 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2292 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | goto connection_empty; |
| 2294 | |
| 2295 | /* Return result. */ |
| 2296 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2297 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | return 1; |
| 2299 | |
| 2300 | connection_closed: |
| 2301 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | xref_unlock(&socket->xref, peer); |
| 2303 | |
| 2304 | no_peer: |
| 2305 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | /* If the buffer containds data. */ |
| 2307 | if (socket->b.n > 0) { |
| 2308 | luaL_pushresult(&socket->b); |
| 2309 | return 1; |
| 2310 | } |
| 2311 | lua_pushnil(L); |
| 2312 | lua_pushstring(L, "connection closed."); |
| 2313 | return 2; |
| 2314 | |
| 2315 | connection_empty: |
| 2316 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2317 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2318 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2320 | } |
| 2321 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2322 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_receive_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | return 0; |
| 2324 | } |
| 2325 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2326 | /* This Lua function gets two parameters. The first one can be string |
| 2327 | * or a number. If the string is "*l", the user requires one line. If |
| 2328 | * the string is "*a", the user requires all the contents of the stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2329 | * If the value is a number, the user require a number of bytes equal |
| 2330 | * to the value. The default value is "*l" (a line). |
| 2331 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2332 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | * integer takes this values: |
| 2334 | * -1 : read a line |
| 2335 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2336 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2337 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2338 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2339 | * concatenated with the read data. |
| 2340 | */ |
| 2341 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2342 | { |
| 2343 | int wanted = HLSR_READ_LINE; |
| 2344 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2345 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2346 | char *error; |
| 2347 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2348 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2349 | |
| 2350 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2351 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2352 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2353 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | /* Check if we run on the same thread than the xreator thread. |
| 2356 | * We cannot access to the socket if the thread is different. |
| 2357 | */ |
| 2358 | if (socket->tid != tid) |
| 2359 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2360 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | /* check for pattern. */ |
| 2362 | if (lua_gettop(L) >= 2) { |
| 2363 | type = lua_type(L, 2); |
| 2364 | if (type == LUA_TSTRING) { |
| 2365 | pattern = lua_tostring(L, 2); |
| 2366 | if (strcmp(pattern, "*a") == 0) |
| 2367 | wanted = HLSR_READ_ALL; |
| 2368 | else if (strcmp(pattern, "*l") == 0) |
| 2369 | wanted = HLSR_READ_LINE; |
| 2370 | else { |
| 2371 | wanted = strtoll(pattern, &error, 10); |
| 2372 | if (*error != '\0') |
| 2373 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2374 | } |
| 2375 | } |
| 2376 | else if (type == LUA_TNUMBER) { |
| 2377 | wanted = lua_tointeger(L, 2); |
| 2378 | if (wanted < 0) |
| 2379 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2380 | } |
| 2381 | } |
| 2382 | |
| 2383 | /* Set pattern. */ |
| 2384 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2385 | |
| 2386 | /* Check if we would replace the top by itself. */ |
| 2387 | if (lua_gettop(L) != 2) |
| 2388 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2390 | /* Save index of the top of the stack because since buffers are used, it |
| 2391 | * may change |
| 2392 | */ |
| 2393 | lastarg = lua_gettop(L); |
| 2394 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2395 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | luaL_buffinit(L, &socket->b); |
| 2397 | |
| 2398 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2399 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2401 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2402 | pattern = lua_tolstring(L, 3, &len); |
| 2403 | luaL_addlstring(&socket->b, pattern, len); |
| 2404 | } |
| 2405 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2406 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2407 | } |
| 2408 | |
| 2409 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2410 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2411 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2412 | static int hlua_socket_write_yield(struct lua_State *L,int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2413 | { |
| 2414 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2415 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2416 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | struct appctx *appctx; |
| 2418 | size_t buf_len; |
| 2419 | const char *buf; |
| 2420 | int len; |
| 2421 | int send_len; |
| 2422 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2425 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2426 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2427 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2428 | hlua = hlua_gethlua(L); |
| 2429 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2430 | /* Check if this lua stack is schedulable. */ |
| 2431 | if (!hlua || !hlua->task) |
| 2432 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2433 | "'frontend', 'backend' or 'task'")); |
| 2434 | |
| 2435 | /* Get object */ |
| 2436 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2437 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2438 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2440 | /* Check if we run on the same thread than the xreator thread. |
| 2441 | * We cannot access to the socket if the thread is different. |
| 2442 | */ |
| 2443 | if (socket->tid != tid) |
| 2444 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2445 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2446 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2447 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2448 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2449 | lua_pushinteger(L, -1); |
| 2450 | return 1; |
| 2451 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2452 | |
| 2453 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2454 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2455 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2456 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2457 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2459 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2460 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2461 | lua_pushinteger(L, -1); |
| 2462 | return 1; |
| 2463 | } |
| 2464 | |
| 2465 | /* Update the input buffer data. */ |
| 2466 | buf += sent; |
| 2467 | send_len = buf_len - sent; |
| 2468 | |
| 2469 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | if (sent >= buf_len) { |
| 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2472 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2474 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2475 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2476 | * the request buffer if its not required. |
| 2477 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2478 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2479 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2480 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2481 | } |
| 2482 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2483 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2484 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2485 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2487 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2488 | |
| 2489 | /* send data */ |
| 2490 | if (len < send_len) |
| 2491 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2492 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2493 | |
| 2494 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2495 | * the data" (-2) are not expected because the available length |
| 2496 | * is tested. |
| 2497 | * Other unknown error are also not expected. |
| 2498 | */ |
| 2499 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2500 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2501 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2502 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2503 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2504 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2505 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | return 1; |
| 2508 | } |
| 2509 | |
| 2510 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2511 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2512 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2513 | s->req.rex = TICK_ETERNITY; |
| 2514 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | |
| 2516 | /* Update length sent. */ |
| 2517 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2518 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
| 2520 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | if (sent + len >= buf_len) { |
| 2522 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2523 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2524 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | |
| 2526 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2527 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2528 | xref_unlock(&socket->xref, peer); |
| 2529 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2530 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2531 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2532 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_write_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | return 0; |
| 2534 | } |
| 2535 | |
| 2536 | /* This function initiate the send of data. It just check the input |
| 2537 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2538 | * amount of data written to the buffer. This is used by the function |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2539 | * "hlua_socket_write_yield" that can yield. |
| 2540 | * |
| 2541 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2542 | * the associated object. The second is a string buffer. The third is |
| 2543 | * a facultative integer that represents where is the buffer position |
| 2544 | * of the start of the data that can send. The first byte is the |
| 2545 | * position "1". The default value is "1". The fourth argument is a |
| 2546 | * facultative integer that represents where is the buffer position |
| 2547 | * of the end of the data that can send. The default is the last byte. |
| 2548 | */ |
| 2549 | static int hlua_socket_send(struct lua_State *L) |
| 2550 | { |
| 2551 | int i; |
| 2552 | int j; |
| 2553 | const char *buf; |
| 2554 | size_t buf_len; |
| 2555 | |
| 2556 | /* Check number of arguments. */ |
| 2557 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2558 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2559 | |
| 2560 | /* Get the string. */ |
| 2561 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2562 | |
| 2563 | /* Get and check j. */ |
| 2564 | if (lua_gettop(L) == 4) { |
| 2565 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2566 | if (j < 0) |
| 2567 | j = buf_len + j + 1; |
| 2568 | if (j > buf_len) |
| 2569 | j = buf_len + 1; |
| 2570 | lua_pop(L, 1); |
| 2571 | } |
| 2572 | else |
| 2573 | j = buf_len; |
| 2574 | |
| 2575 | /* Get and check i. */ |
| 2576 | if (lua_gettop(L) == 3) { |
| 2577 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2578 | if (i < 0) |
| 2579 | i = buf_len + i + 1; |
| 2580 | if (i > buf_len) |
| 2581 | i = buf_len + 1; |
| 2582 | lua_pop(L, 1); |
| 2583 | } else |
| 2584 | i = 1; |
| 2585 | |
| 2586 | /* Check bth i and j. */ |
| 2587 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2588 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2589 | return 1; |
| 2590 | } |
| 2591 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2592 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | return 1; |
| 2594 | } |
| 2595 | if (i == 0) |
| 2596 | i = 1; |
| 2597 | if (j == 0) |
| 2598 | j = 1; |
| 2599 | |
| 2600 | /* Pop the string. */ |
| 2601 | lua_pop(L, 1); |
| 2602 | |
| 2603 | /* Update the buffer length. */ |
| 2604 | buf += i - 1; |
| 2605 | buf_len = j - i + 1; |
| 2606 | lua_pushlstring(L, buf, buf_len); |
| 2607 | |
| 2608 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2609 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2610 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2611 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2612 | } |
| 2613 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2614 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Christopher Faulet | e6465b3 | 2021-10-22 15:36:08 +0200 | [diff] [blame] | 2615 | __LJMP static inline int hlua_socket_info(struct lua_State *L, const struct sockaddr_storage *addr) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | { |
| 2617 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2618 | int ret; |
| 2619 | int len; |
| 2620 | char *p; |
| 2621 | |
| 2622 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2623 | if (ret <= 0) { |
| 2624 | lua_pushnil(L); |
| 2625 | return 1; |
| 2626 | } |
| 2627 | |
| 2628 | if (ret == AF_UNIX) { |
| 2629 | lua_pushstring(L, buffer+1); |
| 2630 | return 1; |
| 2631 | } |
| 2632 | else if (ret == AF_INET6) { |
| 2633 | buffer[0] = '['; |
| 2634 | len = strlen(buffer); |
| 2635 | buffer[len] = ']'; |
| 2636 | len++; |
| 2637 | buffer[len] = ':'; |
| 2638 | len++; |
| 2639 | p = buffer; |
| 2640 | } |
| 2641 | else if (ret == AF_INET) { |
| 2642 | p = buffer + 1; |
| 2643 | len = strlen(p); |
| 2644 | p[len] = ':'; |
| 2645 | len++; |
| 2646 | } |
| 2647 | else { |
| 2648 | lua_pushnil(L); |
| 2649 | return 1; |
| 2650 | } |
| 2651 | |
| 2652 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2653 | lua_pushnil(L); |
| 2654 | return 1; |
| 2655 | } |
| 2656 | |
| 2657 | lua_pushstring(L, p); |
| 2658 | return 1; |
| 2659 | } |
| 2660 | |
| 2661 | /* Returns information about the peer of the connection. */ |
| 2662 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2663 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2664 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | struct xref *peer; |
| 2666 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2667 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2668 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2669 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2670 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2671 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2672 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2673 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2675 | /* Check if we run on the same thread than the xreator thread. |
| 2676 | * We cannot access to the socket if the thread is different. |
| 2677 | */ |
| 2678 | if (socket->tid != tid) |
| 2679 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2680 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2681 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2682 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2683 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2684 | lua_pushnil(L); |
| 2685 | return 1; |
| 2686 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2687 | |
| 2688 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2689 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2690 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2691 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2693 | lua_pushnil(L); |
| 2694 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2695 | } |
| 2696 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2697 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2698 | xref_unlock(&socket->xref, peer); |
| 2699 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2700 | } |
| 2701 | |
| 2702 | /* Returns information about my connection side. */ |
| 2703 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2704 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2705 | struct hlua_socket *socket; |
| 2706 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2707 | struct appctx *appctx; |
| 2708 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2709 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2710 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2711 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2712 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2713 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2714 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2715 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2716 | /* Check if we run on the same thread than the xreator thread. |
| 2717 | * We cannot access to the socket if the thread is different. |
| 2718 | */ |
| 2719 | if (socket->tid != tid) |
| 2720 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2721 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2722 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2723 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2724 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2725 | lua_pushnil(L); |
| 2726 | return 1; |
| 2727 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2728 | |
| 2729 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2730 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2731 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2732 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2733 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2734 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2735 | lua_pushnil(L); |
| 2736 | return 1; |
| 2737 | } |
| 2738 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2739 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2740 | xref_unlock(&socket->xref, peer); |
| 2741 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2742 | } |
| 2743 | |
| 2744 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2745 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2746 | .obj_type = OBJ_TYPE_APPLET, |
| 2747 | .name = "<LUA_TCP>", |
| 2748 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2749 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2750 | .release = hlua_socket_release, |
| 2751 | }; |
| 2752 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2753 | __LJMP static int hlua_socket_connect_yield(struct lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2754 | { |
| 2755 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2756 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2757 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2758 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2759 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2760 | struct stream *s; |
| 2761 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2762 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2763 | hlua = hlua_gethlua(L); |
| 2764 | if (!hlua) |
| 2765 | return 0; |
| 2766 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2767 | /* Check if we run on the same thread than the xreator thread. |
| 2768 | * We cannot access to the socket if the thread is different. |
| 2769 | */ |
| 2770 | if (socket->tid != tid) |
| 2771 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2772 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2773 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2774 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2775 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2776 | lua_pushnil(L); |
| 2777 | lua_pushstring(L, "Can't connect"); |
| 2778 | return 2; |
| 2779 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2780 | |
| 2781 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2782 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2783 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2784 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2785 | /* Check if we run on the same thread than the xreator thread. |
| 2786 | * We cannot access to the socket if the thread is different. |
| 2787 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2788 | if (socket->tid != tid) { |
| 2789 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2790 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2791 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2792 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2793 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2794 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2796 | lua_pushnil(L); |
| 2797 | lua_pushstring(L, "Can't connect"); |
| 2798 | return 2; |
| 2799 | } |
| 2800 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2801 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2802 | |
| 2803 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2804 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2805 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | lua_pushinteger(L, 1); |
| 2807 | return 1; |
| 2808 | } |
| 2809 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2810 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2811 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2812 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2813 | } |
| 2814 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2815 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2816 | return 0; |
| 2817 | } |
| 2818 | |
| 2819 | /* This function fail or initite the connection. */ |
| 2820 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2821 | { |
| 2822 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2823 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2824 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2825 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2826 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2827 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2828 | int low, high; |
| 2829 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2830 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2831 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2832 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2833 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2834 | if (lua_gettop(L) < 2) |
| 2835 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2836 | |
| 2837 | /* Get args. */ |
| 2838 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2839 | |
| 2840 | /* Check if we run on the same thread than the xreator thread. |
| 2841 | * We cannot access to the socket if the thread is different. |
| 2842 | */ |
| 2843 | if (socket->tid != tid) |
| 2844 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2845 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2846 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2847 | if (lua_gettop(L) >= 3) { |
| 2848 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2849 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2850 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2851 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2852 | * that are not enclosed within square brackets. |
| 2853 | */ |
| 2854 | if (port > 0) { |
| 2855 | luaL_buffinit(L, &b); |
| 2856 | luaL_addstring(&b, ip); |
| 2857 | luaL_addchar(&b, ':'); |
| 2858 | luaL_pushresult(&b); |
| 2859 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2860 | } |
| 2861 | } |
| 2862 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2863 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2864 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2865 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2866 | lua_pushnil(L); |
| 2867 | return 1; |
| 2868 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2869 | |
| 2870 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2871 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, NULL, NULL, PA_O_PORT_OK | PA_O_STREAM); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2872 | if (!addr) { |
| 2873 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2874 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2875 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2876 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2877 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2878 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2879 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2880 | if (port == -1) { |
| 2881 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2882 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2883 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2884 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2885 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2886 | if (port == -1) { |
| 2887 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2888 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2889 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2890 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2891 | } |
| 2892 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2893 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2894 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2895 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2896 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2897 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2898 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2899 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2900 | xref_unlock(&socket->xref, peer); |
| 2901 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2902 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2903 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2904 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2905 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2906 | if (!hlua) |
| 2907 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2908 | |
| 2909 | /* inform the stream that we want to be notified whenever the |
| 2910 | * connection completes. |
| 2911 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2912 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2913 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2914 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2915 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2916 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2917 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2918 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2919 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2920 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2921 | } |
| 2922 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2923 | |
| 2924 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2925 | /* Return yield waiting for connection. */ |
| 2926 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2927 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_socket_connect_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2928 | |
| 2929 | return 0; |
| 2930 | } |
| 2931 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2932 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2933 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2934 | { |
| 2935 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2936 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2937 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2938 | |
| 2939 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2940 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2941 | |
| 2942 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2943 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2944 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2945 | lua_pushnil(L); |
| 2946 | return 1; |
| 2947 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2948 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2949 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2950 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2951 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2952 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2953 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2954 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2955 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2956 | |
| 2957 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2958 | { |
| 2959 | return 0; |
| 2960 | } |
| 2961 | |
| 2962 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2963 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2964 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2965 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2966 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2967 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2968 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2969 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2970 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2971 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2972 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2973 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2974 | /* convert the timeout to millis */ |
| 2975 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2976 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2977 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2978 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2979 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2980 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2981 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2982 | WILL_LJMP(luaL_error(L, "settimeout: cannot set values larger than %d ms", INT_MAX)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2983 | |
| 2984 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2985 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2986 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2987 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2988 | /* Check if we run on the same thread than the xreator thread. |
| 2989 | * We cannot access to the socket if the thread is different. |
| 2990 | */ |
| 2991 | if (socket->tid != tid) |
| 2992 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2993 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2994 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2995 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2996 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2997 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2998 | WILL_LJMP(lua_error(L)); |
| 2999 | return 0; |
| 3000 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3001 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 3002 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3003 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3004 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3005 | s->req.rto = tmout; |
| 3006 | s->req.wto = tmout; |
| 3007 | s->res.rto = tmout; |
| 3008 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 3009 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 3010 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 3011 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 3012 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 3013 | |
| 3014 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 3015 | task_queue(s->task); |
| 3016 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 3017 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3018 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3019 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3020 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3021 | } |
| 3022 | |
| 3023 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3024 | { |
| 3025 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3026 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3027 | struct appctx *appctx; |
| 3028 | |
| 3029 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3030 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3031 | hlua_pusherror(L, "socket: full stack"); |
| 3032 | goto out_fail_conf; |
| 3033 | } |
| 3034 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3035 | /* Create the object: obj[0] = userdata. */ |
| 3036 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3037 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3038 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3039 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3040 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3041 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3042 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3043 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3044 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3045 | goto out_fail_conf; |
| 3046 | } |
| 3047 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3048 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3049 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3050 | lua_setmetatable(L, -2); |
| 3051 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3052 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3053 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3054 | if (!appctx) { |
| 3055 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3056 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3057 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3058 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3059 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3060 | ctx->die = 0; |
| 3061 | LIST_INIT(&ctx->wake_on_write); |
| 3062 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3063 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3064 | if (appctx_init(appctx) == -1) { |
| 3065 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3066 | goto out_fail_appctx; |
| 3067 | } |
| 3068 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3069 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3070 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3071 | return 1; |
| 3072 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3073 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3074 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3075 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3076 | WILL_LJMP(lua_error(L)); |
| 3077 | return 0; |
| 3078 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3079 | |
| 3080 | /* |
| 3081 | * |
| 3082 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | * Class Channel |
| 3084 | * |
| 3085 | * |
| 3086 | */ |
| 3087 | |
| 3088 | /* Returns the struct hlua_channel join to the class channel in the |
| 3089 | * stack entry "ud" or throws an argument error. |
| 3090 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3091 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3092 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3093 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3094 | } |
| 3095 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3096 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3097 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3098 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3099 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3102 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | return 0; |
| 3104 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3105 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3106 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3107 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | |
| 3109 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3110 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3111 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | return 1; |
| 3113 | } |
| 3114 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3115 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3116 | * in the stack, filling the current offset and length of the filter. If no |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 3117 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3118 | * initialized. |
| 3119 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3120 | static struct filter *hlua_channel_filter(lua_State *L, int ud, struct channel *chn, size_t *offset, size_t *len) |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3121 | { |
| 3122 | struct filter *filter = NULL; |
| 3123 | |
| 3124 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3125 | struct hlua_flt_ctx *flt_ctx; |
| 3126 | |
| 3127 | filter = lua_touserdata (L, -1); |
| 3128 | flt_ctx = filter->ctx; |
| 3129 | if (hlua_filter_from_payload(filter)) { |
| 3130 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3131 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3132 | } |
| 3133 | } |
| 3134 | |
| 3135 | lua_pop(L, 1); |
| 3136 | return filter; |
| 3137 | } |
| 3138 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3139 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3140 | * offset <offset>, and put it in a LUA string variable. It is the caller |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3141 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3142 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3143 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3144 | */ |
| 3145 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L, size_t offset, size_t len) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3146 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3147 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3148 | luaL_Buffer b; |
| 3149 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3150 | block1 = len; |
| 3151 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3152 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3153 | block2 = len - block1; |
| 3154 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3155 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3156 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3157 | if (block2) |
| 3158 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3160 | return len; |
| 3161 | } |
| 3162 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3163 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3164 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3165 | * number of bytes copied. |
| 3166 | */ |
| 3167 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3168 | { |
| 3169 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3170 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3171 | /* Nothing to do, just return */ |
| 3172 | if (unlikely(istlen(str) == 0)) |
| 3173 | goto end; |
| 3174 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3175 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3176 | ret = -1; |
| 3177 | goto end; |
| 3178 | } |
| 3179 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3180 | |
| 3181 | end: |
| 3182 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | } |
| 3184 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3185 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3186 | */ |
| 3187 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3188 | { |
| 3189 | size_t end = offset + len; |
| 3190 | |
| 3191 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3192 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3193 | b_data(&chn->buf) - end, -len); |
| 3194 | b_sub(&chn->buf, len); |
| 3195 | } |
| 3196 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3197 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3198 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3199 | * offset by default). If there is not enough input data and more data can be |
| 3200 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3201 | * |
| 3202 | * From an action, All input data are considered. For a filter, the offset and |
| 3203 | * the length of input data to consider are retrieved from the filter context. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3204 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3205 | __LJMP static int hlua_channel_get_data_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3206 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3207 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | struct filter *filter; |
| 3209 | size_t input, output; |
| 3210 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3211 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3212 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3213 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3214 | output = co_data(chn); |
| 3215 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3216 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3217 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3218 | if (filter && !hlua_filter_from_payload(filter)) |
| 3219 | WILL_LJMP(lua_error(L)); |
| 3220 | |
| 3221 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3222 | if (lua_gettop(L) > 1) { |
| 3223 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3224 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3225 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3226 | offset += output; |
| 3227 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | lua_pushfstring(L, "offset out of range."); |
| 3229 | WILL_LJMP(lua_error(L)); |
| 3230 | } |
| 3231 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3232 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3233 | if (lua_gettop(L) == 3) { |
| 3234 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3235 | if (!len) |
| 3236 | goto dup; |
| 3237 | if (len == -1) |
| 3238 | len = global.tune.bufsize; |
| 3239 | if (len < 0) { |
| 3240 | lua_pushfstring(L, "length out of range."); |
| 3241 | WILL_LJMP(lua_error(L)); |
| 3242 | } |
| 3243 | } |
| 3244 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3245 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3246 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3247 | /* Yield waiting for more data, as requested */ |
| 3248 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 3249 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3250 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3251 | } |
| 3252 | |
| 3253 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3254 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3255 | return 1; |
| 3256 | } |
| 3257 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3258 | /* Copies the first line (including the trailing LF) of input data in the |
| 3259 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3260 | * a length (all remaining input data starting for the offset by default). If |
| 3261 | * there is not enough input data and more data can be received, the function |
| 3262 | * yields. If a length is explicitly specified, no more data are |
| 3263 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3264 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3265 | * |
| 3266 | * From an action, All input data are considered. For a filter, the offset and |
| 3267 | * 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] | 3268 | */ |
| 3269 | __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] | 3270 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3271 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3272 | struct filter *filter; |
| 3273 | size_t l, input, output; |
| 3274 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3275 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3276 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3277 | output = co_data(chn); |
| 3278 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3279 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3280 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3281 | if (filter && !hlua_filter_from_payload(filter)) |
| 3282 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3283 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3284 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3285 | if (lua_gettop(L) > 1) { |
| 3286 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3287 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3288 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3289 | offset += output; |
| 3290 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3291 | lua_pushfstring(L, "offset out of range."); |
| 3292 | WILL_LJMP(lua_error(L)); |
| 3293 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3294 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3295 | |
| 3296 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3297 | if (lua_gettop(L) == 3) { |
| 3298 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3299 | if (!len) |
| 3300 | goto dup; |
| 3301 | if (len == -1) |
| 3302 | len = global.tune.bufsize; |
| 3303 | if (len < 0) { |
| 3304 | lua_pushfstring(L, "length out of range."); |
| 3305 | WILL_LJMP(lua_error(L)); |
| 3306 | } |
| 3307 | } |
| 3308 | |
| 3309 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3310 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3311 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3312 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3313 | len = l+1; |
| 3314 | goto dup; |
| 3315 | } |
| 3316 | } |
| 3317 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3318 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3319 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3320 | /* Yield waiting for more data */ |
| 3321 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3322 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3323 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3324 | } |
| 3325 | |
| 3326 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3327 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3328 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3329 | } |
| 3330 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3331 | /* [ DEPRECATED ] |
| 3332 | * |
| 3333 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3334 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3335 | * |
| 3336 | * From an action, All input data are considered. For a filter, the offset and |
| 3337 | * 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] | 3338 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3339 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3340 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3341 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3342 | struct filter *filter; |
| 3343 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3344 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3345 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3346 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3347 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3348 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3349 | WILL_LJMP(lua_error(L)); |
| 3350 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3351 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3352 | offset = co_data(chn); |
| 3353 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3354 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3355 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3356 | if (filter && !hlua_filter_from_payload(filter)) |
| 3357 | WILL_LJMP(lua_error(L)); |
| 3358 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3359 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3360 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3361 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3362 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3363 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3364 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3365 | return 1; |
| 3366 | } |
| 3367 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3368 | /* [ DEPRECATED ] |
| 3369 | * |
| 3370 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3371 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3372 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3373 | * |
| 3374 | * From an action, All input data are considered. For a filter, the offset and |
| 3375 | * 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] | 3376 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3377 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3378 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3379 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3380 | struct filter *filter; |
| 3381 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3382 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3383 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3384 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3385 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3386 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3387 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3388 | WILL_LJMP(lua_error(L)); |
| 3389 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3390 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3391 | offset = co_data(chn); |
| 3392 | len = ci_data(chn); |
| 3393 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3394 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3395 | if (filter && !hlua_filter_from_payload(filter)) |
| 3396 | WILL_LJMP(lua_error(L)); |
| 3397 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3398 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3399 | lua_pushnil(L); |
| 3400 | return 1; |
| 3401 | } |
| 3402 | |
| 3403 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3404 | _hlua_channel_delete(chn, offset, ret); |
| 3405 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3406 | } |
| 3407 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3408 | /* This functions consumes and returns one line. If the channel is closed, |
| 3409 | * 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] | 3410 | * 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] | 3411 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3412 | * |
| 3413 | * From an action, All input data are considered. For a filter, the offset and |
| 3414 | * 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] | 3415 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3416 | __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] | 3417 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3418 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3419 | struct filter *filter; |
| 3420 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3421 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3422 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3423 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3424 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3425 | offset = co_data(chn); |
| 3426 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3427 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3428 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3429 | if (filter && !hlua_filter_from_payload(filter)) |
| 3430 | WILL_LJMP(lua_error(L)); |
| 3431 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3432 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3433 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3434 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3435 | } |
| 3436 | |
| 3437 | for (l = 0; l < len; l++) { |
| 3438 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3439 | len = l+1; |
| 3440 | goto dup; |
| 3441 | } |
| 3442 | } |
| 3443 | |
| 3444 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3445 | /* Yield waiting for more data */ |
| 3446 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3447 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3448 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3449 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3450 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3451 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3452 | return 1; |
| 3453 | } |
| 3454 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3455 | /* [ DEPRECATED ] |
| 3456 | * |
| 3457 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3458 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3459 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3460 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3461 | struct channel *chn; |
| 3462 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3463 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3464 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3465 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3466 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3467 | WILL_LJMP(lua_error(L)); |
| 3468 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3469 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3470 | } |
| 3471 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3472 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3473 | * available input data are returned. The offset may be negactive to start from |
| 3474 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3475 | * size. |
| 3476 | */ |
| 3477 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3478 | { |
| 3479 | struct channel *chn; |
| 3480 | |
| 3481 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3482 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3483 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3484 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3485 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3486 | WILL_LJMP(lua_error(L)); |
| 3487 | } |
| 3488 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3489 | } |
| 3490 | |
| 3491 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3492 | * available input data are returned. The offset may be negactive to start from |
| 3493 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3494 | * size. |
| 3495 | */ |
| 3496 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3497 | { |
| 3498 | struct channel *chn; |
| 3499 | |
| 3500 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3501 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3502 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3503 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3504 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3505 | WILL_LJMP(lua_error(L)); |
| 3506 | } |
| 3507 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3508 | } |
| 3509 | |
| 3510 | /* Appends a string into the input side of channel. It returns the length of the |
| 3511 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3512 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3513 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3514 | * |
| 3515 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3516 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3517 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3518 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3519 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3520 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3521 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3522 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3523 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3524 | |
| 3525 | MAY_LJMP(check_args(L, 2, "append")); |
| 3526 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3527 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3528 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3529 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3530 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3531 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3532 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3533 | offset = co_data(chn); |
| 3534 | len = ci_data(chn); |
| 3535 | |
| 3536 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3537 | if (filter && !hlua_filter_from_payload(filter)) |
| 3538 | WILL_LJMP(lua_error(L)); |
| 3539 | |
| 3540 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3541 | if (ret > 0 && filter) { |
| 3542 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3543 | |
| 3544 | flt_update_offsets(filter, chn, ret); |
| 3545 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3546 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3547 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3548 | return 1; |
| 3549 | } |
| 3550 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3551 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3552 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3553 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3554 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3555 | * |
| 3556 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3557 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3558 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3559 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3560 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3561 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3562 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3563 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3564 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3565 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3566 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3567 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3568 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3569 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3570 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3571 | WILL_LJMP(lua_error(L)); |
| 3572 | } |
| 3573 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3574 | offset = co_data(chn); |
| 3575 | len = ci_data(chn); |
| 3576 | |
| 3577 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3578 | if (filter && !hlua_filter_from_payload(filter)) |
| 3579 | WILL_LJMP(lua_error(L)); |
| 3580 | |
| 3581 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3582 | if (ret > 0 && filter) { |
| 3583 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3584 | |
| 3585 | flt_update_offsets(filter, chn, ret); |
| 3586 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3587 | } |
| 3588 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3589 | lua_pushinteger(L, ret); |
| 3590 | return 1; |
| 3591 | } |
| 3592 | |
| 3593 | /* Inserts a given amount of input data at the given offset by a string |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3594 | * content. By default the string is appended in front of input data. It |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3595 | * returns the length of the written string, or -1 if the channel is closed or |
| 3596 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3597 | * |
| 3598 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3599 | */ |
| 3600 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3601 | { |
| 3602 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3603 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3604 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3605 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3606 | int ret, offset; |
| 3607 | |
| 3608 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3609 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3610 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3611 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3612 | |
| 3613 | output = co_data(chn); |
| 3614 | input = ci_data(chn); |
| 3615 | |
| 3616 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3617 | if (filter && !hlua_filter_from_payload(filter)) |
| 3618 | WILL_LJMP(lua_error(L)); |
| 3619 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3620 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3621 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3622 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3623 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3624 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3625 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame] | 3626 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3627 | lua_pushfstring(L, "offset out of range."); |
| 3628 | WILL_LJMP(lua_error(L)); |
| 3629 | } |
| 3630 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3631 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3632 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3633 | WILL_LJMP(lua_error(L)); |
| 3634 | } |
| 3635 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3636 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3637 | if (ret > 0 && filter) { |
| 3638 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3639 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3640 | flt_update_offsets(filter, chn, ret); |
| 3641 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3642 | } |
| 3643 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | lua_pushinteger(L, ret); |
| 3645 | return 1; |
| 3646 | } |
| 3647 | /* Replaces a given amount of input data at the given offset by a string |
| 3648 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3649 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3650 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3651 | * |
| 3652 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3653 | */ |
| 3654 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3655 | { |
| 3656 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3657 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3658 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3659 | size_t sz, input, output; |
| 3660 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3661 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3662 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3663 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3664 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3665 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3666 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3667 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3668 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3669 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3670 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3671 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3672 | output = co_data(chn); |
| 3673 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3674 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3675 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3676 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3677 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3678 | |
| 3679 | offset = output; |
| 3680 | if (lua_gettop(L) > 2) { |
| 3681 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3682 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3683 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3684 | offset += output; |
| 3685 | if (offset < output || offset > input + output) { |
| 3686 | lua_pushfstring(L, "offset out of range."); |
| 3687 | WILL_LJMP(lua_error(L)); |
| 3688 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3689 | } |
| 3690 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3691 | len = output + input - offset; |
| 3692 | if (lua_gettop(L) == 4) { |
| 3693 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3694 | if (!len) |
| 3695 | goto set; |
| 3696 | if (len == -1) |
| 3697 | len = output + input - offset; |
| 3698 | if (len < 0 || offset + len > output + input) { |
| 3699 | lua_pushfstring(L, "length out of range."); |
| 3700 | WILL_LJMP(lua_error(L)); |
| 3701 | } |
| 3702 | } |
| 3703 | |
| 3704 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3705 | /* 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] | 3706 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3707 | lua_pushinteger(L, -1); |
| 3708 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3709 | _hlua_channel_delete(chn, offset, len); |
| 3710 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3711 | if (filter) { |
| 3712 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3713 | |
| 3714 | len -= (ret > 0 ? ret : 0); |
| 3715 | flt_update_offsets(filter, chn, -len); |
| 3716 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3717 | } |
| 3718 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3719 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3720 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3721 | return 1; |
| 3722 | } |
| 3723 | |
| 3724 | /* 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] | 3725 | * 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] | 3726 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3727 | * |
| 3728 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3729 | */ |
| 3730 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3731 | { |
| 3732 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3733 | struct filter *filter; |
| 3734 | size_t input, output; |
| 3735 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3736 | |
| 3737 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3738 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3739 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3740 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3741 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3742 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3743 | WILL_LJMP(lua_error(L)); |
| 3744 | } |
| 3745 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3746 | output = co_data(chn); |
| 3747 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3748 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3749 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3750 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3751 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3752 | |
| 3753 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3754 | if (lua_gettop(L) > 1) { |
| 3755 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3756 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3757 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3758 | offset += output; |
| 3759 | if (offset < output || offset > input + output) { |
| 3760 | lua_pushfstring(L, "offset out of range."); |
| 3761 | WILL_LJMP(lua_error(L)); |
| 3762 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3763 | } |
| 3764 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3765 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3766 | if (lua_gettop(L) == 3) { |
| 3767 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3768 | if (!len) |
| 3769 | goto end; |
| 3770 | if (len == -1) |
| 3771 | len = output + input - offset; |
| 3772 | if (len < 0 || offset + len > output + input) { |
| 3773 | lua_pushfstring(L, "length out of range."); |
| 3774 | WILL_LJMP(lua_error(L)); |
| 3775 | } |
| 3776 | } |
| 3777 | |
| 3778 | _hlua_channel_delete(chn, offset, len); |
| 3779 | if (filter) { |
| 3780 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3781 | |
| 3782 | flt_update_offsets(filter, chn, -len); |
| 3783 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3784 | } |
| 3785 | |
| 3786 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3787 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3788 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3789 | } |
| 3790 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3791 | /* Append data in the output side of the buffer. This data is immediately |
| 3792 | * sent. The function returns the amount of data written. If the buffer |
| 3793 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3794 | * if the channel is closed. |
| 3795 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3796 | __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] | 3797 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3798 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3799 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3800 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3801 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3802 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3803 | struct hlua *hlua; |
| 3804 | |
| 3805 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3806 | hlua = hlua_gethlua(L); |
| 3807 | if (!hlua) { |
| 3808 | lua_pushnil(L); |
| 3809 | return 1; |
| 3810 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3811 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3812 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3813 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3814 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3815 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3816 | offset = co_data(chn); |
| 3817 | len = ci_data(chn); |
| 3818 | |
| 3819 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3820 | if (filter && !hlua_filter_from_payload(filter)) |
| 3821 | WILL_LJMP(lua_error(L)); |
| 3822 | |
| 3823 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3824 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3825 | lua_pushinteger(L, -1); |
| 3826 | return 1; |
| 3827 | } |
| 3828 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3829 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3830 | if (len > sz -l) { |
| 3831 | if (filter) { |
| 3832 | lua_pushinteger(L, -1); |
| 3833 | return 1; |
| 3834 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3835 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3836 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3837 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3838 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3839 | if (ret == -1) { |
| 3840 | lua_pop(L, 1); |
| 3841 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3842 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3843 | } |
| 3844 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3845 | if (filter) { |
| 3846 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3847 | |
| 3848 | |
| 3849 | flt_update_offsets(filter, chn, ret); |
| 3850 | FLT_OFF(filter, chn) += ret; |
| 3851 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3852 | } |
| 3853 | else |
| 3854 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3855 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3856 | l += ret; |
| 3857 | lua_pop(L, 1); |
| 3858 | lua_pushinteger(L, l); |
| 3859 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3860 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3861 | if (l < sz) { |
| 3862 | /* Yield only if the channel's output is not empty. |
| 3863 | * Otherwise it means we cannot add more data. */ |
| 3864 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3865 | return 1; |
| 3866 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3867 | /* If we are waiting for space in the response buffer, we |
| 3868 | * must set the flag WAKERESWR. This flag required the task |
| 3869 | * wake up if any activity is detected on the response buffer. |
| 3870 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3871 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3872 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3873 | else |
| 3874 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3875 | 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] | 3876 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3877 | |
| 3878 | return 1; |
| 3879 | } |
| 3880 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3881 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3882 | * yield the LUA process, and resume it without checking the |
| 3883 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3884 | * |
| 3885 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3886 | */ |
| 3887 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3888 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3889 | struct channel *chn; |
| 3890 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3891 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3892 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3893 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3894 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3895 | WILL_LJMP(lua_error(L)); |
| 3896 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3897 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3898 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3899 | } |
| 3900 | |
| 3901 | /* This function forward and amount of butes. The data pass from |
| 3902 | * the input side of the buffer to the output side, and can be |
| 3903 | * forwarded. This function never fails. |
| 3904 | * |
| 3905 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3906 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3907 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3908 | __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] | 3909 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3910 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3911 | struct filter *filter; |
| 3912 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3913 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3914 | struct hlua *hlua; |
| 3915 | |
| 3916 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3917 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3918 | if (!hlua) { |
| 3919 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3920 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3921 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3922 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3923 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3924 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3925 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3926 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3927 | offset = co_data(chn); |
| 3928 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3929 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3930 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3931 | if (filter && !hlua_filter_from_payload(filter)) |
| 3932 | WILL_LJMP(lua_error(L)); |
| 3933 | |
| 3934 | max = fwd - l; |
| 3935 | if (max > len) |
| 3936 | max = len; |
| 3937 | |
| 3938 | if (filter) { |
| 3939 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3940 | |
| 3941 | FLT_OFF(filter, chn) += max; |
| 3942 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3943 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3944 | } |
| 3945 | else |
| 3946 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3947 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3948 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3949 | lua_pop(L, 1); |
| 3950 | lua_pushinteger(L, l); |
| 3951 | |
| 3952 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3953 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3954 | /* The the input channel or the output channel are closed, we |
| 3955 | * must return the amount of data forwarded. |
| 3956 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3957 | 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] | 3958 | return 1; |
| 3959 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3960 | /* If we are waiting for space data in the response buffer, we |
| 3961 | * must set the flag WAKERESWR. This flag required the task |
| 3962 | * wake up if any activity is detected on the response buffer. |
| 3963 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3964 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3965 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3966 | else |
| 3967 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3968 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3969 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3970 | 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] | 3971 | } |
| 3972 | |
| 3973 | return 1; |
| 3974 | } |
| 3975 | |
| 3976 | /* Just check the input and prepare the stack for the previous |
| 3977 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3978 | * |
| 3979 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3980 | */ |
| 3981 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3982 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3983 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3984 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3985 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3986 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3987 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3988 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3989 | WILL_LJMP(lua_error(L)); |
| 3990 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3991 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3992 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3993 | } |
| 3994 | |
| 3995 | /* Just returns the number of bytes available in the input |
| 3996 | * side of the buffer. This function never fails. |
| 3997 | */ |
| 3998 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3999 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4000 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4001 | struct filter *filter; |
| 4002 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4003 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4004 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4005 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4006 | |
| 4007 | output = co_data(chn); |
| 4008 | input = ci_data(chn); |
| 4009 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 4010 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 4011 | lua_pushinteger(L, input); |
| 4012 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4013 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4014 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 4015 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 4016 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4017 | return 1; |
| 4018 | } |
| 4019 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4020 | /* Returns true if the channel is full. */ |
| 4021 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4022 | { |
| 4023 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4024 | |
| 4025 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4026 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4027 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4028 | * applet). |
| 4029 | */ |
| 4030 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4031 | return 1; |
| 4032 | } |
| 4033 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4034 | /* Returns true if the channel may still receive data. */ |
| 4035 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4036 | { |
| 4037 | struct channel *chn; |
| 4038 | |
| 4039 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4040 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4041 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4042 | return 1; |
| 4043 | } |
| 4044 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4045 | /* Returns true if the channel is the response channel. */ |
| 4046 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4047 | { |
| 4048 | struct channel *chn; |
| 4049 | |
| 4050 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4051 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4052 | |
| 4053 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4054 | return 1; |
| 4055 | } |
| 4056 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4057 | /* Just returns the number of bytes available in the output |
| 4058 | * side of the buffer. This function never fails. |
| 4059 | */ |
| 4060 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4061 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4062 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4063 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4064 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4065 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4066 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4067 | |
| 4068 | output = co_data(chn); |
| 4069 | input = ci_data(chn); |
| 4070 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4071 | |
| 4072 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4073 | return 1; |
| 4074 | } |
| 4075 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4076 | /* |
| 4077 | * |
| 4078 | * |
| 4079 | * Class Fetches |
| 4080 | * |
| 4081 | * |
| 4082 | */ |
| 4083 | |
| 4084 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4085 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4086 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4087 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4088 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4089 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4090 | } |
| 4091 | |
| 4092 | /* This function creates and push in the stack a fetch object according |
| 4093 | * with a current TXN. |
| 4094 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4095 | 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] | 4096 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4097 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4098 | |
| 4099 | /* Check stack size. */ |
| 4100 | if (!lua_checkstack(L, 3)) |
| 4101 | return 0; |
| 4102 | |
| 4103 | /* Create the object: obj[0] = userdata. |
| 4104 | * Note that the base of the Fetches object is the |
| 4105 | * transaction object. |
| 4106 | */ |
| 4107 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4108 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4109 | lua_rawseti(L, -2, 0); |
| 4110 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4111 | hsmp->s = txn->s; |
| 4112 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4113 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4114 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4115 | |
| 4116 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4117 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4118 | lua_setmetatable(L, -2); |
| 4119 | |
| 4120 | return 1; |
| 4121 | } |
| 4122 | |
| 4123 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4124 | * It uses closure argument to store the associated sample-fetch. It |
| 4125 | * returns only one argument or throws an error. An error is thrown |
| 4126 | * only if an error is encountered during the argument parsing. If |
| 4127 | * the "sample-fetch" function fails, nil is returned. |
| 4128 | */ |
| 4129 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4130 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4131 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4132 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4133 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4134 | int i; |
| 4135 | struct sample smp; |
| 4136 | |
| 4137 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4138 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4139 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4140 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4141 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4142 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4143 | /* Check execution authorization. */ |
| 4144 | if (f->use & SMP_USE_HTTP_ANY && |
| 4145 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4146 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4147 | "is not available in Lua services", f->kw); |
| 4148 | WILL_LJMP(lua_error(L)); |
| 4149 | } |
| 4150 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4151 | /* Get extra arguments. */ |
| 4152 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4153 | if (i >= ARGM_NBARGS) |
| 4154 | break; |
| 4155 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4156 | } |
| 4157 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4158 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4159 | |
| 4160 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4161 | 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] | 4162 | |
| 4163 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4164 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4165 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4166 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4167 | } |
| 4168 | |
| 4169 | /* Initialise the sample. */ |
| 4170 | memset(&smp, 0, sizeof(smp)); |
| 4171 | |
| 4172 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4173 | 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] | 4174 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4175 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4176 | lua_pushstring(L, ""); |
| 4177 | else |
| 4178 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4179 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4180 | } |
| 4181 | |
| 4182 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4183 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4184 | hlua_smp2lua_str(L, &smp); |
| 4185 | else |
| 4186 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4187 | |
| 4188 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4189 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4190 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4191 | |
| 4192 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4193 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4194 | WILL_LJMP(lua_error(L)); |
| 4195 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4196 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4197 | |
| 4198 | /* |
| 4199 | * |
| 4200 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4201 | * Class Converters |
| 4202 | * |
| 4203 | * |
| 4204 | */ |
| 4205 | |
| 4206 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4207 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4208 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4209 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4210 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4211 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4212 | } |
| 4213 | |
| 4214 | /* This function creates and push in the stack a Converters object |
| 4215 | * according with a current TXN. |
| 4216 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4217 | 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] | 4218 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4219 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4220 | |
| 4221 | /* Check stack size. */ |
| 4222 | if (!lua_checkstack(L, 3)) |
| 4223 | return 0; |
| 4224 | |
| 4225 | /* Create the object: obj[0] = userdata. |
| 4226 | * Note that the base of the Converters object is the |
| 4227 | * same than the TXN object. |
| 4228 | */ |
| 4229 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4230 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4231 | lua_rawseti(L, -2, 0); |
| 4232 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4233 | hsmp->s = txn->s; |
| 4234 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4235 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4236 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4237 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4238 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4239 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4240 | lua_setmetatable(L, -2); |
| 4241 | |
| 4242 | return 1; |
| 4243 | } |
| 4244 | |
| 4245 | /* This function is an LUA binding. It is called with each converter. |
| 4246 | * It uses closure argument to store the associated converter. It |
| 4247 | * returns only one argument or throws an error. An error is thrown |
| 4248 | * only if an error is encountered during the argument parsing. If |
| 4249 | * the converter function function fails, nil is returned. |
| 4250 | */ |
| 4251 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4252 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4253 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4254 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4255 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4256 | int i; |
| 4257 | struct sample smp; |
| 4258 | |
| 4259 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4260 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4261 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4262 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4263 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4264 | |
| 4265 | /* Get extra arguments. */ |
| 4266 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4267 | if (i >= ARGM_NBARGS) |
| 4268 | break; |
| 4269 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4270 | } |
| 4271 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4272 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4273 | |
| 4274 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4275 | 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] | 4276 | |
| 4277 | /* Run the special args checker. */ |
| 4278 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4279 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4280 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4281 | } |
| 4282 | |
| 4283 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4284 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4285 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4286 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4287 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4288 | } |
| 4289 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4290 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4291 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4292 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4293 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4294 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4295 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4296 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4297 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4298 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4299 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4300 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4301 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4302 | } |
| 4303 | |
| 4304 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4305 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4306 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4307 | lua_pushstring(L, ""); |
| 4308 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4309 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4310 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4311 | } |
| 4312 | |
| 4313 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4314 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4315 | hlua_smp2lua_str(L, &smp); |
| 4316 | else |
| 4317 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4318 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4319 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4320 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4321 | |
| 4322 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4323 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4324 | WILL_LJMP(lua_error(L)); |
| 4325 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4326 | } |
| 4327 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4328 | /* |
| 4329 | * |
| 4330 | * |
| 4331 | * Class AppletTCP |
| 4332 | * |
| 4333 | * |
| 4334 | */ |
| 4335 | |
| 4336 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4337 | * a class stream, otherwise it throws an error. |
| 4338 | */ |
| 4339 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4340 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4341 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4342 | } |
| 4343 | |
| 4344 | /* This function creates and push in the stack an Applet object |
| 4345 | * according with a current TXN. |
| 4346 | */ |
| 4347 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4348 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4349 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4350 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4351 | struct proxy *p; |
| 4352 | |
| 4353 | ALREADY_CHECKED(s); |
| 4354 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4355 | |
| 4356 | /* Check stack size. */ |
| 4357 | if (!lua_checkstack(L, 3)) |
| 4358 | return 0; |
| 4359 | |
| 4360 | /* Create the object: obj[0] = userdata. |
| 4361 | * Note that the base of the Converters object is the |
| 4362 | * same than the TXN object. |
| 4363 | */ |
| 4364 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4365 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4366 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4367 | luactx->appctx = ctx; |
| 4368 | luactx->htxn.s = s; |
| 4369 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4370 | |
| 4371 | /* Create the "f" field that contains a list of fetches. */ |
| 4372 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4373 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4374 | return 0; |
| 4375 | lua_settable(L, -3); |
| 4376 | |
| 4377 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4378 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4379 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4380 | return 0; |
| 4381 | lua_settable(L, -3); |
| 4382 | |
| 4383 | /* Create the "c" field that contains a list of converters. */ |
| 4384 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4385 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4386 | return 0; |
| 4387 | lua_settable(L, -3); |
| 4388 | |
| 4389 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4390 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4391 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4392 | return 0; |
| 4393 | lua_settable(L, -3); |
| 4394 | |
| 4395 | /* Pop a class stream metatable and affect it to the table. */ |
| 4396 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4397 | lua_setmetatable(L, -2); |
| 4398 | |
| 4399 | return 1; |
| 4400 | } |
| 4401 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4402 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4403 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4404 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4405 | struct stream *s; |
| 4406 | const char *name; |
| 4407 | size_t len; |
| 4408 | struct sample smp; |
| 4409 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4410 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4411 | 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] | 4412 | |
| 4413 | /* It is useles to retrieve the stream, but this function |
| 4414 | * runs only in a stream context. |
| 4415 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4416 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4417 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4418 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4419 | |
| 4420 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4421 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4422 | hlua_lua2smp(L, 3, &smp); |
| 4423 | |
| 4424 | /* Store the sample in a variable. */ |
| 4425 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4426 | |
| 4427 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4428 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4429 | else |
| 4430 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4431 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 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_unset_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, "unset_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 | /* Unset the variable. */ |
| 4453 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4454 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4455 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4456 | } |
| 4457 | |
| 4458 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4459 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4460 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4461 | struct stream *s; |
| 4462 | const char *name; |
| 4463 | size_t len; |
| 4464 | struct sample smp; |
| 4465 | |
| 4466 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4467 | |
| 4468 | /* It is useles to retrieve the stream, but this function |
| 4469 | * runs only in a stream context. |
| 4470 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4471 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4472 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4473 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4474 | |
| 4475 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4476 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4477 | lua_pushnil(L); |
| 4478 | return 1; |
| 4479 | } |
| 4480 | |
| 4481 | return hlua_smp2lua(L, &smp); |
| 4482 | } |
| 4483 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4484 | __LJMP static int hlua_applet_tcp_set_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 | return 0; |
| 4493 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4494 | |
| 4495 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4496 | |
| 4497 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4498 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4499 | |
| 4500 | /* Get and store new value. */ |
| 4501 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4502 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4503 | |
| 4504 | return 0; |
| 4505 | } |
| 4506 | |
| 4507 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 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)); |
| 4510 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4511 | struct hlua *hlua; |
| 4512 | |
| 4513 | /* 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] | 4514 | if (!s->hlua) { |
| 4515 | lua_pushnil(L); |
| 4516 | return 1; |
| 4517 | } |
| 4518 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4519 | |
| 4520 | /* Push configuration index in the stack. */ |
| 4521 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4522 | |
| 4523 | return 1; |
| 4524 | } |
| 4525 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4526 | /* If expected data not yet available, it returns a yield. This function |
| 4527 | * consumes the data in the buffer. It returns a string containing the |
| 4528 | * data. This string can be empty. |
| 4529 | */ |
| 4530 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4531 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4532 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4533 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4534 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4535 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4536 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4537 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4538 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4539 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4540 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4541 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4542 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4543 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4545 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4546 | 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] | 4547 | } |
| 4548 | |
| 4549 | /* End of data: commit the total strings and return. */ |
| 4550 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4551 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4552 | return 1; |
| 4553 | } |
| 4554 | |
| 4555 | /* Ensure that the block 2 length is usable. */ |
| 4556 | if (ret == 1) |
| 4557 | len2 = 0; |
| 4558 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4559 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4560 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4561 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4562 | |
| 4563 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4564 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4565 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4566 | return 1; |
| 4567 | } |
| 4568 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4569 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4570 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4571 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4572 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4573 | |
| 4574 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4575 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | |
| 4577 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4578 | } |
| 4579 | |
| 4580 | /* If expected data not yet available, it returns a yield. This function |
| 4581 | * consumes the data in the buffer. It returns a string containing the |
| 4582 | * data. This string can be empty. |
| 4583 | */ |
| 4584 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4585 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4586 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4587 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4588 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4589 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4590 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4591 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4592 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4593 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4594 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4595 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4596 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4597 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4598 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4599 | if (ret == 0) { |
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 | |
| 4604 | /* End of data: commit the total strings and return. */ |
| 4605 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4606 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4607 | return 1; |
| 4608 | } |
| 4609 | |
| 4610 | /* Ensure that the block 2 length is usable. */ |
| 4611 | if (ret == 1) |
| 4612 | len2 = 0; |
| 4613 | |
| 4614 | if (len == -1) { |
| 4615 | |
| 4616 | /* If len == -1, catenate all the data avalaile and |
| 4617 | * yield because we want to get all the data until |
| 4618 | * the end of data stream. |
| 4619 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4620 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4621 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4622 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4623 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4624 | 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] | 4625 | |
| 4626 | } else { |
| 4627 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4628 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4629 | if (len1 > len) |
| 4630 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4631 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4632 | len -= len1; |
| 4633 | |
| 4634 | /* Copy the second block. */ |
| 4635 | if (len2 > len) |
| 4636 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4637 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4638 | len -= len2; |
| 4639 | |
| 4640 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4641 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4643 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4644 | if (len > 0) { |
| 4645 | lua_pushinteger(L, len); |
| 4646 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4647 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4648 | 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] | 4649 | } |
| 4650 | |
| 4651 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4652 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4653 | return 1; |
| 4654 | } |
| 4655 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4656 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4657 | hlua_pusherror(L, "Lua: internal error"); |
| 4658 | WILL_LJMP(lua_error(L)); |
| 4659 | return 0; |
| 4660 | } |
| 4661 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4662 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4663 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4664 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4665 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4666 | int len = -1; |
| 4667 | |
| 4668 | if (lua_gettop(L) > 2) |
| 4669 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4670 | if (lua_gettop(L) >= 2) { |
| 4671 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4672 | lua_pop(L, 1); |
| 4673 | } |
| 4674 | |
| 4675 | /* Confirm or set the required length */ |
| 4676 | lua_pushinteger(L, len); |
| 4677 | |
| 4678 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4679 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4680 | |
| 4681 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4682 | } |
| 4683 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4684 | /* Append data in the output side of the buffer. This data is immediately |
| 4685 | * sent. The function returns the amount of data written. If the buffer |
| 4686 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4687 | * if the channel is closed. |
| 4688 | */ |
| 4689 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4690 | { |
| 4691 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4692 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4693 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4694 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4695 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4696 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4697 | int max; |
| 4698 | |
| 4699 | /* Get the max amount of data which can write as input in the channel. */ |
| 4700 | max = channel_recv_max(chn); |
| 4701 | if (max > (len - l)) |
| 4702 | max = len - l; |
| 4703 | |
| 4704 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4705 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4706 | |
| 4707 | /* update counters. */ |
| 4708 | l += max; |
| 4709 | lua_pop(L, 1); |
| 4710 | lua_pushinteger(L, l); |
| 4711 | |
| 4712 | /* If some data is not send, declares the situation to the |
| 4713 | * applet, and returns a yield. |
| 4714 | */ |
| 4715 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4716 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4717 | 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] | 4718 | } |
| 4719 | |
| 4720 | return 1; |
| 4721 | } |
| 4722 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4723 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4724 | * yield the LUA process, and resume it without checking the |
| 4725 | * input arguments. |
| 4726 | */ |
| 4727 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4728 | { |
| 4729 | MAY_LJMP(check_args(L, 2, "send")); |
| 4730 | lua_pushinteger(L, 0); |
| 4731 | |
| 4732 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4733 | } |
| 4734 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4735 | /* |
| 4736 | * |
| 4737 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4738 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4739 | * |
| 4740 | * |
| 4741 | */ |
| 4742 | |
| 4743 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4744 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4745 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4746 | __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] | 4747 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4748 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4749 | } |
| 4750 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4751 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4752 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4753 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4754 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4755 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4756 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4757 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4758 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4759 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4760 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4761 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4762 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4763 | struct htx *htx; |
| 4764 | struct htx_blk *blk; |
| 4765 | struct htx_sl *sl; |
| 4766 | struct ist path; |
| 4767 | unsigned long long len = 0; |
| 4768 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4769 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4770 | |
| 4771 | /* Check stack size. */ |
| 4772 | if (!lua_checkstack(L, 3)) |
| 4773 | return 0; |
| 4774 | |
| 4775 | /* Create the object: obj[0] = userdata. |
| 4776 | * Note that the base of the Converters object is the |
| 4777 | * same than the TXN object. |
| 4778 | */ |
| 4779 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4780 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4781 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4782 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4783 | http_ctx->status = 200; /* Default status code returned. */ |
| 4784 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4785 | luactx->htxn.s = s; |
| 4786 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4787 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4788 | /* Create the "f" field that contains a list of fetches. */ |
| 4789 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4790 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | return 0; |
| 4792 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4793 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4794 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4795 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4796 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | return 0; |
| 4798 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4799 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4800 | /* Create the "c" field that contains a list of converters. */ |
| 4801 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4802 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | return 0; |
| 4804 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4805 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4806 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4807 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4808 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4809 | return 0; |
| 4810 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4811 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4812 | htx = htxbuf(&s->req.buf); |
| 4813 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4814 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4815 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4816 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4817 | /* Stores the request method. */ |
| 4818 | lua_pushstring(L, "method"); |
| 4819 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4820 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4821 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4822 | /* Stores the http version. */ |
| 4823 | lua_pushstring(L, "version"); |
| 4824 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4825 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4826 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4827 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4828 | * the array on the top of the stack. |
| 4829 | */ |
| 4830 | lua_pushstring(L, "headers"); |
| 4831 | htxn.s = s; |
| 4832 | htxn.p = px; |
| 4833 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4834 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4835 | return 0; |
| 4836 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4837 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4838 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4839 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4840 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4841 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4842 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4843 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4844 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4845 | q = p; |
| 4846 | while (q < end && *q != '?') |
| 4847 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4848 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4849 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4850 | lua_pushstring(L, "path"); |
| 4851 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4852 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4853 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4854 | /* Stores the query string. */ |
| 4855 | lua_pushstring(L, "qs"); |
| 4856 | if (*q == '?') |
| 4857 | q++; |
| 4858 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4859 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4860 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4861 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4862 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4863 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4864 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4865 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4866 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4867 | break; |
| 4868 | if (type == HTX_BLK_DATA) |
| 4869 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4870 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4871 | if (htx->extra != ULLONG_MAX) |
| 4872 | len += htx->extra; |
| 4873 | |
| 4874 | /* Stores the request path. */ |
| 4875 | lua_pushstring(L, "length"); |
| 4876 | lua_pushinteger(L, len); |
| 4877 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4878 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4879 | /* Create an empty array of HTTP request headers. */ |
| 4880 | lua_pushstring(L, "response"); |
| 4881 | lua_newtable(L); |
| 4882 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4884 | /* Pop a class stream metatable and affect it to the table. */ |
| 4885 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4886 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4887 | |
| 4888 | return 1; |
| 4889 | } |
| 4890 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4891 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4892 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4893 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4894 | struct stream *s; |
| 4895 | const char *name; |
| 4896 | size_t len; |
| 4897 | struct sample smp; |
| 4898 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4899 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4900 | 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] | 4901 | |
| 4902 | /* It is useles to retrieve the stream, but this function |
| 4903 | * runs only in a stream context. |
| 4904 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4905 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4906 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4907 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4908 | |
| 4909 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4910 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4911 | hlua_lua2smp(L, 3, &smp); |
| 4912 | |
| 4913 | /* Store the sample in a variable. */ |
| 4914 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4915 | |
| 4916 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4917 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4918 | else |
| 4919 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4920 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 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_unset_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, "unset_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 | /* Unset the variable. */ |
| 4942 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4943 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4944 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4945 | } |
| 4946 | |
| 4947 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4948 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4949 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4950 | struct stream *s; |
| 4951 | const char *name; |
| 4952 | size_t len; |
| 4953 | struct sample smp; |
| 4954 | |
| 4955 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4956 | |
| 4957 | /* It is useles to retrieve the stream, but this function |
| 4958 | * runs only in a stream context. |
| 4959 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4960 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4961 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4962 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4963 | |
| 4964 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4965 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4966 | lua_pushnil(L); |
| 4967 | return 1; |
| 4968 | } |
| 4969 | |
| 4970 | return hlua_smp2lua(L, &smp); |
| 4971 | } |
| 4972 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4973 | __LJMP static int hlua_applet_http_set_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 | return 0; |
| 4982 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4983 | |
| 4984 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4985 | |
| 4986 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4987 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4988 | |
| 4989 | /* Get and store new value. */ |
| 4990 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4991 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4992 | |
| 4993 | return 0; |
| 4994 | } |
| 4995 | |
| 4996 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 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)); |
| 4999 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 5000 | struct hlua *hlua; |
| 5001 | |
| 5002 | /* 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] | 5003 | if (!s->hlua) { |
| 5004 | lua_pushnil(L); |
| 5005 | return 1; |
| 5006 | } |
| 5007 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 5008 | |
| 5009 | /* Push configuration index in the stack. */ |
| 5010 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5011 | |
| 5012 | return 1; |
| 5013 | } |
| 5014 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5015 | /* If expected data not yet available, it returns a yield. This function |
| 5016 | * consumes the data in the buffer. It returns a string containing the |
| 5017 | * data. This string can be empty. |
| 5018 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5019 | __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] | 5020 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5021 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5022 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5023 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5024 | struct htx *htx; |
| 5025 | struct htx_blk *blk; |
| 5026 | size_t count; |
| 5027 | int stop = 0; |
| 5028 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5029 | htx = htx_from_buf(&req->buf); |
| 5030 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5031 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5032 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5033 | while (count && !stop && blk) { |
| 5034 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5035 | uint32_t sz = htx_get_blksz(blk); |
| 5036 | struct ist v; |
| 5037 | uint32_t vlen; |
| 5038 | char *nl; |
| 5039 | |
| 5040 | vlen = sz; |
| 5041 | if (vlen > count) { |
| 5042 | if (type != HTX_BLK_DATA) |
| 5043 | break; |
| 5044 | vlen = count; |
| 5045 | } |
| 5046 | |
| 5047 | switch (type) { |
| 5048 | case HTX_BLK_UNUSED: |
| 5049 | break; |
| 5050 | |
| 5051 | case HTX_BLK_DATA: |
| 5052 | v = htx_get_blk_value(htx, blk); |
| 5053 | v.len = vlen; |
| 5054 | nl = istchr(v, '\n'); |
| 5055 | if (nl != NULL) { |
| 5056 | stop = 1; |
| 5057 | vlen = nl - v.ptr + 1; |
| 5058 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5059 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5060 | break; |
| 5061 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5062 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5063 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5064 | stop = 1; |
| 5065 | break; |
| 5066 | |
| 5067 | default: |
| 5068 | break; |
| 5069 | } |
| 5070 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5071 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5072 | count -= vlen; |
| 5073 | if (sz == vlen) |
| 5074 | blk = htx_remove_blk(htx, blk); |
| 5075 | else { |
| 5076 | htx_cut_data_blk(htx, blk, vlen); |
| 5077 | break; |
| 5078 | } |
| 5079 | } |
| 5080 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5081 | /* The message was fully consumed and no more data are expected |
| 5082 | * (EOM flag set). |
| 5083 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5084 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5085 | stop = 1; |
| 5086 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5087 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5088 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5089 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5090 | 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] | 5091 | } |
| 5092 | |
| 5093 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5094 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5095 | return 1; |
| 5096 | } |
| 5097 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5098 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5099 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5100 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5101 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5102 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5103 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5104 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5105 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5106 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5107 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5108 | } |
| 5109 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5110 | /* If expected data not yet available, it returns a yield. This function |
| 5111 | * consumes the data in the buffer. It returns a string containing the |
| 5112 | * data. This string can be empty. |
| 5113 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5114 | __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] | 5115 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5116 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5117 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5118 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5119 | struct htx *htx; |
| 5120 | struct htx_blk *blk; |
| 5121 | size_t count; |
| 5122 | int len; |
| 5123 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5124 | htx = htx_from_buf(&req->buf); |
| 5125 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5126 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5127 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5128 | while (count && len && blk) { |
| 5129 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5130 | uint32_t sz = htx_get_blksz(blk); |
| 5131 | struct ist v; |
| 5132 | uint32_t vlen; |
| 5133 | |
| 5134 | vlen = sz; |
| 5135 | if (len > 0 && vlen > len) |
| 5136 | vlen = len; |
| 5137 | if (vlen > count) { |
| 5138 | if (type != HTX_BLK_DATA) |
| 5139 | break; |
| 5140 | vlen = count; |
| 5141 | } |
| 5142 | |
| 5143 | switch (type) { |
| 5144 | case HTX_BLK_UNUSED: |
| 5145 | break; |
| 5146 | |
| 5147 | case HTX_BLK_DATA: |
| 5148 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5149 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5150 | break; |
| 5151 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5152 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5153 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5154 | len = 0; |
| 5155 | break; |
| 5156 | |
| 5157 | default: |
| 5158 | break; |
| 5159 | } |
| 5160 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5161 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5162 | count -= vlen; |
| 5163 | if (len > 0) |
| 5164 | len -= vlen; |
| 5165 | if (sz == vlen) |
| 5166 | blk = htx_remove_blk(htx, blk); |
| 5167 | else { |
| 5168 | htx_cut_data_blk(htx, blk, vlen); |
| 5169 | break; |
| 5170 | } |
| 5171 | } |
| 5172 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5173 | /* The message was fully consumed and no more data are expected |
| 5174 | * (EOM flag set). |
| 5175 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5176 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5177 | len = 0; |
| 5178 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5179 | htx_to_buf(htx, &req->buf); |
| 5180 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5181 | /* If we are no other data available, yield waiting for new data. */ |
| 5182 | if (len) { |
| 5183 | if (len > 0) { |
| 5184 | lua_pushinteger(L, len); |
| 5185 | lua_replace(L, 2); |
| 5186 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5187 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5188 | 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] | 5189 | } |
| 5190 | |
| 5191 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5192 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5193 | return 1; |
| 5194 | } |
| 5195 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5196 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5197 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5198 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5199 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5200 | int len = -1; |
| 5201 | |
| 5202 | /* Check arguments. */ |
| 5203 | if (lua_gettop(L) > 2) |
| 5204 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5205 | if (lua_gettop(L) >= 2) { |
| 5206 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5207 | lua_pop(L, 1); |
| 5208 | } |
| 5209 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5210 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5211 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5212 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5213 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5214 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5215 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5216 | } |
| 5217 | |
| 5218 | /* Append data in the output side of the buffer. This data is immediately |
| 5219 | * sent. The function returns the amount of data written. If the buffer |
| 5220 | * cannot contain the data, the function yields. The function returns -1 |
| 5221 | * if the channel is closed. |
| 5222 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5223 | __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] | 5224 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5225 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5226 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5227 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5228 | struct htx *htx = htx_from_buf(&res->buf); |
| 5229 | const char *data; |
| 5230 | size_t len; |
| 5231 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5232 | int max; |
| 5233 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5234 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5235 | if (!max) |
| 5236 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5237 | |
| 5238 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5239 | |
| 5240 | /* Get the max amount of data which can write as input in the channel. */ |
| 5241 | if (max > (len - l)) |
| 5242 | max = len - l; |
| 5243 | |
| 5244 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5245 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5246 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5247 | |
| 5248 | /* update counters. */ |
| 5249 | l += max; |
| 5250 | lua_pop(L, 1); |
| 5251 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5252 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5253 | /* If some data is not send, declares the situation to the |
| 5254 | * applet, and returns a yield. |
| 5255 | */ |
| 5256 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5257 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5258 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5259 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5260 | 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] | 5261 | } |
| 5262 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5263 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5264 | return 1; |
| 5265 | } |
| 5266 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5267 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5268 | * yield the LUA process, and resume it without checking the |
| 5269 | * input arguments. |
| 5270 | */ |
| 5271 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5272 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5273 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5274 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5275 | |
| 5276 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5277 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5278 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5279 | WILL_LJMP(lua_error(L)); |
| 5280 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5281 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5282 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5283 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5284 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5285 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5286 | } |
| 5287 | |
| 5288 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5289 | { |
| 5290 | const char *name; |
| 5291 | int ret; |
| 5292 | |
| 5293 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5294 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5295 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5296 | |
| 5297 | /* Push in the stack the "response" entry. */ |
| 5298 | ret = lua_getfield(L, 1, "response"); |
| 5299 | if (ret != LUA_TTABLE) { |
| 5300 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5301 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5302 | WILL_LJMP(lua_error(L)); |
| 5303 | } |
| 5304 | |
| 5305 | /* check if the header is already registered if it is not |
| 5306 | * the case, register it. |
| 5307 | */ |
| 5308 | ret = lua_getfield(L, -1, name); |
| 5309 | if (ret == LUA_TNIL) { |
| 5310 | |
| 5311 | /* Entry not found. */ |
| 5312 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5313 | |
| 5314 | /* Insert the new header name in the array in the top of the stack. |
| 5315 | * It left the new array in the top of the stack. |
| 5316 | */ |
| 5317 | lua_newtable(L); |
| 5318 | lua_pushvalue(L, 2); |
| 5319 | lua_pushvalue(L, -2); |
| 5320 | lua_settable(L, -4); |
| 5321 | |
| 5322 | } else if (ret != LUA_TTABLE) { |
| 5323 | |
| 5324 | /* corruption error. */ |
| 5325 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5326 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5327 | WILL_LJMP(lua_error(L)); |
| 5328 | } |
| 5329 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5330 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5331 | * the header value as new entry. |
| 5332 | */ |
| 5333 | lua_pushvalue(L, 3); |
| 5334 | ret = lua_rawlen(L, -2); |
| 5335 | lua_rawseti(L, -2, ret + 1); |
| 5336 | lua_pushboolean(L, 1); |
| 5337 | return 1; |
| 5338 | } |
| 5339 | |
| 5340 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5341 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5342 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5343 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5344 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5345 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5346 | |
| 5347 | if (status < 100 || status > 599) { |
| 5348 | lua_pushboolean(L, 0); |
| 5349 | return 1; |
| 5350 | } |
| 5351 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5352 | http_ctx->status = status; |
| 5353 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5354 | lua_pushboolean(L, 1); |
| 5355 | return 1; |
| 5356 | } |
| 5357 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5358 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5359 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5360 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5361 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5362 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5363 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5364 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5365 | struct htx *htx; |
| 5366 | struct htx_sl *sl; |
| 5367 | struct h1m h1m; |
| 5368 | const char *status, *reason; |
| 5369 | const char *name, *value; |
| 5370 | size_t nlen, vlen; |
| 5371 | unsigned int flags; |
| 5372 | |
| 5373 | /* Send the message at once. */ |
| 5374 | htx = htx_from_buf(&res->buf); |
| 5375 | h1m_init_res(&h1m); |
| 5376 | |
| 5377 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5378 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5379 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5380 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5381 | reason = http_get_reason(http_ctx->status); |
| 5382 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5383 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5384 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5385 | } |
| 5386 | else { |
| 5387 | flags = HTX_SL_F_IS_RESP; |
| 5388 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5389 | } |
| 5390 | if (!sl) { |
| 5391 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5392 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5393 | WILL_LJMP(lua_error(L)); |
| 5394 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5395 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5396 | |
| 5397 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5398 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5399 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5400 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5401 | WILL_LJMP(lua_error(L)); |
| 5402 | } |
| 5403 | |
| 5404 | /* Browse the list of headers. */ |
| 5405 | lua_pushnil(L); |
| 5406 | while(lua_next(L, -2) != 0) { |
| 5407 | /* We expect a string as -2. */ |
| 5408 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5409 | 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] | 5410 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5411 | lua_typename(L, lua_type(L, -2))); |
| 5412 | WILL_LJMP(lua_error(L)); |
| 5413 | } |
| 5414 | name = lua_tolstring(L, -2, &nlen); |
| 5415 | |
| 5416 | /* We expect an array as -1. */ |
| 5417 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5418 | 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] | 5419 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5420 | name, |
| 5421 | lua_typename(L, lua_type(L, -1))); |
| 5422 | WILL_LJMP(lua_error(L)); |
| 5423 | } |
| 5424 | |
| 5425 | /* Browse the table who is on the top of the stack. */ |
| 5426 | lua_pushnil(L); |
| 5427 | while(lua_next(L, -2) != 0) { |
| 5428 | int id; |
| 5429 | |
| 5430 | /* We expect a number as -2. */ |
| 5431 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5432 | 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] | 5433 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5434 | name, |
| 5435 | lua_typename(L, lua_type(L, -2))); |
| 5436 | WILL_LJMP(lua_error(L)); |
| 5437 | } |
| 5438 | id = lua_tointeger(L, -2); |
| 5439 | |
| 5440 | /* We expect a string as -2. */ |
| 5441 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5442 | 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] | 5443 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5444 | name, id, |
| 5445 | lua_typename(L, lua_type(L, -1))); |
| 5446 | WILL_LJMP(lua_error(L)); |
| 5447 | } |
| 5448 | value = lua_tolstring(L, -1, &vlen); |
| 5449 | |
| 5450 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5451 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5452 | int ret; |
| 5453 | |
| 5454 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5455 | if (ret < 0) { |
| 5456 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5457 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5458 | name); |
| 5459 | WILL_LJMP(lua_error(L)); |
| 5460 | } |
| 5461 | else if (ret == 0) |
| 5462 | goto next; /* Skip it */ |
| 5463 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5464 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5465 | struct ist v = ist2(value, vlen); |
| 5466 | int ret; |
| 5467 | |
| 5468 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5469 | if (ret < 0) { |
| 5470 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5471 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5472 | name); |
| 5473 | WILL_LJMP(lua_error(L)); |
| 5474 | } |
| 5475 | else if (ret == 0) |
| 5476 | goto next; /* Skip it */ |
| 5477 | } |
| 5478 | |
| 5479 | /* Add a new header */ |
| 5480 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5481 | 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] | 5482 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5483 | name); |
| 5484 | WILL_LJMP(lua_error(L)); |
| 5485 | } |
| 5486 | next: |
| 5487 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5488 | lua_pop(L, 1); |
| 5489 | } |
| 5490 | |
| 5491 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5492 | lua_pop(L, 1); |
| 5493 | } |
| 5494 | |
| 5495 | if (h1m.flags & H1_MF_CHNK) |
| 5496 | h1m.flags &= ~H1_MF_CLEN; |
| 5497 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5498 | h1m.flags |= H1_MF_XFER_LEN; |
| 5499 | |
| 5500 | /* Uset HTX start-line flags */ |
| 5501 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5502 | flags |= HTX_SL_F_XFER_ENC; |
| 5503 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5504 | flags |= HTX_SL_F_XFER_LEN; |
| 5505 | if (h1m.flags & H1_MF_CHNK) |
| 5506 | flags |= HTX_SL_F_CHNK; |
| 5507 | else if (h1m.flags & H1_MF_CLEN) |
| 5508 | flags |= HTX_SL_F_CLEN; |
| 5509 | if (h1m.body_len == 0) |
| 5510 | flags |= HTX_SL_F_BODYLESS; |
| 5511 | } |
| 5512 | sl->flags |= flags; |
| 5513 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5514 | /* 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] | 5515 | * and the status code implies the presence of a message body, we must |
| 5516 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5517 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5518 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5519 | */ |
| 5520 | 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] | 5521 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5522 | /* Add a new header */ |
| 5523 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5524 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5525 | 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] | 5526 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5527 | WILL_LJMP(lua_error(L)); |
| 5528 | } |
| 5529 | } |
| 5530 | |
| 5531 | /* Finalize headers. */ |
| 5532 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5533 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5534 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5535 | WILL_LJMP(lua_error(L)); |
| 5536 | } |
| 5537 | |
| 5538 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5539 | b_reset(&res->buf); |
| 5540 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5541 | WILL_LJMP(lua_error(L)); |
| 5542 | } |
| 5543 | |
| 5544 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5545 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5546 | |
| 5547 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5548 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5549 | return 0; |
| 5550 | |
| 5551 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5552 | /* We will build the status line and the headers of the HTTP response. |
| 5553 | * We will try send at once if its not possible, we give back the hand |
| 5554 | * waiting for more room. |
| 5555 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5556 | __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] | 5557 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5558 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5559 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5560 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5561 | |
| 5562 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5563 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5564 | 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] | 5565 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5566 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5567 | } |
| 5568 | |
| 5569 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5570 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5571 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5572 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5573 | } |
| 5574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5575 | /* |
| 5576 | * |
| 5577 | * |
| 5578 | * Class HTTP |
| 5579 | * |
| 5580 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5581 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5582 | |
| 5583 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5584 | * a class stream, otherwise it throws an error. |
| 5585 | */ |
| 5586 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5587 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5588 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5589 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5590 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5591 | /* This function creates and push in the stack a HTTP object |
| 5592 | * according with a current TXN. |
| 5593 | */ |
| 5594 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5595 | { |
| 5596 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5597 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5598 | /* Check stack size. */ |
| 5599 | if (!lua_checkstack(L, 3)) |
| 5600 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5601 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5602 | /* Create the object: obj[0] = userdata. |
| 5603 | * Note that the base of the Converters object is the |
| 5604 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5605 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5606 | lua_newtable(L); |
| 5607 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5608 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5609 | |
| 5610 | htxn->s = txn->s; |
| 5611 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5612 | htxn->dir = txn->dir; |
| 5613 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5614 | |
| 5615 | /* Pop a class stream metatable and affect it to the table. */ |
| 5616 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5617 | lua_setmetatable(L, -2); |
| 5618 | |
| 5619 | return 1; |
| 5620 | } |
| 5621 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5622 | /* This function creates and returns an array containing the status-line |
| 5623 | * elements. This function does not fails. |
| 5624 | */ |
| 5625 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5626 | { |
| 5627 | /* Create the table. */ |
| 5628 | lua_newtable(L); |
| 5629 | |
| 5630 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5631 | lua_pushstring(L, "version"); |
| 5632 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5633 | lua_settable(L, -3); |
| 5634 | lua_pushstring(L, "code"); |
| 5635 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5636 | lua_settable(L, -3); |
| 5637 | lua_pushstring(L, "reason"); |
| 5638 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5639 | lua_settable(L, -3); |
| 5640 | } |
| 5641 | else { |
| 5642 | lua_pushstring(L, "method"); |
| 5643 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5644 | lua_settable(L, -3); |
| 5645 | lua_pushstring(L, "uri"); |
| 5646 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5647 | lua_settable(L, -3); |
| 5648 | lua_pushstring(L, "version"); |
| 5649 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5650 | lua_settable(L, -3); |
| 5651 | } |
| 5652 | return 1; |
| 5653 | } |
| 5654 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5655 | /* This function creates ans returns an array of HTTP headers. |
| 5656 | * This function does not fails. It is used as wrapper with the |
| 5657 | * 2 following functions. |
| 5658 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5659 | __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] | 5660 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5661 | struct htx *htx; |
| 5662 | int32_t pos; |
| 5663 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5664 | /* Create the table. */ |
| 5665 | lua_newtable(L); |
| 5666 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5667 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5668 | htx = htxbuf(&msg->chn->buf); |
| 5669 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5670 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5671 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5672 | struct ist n, v; |
| 5673 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5674 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5675 | if (type == HTX_BLK_HDR) { |
| 5676 | n = htx_get_blk_name(htx,blk); |
| 5677 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5678 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5679 | else if (type == HTX_BLK_EOH) |
| 5680 | break; |
| 5681 | else |
| 5682 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5683 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5684 | /* Check for existing entry: |
| 5685 | * assume that the table is on the top of the stack, and |
| 5686 | * push the key in the stack, the function lua_gettable() |
| 5687 | * perform the lookup. |
| 5688 | */ |
| 5689 | lua_pushlstring(L, n.ptr, n.len); |
| 5690 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5691 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5692 | switch (lua_type(L, -1)) { |
| 5693 | case LUA_TNIL: |
| 5694 | /* Table not found, create it. */ |
| 5695 | lua_pop(L, 1); /* remove the nil value. */ |
| 5696 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5697 | lua_newtable(L); /* create and push empty table. */ |
| 5698 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5699 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5700 | 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] | 5701 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5702 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5703 | case LUA_TTABLE: |
| 5704 | /* Entry found: push the value in the table. */ |
| 5705 | len = lua_rawlen(L, -1); |
| 5706 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5707 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5708 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5709 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5710 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5711 | default: |
| 5712 | /* Other cases are errors. */ |
| 5713 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5714 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5715 | } |
| 5716 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5717 | return 1; |
| 5718 | } |
| 5719 | |
| 5720 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5721 | { |
| 5722 | struct hlua_txn *htxn; |
| 5723 | |
| 5724 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5725 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5726 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5727 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5728 | WILL_LJMP(lua_error(L)); |
| 5729 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5730 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5731 | } |
| 5732 | |
| 5733 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5734 | { |
| 5735 | struct hlua_txn *htxn; |
| 5736 | |
| 5737 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5738 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5739 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5740 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5741 | WILL_LJMP(lua_error(L)); |
| 5742 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5743 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5744 | } |
| 5745 | |
| 5746 | /* This function replace full header, or just a value in |
| 5747 | * the request or in the response. It is a wrapper fir the |
| 5748 | * 4 following functions. |
| 5749 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5750 | __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] | 5751 | { |
| 5752 | size_t name_len; |
| 5753 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5754 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5755 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5756 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5757 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5758 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5759 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5760 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5761 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5762 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5763 | 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] | 5764 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5765 | return 0; |
| 5766 | } |
| 5767 | |
| 5768 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5769 | { |
| 5770 | struct hlua_txn *htxn; |
| 5771 | |
| 5772 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5773 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5774 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5775 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5776 | WILL_LJMP(lua_error(L)); |
| 5777 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5778 | 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] | 5779 | } |
| 5780 | |
| 5781 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5782 | { |
| 5783 | struct hlua_txn *htxn; |
| 5784 | |
| 5785 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5786 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5787 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5788 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5789 | WILL_LJMP(lua_error(L)); |
| 5790 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5791 | 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] | 5792 | } |
| 5793 | |
| 5794 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5795 | { |
| 5796 | struct hlua_txn *htxn; |
| 5797 | |
| 5798 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5799 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5800 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5801 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5802 | WILL_LJMP(lua_error(L)); |
| 5803 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5804 | 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] | 5805 | } |
| 5806 | |
| 5807 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5808 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5809 | struct hlua_txn *htxn; |
| 5810 | |
| 5811 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5812 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5813 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5814 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5815 | WILL_LJMP(lua_error(L)); |
| 5816 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5817 | 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] | 5818 | } |
| 5819 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5820 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5821 | * It is a wrapper for the 2 following functions. |
| 5822 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5823 | __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] | 5824 | { |
| 5825 | size_t len; |
| 5826 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5827 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5828 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5829 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5830 | ctx.blk = NULL; |
| 5831 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5832 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5833 | return 0; |
| 5834 | } |
| 5835 | |
| 5836 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5837 | { |
| 5838 | struct hlua_txn *htxn; |
| 5839 | |
| 5840 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5841 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5842 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5843 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5844 | WILL_LJMP(lua_error(L)); |
| 5845 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5846 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5847 | } |
| 5848 | |
| 5849 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5850 | { |
| 5851 | struct hlua_txn *htxn; |
| 5852 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5853 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5854 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5855 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5856 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5857 | WILL_LJMP(lua_error(L)); |
| 5858 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5859 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5860 | } |
| 5861 | |
| 5862 | /* This function adds an header. It is a wrapper used by |
| 5863 | * the 2 following functions. |
| 5864 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5865 | __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] | 5866 | { |
| 5867 | size_t name_len; |
| 5868 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5869 | size_t value_len; |
| 5870 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5871 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5872 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5873 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5874 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5875 | return 0; |
| 5876 | } |
| 5877 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5878 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5879 | { |
| 5880 | struct hlua_txn *htxn; |
| 5881 | |
| 5882 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5883 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5884 | |
| 5885 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5886 | WILL_LJMP(lua_error(L)); |
| 5887 | |
| 5888 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5889 | } |
| 5890 | |
| 5891 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5892 | { |
| 5893 | struct hlua_txn *htxn; |
| 5894 | |
| 5895 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5896 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5897 | |
| 5898 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5899 | WILL_LJMP(lua_error(L)); |
| 5900 | |
| 5901 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5902 | } |
| 5903 | |
| 5904 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5905 | { |
| 5906 | struct hlua_txn *htxn; |
| 5907 | |
| 5908 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5909 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5910 | |
| 5911 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5912 | WILL_LJMP(lua_error(L)); |
| 5913 | |
| 5914 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5915 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5916 | } |
| 5917 | |
| 5918 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5919 | { |
| 5920 | struct hlua_txn *htxn; |
| 5921 | |
| 5922 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5923 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5924 | |
| 5925 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5926 | WILL_LJMP(lua_error(L)); |
| 5927 | |
| 5928 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5929 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5930 | } |
| 5931 | |
| 5932 | /* This function set the method. */ |
| 5933 | static int hlua_http_req_set_meth(lua_State *L) |
| 5934 | { |
| 5935 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5936 | size_t name_len; |
| 5937 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5938 | |
| 5939 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5940 | WILL_LJMP(lua_error(L)); |
| 5941 | |
| 5942 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5943 | return 1; |
| 5944 | } |
| 5945 | |
| 5946 | /* This function set the method. */ |
| 5947 | static int hlua_http_req_set_path(lua_State *L) |
| 5948 | { |
| 5949 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5950 | size_t name_len; |
| 5951 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5952 | |
| 5953 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5954 | WILL_LJMP(lua_error(L)); |
| 5955 | |
| 5956 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5957 | return 1; |
| 5958 | } |
| 5959 | |
| 5960 | /* This function set the query-string. */ |
| 5961 | static int hlua_http_req_set_query(lua_State *L) |
| 5962 | { |
| 5963 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5964 | size_t name_len; |
| 5965 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5966 | |
| 5967 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5968 | WILL_LJMP(lua_error(L)); |
| 5969 | |
| 5970 | /* Check length. */ |
| 5971 | if (name_len > trash.size - 1) { |
| 5972 | lua_pushboolean(L, 0); |
| 5973 | return 1; |
| 5974 | } |
| 5975 | |
| 5976 | /* Add the mark question as prefix. */ |
| 5977 | chunk_reset(&trash); |
| 5978 | trash.area[trash.data++] = '?'; |
| 5979 | memcpy(trash.area + trash.data, name, name_len); |
| 5980 | trash.data += name_len; |
| 5981 | |
| 5982 | lua_pushboolean(L, |
| 5983 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5984 | return 1; |
| 5985 | } |
| 5986 | |
| 5987 | /* This function set the uri. */ |
| 5988 | static int hlua_http_req_set_uri(lua_State *L) |
| 5989 | { |
| 5990 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5991 | size_t name_len; |
| 5992 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5993 | |
| 5994 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5995 | WILL_LJMP(lua_error(L)); |
| 5996 | |
| 5997 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5998 | return 1; |
| 5999 | } |
| 6000 | |
| 6001 | /* This function set the response code & optionally reason. */ |
| 6002 | static int hlua_http_res_set_status(lua_State *L) |
| 6003 | { |
| 6004 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 6005 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6006 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 6007 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 6008 | |
| 6009 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 6010 | WILL_LJMP(lua_error(L)); |
| 6011 | |
| 6012 | http_res_set_status(code, reason, htxn->s); |
| 6013 | return 0; |
| 6014 | } |
| 6015 | |
| 6016 | /* |
| 6017 | * |
| 6018 | * |
| 6019 | * Class HTTPMessage |
| 6020 | * |
| 6021 | * |
| 6022 | */ |
| 6023 | |
| 6024 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6025 | * otherwise it throws an error. |
| 6026 | */ |
| 6027 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6028 | { |
| 6029 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6030 | } |
| 6031 | |
| 6032 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6033 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6034 | 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] | 6035 | { |
| 6036 | /* Check stack size. */ |
| 6037 | if (!lua_checkstack(L, 3)) |
| 6038 | return 0; |
| 6039 | |
| 6040 | lua_newtable(L); |
| 6041 | lua_pushlightuserdata(L, msg); |
| 6042 | lua_rawseti(L, -2, 0); |
| 6043 | |
| 6044 | /* Create the "channel" field that contains the request channel object. */ |
| 6045 | lua_pushstring(L, "channel"); |
| 6046 | if (!hlua_channel_new(L, msg->chn)) |
| 6047 | return 0; |
| 6048 | lua_rawset(L, -3); |
| 6049 | |
| 6050 | /* Pop a class stream metatable and affect it to the table. */ |
| 6051 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6052 | lua_setmetatable(L, -2); |
| 6053 | |
| 6054 | return 1; |
| 6055 | } |
| 6056 | |
| 6057 | /* Helper function returning a filter attached to the HTTP message at the |
| 6058 | * 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] | 6059 | * 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] | 6060 | * filled with output and input length respectively. |
| 6061 | */ |
| 6062 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6063 | { |
| 6064 | struct channel *chn = msg->chn; |
| 6065 | struct htx *htx = htxbuf(&chn->buf); |
| 6066 | struct filter *filter = NULL; |
| 6067 | |
| 6068 | *offset = co_data(msg->chn); |
| 6069 | *len = htx->data - co_data(msg->chn); |
| 6070 | |
| 6071 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6072 | filter = lua_touserdata (L, -1); |
| 6073 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6074 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6075 | |
| 6076 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6077 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6078 | } |
| 6079 | } |
| 6080 | |
| 6081 | lua_pop(L, 1); |
| 6082 | return filter; |
| 6083 | } |
| 6084 | |
| 6085 | /* Returns true if the channel attached to the HTTP message is the response |
| 6086 | * channel. |
| 6087 | */ |
| 6088 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6089 | { |
| 6090 | struct http_msg *msg; |
| 6091 | |
| 6092 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6093 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6094 | |
| 6095 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6096 | return 1; |
| 6097 | } |
| 6098 | |
| 6099 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6100 | * on hlua_http_get_stline(). |
| 6101 | */ |
| 6102 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6103 | { |
| 6104 | struct http_msg *msg; |
| 6105 | struct htx *htx; |
| 6106 | struct htx_sl *sl; |
| 6107 | |
| 6108 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6109 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6110 | |
| 6111 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6112 | WILL_LJMP(lua_error(L)); |
| 6113 | |
| 6114 | htx = htxbuf(&msg->chn->buf); |
| 6115 | sl = http_get_stline(htx); |
| 6116 | if (!sl) |
| 6117 | return 0; |
| 6118 | return hlua_http_get_stline(L, sl); |
| 6119 | } |
| 6120 | |
| 6121 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6122 | * hlua_http_get_headers(). |
| 6123 | */ |
| 6124 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6125 | { |
| 6126 | struct http_msg *msg; |
| 6127 | |
| 6128 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6129 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6130 | |
| 6131 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6132 | WILL_LJMP(lua_error(L)); |
| 6133 | |
| 6134 | return hlua_http_get_headers(L, msg); |
| 6135 | } |
| 6136 | |
| 6137 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6138 | * name. It relies on hlua_http_del_hdr(). |
| 6139 | */ |
| 6140 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6141 | { |
| 6142 | struct http_msg *msg; |
| 6143 | |
| 6144 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 6145 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6146 | |
| 6147 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6148 | WILL_LJMP(lua_error(L)); |
| 6149 | |
| 6150 | return hlua_http_del_hdr(L, msg); |
| 6151 | } |
| 6152 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6153 | /* 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] | 6154 | * message given its name against a regex and replaces it if it matches. It |
| 6155 | * relies on hlua_http_rep_hdr(). |
| 6156 | */ |
| 6157 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6158 | { |
| 6159 | struct http_msg *msg; |
| 6160 | |
| 6161 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6162 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6163 | |
| 6164 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6165 | WILL_LJMP(lua_error(L)); |
| 6166 | |
| 6167 | return hlua_http_rep_hdr(L, msg, 1); |
| 6168 | } |
| 6169 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6170 | /* 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] | 6171 | * message given its name against a regex and replaces it if it matches. It |
| 6172 | * relies on hlua_http_rep_hdr(). |
| 6173 | */ |
| 6174 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6175 | { |
| 6176 | struct http_msg *msg; |
| 6177 | |
| 6178 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6179 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6180 | |
| 6181 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6182 | WILL_LJMP(lua_error(L)); |
| 6183 | |
| 6184 | return hlua_http_rep_hdr(L, msg, 0); |
| 6185 | } |
| 6186 | |
| 6187 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6188 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6189 | { |
| 6190 | struct http_msg *msg; |
| 6191 | |
| 6192 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6193 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6194 | |
| 6195 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6196 | WILL_LJMP(lua_error(L)); |
| 6197 | |
| 6198 | return hlua_http_add_hdr(L, msg); |
| 6199 | } |
| 6200 | |
| 6201 | /* Add an header in the HTTP message removing existing headers with the same |
| 6202 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6203 | */ |
| 6204 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6205 | { |
| 6206 | struct http_msg *msg; |
| 6207 | |
| 6208 | MAY_LJMP(check_args(L, 3, "set_header")); |
| 6209 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6210 | |
| 6211 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6212 | WILL_LJMP(lua_error(L)); |
| 6213 | |
| 6214 | hlua_http_del_hdr(L, msg); |
| 6215 | return hlua_http_add_hdr(L, msg); |
| 6216 | } |
| 6217 | |
| 6218 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6219 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6220 | { |
| 6221 | struct stream *s; |
| 6222 | struct http_msg *msg; |
| 6223 | const char *name; |
| 6224 | size_t name_len; |
| 6225 | |
| 6226 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6227 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6228 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6229 | |
| 6230 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6231 | WILL_LJMP(lua_error(L)); |
| 6232 | |
| 6233 | s = chn_strm(msg->chn); |
| 6234 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6235 | return 1; |
| 6236 | } |
| 6237 | |
| 6238 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6239 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6240 | { |
| 6241 | struct stream *s; |
| 6242 | struct http_msg *msg; |
| 6243 | const char *name; |
| 6244 | size_t name_len; |
| 6245 | |
| 6246 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6247 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6248 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6249 | |
| 6250 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6251 | WILL_LJMP(lua_error(L)); |
| 6252 | |
| 6253 | s = chn_strm(msg->chn); |
| 6254 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6255 | return 1; |
| 6256 | } |
| 6257 | |
| 6258 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6259 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6260 | { |
| 6261 | struct stream *s; |
| 6262 | struct http_msg *msg; |
| 6263 | const char *name; |
| 6264 | size_t name_len; |
| 6265 | |
| 6266 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6267 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6268 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6269 | |
| 6270 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6271 | WILL_LJMP(lua_error(L)); |
| 6272 | |
| 6273 | /* Check length. */ |
| 6274 | if (name_len > trash.size - 1) { |
| 6275 | lua_pushboolean(L, 0); |
| 6276 | return 1; |
| 6277 | } |
| 6278 | |
| 6279 | /* Add the mark question as prefix. */ |
| 6280 | chunk_reset(&trash); |
| 6281 | trash.area[trash.data++] = '?'; |
| 6282 | memcpy(trash.area + trash.data, name, name_len); |
| 6283 | trash.data += name_len; |
| 6284 | |
| 6285 | s = chn_strm(msg->chn); |
| 6286 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6287 | return 1; |
| 6288 | } |
| 6289 | |
| 6290 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6291 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6292 | { |
| 6293 | struct stream *s; |
| 6294 | struct http_msg *msg; |
| 6295 | const char *name; |
| 6296 | size_t name_len; |
| 6297 | |
| 6298 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6299 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6300 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6301 | |
| 6302 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6303 | WILL_LJMP(lua_error(L)); |
| 6304 | |
| 6305 | s = chn_strm(msg->chn); |
| 6306 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6307 | return 1; |
| 6308 | } |
| 6309 | |
| 6310 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6311 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6312 | { |
| 6313 | struct http_msg *msg; |
| 6314 | unsigned int code; |
| 6315 | const char *reason; |
| 6316 | size_t reason_len; |
| 6317 | |
| 6318 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6319 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6320 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6321 | |
| 6322 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6323 | WILL_LJMP(lua_error(L)); |
| 6324 | |
| 6325 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6326 | return 1; |
| 6327 | } |
| 6328 | |
| 6329 | /* Returns true if the HTTP message is full. */ |
| 6330 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6331 | { |
| 6332 | struct http_msg *msg; |
| 6333 | |
| 6334 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6335 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6336 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6337 | return 1; |
| 6338 | } |
| 6339 | |
| 6340 | /* Returns true if the HTTP message may still receive data. */ |
| 6341 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6342 | { |
| 6343 | struct http_msg *msg; |
| 6344 | struct htx *htx; |
| 6345 | |
| 6346 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6347 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6348 | htx = htxbuf(&msg->chn->buf); |
| 6349 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6350 | return 1; |
| 6351 | } |
| 6352 | |
| 6353 | /* Returns true if the HTTP message EOM was received */ |
| 6354 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6355 | { |
| 6356 | struct http_msg *msg; |
| 6357 | struct htx *htx; |
| 6358 | |
| 6359 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6360 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6361 | htx = htxbuf(&msg->chn->buf); |
| 6362 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6363 | return 1; |
| 6364 | } |
| 6365 | |
| 6366 | /* Returns the number of bytes available in the input side of the HTTP |
| 6367 | * message. This function never fails. |
| 6368 | */ |
| 6369 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6370 | { |
| 6371 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6372 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6373 | |
| 6374 | MAY_LJMP(check_args(L, 1, "input")); |
| 6375 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6376 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6377 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6378 | return 1; |
| 6379 | } |
| 6380 | |
| 6381 | /* Returns the number of bytes available in the output side of the HTTP |
| 6382 | * message. This function never fails. |
| 6383 | */ |
| 6384 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6385 | { |
| 6386 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6387 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6388 | |
| 6389 | MAY_LJMP(check_args(L, 1, "output")); |
| 6390 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6391 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6392 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6393 | return 1; |
| 6394 | } |
| 6395 | |
| 6396 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6397 | * 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] | 6398 | * 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] | 6399 | * block. This function is called during the payload filtering, so the headers |
| 6400 | * are already scheduled for output (from the filter point of view). |
| 6401 | */ |
| 6402 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6403 | { |
| 6404 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6405 | struct htx_blk *blk; |
| 6406 | struct htx_ret htxret; |
| 6407 | luaL_Buffer b; |
| 6408 | int ret = 0; |
| 6409 | |
| 6410 | luaL_buffinit(L, &b); |
| 6411 | htxret = htx_find_offset(htx, offset); |
| 6412 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6413 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6414 | struct ist v; |
| 6415 | |
| 6416 | switch (type) { |
| 6417 | case HTX_BLK_UNUSED: |
| 6418 | break; |
| 6419 | |
| 6420 | case HTX_BLK_DATA: |
| 6421 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6422 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6423 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6424 | |
| 6425 | luaL_addlstring(&b, v.ptr, v.len); |
| 6426 | ret += v.len; |
| 6427 | break; |
| 6428 | |
| 6429 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6430 | if (!ret) |
| 6431 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6432 | goto end; |
| 6433 | } |
| 6434 | offset = 0; |
| 6435 | } |
| 6436 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6437 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6438 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6439 | goto no_data; |
| 6440 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6441 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6442 | |
| 6443 | no_data: |
| 6444 | /* Remove the empty string and push nil on the stack */ |
| 6445 | lua_pop(L, 1); |
| 6446 | lua_pushnil(L); |
| 6447 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6448 | } |
| 6449 | |
| 6450 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6451 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6452 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6453 | * the filter context. |
| 6454 | */ |
| 6455 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6456 | { |
| 6457 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6458 | struct htx_ret htxret; |
| 6459 | int /*max, */ret = 0; |
| 6460 | |
| 6461 | /* Nothing to do, just return */ |
| 6462 | if (unlikely(istlen(str) == 0)) |
| 6463 | goto end; |
| 6464 | |
| 6465 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6466 | ret = -1; |
| 6467 | goto end; |
| 6468 | } |
| 6469 | |
| 6470 | htxret = htx_find_offset(htx, offset); |
| 6471 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6472 | if (!htx_add_last_data(htx, str)) |
| 6473 | goto end; |
| 6474 | } |
| 6475 | else { |
| 6476 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6477 | v.ptr += htxret.ret; |
| 6478 | v.len = 0; |
| 6479 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6480 | goto end; |
| 6481 | } |
| 6482 | ret = str.len; |
| 6483 | if (ret) { |
| 6484 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6485 | flt_update_offsets(filter, msg->chn, ret); |
| 6486 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6487 | } |
| 6488 | |
| 6489 | end: |
| 6490 | htx_to_buf(htx, &msg->chn->buf); |
| 6491 | return ret; |
| 6492 | } |
| 6493 | |
| 6494 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6495 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6496 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6497 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6498 | * update the filter context. |
| 6499 | */ |
| 6500 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6501 | { |
| 6502 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6503 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6504 | struct htx_blk *blk; |
| 6505 | struct htx_ret htxret; |
| 6506 | size_t ret = 0; |
| 6507 | |
| 6508 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6509 | 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] | 6510 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6511 | * of special blocks like HTX_BLK_EOT. |
| 6512 | * We simply truncate after offset |
| 6513 | * (truncate targeted blk and discard the following ones) |
| 6514 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6515 | htx_truncate(htx, offset); |
| 6516 | ret = len; |
| 6517 | goto end; |
| 6518 | } |
| 6519 | |
| 6520 | htxret = htx_find_offset(htx, offset); |
| 6521 | blk = htxret.blk; |
| 6522 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6523 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6524 | struct ist v; |
| 6525 | |
| 6526 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6527 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6528 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6529 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | a029d78 | 2022-10-08 12:33:18 +0200 | [diff] [blame] | 6530 | v = istadv(v, htxret.ret); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6531 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6532 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6533 | /* trimming data in blk: discard everything after the offset |
| 6534 | * (replace 'v' with 'IST_NULL') |
| 6535 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6536 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6537 | if (blk && v.len < len) { |
| 6538 | /* In this case, caller wants to keep removing data, |
| 6539 | * but we need to spare current blk |
| 6540 | * because it was already trimmed |
| 6541 | */ |
| 6542 | blk = htx_get_next_blk(htx, blk); |
| 6543 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6544 | len -= v.len; |
| 6545 | ret += v.len; |
| 6546 | } |
| 6547 | |
| 6548 | |
| 6549 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6550 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6551 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6552 | uint32_t sz = htx_get_blksz(blk); |
| 6553 | |
| 6554 | switch (type) { |
| 6555 | case HTX_BLK_UNUSED: |
| 6556 | break; |
| 6557 | |
| 6558 | case HTX_BLK_DATA: |
| 6559 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6560 | /* don't discard whole blk, only part of it |
| 6561 | * (from the beginning) |
| 6562 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6563 | htx_cut_data_blk(htx, blk, len); |
| 6564 | ret += len; |
| 6565 | goto end; |
| 6566 | } |
| 6567 | break; |
| 6568 | |
| 6569 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6570 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6571 | goto end; |
| 6572 | } |
| 6573 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6574 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6575 | len -= sz; |
| 6576 | ret += sz; |
| 6577 | blk = htx_remove_blk(htx, blk); |
| 6578 | } |
| 6579 | |
| 6580 | end: |
| 6581 | flt_update_offsets(filter, msg->chn, -ret); |
| 6582 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6583 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6584 | * to loose the EOM flag if the message is empty. |
| 6585 | */ |
| 6586 | } |
| 6587 | |
| 6588 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6589 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6590 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6591 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6592 | * the stack. |
| 6593 | */ |
| 6594 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6595 | { |
| 6596 | struct http_msg *msg; |
| 6597 | struct filter *filter; |
| 6598 | size_t output, input; |
| 6599 | int offset, len; |
| 6600 | |
| 6601 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6602 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6603 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6604 | |
| 6605 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6606 | WILL_LJMP(lua_error(L)); |
| 6607 | |
| 6608 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6609 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6610 | WILL_LJMP(lua_error(L)); |
| 6611 | |
| 6612 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6613 | lua_pushnil(L); |
| 6614 | return 1; |
| 6615 | } |
| 6616 | |
| 6617 | offset = output; |
| 6618 | if (lua_gettop(L) > 1) { |
| 6619 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6620 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6621 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6622 | offset += output; |
| 6623 | if (offset < output || offset > input + output) { |
| 6624 | lua_pushfstring(L, "offset out of range."); |
| 6625 | WILL_LJMP(lua_error(L)); |
| 6626 | } |
| 6627 | } |
| 6628 | len = output + input - offset; |
| 6629 | if (lua_gettop(L) == 3) { |
| 6630 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6631 | if (!len) |
| 6632 | goto dup; |
| 6633 | if (len == -1) |
| 6634 | len = global.tune.bufsize; |
| 6635 | if (len < 0) { |
| 6636 | lua_pushfstring(L, "length out of range."); |
| 6637 | WILL_LJMP(lua_error(L)); |
| 6638 | } |
| 6639 | } |
| 6640 | |
| 6641 | dup: |
| 6642 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6643 | return 1; |
| 6644 | } |
| 6645 | |
| 6646 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6647 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6648 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6649 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6650 | * yield. An exception is returned if it is called from another callback. |
| 6651 | */ |
| 6652 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6653 | { |
| 6654 | struct http_msg *msg; |
| 6655 | struct filter *filter; |
| 6656 | const char *str; |
| 6657 | size_t offset, len, sz; |
| 6658 | int ret; |
| 6659 | |
| 6660 | MAY_LJMP(check_args(L, 2, "append")); |
| 6661 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6662 | |
| 6663 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6664 | WILL_LJMP(lua_error(L)); |
| 6665 | |
| 6666 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6667 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6668 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6669 | WILL_LJMP(lua_error(L)); |
| 6670 | |
| 6671 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6672 | lua_pushinteger(L, ret); |
| 6673 | return 1; |
| 6674 | } |
| 6675 | |
| 6676 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6677 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6678 | * channel function used to prepend data, this one can only be called inside a |
| 6679 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6680 | * returned if it is called from another callback. |
| 6681 | */ |
| 6682 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6683 | { |
| 6684 | struct http_msg *msg; |
| 6685 | struct filter *filter; |
| 6686 | const char *str; |
| 6687 | size_t offset, len, sz; |
| 6688 | int ret; |
| 6689 | |
| 6690 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6691 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6692 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6693 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6694 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6695 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6696 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6697 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6698 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6699 | WILL_LJMP(lua_error(L)); |
| 6700 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6701 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6702 | lua_pushinteger(L, ret); |
| 6703 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6704 | } |
| 6705 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6706 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6707 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6708 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6709 | * this one can only be called inside a filter, from http_payload callback. So |
| 6710 | * it cannot yield. An exception is returned if it is called from another |
| 6711 | * callback. |
| 6712 | */ |
| 6713 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6714 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6715 | struct http_msg *msg; |
| 6716 | struct filter *filter; |
| 6717 | const char *str; |
| 6718 | size_t input, output, sz; |
| 6719 | int offset; |
| 6720 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6721 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6722 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6723 | 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] | 6724 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6725 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6726 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6727 | WILL_LJMP(lua_error(L)); |
| 6728 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6729 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6730 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6732 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6733 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6734 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6735 | if (lua_gettop(L) > 2) { |
| 6736 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6737 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6738 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6739 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6740 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6741 | lua_pushfstring(L, "offset out of range."); |
| 6742 | WILL_LJMP(lua_error(L)); |
| 6743 | } |
| 6744 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6745 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6746 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6747 | lua_pushinteger(L, ret); |
| 6748 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6749 | } |
| 6750 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6751 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6752 | * default all DATA blocks are removed. It returns the amount of data |
| 6753 | * removed. Unlike the channel function used to remove data, this one can only |
| 6754 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6755 | * exception is returned if it is called from another callback. |
| 6756 | */ |
| 6757 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6758 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6759 | struct http_msg *msg; |
| 6760 | struct filter *filter; |
| 6761 | size_t input, output; |
| 6762 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6763 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6764 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6765 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6766 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6767 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6768 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6769 | WILL_LJMP(lua_error(L)); |
| 6770 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6771 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6772 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6773 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6774 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6775 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6776 | if (lua_gettop(L) > 1) { |
| 6777 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6778 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6779 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6780 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6781 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6782 | lua_pushfstring(L, "offset out of range."); |
| 6783 | WILL_LJMP(lua_error(L)); |
| 6784 | } |
| 6785 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6786 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6787 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6788 | if (lua_gettop(L) == 3) { |
| 6789 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6790 | if (!len) |
| 6791 | goto end; |
| 6792 | if (len == -1) |
| 6793 | len = output + input - offset; |
| 6794 | if (len < 0 || offset + len > output + input) { |
| 6795 | lua_pushfstring(L, "length out of range."); |
| 6796 | WILL_LJMP(lua_error(L)); |
| 6797 | } |
| 6798 | } |
| 6799 | |
| 6800 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6801 | |
| 6802 | end: |
| 6803 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6804 | return 1; |
| 6805 | } |
| 6806 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6807 | /* 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] | 6808 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6809 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6810 | * function used to replace data, this one can only be called inside a filter, |
| 6811 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6812 | * it is called from another callback. |
| 6813 | */ |
| 6814 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6815 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6816 | struct http_msg *msg; |
| 6817 | struct filter *filter; |
| 6818 | struct htx *htx; |
| 6819 | const char *str; |
| 6820 | size_t input, output, sz; |
| 6821 | int offset, len; |
| 6822 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6823 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6824 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6825 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6826 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6827 | |
| 6828 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6829 | WILL_LJMP(lua_error(L)); |
| 6830 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6831 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6832 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6833 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6834 | WILL_LJMP(lua_error(L)); |
| 6835 | |
| 6836 | offset = output; |
| 6837 | if (lua_gettop(L) > 2) { |
| 6838 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6839 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6840 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6841 | offset += output; |
| 6842 | if (offset < output || offset > input + output) { |
| 6843 | lua_pushfstring(L, "offset out of range."); |
| 6844 | WILL_LJMP(lua_error(L)); |
| 6845 | } |
| 6846 | } |
| 6847 | |
| 6848 | len = output + input - offset; |
| 6849 | if (lua_gettop(L) == 4) { |
| 6850 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6851 | if (!len) |
| 6852 | goto set; |
| 6853 | if (len == -1) |
| 6854 | len = output + input - offset; |
| 6855 | if (len < 0 || offset + len > output + input) { |
| 6856 | lua_pushfstring(L, "length out of range."); |
| 6857 | WILL_LJMP(lua_error(L)); |
| 6858 | } |
| 6859 | } |
| 6860 | |
| 6861 | set: |
| 6862 | /* Be sure we can copied the string once input data will be removed. */ |
| 6863 | htx = htx_from_buf(&msg->chn->buf); |
| 6864 | if (sz > htx_free_data_space(htx) + len) |
| 6865 | lua_pushinteger(L, -1); |
| 6866 | else { |
| 6867 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6868 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6869 | lua_pushinteger(L, ret); |
| 6870 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6871 | return 1; |
| 6872 | } |
| 6873 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6874 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6875 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6876 | * the channel function used to send data, this one can only be called inside a |
| 6877 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6878 | * returned if it is called from another callback. |
| 6879 | */ |
| 6880 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6881 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6882 | struct http_msg *msg; |
| 6883 | struct filter *filter; |
| 6884 | struct htx *htx; |
| 6885 | const char *str; |
| 6886 | size_t offset, len, sz; |
| 6887 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6888 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6889 | MAY_LJMP(check_args(L, 2, "send")); |
| 6890 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6891 | |
| 6892 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6893 | WILL_LJMP(lua_error(L)); |
| 6894 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6895 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6896 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6897 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6898 | WILL_LJMP(lua_error(L)); |
| 6899 | |
| 6900 | /* Return an error if the channel's output is closed */ |
| 6901 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6902 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6903 | return 1; |
| 6904 | } |
| 6905 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6906 | htx = htx_from_buf(&msg->chn->buf); |
| 6907 | if (sz > htx_free_data_space(htx)) { |
| 6908 | lua_pushinteger(L, -1); |
| 6909 | return 1; |
| 6910 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6911 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6912 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6913 | if (ret > 0) { |
| 6914 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6915 | |
| 6916 | FLT_OFF(filter, msg->chn) += ret; |
| 6917 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6918 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6919 | } |
| 6920 | |
| 6921 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6922 | return 1; |
| 6923 | } |
| 6924 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6925 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6926 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6927 | * channel function used to forward data, this one can only be called inside a |
| 6928 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6929 | * returned if it is called from another callback. All other functions deal with |
| 6930 | * DATA block, this one not. |
| 6931 | */ |
| 6932 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6933 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6934 | struct http_msg *msg; |
| 6935 | struct filter *filter; |
| 6936 | size_t offset, len; |
| 6937 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6938 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6939 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6940 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6941 | |
| 6942 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6943 | WILL_LJMP(lua_error(L)); |
| 6944 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6945 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6946 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6947 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6948 | WILL_LJMP(lua_error(L)); |
| 6949 | |
| 6950 | /* Nothing to do, just return */ |
| 6951 | if (!fwd) |
| 6952 | goto end; |
| 6953 | |
| 6954 | /* Return an error if the channel's output is closed */ |
| 6955 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6956 | ret = -1; |
| 6957 | goto end; |
| 6958 | } |
| 6959 | |
| 6960 | ret = fwd; |
| 6961 | if (ret > len) |
| 6962 | ret = len; |
| 6963 | |
| 6964 | if (ret) { |
| 6965 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6966 | |
| 6967 | FLT_OFF(filter, msg->chn) += ret; |
| 6968 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6969 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6970 | } |
| 6971 | |
| 6972 | end: |
| 6973 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6974 | return 1; |
| 6975 | } |
| 6976 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6977 | /* Set EOM flag on the HTX message. |
| 6978 | * |
| 6979 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6980 | * really know how to do without this feature. |
| 6981 | */ |
| 6982 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6983 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6984 | struct http_msg *msg; |
| 6985 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6986 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6987 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6988 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6989 | htx = htxbuf(&msg->chn->buf); |
| 6990 | htx->flags |= HTX_FL_EOM; |
| 6991 | return 0; |
| 6992 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6993 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6994 | /* Unset EOM flag on the HTX message. |
| 6995 | * |
| 6996 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6997 | * really know how to do without this feature. |
| 6998 | */ |
| 6999 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 7000 | { |
| 7001 | struct http_msg *msg; |
| 7002 | struct htx *htx; |
| 7003 | |
| 7004 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 7005 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 7006 | htx = htxbuf(&msg->chn->buf); |
| 7007 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 7008 | return 0; |
| 7009 | } |
| 7010 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7011 | /* |
| 7012 | * |
| 7013 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7014 | * Class HTTPClient |
| 7015 | * |
| 7016 | * |
| 7017 | */ |
| 7018 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7019 | { |
| 7020 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7021 | } |
| 7022 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7023 | |
| 7024 | /* stops the httpclient and ask it to kill itself */ |
| 7025 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7026 | { |
| 7027 | struct hlua_httpclient *hlua_hc; |
| 7028 | |
| 7029 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7030 | |
| 7031 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7032 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7033 | if (hlua_hc->hc) |
| 7034 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 7035 | |
| 7036 | LIST_DELETE(&hlua_hc->by_hlua); |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7037 | |
| 7038 | hlua_hc->hc = NULL; |
| 7039 | |
| 7040 | |
| 7041 | return 0; |
| 7042 | } |
| 7043 | |
| 7044 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7045 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7046 | { |
| 7047 | struct hlua_httpclient *hlua_hc; |
| 7048 | struct hlua *hlua; |
| 7049 | |
| 7050 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7051 | hlua = hlua_gethlua(L); |
| 7052 | if (!hlua) |
| 7053 | return 0; |
| 7054 | |
| 7055 | /* Check stack size. */ |
| 7056 | if (!lua_checkstack(L, 3)) { |
| 7057 | hlua_pusherror(L, "httpclient: full stack"); |
| 7058 | goto err; |
| 7059 | } |
| 7060 | /* Create the object: obj[0] = userdata. */ |
| 7061 | lua_newtable(L); |
| 7062 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7063 | lua_rawseti(L, -2, 0); |
| 7064 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7065 | |
| 7066 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7067 | if (!hlua_hc->hc) |
| 7068 | goto err; |
| 7069 | |
William Lallemand | bb58142 | 2022-10-20 10:57:28 +0200 | [diff] [blame] | 7070 | LIST_APPEND(&hlua->hc_list, &hlua_hc->by_hlua); |
| 7071 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7072 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7073 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7074 | lua_setmetatable(L, -2); |
| 7075 | |
| 7076 | return 1; |
| 7077 | |
| 7078 | err: |
| 7079 | WILL_LJMP(lua_error(L)); |
| 7080 | return 0; |
| 7081 | } |
| 7082 | |
| 7083 | |
| 7084 | /* |
| 7085 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7086 | * httpclient receives some data |
| 7087 | * |
| 7088 | */ |
| 7089 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7090 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7091 | { |
| 7092 | struct hlua *hlua = hc->caller; |
| 7093 | |
| 7094 | if (!hlua || !hlua->task) |
| 7095 | return; |
| 7096 | |
| 7097 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7098 | } |
| 7099 | |
| 7100 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7101 | * Fill the lua stack with headers from the httpclient response |
| 7102 | * This works the same way as the hlua_http_get_headers() function |
| 7103 | */ |
| 7104 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7105 | { |
| 7106 | struct http_hdr *hdr; |
| 7107 | |
| 7108 | lua_newtable(L); |
| 7109 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7110 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7111 | struct ist n, v; |
| 7112 | int len; |
| 7113 | |
| 7114 | n = hdr->n; |
| 7115 | v = hdr->v; |
| 7116 | |
| 7117 | /* Check for existing entry: |
| 7118 | * assume that the table is on the top of the stack, and |
| 7119 | * push the key in the stack, the function lua_gettable() |
| 7120 | * perform the lookup. |
| 7121 | */ |
| 7122 | |
| 7123 | lua_pushlstring(L, n.ptr, n.len); |
| 7124 | lua_gettable(L, -2); |
| 7125 | |
| 7126 | switch (lua_type(L, -1)) { |
| 7127 | case LUA_TNIL: |
| 7128 | /* Table not found, create it. */ |
| 7129 | lua_pop(L, 1); /* remove the nil value. */ |
| 7130 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7131 | lua_newtable(L); /* create and push empty table. */ |
| 7132 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7133 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7134 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7135 | break; |
| 7136 | |
| 7137 | case LUA_TTABLE: |
| 7138 | /* Entry found: push the value in the table. */ |
| 7139 | len = lua_rawlen(L, -1); |
| 7140 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7141 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7142 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7143 | break; |
| 7144 | |
| 7145 | default: |
| 7146 | /* Other cases are errors. */ |
| 7147 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7148 | WILL_LJMP(lua_error(L)); |
| 7149 | } |
| 7150 | } |
| 7151 | return 1; |
| 7152 | } |
| 7153 | |
| 7154 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7155 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7156 | * |
| 7157 | * Caller must free the result |
| 7158 | */ |
| 7159 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7160 | { |
| 7161 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7162 | struct http_hdr *result = NULL; |
| 7163 | uint32_t hdr_num = 0; |
| 7164 | |
| 7165 | lua_pushnil(L); |
| 7166 | while (lua_next(L, -2) != 0) { |
| 7167 | struct ist name, value; |
| 7168 | const char *n, *v; |
| 7169 | size_t nlen, vlen; |
| 7170 | |
| 7171 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7172 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7173 | goto next_hdr; |
| 7174 | } |
| 7175 | |
| 7176 | n = lua_tolstring(L, -2, &nlen); |
| 7177 | name = ist2(n, nlen); |
| 7178 | |
| 7179 | /* Loop on header's values */ |
| 7180 | lua_pushnil(L); |
| 7181 | while (lua_next(L, -2)) { |
| 7182 | if (!lua_isstring(L, -1)) { |
| 7183 | /* Skip the value if it is not a string */ |
| 7184 | goto next_value; |
| 7185 | } |
| 7186 | |
| 7187 | v = lua_tolstring(L, -1, &vlen); |
| 7188 | value = ist2(v, vlen); |
| 7189 | name = ist2(n, nlen); |
| 7190 | |
| 7191 | hdrs[hdr_num].n = istdup(name); |
| 7192 | hdrs[hdr_num].v = istdup(value); |
| 7193 | |
| 7194 | hdr_num++; |
| 7195 | |
| 7196 | next_value: |
| 7197 | lua_pop(L, 1); |
| 7198 | } |
| 7199 | |
| 7200 | next_hdr: |
| 7201 | lua_pop(L, 1); |
| 7202 | |
| 7203 | } |
| 7204 | |
| 7205 | if (hdr_num) { |
| 7206 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7207 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7208 | if (!result) |
| 7209 | goto skip_headers; |
| 7210 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7211 | |
| 7212 | result[hdr_num].n = IST_NULL; |
| 7213 | result[hdr_num].v = IST_NULL; |
| 7214 | } |
| 7215 | |
| 7216 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7217 | |
| 7218 | return result; |
| 7219 | } |
| 7220 | |
| 7221 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7222 | /* |
| 7223 | * For each yield, checks if there is some data in the httpclient and push them |
| 7224 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7225 | * the stack |
| 7226 | */ |
| 7227 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7228 | { |
| 7229 | struct buffer *tr; |
| 7230 | int res; |
| 7231 | struct hlua *hlua = hlua_gethlua(L); |
| 7232 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7233 | |
| 7234 | |
| 7235 | tr = get_trash_chunk(); |
| 7236 | |
| 7237 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7238 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7239 | |
| 7240 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7241 | |
| 7242 | luaL_pushresult(&hlua_hc->b); |
| 7243 | lua_settable(L, -3); |
| 7244 | |
| 7245 | lua_pushstring(L, "status"); |
| 7246 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7247 | lua_settable(L, -3); |
| 7248 | |
| 7249 | |
| 7250 | lua_pushstring(L, "reason"); |
| 7251 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7252 | lua_settable(L, -3); |
| 7253 | |
| 7254 | lua_pushstring(L, "headers"); |
| 7255 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7256 | lua_settable(L, -3); |
| 7257 | |
| 7258 | return 1; |
| 7259 | } |
| 7260 | |
| 7261 | if (httpclient_data(hlua_hc->hc)) |
| 7262 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7263 | |
| 7264 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7265 | |
| 7266 | return 0; |
| 7267 | } |
| 7268 | |
| 7269 | /* |
| 7270 | * Call this when trying to stream a body during a request |
| 7271 | */ |
| 7272 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7273 | { |
| 7274 | struct hlua *hlua; |
| 7275 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7276 | const char *body_str = NULL; |
| 7277 | int ret; |
| 7278 | int end = 0; |
| 7279 | size_t buf_len; |
| 7280 | size_t to_send = 0; |
| 7281 | |
| 7282 | hlua = hlua_gethlua(L); |
| 7283 | |
| 7284 | if (!hlua || !hlua->task) |
| 7285 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7286 | "'frontend', 'backend' or 'task'")); |
| 7287 | |
| 7288 | ret = lua_getfield(L, -1, "body"); |
| 7289 | if (ret != LUA_TSTRING) |
| 7290 | goto rcv; |
| 7291 | |
| 7292 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7293 | lua_pop(L, 1); |
| 7294 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7295 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7296 | |
| 7297 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7298 | end = 1; |
| 7299 | |
| 7300 | /* the end flag is always set since we are using the whole remaining size */ |
| 7301 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7302 | |
| 7303 | if (buf_len > hlua_hc->sent) { |
| 7304 | /* still need to process the buffer */ |
| 7305 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7306 | } else { |
| 7307 | goto rcv; |
| 7308 | /* we sent the whole request buffer we can recv */ |
| 7309 | } |
| 7310 | return 0; |
| 7311 | |
| 7312 | rcv: |
| 7313 | |
| 7314 | /* we return a "res" object */ |
| 7315 | lua_newtable(L); |
| 7316 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7317 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7318 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7319 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7320 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7321 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7322 | |
| 7323 | return 1; |
| 7324 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7325 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7326 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7327 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7328 | */ |
| 7329 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7330 | __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] | 7331 | { |
| 7332 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7333 | struct http_hdr *hdrs = NULL; |
| 7334 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7335 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7336 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7337 | const char *body_str = NULL; |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7338 | size_t buf_len = 0; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7339 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7340 | |
| 7341 | hlua = hlua_gethlua(L); |
| 7342 | |
| 7343 | if (!hlua || !hlua->task) |
| 7344 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7345 | "'frontend', 'backend' or 'task'")); |
| 7346 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7347 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7348 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7349 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7350 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7351 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7352 | lua_pushnil(L); /* first key */ |
| 7353 | while (lua_next(L, 2)) { |
| 7354 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7355 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7356 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7357 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7358 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7359 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7360 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7361 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7362 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7363 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7364 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7365 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7366 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7367 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7368 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7369 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7370 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7371 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7372 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7373 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7374 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7375 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7376 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7377 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7378 | } else { |
| 7379 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7380 | } |
| 7381 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7382 | lua_pop(L, 1); |
| 7383 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7384 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7385 | if (!url_str) { |
| 7386 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7387 | return 0; |
| 7388 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7389 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7390 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7391 | |
| 7392 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7393 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7394 | |
| 7395 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7396 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7397 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7398 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7399 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7400 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7401 | /* a body is available, it will use the request callback */ |
Christopher Faulet | 380ae9c | 2022-10-14 14:57:04 +0200 | [diff] [blame] | 7402 | if (body_str && buf_len) { |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7403 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7404 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7405 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7406 | 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] | 7407 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7408 | /* free the temporary headers array */ |
| 7409 | hdrs_i = hdrs; |
| 7410 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7411 | istfree(&hdrs_i->n); |
| 7412 | istfree(&hdrs_i->v); |
| 7413 | hdrs_i++; |
| 7414 | } |
| 7415 | ha_free(&hdrs); |
| 7416 | |
| 7417 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7418 | if (ret != ERR_NONE) { |
| 7419 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7420 | return 0; |
| 7421 | } |
| 7422 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7423 | if (!httpclient_start(hlua_hc->hc)) |
| 7424 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7425 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7426 | 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] | 7427 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7428 | return 0; |
| 7429 | } |
| 7430 | |
| 7431 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7432 | * Sends an HTTP HEAD request and wait for a response |
| 7433 | * |
| 7434 | * httpclient:head(url, headers, payload) |
| 7435 | */ |
| 7436 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7437 | { |
| 7438 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7439 | } |
| 7440 | |
| 7441 | /* |
| 7442 | * Send an HTTP GET request and wait for a response |
| 7443 | * |
| 7444 | * httpclient:get(url, headers, payload) |
| 7445 | */ |
| 7446 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7447 | { |
| 7448 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7449 | |
| 7450 | } |
| 7451 | |
| 7452 | /* |
| 7453 | * Sends an HTTP PUT request and wait for a response |
| 7454 | * |
| 7455 | * httpclient:put(url, headers, payload) |
| 7456 | */ |
| 7457 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7458 | { |
| 7459 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7460 | } |
| 7461 | |
| 7462 | /* |
| 7463 | * Send an HTTP POST request and wait for a response |
| 7464 | * |
| 7465 | * httpclient:post(url, headers, payload) |
| 7466 | */ |
| 7467 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7468 | { |
| 7469 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7470 | } |
| 7471 | |
| 7472 | |
| 7473 | /* |
| 7474 | * Sends an HTTP DELETE request and wait for a response |
| 7475 | * |
| 7476 | * httpclient:delete(url, headers, payload) |
| 7477 | */ |
| 7478 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7479 | { |
| 7480 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7481 | } |
| 7482 | |
| 7483 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7484 | * |
| 7485 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7486 | * Class TXN |
| 7487 | * |
| 7488 | * |
| 7489 | */ |
| 7490 | |
| 7491 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7492 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7493 | */ |
| 7494 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7495 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7496 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7497 | } |
| 7498 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7499 | __LJMP static int hlua_set_var(lua_State *L) |
| 7500 | { |
| 7501 | struct hlua_txn *htxn; |
| 7502 | const char *name; |
| 7503 | size_t len; |
| 7504 | struct sample smp; |
| 7505 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7506 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7507 | 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] | 7508 | |
| 7509 | /* It is useles to retrieve the stream, but this function |
| 7510 | * runs only in a stream context. |
| 7511 | */ |
| 7512 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7513 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7514 | |
| 7515 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7516 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7517 | hlua_lua2smp(L, 3, &smp); |
| 7518 | |
| 7519 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7520 | 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] | 7521 | |
| 7522 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7523 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7524 | else |
| 7525 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7526 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7527 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7528 | } |
| 7529 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7530 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7531 | { |
| 7532 | struct hlua_txn *htxn; |
| 7533 | const char *name; |
| 7534 | size_t len; |
| 7535 | struct sample smp; |
| 7536 | |
| 7537 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7538 | |
| 7539 | /* It is useles to retrieve the stream, but this function |
| 7540 | * runs only in a stream context. |
| 7541 | */ |
| 7542 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7543 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7544 | |
| 7545 | /* Unset the variable. */ |
| 7546 | 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] | 7547 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7548 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7549 | } |
| 7550 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7551 | __LJMP static int hlua_get_var(lua_State *L) |
| 7552 | { |
| 7553 | struct hlua_txn *htxn; |
| 7554 | const char *name; |
| 7555 | size_t len; |
| 7556 | struct sample smp; |
| 7557 | |
| 7558 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7559 | |
| 7560 | /* It is useles to retrieve the stream, but this function |
| 7561 | * runs only in a stream context. |
| 7562 | */ |
| 7563 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7564 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7565 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7566 | 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] | 7567 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7568 | lua_pushnil(L); |
| 7569 | return 1; |
| 7570 | } |
| 7571 | |
| 7572 | return hlua_smp2lua(L, &smp); |
| 7573 | } |
| 7574 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7575 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7576 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7577 | struct hlua *hlua; |
| 7578 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7579 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7580 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7581 | /* It is useles to retrieve the stream, but this function |
| 7582 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7583 | */ |
| 7584 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7585 | |
| 7586 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7587 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7588 | if (!hlua) |
| 7589 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7590 | |
| 7591 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7592 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7593 | |
| 7594 | /* Get and store new value. */ |
| 7595 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7596 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7597 | |
| 7598 | return 0; |
| 7599 | } |
| 7600 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7601 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7602 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7603 | struct hlua *hlua; |
| 7604 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7605 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7606 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7607 | /* It is useles to retrieve the stream, but this function |
| 7608 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7609 | */ |
| 7610 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7611 | |
| 7612 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7613 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7614 | if (!hlua) { |
| 7615 | lua_pushnil(L); |
| 7616 | return 1; |
| 7617 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7618 | |
| 7619 | /* Push configuration index in the stack. */ |
| 7620 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7621 | |
| 7622 | return 1; |
| 7623 | } |
| 7624 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7625 | /* Create stack entry containing a class TXN. This function |
| 7626 | * return 0 if the stack does not contains free slots, |
| 7627 | * otherwise it returns 1. |
| 7628 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7629 | 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] | 7630 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7631 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7632 | |
| 7633 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7634 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7635 | return 0; |
| 7636 | |
| 7637 | /* NOTE: The allocation never fails. The failure |
| 7638 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7639 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7640 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7641 | /* Create the object: obj[0] = userdata. */ |
| 7642 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7643 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7644 | lua_rawseti(L, -2, 0); |
| 7645 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7646 | htxn->s = s; |
| 7647 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7648 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7649 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7650 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7651 | /* Create the "f" field that contains a list of fetches. */ |
| 7652 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7653 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7654 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7655 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7656 | |
| 7657 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7658 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7659 | 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] | 7660 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7661 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7662 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7663 | /* Create the "c" field that contains a list of converters. */ |
| 7664 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7665 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7666 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7667 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7668 | |
| 7669 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7670 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7671 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7672 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7673 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7674 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7675 | /* Create the "req" field that contains the request channel object. */ |
| 7676 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7677 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7678 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7679 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7680 | |
| 7681 | /* Create the "res" field that contains the response channel object. */ |
| 7682 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7683 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7684 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7685 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7686 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7687 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7688 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7689 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7690 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7691 | return 0; |
| 7692 | } |
| 7693 | else |
| 7694 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7695 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7696 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7697 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7698 | /* HTTPMessage object are created when a lua TXN is created from |
| 7699 | * a filter context only |
| 7700 | */ |
| 7701 | |
| 7702 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7703 | lua_pushstring(L, "http_req"); |
| 7704 | if (p->mode == PR_MODE_HTTP) { |
| 7705 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7706 | return 0; |
| 7707 | } |
| 7708 | else |
| 7709 | lua_pushnil(L); |
| 7710 | lua_rawset(L, -3); |
| 7711 | |
| 7712 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7713 | lua_pushstring(L, "http_res"); |
| 7714 | if (p->mode == PR_MODE_HTTP) { |
| 7715 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7716 | return 0; |
| 7717 | } |
| 7718 | else |
| 7719 | lua_pushnil(L); |
| 7720 | lua_rawset(L, -3); |
| 7721 | } |
| 7722 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7723 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7724 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7725 | lua_setmetatable(L, -2); |
| 7726 | |
| 7727 | return 1; |
| 7728 | } |
| 7729 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7730 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7731 | { |
| 7732 | const char *msg; |
| 7733 | struct hlua_txn *htxn; |
| 7734 | |
| 7735 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7736 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7737 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7738 | |
| 7739 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7740 | return 0; |
| 7741 | } |
| 7742 | |
| 7743 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7744 | { |
| 7745 | int level; |
| 7746 | const char *msg; |
| 7747 | struct hlua_txn *htxn; |
| 7748 | |
| 7749 | MAY_LJMP(check_args(L, 3, "log")); |
| 7750 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7751 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7752 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7753 | |
| 7754 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7755 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7756 | |
| 7757 | hlua_sendlog(htxn->s->be, level, msg); |
| 7758 | return 0; |
| 7759 | } |
| 7760 | |
| 7761 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7762 | { |
| 7763 | const char *msg; |
| 7764 | struct hlua_txn *htxn; |
| 7765 | |
| 7766 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7767 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7768 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7769 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7770 | return 0; |
| 7771 | } |
| 7772 | |
| 7773 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7774 | { |
| 7775 | const char *msg; |
| 7776 | struct hlua_txn *htxn; |
| 7777 | |
| 7778 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7779 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7780 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7781 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7782 | return 0; |
| 7783 | } |
| 7784 | |
| 7785 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7786 | { |
| 7787 | const char *msg; |
| 7788 | struct hlua_txn *htxn; |
| 7789 | |
| 7790 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7791 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7792 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7793 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7794 | return 0; |
| 7795 | } |
| 7796 | |
| 7797 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7798 | { |
| 7799 | const char *msg; |
| 7800 | struct hlua_txn *htxn; |
| 7801 | |
| 7802 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7803 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7804 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7805 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7806 | return 0; |
| 7807 | } |
| 7808 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7809 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7810 | { |
| 7811 | struct hlua_txn *htxn; |
| 7812 | int ll; |
| 7813 | |
| 7814 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7815 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7816 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7817 | |
| 7818 | if (ll < 0 || ll > 7) |
| 7819 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7820 | |
| 7821 | htxn->s->logs.level = ll; |
| 7822 | return 0; |
| 7823 | } |
| 7824 | |
| 7825 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7826 | { |
| 7827 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7828 | int tos; |
| 7829 | |
| 7830 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7831 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7832 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7833 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7834 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7835 | return 0; |
| 7836 | } |
| 7837 | |
| 7838 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7839 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7840 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7841 | int mark; |
| 7842 | |
| 7843 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7844 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7845 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7846 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7847 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7848 | return 0; |
| 7849 | } |
| 7850 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7851 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7852 | { |
| 7853 | struct hlua_txn *htxn; |
| 7854 | |
| 7855 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7856 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7857 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7858 | return 0; |
| 7859 | } |
| 7860 | |
| 7861 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7862 | { |
| 7863 | struct hlua_txn *htxn; |
| 7864 | |
| 7865 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7866 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7867 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7868 | return 0; |
| 7869 | } |
| 7870 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7871 | /* 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] | 7872 | * 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] | 7873 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7874 | * error. The Reply must be on top of the stack. |
| 7875 | */ |
| 7876 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7877 | { |
| 7878 | struct htx *htx; |
| 7879 | struct htx_sl *sl; |
| 7880 | struct h1m h1m; |
| 7881 | const char *status, *reason, *body; |
| 7882 | size_t status_len, reason_len, body_len; |
| 7883 | int ret, code, flags; |
| 7884 | |
| 7885 | code = 200; |
| 7886 | status = "200"; |
| 7887 | status_len = 3; |
| 7888 | ret = lua_getfield(L, -1, "status"); |
| 7889 | if (ret == LUA_TNUMBER) { |
| 7890 | code = lua_tointeger(L, -1); |
| 7891 | status = lua_tolstring(L, -1, &status_len); |
| 7892 | } |
| 7893 | lua_pop(L, 1); |
| 7894 | |
| 7895 | reason = http_get_reason(code); |
| 7896 | reason_len = strlen(reason); |
| 7897 | ret = lua_getfield(L, -1, "reason"); |
| 7898 | if (ret == LUA_TSTRING) |
| 7899 | reason = lua_tolstring(L, -1, &reason_len); |
| 7900 | lua_pop(L, 1); |
| 7901 | |
| 7902 | body = NULL; |
| 7903 | body_len = 0; |
| 7904 | ret = lua_getfield(L, -1, "body"); |
| 7905 | if (ret == LUA_TSTRING) |
| 7906 | body = lua_tolstring(L, -1, &body_len); |
| 7907 | lua_pop(L, 1); |
| 7908 | |
| 7909 | /* Prepare the response before inserting the headers */ |
| 7910 | h1m_init_res(&h1m); |
| 7911 | htx = htx_from_buf(&s->res.buf); |
| 7912 | channel_htx_truncate(&s->res, htx); |
| 7913 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7914 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7915 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7916 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7917 | } |
| 7918 | else { |
| 7919 | flags = HTX_SL_F_IS_RESP; |
| 7920 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7921 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7922 | } |
| 7923 | if (!sl) |
| 7924 | goto fail; |
| 7925 | sl->info.res.status = code; |
| 7926 | |
| 7927 | /* Push in the stack the "headers" entry. */ |
| 7928 | ret = lua_getfield(L, -1, "headers"); |
| 7929 | if (ret != LUA_TTABLE) |
| 7930 | goto skip_headers; |
| 7931 | |
| 7932 | lua_pushnil(L); |
| 7933 | while (lua_next(L, -2) != 0) { |
| 7934 | struct ist name, value; |
| 7935 | const char *n, *v; |
| 7936 | size_t nlen, vlen; |
| 7937 | |
| 7938 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7939 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7940 | goto next_hdr; |
| 7941 | } |
| 7942 | |
| 7943 | n = lua_tolstring(L, -2, &nlen); |
| 7944 | name = ist2(n, nlen); |
| 7945 | if (isteqi(name, ist("content-length"))) { |
| 7946 | /* Always skip content-length header. It will be added |
| 7947 | * later with the correct len |
| 7948 | */ |
| 7949 | goto next_hdr; |
| 7950 | } |
| 7951 | |
| 7952 | /* Loop on header's values */ |
| 7953 | lua_pushnil(L); |
| 7954 | while (lua_next(L, -2)) { |
| 7955 | if (!lua_isstring(L, -1)) { |
| 7956 | /* Skip the value if it is not a string */ |
| 7957 | goto next_value; |
| 7958 | } |
| 7959 | |
| 7960 | v = lua_tolstring(L, -1, &vlen); |
| 7961 | value = ist2(v, vlen); |
| 7962 | |
| 7963 | if (isteqi(name, ist("transfer-encoding"))) |
| 7964 | h1_parse_xfer_enc_header(&h1m, value); |
| 7965 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7966 | goto fail; |
| 7967 | |
| 7968 | next_value: |
| 7969 | lua_pop(L, 1); |
| 7970 | } |
| 7971 | |
| 7972 | next_hdr: |
| 7973 | lua_pop(L, 1); |
| 7974 | } |
| 7975 | skip_headers: |
| 7976 | lua_pop(L, 1); |
| 7977 | |
| 7978 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7979 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7980 | const char *clen = ultoa(body_len); |
| 7981 | |
| 7982 | h1m.flags |= H1_MF_CLEN; |
| 7983 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7984 | goto fail; |
| 7985 | } |
| 7986 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7987 | h1m.flags |= H1_MF_XFER_LEN; |
| 7988 | |
| 7989 | /* Update HTX start-line flags */ |
| 7990 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7991 | flags |= HTX_SL_F_XFER_ENC; |
| 7992 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7993 | flags |= HTX_SL_F_XFER_LEN; |
| 7994 | if (h1m.flags & H1_MF_CHNK) |
| 7995 | flags |= HTX_SL_F_CHNK; |
| 7996 | else if (h1m.flags & H1_MF_CLEN) |
| 7997 | flags |= HTX_SL_F_CLEN; |
| 7998 | if (h1m.body_len == 0) |
| 7999 | flags |= HTX_SL_F_BODYLESS; |
| 8000 | } |
| 8001 | sl->flags |= flags; |
| 8002 | |
| 8003 | |
| 8004 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8005 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8006 | goto fail; |
| 8007 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 8008 | htx->flags |= HTX_FL_EOM; |
| 8009 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8010 | /* Now, forward the response and terminate the transaction */ |
| 8011 | s->txn->status = code; |
| 8012 | htx_to_buf(htx, &s->res.buf); |
| 8013 | if (!http_forward_proxy_resp(s, 1)) |
| 8014 | goto fail; |
| 8015 | |
| 8016 | return 1; |
| 8017 | |
| 8018 | fail: |
| 8019 | channel_htx_truncate(&s->res, htx); |
| 8020 | return 0; |
| 8021 | } |
| 8022 | |
| 8023 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8024 | * processing is just aborted. Nothing is returned to the client and all |
| 8025 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8026 | * is forwarded to the client before terminating the transaction. On success, |
| 8027 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8028 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8029 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8030 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8031 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8032 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8033 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8034 | struct stream *s; |
| 8035 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8036 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8037 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8038 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8039 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8040 | * just end the execution of the current lua code. */ |
| 8041 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8042 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8043 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8044 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8045 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8046 | struct channel *req = &s->req; |
| 8047 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8048 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8049 | channel_auto_read(req); |
| 8050 | channel_abort(req); |
| 8051 | channel_auto_close(req); |
| 8052 | channel_erase(req); |
| 8053 | |
| 8054 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 8055 | channel_auto_read(res); |
| 8056 | channel_auto_close(res); |
| 8057 | channel_shutr_now(res); |
| 8058 | |
| 8059 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8060 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8061 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8062 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8063 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8064 | /* No reply or invalid reply */ |
| 8065 | s->txn->status = 0; |
| 8066 | http_reply_and_close(s, 0, NULL); |
| 8067 | } |
| 8068 | else { |
| 8069 | /* Remove extra args to have the reply on top of the stack */ |
| 8070 | if (lua_gettop(L) > 2) |
| 8071 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8072 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8073 | if (!hlua_txn_forward_reply(L, s)) { |
| 8074 | if (!(s->flags & SF_ERR_MASK)) |
| 8075 | s->flags |= SF_ERR_PRXCOND; |
| 8076 | lua_pushinteger(L, ACT_RET_ERR); |
| 8077 | WILL_LJMP(hlua_done(L)); |
| 8078 | return 0; /* Never reached */ |
| 8079 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8080 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8081 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8082 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8083 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8084 | /* let's log the request time */ |
| 8085 | s->logs.tv_request = now; |
| 8086 | 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] | 8087 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8088 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8089 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8090 | done: |
| 8091 | if (!(s->flags & SF_ERR_MASK)) |
| 8092 | s->flags |= SF_ERR_LOCAL; |
| 8093 | if (!(s->flags & SF_FINST_MASK)) |
| 8094 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8095 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8096 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8097 | lua_pushinteger(L, -1); |
| 8098 | else |
| 8099 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8100 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8101 | return 0; |
| 8102 | } |
| 8103 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8104 | /* |
| 8105 | * |
| 8106 | * |
| 8107 | * Class REPLY |
| 8108 | * |
| 8109 | * |
| 8110 | */ |
| 8111 | |
| 8112 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8113 | * free slots, the function fails and returns 0; |
| 8114 | */ |
| 8115 | static int hlua_txn_reply_new(lua_State *L) |
| 8116 | { |
| 8117 | struct hlua_txn *htxn; |
| 8118 | const char *reason, *body = NULL; |
| 8119 | int ret, status; |
| 8120 | |
| 8121 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8122 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8123 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8124 | WILL_LJMP(lua_error(L)); |
| 8125 | } |
| 8126 | |
| 8127 | /* Default value */ |
| 8128 | status = 200; |
| 8129 | reason = http_get_reason(status); |
| 8130 | |
| 8131 | if (lua_istable(L, 2)) { |
| 8132 | /* load status and reason from the table argument at index 2 */ |
| 8133 | ret = lua_getfield(L, 2, "status"); |
| 8134 | if (ret == LUA_TNIL) |
| 8135 | goto reason; |
| 8136 | else if (ret != LUA_TNUMBER) { |
| 8137 | /* invalid status: ignore the reason */ |
| 8138 | goto body; |
| 8139 | } |
| 8140 | status = lua_tointeger(L, -1); |
| 8141 | |
| 8142 | reason: |
| 8143 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8144 | ret = lua_getfield(L, 2, "reason"); |
| 8145 | if (ret == LUA_TSTRING) |
| 8146 | reason = lua_tostring(L, -1); |
| 8147 | |
| 8148 | body: |
| 8149 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8150 | ret = lua_getfield(L, 2, "body"); |
| 8151 | if (ret == LUA_TSTRING) |
| 8152 | body = lua_tostring(L, -1); |
| 8153 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8154 | } |
| 8155 | |
| 8156 | /* Create the Reply table */ |
| 8157 | lua_newtable(L); |
| 8158 | |
| 8159 | /* Add status element */ |
| 8160 | lua_pushstring(L, "status"); |
| 8161 | lua_pushinteger(L, status); |
| 8162 | lua_settable(L, -3); |
| 8163 | |
| 8164 | /* Add reason element */ |
| 8165 | reason = http_get_reason(status); |
| 8166 | lua_pushstring(L, "reason"); |
| 8167 | lua_pushstring(L, reason); |
| 8168 | lua_settable(L, -3); |
| 8169 | |
| 8170 | /* Add body element, nil if undefined */ |
| 8171 | lua_pushstring(L, "body"); |
| 8172 | if (body) |
| 8173 | lua_pushstring(L, body); |
| 8174 | else |
| 8175 | lua_pushnil(L); |
| 8176 | lua_settable(L, -3); |
| 8177 | |
| 8178 | /* Add headers element */ |
| 8179 | lua_pushstring(L, "headers"); |
| 8180 | lua_newtable(L); |
| 8181 | |
| 8182 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8183 | if (lua_istable(L, 2)) { |
| 8184 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8185 | ret = lua_getfield(L, 2, "headers"); |
| 8186 | if (ret == LUA_TTABLE) { |
| 8187 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8188 | lua_pushnil(L); |
| 8189 | while (lua_next(L, -2) != 0) { |
| 8190 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8191 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8192 | /* invalid value type, skip it */ |
| 8193 | lua_pop(L, 1); |
| 8194 | continue; |
| 8195 | } |
| 8196 | |
| 8197 | |
| 8198 | /* Duplicate the key and swap it with the value. */ |
| 8199 | lua_pushvalue(L, -2); |
| 8200 | lua_insert(L, -2); |
| 8201 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8202 | |
| 8203 | lua_newtable(L); |
| 8204 | lua_insert(L, -2); |
| 8205 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8206 | |
| 8207 | if (lua_isstring(L, -1)) { |
| 8208 | /* push the value in the inner table */ |
| 8209 | lua_rawseti(L, -2, 1); |
| 8210 | } |
| 8211 | else { /* table */ |
| 8212 | lua_pushnil(L); |
| 8213 | while (lua_next(L, -2) != 0) { |
| 8214 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8215 | if (!lua_isstring(L, -1)) { |
| 8216 | /* invalid value type, skip it*/ |
| 8217 | lua_pop(L, 1); |
| 8218 | continue; |
| 8219 | } |
| 8220 | /* push the value in the inner table */ |
| 8221 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8222 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8223 | } |
| 8224 | lua_pop(L, 1); |
| 8225 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8226 | } |
| 8227 | |
| 8228 | /* push (k,v) on the stack in the headers table: |
| 8229 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8230 | */ |
| 8231 | lua_settable(L, -5); |
| 8232 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8233 | } |
| 8234 | } |
| 8235 | lua_pop(L, 1); |
| 8236 | } |
| 8237 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8238 | lua_settable(L, -3); |
| 8239 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8240 | |
| 8241 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8242 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8243 | lua_setmetatable(L, -2); |
| 8244 | return 1; |
| 8245 | } |
| 8246 | |
| 8247 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8248 | * provided, the default one corresponding to the status code is used. |
| 8249 | */ |
| 8250 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8251 | { |
| 8252 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8253 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8254 | |
| 8255 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8256 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8257 | |
| 8258 | if (status < 100 || status > 599) { |
| 8259 | lua_pushboolean(L, 0); |
| 8260 | return 1; |
| 8261 | } |
| 8262 | if (!reason) |
| 8263 | reason = http_get_reason(status); |
| 8264 | |
| 8265 | lua_pushinteger(L, status); |
| 8266 | lua_setfield(L, 1, "status"); |
| 8267 | |
| 8268 | lua_pushstring(L, reason); |
| 8269 | lua_setfield(L, 1, "reason"); |
| 8270 | |
| 8271 | lua_pushboolean(L, 1); |
| 8272 | return 1; |
| 8273 | } |
| 8274 | |
| 8275 | /* Add a header into the reply object. Each header name is associated to an |
| 8276 | * array of values in the "headers" table. If the header name is not found, a |
| 8277 | * new entry is created. |
| 8278 | */ |
| 8279 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8280 | { |
| 8281 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8282 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8283 | int ret; |
| 8284 | |
| 8285 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8286 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8287 | |
| 8288 | /* Push in the stack the "headers" entry. */ |
| 8289 | ret = lua_getfield(L, 1, "headers"); |
| 8290 | if (ret != LUA_TTABLE) { |
| 8291 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8292 | WILL_LJMP(lua_error(L)); |
| 8293 | } |
| 8294 | |
| 8295 | /* check if the header is already registered. If not, register it. */ |
| 8296 | ret = lua_getfield(L, -1, name); |
| 8297 | if (ret == LUA_TNIL) { |
| 8298 | /* Entry not found. */ |
| 8299 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8300 | |
| 8301 | /* Insert the new header name in the array in the top of the stack. |
| 8302 | * It left the new array in the top of the stack. |
| 8303 | */ |
| 8304 | lua_newtable(L); |
| 8305 | lua_pushstring(L, name); |
| 8306 | lua_pushvalue(L, -2); |
| 8307 | lua_settable(L, -4); |
| 8308 | } |
| 8309 | else if (ret != LUA_TTABLE) { |
| 8310 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8311 | WILL_LJMP(lua_error(L)); |
| 8312 | } |
| 8313 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8314 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8315 | * the header value as new entry. |
| 8316 | */ |
| 8317 | lua_pushstring(L, value); |
| 8318 | ret = lua_rawlen(L, -2); |
| 8319 | lua_rawseti(L, -2, ret + 1); |
| 8320 | |
| 8321 | lua_pushboolean(L, 1); |
| 8322 | return 1; |
| 8323 | } |
| 8324 | |
| 8325 | /* Remove all occurrences of a given header name. */ |
| 8326 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8327 | { |
| 8328 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8329 | int ret; |
| 8330 | |
| 8331 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8332 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8333 | |
| 8334 | /* Push in the stack the "headers" entry. */ |
| 8335 | ret = lua_getfield(L, 1, "headers"); |
| 8336 | if (ret != LUA_TTABLE) { |
| 8337 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8338 | WILL_LJMP(lua_error(L)); |
| 8339 | } |
| 8340 | |
| 8341 | lua_pushstring(L, name); |
| 8342 | lua_pushnil(L); |
| 8343 | lua_settable(L, -3); |
| 8344 | |
| 8345 | lua_pushboolean(L, 1); |
| 8346 | return 1; |
| 8347 | } |
| 8348 | |
| 8349 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8350 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8351 | { |
| 8352 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8353 | |
| 8354 | /* First argument (self) must be a table */ |
Aurelien DARRAGON | d83d045 | 2022-10-05 11:46:45 +0200 | [diff] [blame] | 8355 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8356 | |
| 8357 | lua_pushstring(L, payload); |
| 8358 | lua_setfield(L, 1, "body"); |
| 8359 | |
| 8360 | lua_pushboolean(L, 1); |
| 8361 | return 1; |
| 8362 | } |
| 8363 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8364 | __LJMP static int hlua_log(lua_State *L) |
| 8365 | { |
| 8366 | int level; |
| 8367 | const char *msg; |
| 8368 | |
| 8369 | MAY_LJMP(check_args(L, 2, "log")); |
| 8370 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8371 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8372 | |
| 8373 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8374 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8375 | |
| 8376 | hlua_sendlog(NULL, level, msg); |
| 8377 | return 0; |
| 8378 | } |
| 8379 | |
| 8380 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8381 | { |
| 8382 | const char *msg; |
| 8383 | |
| 8384 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8385 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8386 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8387 | return 0; |
| 8388 | } |
| 8389 | |
| 8390 | __LJMP static int hlua_log_info(lua_State *L) |
| 8391 | { |
| 8392 | const char *msg; |
| 8393 | |
| 8394 | MAY_LJMP(check_args(L, 1, "info")); |
| 8395 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8396 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8397 | return 0; |
| 8398 | } |
| 8399 | |
| 8400 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8401 | { |
| 8402 | const char *msg; |
| 8403 | |
| 8404 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8405 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8406 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8407 | return 0; |
| 8408 | } |
| 8409 | |
| 8410 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8411 | { |
| 8412 | const char *msg; |
| 8413 | |
| 8414 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8415 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8416 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8417 | return 0; |
| 8418 | } |
| 8419 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8420 | __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] | 8421 | { |
| 8422 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8423 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8424 | 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] | 8425 | return 0; |
| 8426 | } |
| 8427 | |
| 8428 | __LJMP static int hlua_sleep(lua_State *L) |
| 8429 | { |
| 8430 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8431 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8432 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8433 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8434 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8435 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8436 | wakeup_ms = tick_add(now_ms, delay); |
| 8437 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8438 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8439 | 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] | 8440 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8441 | } |
| 8442 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8443 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8444 | { |
| 8445 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8446 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8447 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8448 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8449 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8450 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8451 | wakeup_ms = tick_add(now_ms, delay); |
| 8452 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8453 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8454 | 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] | 8455 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8456 | } |
| 8457 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8458 | /* This functionis an LUA binding. it permits to give back |
| 8459 | * the hand at the HAProxy scheduler. It is used when the |
| 8460 | * LUA processing consumes a lot of time. |
| 8461 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8462 | __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] | 8463 | { |
| 8464 | return 0; |
| 8465 | } |
| 8466 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8467 | __LJMP static int hlua_yield(lua_State *L) |
| 8468 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8469 | 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] | 8470 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8471 | } |
| 8472 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8473 | /* This function change the nice of the currently executed |
| 8474 | * task. It is used set low or high priority at the current |
| 8475 | * task. |
| 8476 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8477 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8478 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8479 | struct hlua *hlua; |
| 8480 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8481 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8482 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8483 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8484 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8485 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8486 | hlua = hlua_gethlua(L); |
| 8487 | |
| 8488 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8489 | if (!hlua || !hlua->task) |
| 8490 | return 0; |
| 8491 | |
| 8492 | if (nice < -1024) |
| 8493 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8494 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8495 | nice = 1024; |
| 8496 | |
| 8497 | hlua->task->nice = nice; |
| 8498 | return 0; |
| 8499 | } |
| 8500 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8501 | /* 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] | 8502 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8503 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8504 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8505 | * |
| 8506 | * Task wrapper are longjmp safe because the only one Lua code |
| 8507 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8508 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8509 | 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] | 8510 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8511 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8512 | enum hlua_exec status; |
| 8513 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8514 | if (task->tid < 0) |
| 8515 | task->tid = tid; |
| 8516 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8517 | /* If it is the first call to the task, we must initialize the |
| 8518 | * execution timeouts. |
| 8519 | */ |
| 8520 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8521 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8522 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8523 | /* Execute the Lua code. */ |
| 8524 | status = hlua_ctx_resume(hlua, 1); |
| 8525 | |
| 8526 | switch (status) { |
| 8527 | /* finished or yield */ |
| 8528 | case HLUA_E_OK: |
| 8529 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8530 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8531 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8532 | break; |
| 8533 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8534 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8535 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8536 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8537 | break; |
| 8538 | |
| 8539 | /* finished with error. */ |
| 8540 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8541 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8542 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8543 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8544 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8545 | break; |
| 8546 | |
| 8547 | case HLUA_E_ERR: |
| 8548 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8549 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8550 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8551 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8552 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8553 | break; |
| 8554 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8555 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8556 | } |
| 8557 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8558 | /* This function is an LUA binding that register LUA function to be |
| 8559 | * executed after the HAProxy configuration parsing and before the |
| 8560 | * HAProxy scheduler starts. This function expect only one LUA |
| 8561 | * argument that is a function. This function returns nothing, but |
| 8562 | * throws if an error is encountered. |
| 8563 | */ |
| 8564 | __LJMP static int hlua_register_init(lua_State *L) |
| 8565 | { |
| 8566 | struct hlua_init_function *init; |
| 8567 | int ref; |
| 8568 | |
| 8569 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8570 | |
| 8571 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8572 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8573 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8574 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8575 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8576 | |
| 8577 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8578 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8579 | return 0; |
| 8580 | } |
| 8581 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8582 | /* This functio is an LUA binding. It permits to register a task |
| 8583 | * executed in parallel of the main HAroxy activity. The task is |
| 8584 | * created and it is set in the HAProxy scheduler. It can be called |
| 8585 | * from the "init" section, "post init" or during the runtime. |
| 8586 | * |
| 8587 | * Lua prototype: |
| 8588 | * |
| 8589 | * <none> core.register_task(<function>) |
| 8590 | */ |
| 8591 | static int hlua_register_task(lua_State *L) |
| 8592 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8593 | struct hlua *hlua = NULL; |
| 8594 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8595 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8596 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8597 | |
| 8598 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8599 | |
| 8600 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8601 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8602 | /* Get the reference state. If the reference is NULL, L is the master |
| 8603 | * state, otherwise hlua->T is. |
| 8604 | */ |
| 8605 | hlua = hlua_gethlua(L); |
| 8606 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8607 | /* we are in runtime processing */ |
| 8608 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8609 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8610 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8611 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8612 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8613 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8614 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8615 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8616 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8617 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8618 | /* We are in the common lua state, execute the task anywhere, |
| 8619 | * otherwise, inherit the current thread identifier |
| 8620 | */ |
| 8621 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8622 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8623 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8624 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8625 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8626 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8627 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8628 | task->context = hlua; |
| 8629 | task->process = hlua_process_task; |
| 8630 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8631 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8632 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8633 | |
| 8634 | /* Restore the function in the stack. */ |
| 8635 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8636 | hlua->nargs = 0; |
| 8637 | |
| 8638 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8639 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8640 | |
| 8641 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8642 | |
| 8643 | alloc_error: |
| 8644 | task_destroy(task); |
| 8645 | hlua_ctx_destroy(hlua); |
| 8646 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8647 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8648 | } |
| 8649 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8650 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8651 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8652 | * resume converters. |
| 8653 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8654 | 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] | 8655 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8656 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8657 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8658 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8659 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8660 | if (!stream) |
| 8661 | return 0; |
| 8662 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8663 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8664 | * Lua context can be not initialized. This behavior |
| 8665 | * permits to save performances because a systematic |
| 8666 | * Lua initialization cause 5% performances loss. |
| 8667 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8668 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8669 | struct hlua *hlua; |
| 8670 | |
| 8671 | hlua = pool_alloc(pool_head_hlua); |
| 8672 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8673 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8674 | return 0; |
| 8675 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8676 | HLUA_INIT(hlua); |
| 8677 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8678 | 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] | 8679 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8680 | return 0; |
| 8681 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8682 | } |
| 8683 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8684 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8685 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8686 | |
| 8687 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8688 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8689 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8690 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8691 | else |
| 8692 | error = "critical error"; |
| 8693 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8694 | return 0; |
| 8695 | } |
| 8696 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8697 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8698 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8699 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8700 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8701 | return 0; |
| 8702 | } |
| 8703 | |
| 8704 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8705 | 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] | 8706 | |
| 8707 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8708 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8709 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8710 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8711 | return 0; |
| 8712 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8713 | hlua_smp2lua(stream->hlua->T, smp); |
| 8714 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8715 | |
| 8716 | /* push keywords in the stack. */ |
| 8717 | if (arg_p) { |
| 8718 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8719 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8720 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8721 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8722 | return 0; |
| 8723 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8724 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8725 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8726 | } |
| 8727 | } |
| 8728 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8729 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8730 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8731 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8732 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8733 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8734 | } |
| 8735 | |
| 8736 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8737 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8738 | /* finished. */ |
| 8739 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8740 | /* If the stack is empty, the function fails. */ |
| 8741 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8742 | return 0; |
| 8743 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8744 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8745 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8746 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8747 | return 1; |
| 8748 | |
| 8749 | /* yield. */ |
| 8750 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8751 | 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] | 8752 | return 0; |
| 8753 | |
| 8754 | /* finished with error. */ |
| 8755 | case HLUA_E_ERRMSG: |
| 8756 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8757 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8758 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8759 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8760 | return 0; |
| 8761 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8762 | case HLUA_E_ETMOUT: |
| 8763 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8764 | return 0; |
| 8765 | |
| 8766 | case HLUA_E_NOMEM: |
| 8767 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8768 | return 0; |
| 8769 | |
| 8770 | case HLUA_E_YIELD: |
| 8771 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8772 | return 0; |
| 8773 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8774 | case HLUA_E_ERR: |
| 8775 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8776 | 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] | 8777 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8778 | |
| 8779 | default: |
| 8780 | return 0; |
| 8781 | } |
| 8782 | } |
| 8783 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8784 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8785 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8786 | * resume sample-fetches. This function will be called by the sample |
| 8787 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8788 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8789 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8790 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8791 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8792 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8793 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8794 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8795 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8796 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8797 | if (!stream) |
| 8798 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8799 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8800 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8801 | * Lua context can be not initialized. This behavior |
| 8802 | * permits to save performances because a systematic |
| 8803 | * Lua initialization cause 5% performances loss. |
| 8804 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8805 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8806 | struct hlua *hlua; |
| 8807 | |
| 8808 | hlua = pool_alloc(pool_head_hlua); |
| 8809 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8810 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8811 | return 0; |
| 8812 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8813 | hlua->T = NULL; |
| 8814 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8815 | 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] | 8816 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8817 | return 0; |
| 8818 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8819 | } |
| 8820 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8821 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8822 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8823 | |
| 8824 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8825 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8826 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8827 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8828 | else |
| 8829 | error = "critical error"; |
| 8830 | 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] | 8831 | return 0; |
| 8832 | } |
| 8833 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8834 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8835 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8836 | 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] | 8837 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8838 | return 0; |
| 8839 | } |
| 8840 | |
| 8841 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8842 | 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] | 8843 | |
| 8844 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8845 | 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] | 8846 | 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] | 8847 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8848 | return 0; |
| 8849 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8850 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8851 | |
| 8852 | /* push keywords in the stack. */ |
| 8853 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8854 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8855 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8856 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8857 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8858 | return 0; |
| 8859 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8860 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8861 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8862 | } |
| 8863 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8864 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8865 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8866 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8867 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8868 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8869 | } |
| 8870 | |
| 8871 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8872 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8873 | /* finished. */ |
| 8874 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8875 | /* If the stack is empty, the function fails. */ |
| 8876 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8877 | return 0; |
| 8878 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8879 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8880 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8881 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8882 | |
| 8883 | /* Set the end of execution flag. */ |
| 8884 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8885 | return 1; |
| 8886 | |
| 8887 | /* yield. */ |
| 8888 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8889 | 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] | 8890 | return 0; |
| 8891 | |
| 8892 | /* finished with error. */ |
| 8893 | case HLUA_E_ERRMSG: |
| 8894 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8895 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8896 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8897 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8898 | return 0; |
| 8899 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8900 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8901 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8902 | return 0; |
| 8903 | |
| 8904 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8905 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8906 | return 0; |
| 8907 | |
| 8908 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8909 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8910 | return 0; |
| 8911 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8912 | case HLUA_E_ERR: |
| 8913 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8914 | 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] | 8915 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8916 | |
| 8917 | default: |
| 8918 | return 0; |
| 8919 | } |
| 8920 | } |
| 8921 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8922 | /* This function is an LUA binding used for registering |
| 8923 | * "sample-conv" functions. It expects a converter name used |
| 8924 | * in the haproxy configuration file, and an LUA function. |
| 8925 | */ |
| 8926 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8927 | { |
| 8928 | struct sample_conv_kw_list *sck; |
| 8929 | const char *name; |
| 8930 | int ref; |
| 8931 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8932 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8933 | struct sample_conv *sc; |
| 8934 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8935 | |
| 8936 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8937 | |
| 8938 | /* First argument : converter name. */ |
| 8939 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8940 | |
| 8941 | /* Second argument : lua function. */ |
| 8942 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8943 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8944 | /* Check if the converter is already registered */ |
| 8945 | trash = get_trash_chunk(); |
| 8946 | chunk_printf(trash, "lua.%s", name); |
| 8947 | sc = find_sample_conv(trash->area, trash->data); |
| 8948 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8949 | fcn = sc->private; |
| 8950 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8951 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8952 | "This will become a hard error in version 2.5.\n", name); |
| 8953 | } |
| 8954 | fcn->function_ref[hlua_state_id] = ref; |
| 8955 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8956 | } |
| 8957 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8958 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8959 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8960 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8961 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8962 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8963 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8964 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8965 | |
| 8966 | /* Fill fcn. */ |
| 8967 | fcn->name = strdup(name); |
| 8968 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8969 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8970 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8971 | |
| 8972 | /* List head */ |
| 8973 | sck->list.n = sck->list.p = NULL; |
| 8974 | |
| 8975 | /* converter keyword. */ |
| 8976 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8977 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8978 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8979 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8980 | |
| 8981 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8982 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8983 | 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] | 8984 | sck->kw[0].val_args = NULL; |
| 8985 | sck->kw[0].in_type = SMP_T_STR; |
| 8986 | sck->kw[0].out_type = SMP_T_STR; |
| 8987 | sck->kw[0].private = fcn; |
| 8988 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8989 | /* Register this new converter */ |
| 8990 | sample_register_convs(sck); |
| 8991 | |
| 8992 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8993 | |
| 8994 | alloc_error: |
| 8995 | release_hlua_function(fcn); |
| 8996 | ha_free(&sck); |
| 8997 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8998 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8999 | } |
| 9000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9001 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9002 | * "sample-fetch" functions. It expects a converter name used |
| 9003 | * in the haproxy configuration file, and an LUA function. |
| 9004 | */ |
| 9005 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 9006 | { |
| 9007 | const char *name; |
| 9008 | int ref; |
| 9009 | int len; |
| 9010 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9011 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9012 | struct sample_fetch *sf; |
| 9013 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9014 | |
| 9015 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 9016 | |
| 9017 | /* First argument : sample-fetch name. */ |
| 9018 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9019 | |
| 9020 | /* Second argument : lua function. */ |
| 9021 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9022 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9023 | /* Check if the sample-fetch is already registered */ |
| 9024 | trash = get_trash_chunk(); |
| 9025 | chunk_printf(trash, "lua.%s", name); |
| 9026 | sf = find_sample_fetch(trash->area, trash->data); |
| 9027 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9028 | fcn = sf->private; |
| 9029 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9030 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9031 | "This will become a hard error in version 2.5.\n", name); |
| 9032 | } |
| 9033 | fcn->function_ref[hlua_state_id] = ref; |
| 9034 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9035 | } |
| 9036 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9037 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9038 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9039 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9040 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9041 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9042 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9043 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9044 | |
| 9045 | /* Fill fcn. */ |
| 9046 | fcn->name = strdup(name); |
| 9047 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9048 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9049 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9050 | |
| 9051 | /* List head */ |
| 9052 | sfk->list.n = sfk->list.p = NULL; |
| 9053 | |
| 9054 | /* sample-fetch keyword. */ |
| 9055 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9056 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9057 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9058 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9059 | |
| 9060 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9061 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9062 | 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] | 9063 | sfk->kw[0].val_args = NULL; |
| 9064 | sfk->kw[0].out_type = SMP_T_STR; |
| 9065 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9066 | sfk->kw[0].val = 0; |
| 9067 | sfk->kw[0].private = fcn; |
| 9068 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9069 | /* Register this new fetch. */ |
| 9070 | sample_register_fetches(sfk); |
| 9071 | |
| 9072 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9073 | |
| 9074 | alloc_error: |
| 9075 | release_hlua_function(fcn); |
| 9076 | ha_free(&sfk); |
| 9077 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9078 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9079 | } |
| 9080 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9081 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9082 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9083 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9084 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9085 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9086 | unsigned int delay; |
| 9087 | unsigned int wakeup_ms; |
| 9088 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9089 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9090 | hlua = hlua_gethlua(L); |
| 9091 | if (!hlua) { |
| 9092 | return 0; |
| 9093 | } |
| 9094 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9095 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9096 | |
| 9097 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9098 | wakeup_ms = tick_add(now_ms, delay); |
| 9099 | hlua->wake_time = wakeup_ms; |
| 9100 | return 0; |
| 9101 | } |
| 9102 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9103 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9104 | * wrapper during the initialisation period. This function may return any |
| 9105 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9106 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9107 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9108 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9109 | 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] | 9110 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9111 | { |
| 9112 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9113 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9114 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9115 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9116 | |
| 9117 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9118 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9119 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9120 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9121 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9122 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9123 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9124 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9125 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9126 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9127 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9128 | * Lua context can be not initialized. This behavior |
| 9129 | * permits to save performances because a systematic |
| 9130 | * Lua initialization cause 5% performances loss. |
| 9131 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9132 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9133 | struct hlua *hlua; |
| 9134 | |
| 9135 | hlua = pool_alloc(pool_head_hlua); |
| 9136 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9137 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9138 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9139 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9140 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9141 | HLUA_INIT(hlua); |
| 9142 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9143 | 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] | 9144 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9145 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9146 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9147 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9148 | } |
| 9149 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9150 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9151 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9152 | |
| 9153 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9154 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9155 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9156 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9157 | else |
| 9158 | error = "critical error"; |
| 9159 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9160 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9161 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9162 | } |
| 9163 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9164 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9165 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9166 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9167 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9168 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9169 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9170 | } |
| 9171 | |
| 9172 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9173 | 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] | 9174 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9175 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9176 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9177 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9178 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9179 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9180 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9181 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9182 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9183 | |
| 9184 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9185 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9186 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9187 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9188 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9189 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9190 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9191 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9192 | lua_pushstring(s->hlua->T, *arg); |
| 9193 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9194 | } |
| 9195 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9196 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9197 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9198 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9199 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9200 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9201 | } |
| 9202 | |
| 9203 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9204 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9205 | /* finished. */ |
| 9206 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9207 | /* Catch the return value */ |
| 9208 | if (lua_gettop(s->hlua->T) > 0) |
| 9209 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9210 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9211 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9212 | if (act_ret == ACT_RET_YIELD) { |
| 9213 | if (flags & ACT_OPT_FINAL) |
| 9214 | goto err_yield; |
| 9215 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9216 | if (dir == SMP_OPT_DIR_REQ) |
| 9217 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9218 | s->hlua->wake_time); |
| 9219 | else |
| 9220 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9221 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9222 | } |
| 9223 | goto end; |
| 9224 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9225 | /* yield. */ |
| 9226 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9227 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9228 | if (dir == SMP_OPT_DIR_REQ) |
| 9229 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9230 | s->hlua->wake_time); |
| 9231 | else |
| 9232 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9233 | s->hlua->wake_time); |
| 9234 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9235 | /* Some actions can be wake up when a "write" event |
| 9236 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9237 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9238 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9239 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9240 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9241 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9242 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9243 | act_ret = ACT_RET_YIELD; |
| 9244 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9245 | |
| 9246 | /* finished with error. */ |
| 9247 | case HLUA_E_ERRMSG: |
| 9248 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9249 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9250 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9251 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9252 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9253 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9254 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9255 | 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] | 9256 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9257 | |
| 9258 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9259 | 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] | 9260 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9261 | |
| 9262 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9263 | err_yield: |
| 9264 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9265 | 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] | 9266 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9267 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9268 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9269 | case HLUA_E_ERR: |
| 9270 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9271 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9272 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9273 | |
| 9274 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9275 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9276 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9277 | |
| 9278 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9279 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9280 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9281 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9282 | } |
| 9283 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9284 | 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] | 9285 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9286 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9287 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9288 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9289 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9290 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9291 | } |
| 9292 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9293 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9294 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9295 | 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] | 9296 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9297 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9298 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | struct task *task; |
| 9300 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9301 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9302 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9303 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9304 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9305 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9306 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9307 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9308 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9309 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9310 | tcp_ctx->hlua = hlua; |
| 9311 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9312 | |
| 9313 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9314 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9315 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9316 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9317 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9318 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9319 | } |
| 9320 | task->nice = 0; |
| 9321 | task->context = ctx; |
| 9322 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9323 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9324 | |
| 9325 | /* In the execution wrappers linked with a stream, the |
| 9326 | * Lua context can be not initialized. This behavior |
| 9327 | * permits to save performances because a systematic |
| 9328 | * Lua initialization cause 5% performances loss. |
| 9329 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9330 | 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] | 9331 | 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] | 9332 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9333 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9334 | } |
| 9335 | |
| 9336 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9337 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9338 | |
| 9339 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9340 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9341 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9342 | error = lua_tostring(hlua->T, -1); |
| 9343 | else |
| 9344 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9345 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9346 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9347 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9348 | } |
| 9349 | |
| 9350 | /* Check stack available size. */ |
| 9351 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9352 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9353 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9354 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9355 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9356 | } |
| 9357 | |
| 9358 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9359 | 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] | 9360 | |
| 9361 | /* Create and and push object stream in the stack. */ |
| 9362 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9363 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9364 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9365 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9366 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9367 | } |
| 9368 | hlua->nargs = 1; |
| 9369 | |
| 9370 | /* push keywords in the stack. */ |
| 9371 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9372 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9373 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9374 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9375 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9376 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9377 | } |
| 9378 | lua_pushstring(hlua->T, *arg); |
| 9379 | hlua->nargs++; |
| 9380 | } |
| 9381 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9382 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9383 | |
| 9384 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9385 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9386 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9387 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9388 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9389 | } |
| 9390 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9391 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9392 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9393 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9394 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9395 | struct stream *strm = __sc_strm(sc); |
| 9396 | struct channel *res = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9397 | struct act_rule *rule = ctx->rule; |
| 9398 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9399 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9400 | |
| 9401 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9402 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9403 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9404 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9405 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9406 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9407 | |
| 9408 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9409 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9410 | return; |
| 9411 | |
| 9412 | /* Execute the function. */ |
| 9413 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9414 | /* finished. */ |
| 9415 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9416 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9417 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9418 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9419 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9420 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9421 | sc_shutr(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9422 | return; |
| 9423 | |
| 9424 | /* yield. */ |
| 9425 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9426 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9427 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9428 | return; |
| 9429 | |
| 9430 | /* finished with error. */ |
| 9431 | case HLUA_E_ERRMSG: |
| 9432 | /* Display log. */ |
| 9433 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9434 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9435 | lua_pop(hlua->T, 1); |
| 9436 | goto error; |
| 9437 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9438 | case HLUA_E_ETMOUT: |
| 9439 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9440 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9441 | goto error; |
| 9442 | |
| 9443 | case HLUA_E_NOMEM: |
| 9444 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9445 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9446 | goto error; |
| 9447 | |
| 9448 | case HLUA_E_YIELD: /* unexpected */ |
| 9449 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9450 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9451 | goto error; |
| 9452 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9453 | case HLUA_E_ERR: |
| 9454 | /* Display log. */ |
| 9455 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9456 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9457 | goto error; |
| 9458 | |
| 9459 | default: |
| 9460 | goto error; |
| 9461 | } |
| 9462 | |
| 9463 | error: |
| 9464 | |
| 9465 | /* For all other cases, just close the stream. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9466 | sc_shutw(sc); |
| 9467 | sc_shutr(sc); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9468 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9469 | } |
| 9470 | |
| 9471 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9472 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9473 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9474 | |
| 9475 | task_destroy(tcp_ctx->task); |
| 9476 | tcp_ctx->task = NULL; |
| 9477 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9478 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9479 | } |
| 9480 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9481 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9482 | * 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] | 9483 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9484 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9485 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9486 | 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] | 9487 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9488 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9489 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9490 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9491 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9492 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9493 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9494 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9495 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9496 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9497 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9498 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9499 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9500 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9501 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9502 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9503 | http_ctx->hlua = hlua; |
| 9504 | http_ctx->left_bytes = -1; |
| 9505 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9506 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9507 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9508 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9509 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9510 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9511 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9512 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9513 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9514 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9515 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9516 | } |
| 9517 | task->nice = 0; |
| 9518 | task->context = ctx; |
| 9519 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9520 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9521 | |
| 9522 | /* In the execution wrappers linked with a stream, the |
| 9523 | * Lua context can be not initialized. This behavior |
| 9524 | * permits to save performances because a systematic |
| 9525 | * Lua initialization cause 5% performances loss. |
| 9526 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9527 | 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] | 9528 | 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] | 9529 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9530 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9531 | } |
| 9532 | |
| 9533 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9534 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9535 | |
| 9536 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9537 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9538 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9539 | error = lua_tostring(hlua->T, -1); |
| 9540 | else |
| 9541 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9542 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9543 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9544 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9545 | } |
| 9546 | |
| 9547 | /* Check stack available size. */ |
| 9548 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9549 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9550 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9551 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9552 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9553 | } |
| 9554 | |
| 9555 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9556 | 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] | 9557 | |
| 9558 | /* Create and and push object stream in the stack. */ |
| 9559 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9560 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9561 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9562 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9563 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9564 | } |
| 9565 | hlua->nargs = 1; |
| 9566 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9567 | /* push keywords in the stack. */ |
| 9568 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9569 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9570 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9571 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9572 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9573 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9574 | } |
| 9575 | lua_pushstring(hlua->T, *arg); |
| 9576 | hlua->nargs++; |
| 9577 | } |
| 9578 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9579 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9580 | |
| 9581 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9582 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9583 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9584 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9585 | } |
| 9586 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9587 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9588 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9589 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9590 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9591 | struct stream *strm = __sc_strm(sc); |
| 9592 | struct channel *req = sc_oc(sc); |
| 9593 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9594 | struct act_rule *rule = ctx->rule; |
| 9595 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9596 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9597 | struct htx *req_htx, *res_htx; |
| 9598 | |
| 9599 | res_htx = htx_from_buf(&res->buf); |
| 9600 | |
| 9601 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9602 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9603 | goto out; |
| 9604 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9605 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9606 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9607 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9608 | goto out; |
| 9609 | } |
| 9610 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9611 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9612 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9613 | |
| 9614 | /* Set the currently running flag. */ |
| 9615 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9616 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9617 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9618 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9619 | goto out; |
| 9620 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9621 | } |
| 9622 | |
| 9623 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9624 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9625 | |
| 9626 | /* Execute the function. */ |
| 9627 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9628 | /* finished. */ |
| 9629 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9630 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9631 | break; |
| 9632 | |
| 9633 | /* yield. */ |
| 9634 | case HLUA_E_AGAIN: |
| 9635 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9636 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9637 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9638 | |
| 9639 | /* finished with error. */ |
| 9640 | case HLUA_E_ERRMSG: |
| 9641 | /* Display log. */ |
| 9642 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9643 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9644 | lua_pop(hlua->T, 1); |
| 9645 | goto error; |
| 9646 | |
| 9647 | case HLUA_E_ETMOUT: |
| 9648 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9649 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9650 | goto error; |
| 9651 | |
| 9652 | case HLUA_E_NOMEM: |
| 9653 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9654 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9655 | goto error; |
| 9656 | |
| 9657 | case HLUA_E_YIELD: /* unexpected */ |
| 9658 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9659 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9660 | goto error; |
| 9661 | |
| 9662 | case HLUA_E_ERR: |
| 9663 | /* Display log. */ |
| 9664 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9665 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9666 | goto error; |
| 9667 | |
| 9668 | default: |
| 9669 | goto error; |
| 9670 | } |
| 9671 | } |
| 9672 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9673 | if (http_ctx->flags & APPLET_DONE) { |
| 9674 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9675 | goto done; |
| 9676 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9677 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9678 | goto error; |
| 9679 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9680 | /* no more data are expected. If the response buffer is empty |
| 9681 | * for a chunked message, be sure to add something (EOT block in |
| 9682 | * this case) to have something to send. It is important to be |
| 9683 | * sure the EOM flags will be handled by the endpoint. |
| 9684 | */ |
| 9685 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9686 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9687 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9688 | goto out; |
| 9689 | } |
| 9690 | channel_add_input(res, 1); |
| 9691 | } |
| 9692 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9693 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9694 | res->flags |= CF_EOI; |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 9695 | se_fl_set(ctx->sedesc, SE_FL_EOI); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9696 | strm->txn->status = http_ctx->status; |
| 9697 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9698 | } |
| 9699 | |
| 9700 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9701 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9702 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9703 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9704 | sc_shutr(sc); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9705 | } |
| 9706 | |
| 9707 | /* eat the whole request */ |
| 9708 | if (co_data(req)) { |
| 9709 | req_htx = htx_from_buf(&req->buf); |
| 9710 | co_htx_skip(req, req_htx, co_data(req)); |
| 9711 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9712 | } |
| 9713 | } |
| 9714 | |
| 9715 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9716 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9717 | return; |
| 9718 | |
| 9719 | error: |
| 9720 | |
| 9721 | /* If we are in HTTP mode, and we are not send any |
| 9722 | * data, return a 500 server error in best effort: |
| 9723 | * if there is no room available in the buffer, |
| 9724 | * just close the connection. |
| 9725 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9726 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9727 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9728 | |
| 9729 | channel_erase(res); |
| 9730 | res->buf.data = b_data(err); |
| 9731 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9732 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9733 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9734 | } |
| 9735 | if (!(strm->flags & SF_ERR_MASK)) |
| 9736 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9737 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9738 | goto done; |
| 9739 | } |
| 9740 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9741 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9742 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9743 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9744 | |
| 9745 | task_destroy(http_ctx->task); |
| 9746 | http_ctx->task = NULL; |
| 9747 | hlua_ctx_destroy(http_ctx->hlua); |
| 9748 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9749 | } |
| 9750 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9751 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9752 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9753 | * |
| 9754 | * This function can fail with an abort() due to an Lua critical error. |
| 9755 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9756 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9757 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9758 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9759 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9760 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9761 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9762 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9763 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9764 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9765 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9766 | if (!rule->arg.hlua_rule) { |
| 9767 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9768 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9769 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9770 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9771 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9772 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9773 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9774 | if (!rule->arg.hlua_rule->args) { |
| 9775 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9776 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9777 | } |
| 9778 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9779 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9780 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9781 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9782 | /* Expect some arguments */ |
| 9783 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9784 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9785 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9786 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9787 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9788 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9789 | if (!rule->arg.hlua_rule->args[i]) { |
| 9790 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9791 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9792 | } |
| 9793 | (*cur_arg)++; |
| 9794 | } |
| 9795 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9796 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9797 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9798 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9799 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9800 | |
| 9801 | error: |
| 9802 | if (rule->arg.hlua_rule) { |
| 9803 | if (rule->arg.hlua_rule->args) { |
| 9804 | for (i = 0; i < fcn->nargs; i++) |
| 9805 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9806 | ha_free(&rule->arg.hlua_rule->args); |
| 9807 | } |
| 9808 | ha_free(&rule->arg.hlua_rule); |
| 9809 | } |
| 9810 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9811 | } |
| 9812 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9813 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9814 | struct act_rule *rule, char **err) |
| 9815 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9816 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9817 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9818 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9819 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9820 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9821 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9822 | * the call of this analyzer. |
| 9823 | */ |
| 9824 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9825 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9826 | return ACT_RET_PRS_ERR; |
| 9827 | } |
| 9828 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9829 | /* Memory for the rule. */ |
| 9830 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9831 | if (!rule->arg.hlua_rule) { |
| 9832 | memprintf(err, "out of memory error"); |
| 9833 | return ACT_RET_PRS_ERR; |
| 9834 | } |
| 9835 | |
| 9836 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9837 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9838 | |
| 9839 | /* TODO: later accept arguments. */ |
| 9840 | rule->arg.hlua_rule->args = NULL; |
| 9841 | |
| 9842 | /* Add applet pointer in the rule. */ |
| 9843 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9844 | rule->applet.name = fcn->name; |
| 9845 | rule->applet.init = hlua_applet_http_init; |
| 9846 | rule->applet.fct = hlua_applet_http_fct; |
| 9847 | rule->applet.release = hlua_applet_http_release; |
| 9848 | rule->applet.timeout = hlua_timeout_applet; |
| 9849 | |
| 9850 | return ACT_RET_PRS_OK; |
| 9851 | } |
| 9852 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9853 | /* This function is an LUA binding used for registering |
| 9854 | * "sample-conv" functions. It expects a converter name used |
| 9855 | * in the haproxy configuration file, and an LUA function. |
| 9856 | */ |
| 9857 | __LJMP static int hlua_register_action(lua_State *L) |
| 9858 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9859 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9860 | const char *name; |
| 9861 | int ref; |
| 9862 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9863 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9864 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9865 | struct buffer *trash; |
| 9866 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9867 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9868 | /* Initialise the number of expected arguments at 0. */ |
| 9869 | nargs = 0; |
| 9870 | |
| 9871 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9872 | 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] | 9873 | |
| 9874 | /* First argument : converter name. */ |
| 9875 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9876 | |
| 9877 | /* Second argument : environment. */ |
| 9878 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9879 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9880 | |
| 9881 | /* Third argument : lua function. */ |
| 9882 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9883 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9884 | /* 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] | 9885 | if (lua_gettop(L) >= 4) |
| 9886 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9887 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9888 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9889 | lua_pushnil(L); |
| 9890 | while (lua_next(L, 2) != 0) { |
| 9891 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9892 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9893 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9894 | /* Check if action exists */ |
| 9895 | trash = get_trash_chunk(); |
| 9896 | chunk_printf(trash, "lua.%s", name); |
| 9897 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9898 | akw = tcp_req_cont_action(trash->area); |
| 9899 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9900 | akw = tcp_res_cont_action(trash->area); |
| 9901 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9902 | akw = action_http_req_custom(trash->area); |
| 9903 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9904 | akw = action_http_res_custom(trash->area); |
| 9905 | } else { |
| 9906 | akw = NULL; |
| 9907 | } |
| 9908 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9909 | fcn = akw->private; |
| 9910 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9911 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9912 | "This will become a hard error in version 2.5.\n", name); |
| 9913 | } |
| 9914 | fcn->function_ref[hlua_state_id] = ref; |
| 9915 | |
| 9916 | /* pop the environment string. */ |
| 9917 | lua_pop(L, 1); |
| 9918 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9919 | } |
| 9920 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9921 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9922 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9923 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9924 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9925 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9926 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9927 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9928 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9929 | |
| 9930 | /* Fill fcn. */ |
| 9931 | fcn->name = strdup(name); |
| 9932 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9933 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9934 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9935 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9936 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9937 | fcn->nargs = nargs; |
| 9938 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9939 | /* List head */ |
| 9940 | akl->list.n = akl->list.p = NULL; |
| 9941 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9942 | /* action keyword. */ |
| 9943 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9944 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9945 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9946 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9947 | |
| 9948 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9949 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9950 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9951 | akl->kw[0].private = fcn; |
| 9952 | akl->kw[0].parse = action_register_lua; |
| 9953 | |
| 9954 | /* select the action registering point. */ |
| 9955 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9956 | tcp_req_cont_keywords_register(akl); |
| 9957 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9958 | tcp_res_cont_keywords_register(akl); |
| 9959 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9960 | http_req_keywords_register(akl); |
| 9961 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9962 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9963 | else { |
| 9964 | release_hlua_function(fcn); |
| 9965 | if (akl) |
| 9966 | ha_free((char **)&(akl->kw[0].kw)); |
| 9967 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9968 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9969 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9970 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9971 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9972 | |
| 9973 | /* pop the environment string. */ |
| 9974 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9975 | |
| 9976 | /* reset for next loop */ |
| 9977 | akl = NULL; |
| 9978 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9979 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9980 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9981 | |
| 9982 | alloc_error: |
| 9983 | release_hlua_function(fcn); |
| 9984 | ha_free(&akl); |
| 9985 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9986 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9987 | } |
| 9988 | |
| 9989 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9990 | struct act_rule *rule, char **err) |
| 9991 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9992 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9993 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9994 | if (px->mode == PR_MODE_HTTP) { |
| 9995 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9996 | return ACT_RET_PRS_ERR; |
| 9997 | } |
| 9998 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9999 | /* Memory for the rule. */ |
| 10000 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 10001 | if (!rule->arg.hlua_rule) { |
| 10002 | memprintf(err, "out of memory error"); |
| 10003 | return ACT_RET_PRS_ERR; |
| 10004 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10005 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10006 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 10007 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10008 | |
| 10009 | /* TODO: later accept arguments. */ |
| 10010 | rule->arg.hlua_rule->args = NULL; |
| 10011 | |
| 10012 | /* Add applet pointer in the rule. */ |
| 10013 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 10014 | rule->applet.name = fcn->name; |
| 10015 | rule->applet.init = hlua_applet_tcp_init; |
| 10016 | rule->applet.fct = hlua_applet_tcp_fct; |
| 10017 | rule->applet.release = hlua_applet_tcp_release; |
| 10018 | rule->applet.timeout = hlua_timeout_applet; |
| 10019 | |
| 10020 | return 0; |
| 10021 | } |
| 10022 | |
| 10023 | /* This function is an LUA binding used for registering |
| 10024 | * "sample-conv" functions. It expects a converter name used |
| 10025 | * in the haproxy configuration file, and an LUA function. |
| 10026 | */ |
| 10027 | __LJMP static int hlua_register_service(lua_State *L) |
| 10028 | { |
| 10029 | struct action_kw_list *akl; |
| 10030 | const char *name; |
| 10031 | const char *env; |
| 10032 | int ref; |
| 10033 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10034 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10035 | struct buffer *trash; |
| 10036 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10037 | |
| 10038 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10039 | |
| 10040 | /* First argument : converter name. */ |
| 10041 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10042 | |
| 10043 | /* Second argument : environment. */ |
| 10044 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10045 | |
| 10046 | /* Third argument : lua function. */ |
| 10047 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10048 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10049 | /* Check for service already registered */ |
| 10050 | trash = get_trash_chunk(); |
| 10051 | chunk_printf(trash, "lua.%s", name); |
| 10052 | akw = service_find(trash->area); |
| 10053 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10054 | fcn = akw->private; |
| 10055 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10056 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10057 | "This will become a hard error in version 2.5.\n", name); |
| 10058 | } |
| 10059 | fcn->function_ref[hlua_state_id] = ref; |
| 10060 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10061 | } |
| 10062 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10063 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10064 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10065 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10066 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10067 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10068 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10069 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10070 | |
| 10071 | /* Fill fcn. */ |
| 10072 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10073 | fcn->name = calloc(1, len); |
| 10074 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10075 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10076 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10077 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10078 | |
| 10079 | /* List head */ |
| 10080 | akl->list.n = akl->list.p = NULL; |
| 10081 | |
| 10082 | /* converter keyword. */ |
| 10083 | len = strlen("lua.") + strlen(name) + 1; |
| 10084 | akl->kw[0].kw = calloc(1, len); |
| 10085 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10086 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10087 | |
| 10088 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10089 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10090 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10091 | if (strcmp(env, "tcp") == 0) |
| 10092 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10093 | else if (strcmp(env, "http") == 0) |
| 10094 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10095 | else { |
| 10096 | release_hlua_function(fcn); |
| 10097 | if (akl) |
| 10098 | ha_free((char **)&(akl->kw[0].kw)); |
| 10099 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10100 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10101 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10102 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10103 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10104 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10105 | akl->kw[0].private = fcn; |
| 10106 | |
| 10107 | /* End of array. */ |
| 10108 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10109 | |
| 10110 | /* Register this new converter */ |
| 10111 | service_keywords_register(akl); |
| 10112 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10113 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10114 | |
| 10115 | alloc_error: |
| 10116 | release_hlua_function(fcn); |
| 10117 | ha_free(&akl); |
| 10118 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10119 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10120 | } |
| 10121 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10122 | /* This function initialises Lua cli handler. It copies the |
| 10123 | * arguments in the Lua stack and create channel IO objects. |
| 10124 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10125 | 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] | 10126 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10127 | 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] | 10128 | struct hlua *hlua; |
| 10129 | struct hlua_function *fcn; |
| 10130 | int i; |
| 10131 | const char *error; |
| 10132 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10133 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10134 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10135 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10136 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10137 | if (!hlua) { |
| 10138 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10139 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10140 | } |
| 10141 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10142 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10143 | |
| 10144 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10145 | * 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] | 10146 | * applet_http. It is absolutely compatible. |
| 10147 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10148 | ctx->task = task_new_here(); |
| 10149 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10150 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10151 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10152 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10153 | ctx->task->nice = 0; |
| 10154 | ctx->task->context = appctx; |
| 10155 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10156 | |
| 10157 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10158 | 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] | 10159 | 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] | 10160 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10161 | } |
| 10162 | |
| 10163 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10164 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10165 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10166 | error = lua_tostring(hlua->T, -1); |
| 10167 | else |
| 10168 | error = "critical error"; |
| 10169 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10170 | goto error; |
| 10171 | } |
| 10172 | |
| 10173 | /* Check stack available size. */ |
| 10174 | if (!lua_checkstack(hlua->T, 2)) { |
| 10175 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10176 | goto error; |
| 10177 | } |
| 10178 | |
| 10179 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10180 | 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] | 10181 | |
| 10182 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10183 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10184 | */ |
| 10185 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10186 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10187 | goto error; |
| 10188 | } |
| 10189 | hlua->nargs = 1; |
| 10190 | |
| 10191 | /* push keywords in the stack. */ |
| 10192 | for (i = 0; *args[i]; i++) { |
| 10193 | /* Check stack available size. */ |
| 10194 | if (!lua_checkstack(hlua->T, 1)) { |
| 10195 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10196 | goto error; |
| 10197 | } |
| 10198 | lua_pushstring(hlua->T, args[i]); |
| 10199 | hlua->nargs++; |
| 10200 | } |
| 10201 | |
| 10202 | /* We must initialize the execution timeouts. */ |
| 10203 | hlua->max_time = hlua_timeout_session; |
| 10204 | |
| 10205 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10206 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10207 | |
| 10208 | /* It's ok */ |
| 10209 | return 0; |
| 10210 | |
| 10211 | /* It's not ok. */ |
| 10212 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10213 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10214 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10215 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10216 | return 1; |
| 10217 | } |
| 10218 | |
| 10219 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10220 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10221 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10222 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10223 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10224 | struct hlua_function *fcn; |
| 10225 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10226 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10227 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10228 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10229 | |
| 10230 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10231 | 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] | 10232 | return 1; |
| 10233 | |
| 10234 | /* Execute the function. */ |
| 10235 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10236 | |
| 10237 | /* finished. */ |
| 10238 | case HLUA_E_OK: |
| 10239 | return 1; |
| 10240 | |
| 10241 | /* yield. */ |
| 10242 | case HLUA_E_AGAIN: |
| 10243 | /* We want write. */ |
| 10244 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10245 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10246 | /* Set the timeout. */ |
| 10247 | if (hlua->wake_time != TICK_ETERNITY) |
| 10248 | task_schedule(hlua->task, hlua->wake_time); |
| 10249 | return 0; |
| 10250 | |
| 10251 | /* finished with error. */ |
| 10252 | case HLUA_E_ERRMSG: |
| 10253 | /* Display log. */ |
| 10254 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10255 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10256 | lua_pop(hlua->T, 1); |
| 10257 | return 1; |
| 10258 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10259 | case HLUA_E_ETMOUT: |
| 10260 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10261 | fcn->name); |
| 10262 | return 1; |
| 10263 | |
| 10264 | case HLUA_E_NOMEM: |
| 10265 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10266 | fcn->name); |
| 10267 | return 1; |
| 10268 | |
| 10269 | case HLUA_E_YIELD: /* unexpected */ |
| 10270 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10271 | fcn->name); |
| 10272 | return 1; |
| 10273 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10274 | case HLUA_E_ERR: |
| 10275 | /* Display log. */ |
| 10276 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10277 | fcn->name); |
| 10278 | return 1; |
| 10279 | |
| 10280 | default: |
| 10281 | return 1; |
| 10282 | } |
| 10283 | |
| 10284 | return 1; |
| 10285 | } |
| 10286 | |
| 10287 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10288 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10289 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10290 | |
| 10291 | hlua_ctx_destroy(ctx->hlua); |
| 10292 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10293 | } |
| 10294 | |
| 10295 | /* This function is an LUA binding used for registering |
| 10296 | * new keywords in the cli. It expects a list of keywords |
| 10297 | * which are the "path". It is limited to 5 keywords. A |
| 10298 | * description of the command, a function to be executed |
| 10299 | * for the parsing and a function for io handlers. |
| 10300 | */ |
| 10301 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10302 | { |
| 10303 | struct cli_kw_list *cli_kws; |
| 10304 | const char *message; |
| 10305 | int ref_io; |
| 10306 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10307 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10308 | int index; |
| 10309 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10310 | struct buffer *trash; |
| 10311 | const char *kw[5]; |
| 10312 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10313 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10314 | |
| 10315 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10316 | |
| 10317 | /* First argument : an array of maximum 5 keywords. */ |
| 10318 | if (!lua_istable(L, 1)) |
| 10319 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10320 | |
| 10321 | /* Second argument : string with contextual message. */ |
| 10322 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10323 | |
| 10324 | /* Third and fourth argument : lua function. */ |
| 10325 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10326 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10327 | /* Check for CLI service already registered */ |
| 10328 | trash = get_trash_chunk(); |
| 10329 | index = 0; |
| 10330 | lua_pushnil(L); |
| 10331 | memset(kw, 0, sizeof(kw)); |
| 10332 | while (lua_next(L, 1) != 0) { |
| 10333 | if (index >= CLI_PREFIX_KW_NB) |
| 10334 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10335 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10336 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10337 | kw[index] = lua_tostring(L, -1); |
| 10338 | if (index == 0) |
| 10339 | chunk_printf(trash, "%s", kw[index]); |
| 10340 | else |
| 10341 | chunk_appendf(trash, " %s", kw[index]); |
| 10342 | index++; |
| 10343 | lua_pop(L, 1); |
| 10344 | } |
| 10345 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10346 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10347 | fcn = cli_kw->private; |
| 10348 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10349 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10350 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10351 | } |
| 10352 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10353 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10354 | } |
| 10355 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10356 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10357 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10358 | if (!cli_kws) { |
| 10359 | errmsg = "Lua out of memory error."; |
| 10360 | goto error; |
| 10361 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10362 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10363 | if (!fcn) { |
| 10364 | errmsg = "Lua out of memory error."; |
| 10365 | goto error; |
| 10366 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10367 | |
| 10368 | /* Fill path. */ |
| 10369 | index = 0; |
| 10370 | lua_pushnil(L); |
| 10371 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10372 | if (index >= 5) { |
| 10373 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10374 | goto error; |
| 10375 | } |
| 10376 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10377 | errmsg = "1st argument must be a table filled with strings"; |
| 10378 | goto error; |
| 10379 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10380 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10381 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10382 | errmsg = "Lua out of memory error."; |
| 10383 | goto error; |
| 10384 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10385 | index++; |
| 10386 | lua_pop(L, 1); |
| 10387 | } |
| 10388 | |
| 10389 | /* Copy help message. */ |
| 10390 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10391 | if (!cli_kws->kw[0].usage) { |
| 10392 | errmsg = "Lua out of memory error."; |
| 10393 | goto error; |
| 10394 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10395 | |
| 10396 | /* Fill fcn io handler. */ |
| 10397 | len = strlen("<lua.cli>") + 1; |
| 10398 | for (i = 0; i < index; i++) |
| 10399 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10400 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10401 | if (!fcn->name) { |
| 10402 | errmsg = "Lua out of memory error."; |
| 10403 | goto error; |
| 10404 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10405 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10406 | for (i = 0; i < index; i++) { |
| 10407 | strncat((char *)fcn->name, ".", len); |
| 10408 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10409 | } |
| 10410 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10411 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10412 | |
| 10413 | /* Fill last entries. */ |
| 10414 | cli_kws->kw[0].private = fcn; |
| 10415 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10416 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10417 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10418 | |
| 10419 | /* Register this new converter */ |
| 10420 | cli_register_kw(cli_kws); |
| 10421 | |
| 10422 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10423 | |
| 10424 | error: |
| 10425 | release_hlua_function(fcn); |
| 10426 | if (cli_kws) { |
| 10427 | for (i = 0; i < index; i++) |
| 10428 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10429 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10430 | } |
| 10431 | ha_free(&cli_kws); |
| 10432 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10433 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10434 | } |
| 10435 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10436 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10437 | { |
| 10438 | struct hlua_flt_config *conf = fconf->conf; |
| 10439 | lua_State *L; |
| 10440 | int error, pos, state_id, flt_ref; |
| 10441 | |
| 10442 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10443 | L = hlua_states[state_id]; |
| 10444 | pos = lua_gettop(L); |
| 10445 | |
| 10446 | /* The filter parsing function */ |
| 10447 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10448 | |
| 10449 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10450 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10451 | |
| 10452 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10453 | lua_newtable(L); |
| 10454 | lua_pushnil(L); |
| 10455 | |
| 10456 | while (lua_next(L, pos+2)) { |
| 10457 | lua_pushvalue(L, -2); |
| 10458 | lua_insert(L, -2); |
| 10459 | lua_settable(L, -4); |
| 10460 | } |
| 10461 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10462 | |
| 10463 | /* Remove the original lua filter class from the stack */ |
| 10464 | lua_pop(L, 1); |
| 10465 | |
| 10466 | /* Push the copy on the stack */ |
| 10467 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10468 | |
| 10469 | /* extra args are pushed in a table */ |
| 10470 | lua_newtable(L); |
| 10471 | for (pos = 0; conf->args[pos]; pos++) { |
| 10472 | /* Check stack available size. */ |
| 10473 | if (!lua_checkstack(L, 1)) { |
| 10474 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10475 | goto error; |
| 10476 | } |
| 10477 | lua_pushstring(L, conf->args[pos]); |
| 10478 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10479 | } |
| 10480 | |
| 10481 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10482 | switch (error) { |
| 10483 | case LUA_OK: |
| 10484 | /* replace the filter ref */ |
| 10485 | conf->ref[state_id] = flt_ref; |
| 10486 | break; |
| 10487 | case LUA_ERRRUN: |
| 10488 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10489 | goto error; |
| 10490 | case LUA_ERRMEM: |
| 10491 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10492 | goto error; |
| 10493 | case LUA_ERRERR: |
| 10494 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10495 | goto error; |
| 10496 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10497 | case LUA_ERRGCMM: |
| 10498 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10499 | goto error; |
| 10500 | #endif |
| 10501 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10502 | 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] | 10503 | goto error; |
| 10504 | } |
| 10505 | |
| 10506 | lua_settop(L, 0); |
| 10507 | return 0; |
| 10508 | |
| 10509 | error: |
| 10510 | lua_settop(L, 0); |
| 10511 | return -1; |
| 10512 | } |
| 10513 | |
| 10514 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10515 | { |
| 10516 | struct hlua_flt_config *conf = fconf->conf; |
| 10517 | lua_State *L; |
| 10518 | int state_id; |
| 10519 | |
| 10520 | if (!conf) |
| 10521 | return; |
| 10522 | |
| 10523 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10524 | L = hlua_states[state_id]; |
| 10525 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10526 | } |
| 10527 | |
| 10528 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10529 | { |
| 10530 | struct hlua_flt_config *conf = fconf->conf; |
| 10531 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10532 | |
| 10533 | /* Rely on per-thread init for global scripts */ |
| 10534 | if (!state_id) |
| 10535 | return hlua_filter_init_per_thread(px, fconf); |
| 10536 | return 0; |
| 10537 | } |
| 10538 | |
| 10539 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10540 | { |
| 10541 | |
| 10542 | if (fconf->conf) { |
| 10543 | struct hlua_flt_config *conf = fconf->conf; |
| 10544 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10545 | int pos; |
| 10546 | |
| 10547 | /* Rely on per-thread deinit for global scripts */ |
| 10548 | if (!state_id) |
| 10549 | hlua_filter_deinit_per_thread(px, fconf); |
| 10550 | |
| 10551 | for (pos = 0; conf->args[pos]; pos++) |
| 10552 | free(conf->args[pos]); |
| 10553 | free(conf->args); |
| 10554 | } |
| 10555 | ha_free(&fconf->conf); |
| 10556 | ha_free((char **)&fconf->id); |
| 10557 | ha_free(&fconf->ops); |
| 10558 | } |
| 10559 | |
| 10560 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10561 | { |
| 10562 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10563 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10564 | int ret = 1; |
| 10565 | |
| 10566 | /* In the execution wrappers linked with a stream, the |
| 10567 | * Lua context can be not initialized. This behavior |
| 10568 | * permits to save performances because a systematic |
| 10569 | * Lua initialization cause 5% performances loss. |
| 10570 | */ |
| 10571 | if (!s->hlua) { |
| 10572 | struct hlua *hlua; |
| 10573 | |
| 10574 | hlua = pool_alloc(pool_head_hlua); |
| 10575 | if (!hlua) { |
| 10576 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10577 | conf->reg->name); |
| 10578 | ret = 0; |
| 10579 | goto end; |
| 10580 | } |
| 10581 | HLUA_INIT(hlua); |
| 10582 | s->hlua = hlua; |
| 10583 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10584 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10585 | conf->reg->name); |
| 10586 | ret = 0; |
| 10587 | goto end; |
| 10588 | } |
| 10589 | } |
| 10590 | |
| 10591 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10592 | if (!flt_ctx) { |
| 10593 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10594 | conf->reg->name); |
| 10595 | ret = 0; |
| 10596 | goto end; |
| 10597 | } |
| 10598 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10599 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10600 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10601 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10602 | conf->reg->name); |
| 10603 | ret = 0; |
| 10604 | goto end; |
| 10605 | } |
| 10606 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10607 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10608 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10609 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10610 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10611 | conf->reg->name); |
| 10612 | ret = 0; |
| 10613 | goto end; |
| 10614 | } |
| 10615 | |
| 10616 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10617 | /* The following Lua calls can fail. */ |
| 10618 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10619 | const char *error; |
| 10620 | |
| 10621 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10622 | error = lua_tostring(s->hlua->T, -1); |
| 10623 | else |
| 10624 | error = "critical error"; |
| 10625 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10626 | ret = 0; |
| 10627 | goto end; |
| 10628 | } |
| 10629 | |
| 10630 | /* Check stack size. */ |
| 10631 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10632 | 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] | 10633 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10634 | ret = 0; |
| 10635 | goto end; |
| 10636 | } |
| 10637 | |
| 10638 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10639 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10640 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10641 | conf->reg->name); |
| 10642 | RESET_SAFE_LJMP(s->hlua); |
| 10643 | ret = 0; |
| 10644 | goto end; |
| 10645 | } |
| 10646 | lua_insert(s->hlua->T, -2); |
| 10647 | |
| 10648 | /* Push the copy on the stack */ |
| 10649 | s->hlua->nargs = 1; |
| 10650 | |
| 10651 | /* We must initialize the execution timeouts. */ |
| 10652 | s->hlua->max_time = hlua_timeout_session; |
| 10653 | |
| 10654 | /* At this point the execution is safe. */ |
| 10655 | RESET_SAFE_LJMP(s->hlua); |
| 10656 | } |
| 10657 | |
| 10658 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10659 | case HLUA_E_OK: |
| 10660 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10661 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10662 | ret = 0; |
| 10663 | goto end; |
| 10664 | } |
| 10665 | |
| 10666 | /* Attached the filter pointer to the ctx */ |
| 10667 | lua_pushstring(s->hlua->T, "__filter"); |
| 10668 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10669 | lua_settable(s->hlua->T, -3); |
| 10670 | |
| 10671 | /* Save a ref on the filter ctx */ |
| 10672 | lua_pushvalue(s->hlua->T, 1); |
| 10673 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10674 | filter->ctx = flt_ctx; |
| 10675 | break; |
| 10676 | case HLUA_E_ERRMSG: |
| 10677 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10678 | ret = -1; |
| 10679 | goto end; |
| 10680 | case HLUA_E_ETMOUT: |
| 10681 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10682 | ret = 0; |
| 10683 | goto end; |
| 10684 | case HLUA_E_NOMEM: |
| 10685 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10686 | ret = 0; |
| 10687 | goto end; |
| 10688 | case HLUA_E_AGAIN: |
| 10689 | case HLUA_E_YIELD: |
| 10690 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10691 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10692 | ret = 0; |
| 10693 | goto end; |
| 10694 | case HLUA_E_ERR: |
| 10695 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10696 | ret = 0; |
| 10697 | goto end; |
| 10698 | default: |
| 10699 | ret = 0; |
| 10700 | goto end; |
| 10701 | } |
| 10702 | |
| 10703 | end: |
| 10704 | if (s->hlua) |
| 10705 | lua_settop(s->hlua->T, 0); |
| 10706 | if (ret <= 0) { |
| 10707 | if (flt_ctx) { |
| 10708 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10709 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10710 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10711 | } |
| 10712 | } |
| 10713 | return ret; |
| 10714 | } |
| 10715 | |
| 10716 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10717 | { |
| 10718 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10719 | |
| 10720 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10721 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10722 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10723 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10724 | filter->ctx = NULL; |
| 10725 | } |
| 10726 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10727 | static int hlua_filter_from_payload(struct filter *filter) |
| 10728 | { |
| 10729 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10730 | |
| 10731 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10732 | } |
| 10733 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10734 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10735 | int dir, unsigned int flags) |
| 10736 | { |
| 10737 | struct hlua *flt_hlua; |
| 10738 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10739 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10740 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10741 | int ret = 1; |
| 10742 | |
| 10743 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10744 | if (!flt_hlua) |
| 10745 | goto end; |
| 10746 | |
| 10747 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10748 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10749 | |
| 10750 | /* The following Lua calls can fail. */ |
| 10751 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10752 | const char *error; |
| 10753 | |
| 10754 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10755 | error = lua_tostring(flt_hlua->T, -1); |
| 10756 | else |
| 10757 | error = "critical error"; |
| 10758 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10759 | goto end; |
| 10760 | } |
| 10761 | |
| 10762 | /* Check stack size. */ |
| 10763 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10764 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10765 | RESET_SAFE_LJMP(flt_hlua); |
| 10766 | goto end; |
| 10767 | } |
| 10768 | |
| 10769 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10770 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10771 | RESET_SAFE_LJMP(flt_hlua); |
| 10772 | goto end; |
| 10773 | } |
| 10774 | lua_insert(flt_hlua->T, -2); |
| 10775 | |
| 10776 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10777 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10778 | RESET_SAFE_LJMP(flt_hlua); |
| 10779 | goto end; |
| 10780 | } |
| 10781 | flt_hlua->nargs = 2; |
| 10782 | |
| 10783 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10784 | if (dir == SMP_OPT_DIR_REQ) |
| 10785 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10786 | else |
| 10787 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10788 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10789 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10790 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10791 | lua_settable(flt_hlua->T, -3); |
| 10792 | } |
| 10793 | flt_hlua->nargs++; |
| 10794 | } |
| 10795 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10796 | if (dir == SMP_OPT_DIR_REQ) |
| 10797 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10798 | else |
| 10799 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10800 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10801 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10802 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10803 | lua_settable(flt_hlua->T, -3); |
| 10804 | } |
| 10805 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10806 | } |
| 10807 | |
| 10808 | /* Check stack size. */ |
| 10809 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10810 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10811 | RESET_SAFE_LJMP(flt_hlua); |
| 10812 | goto end; |
| 10813 | } |
| 10814 | |
| 10815 | while (extra_idx--) { |
| 10816 | lua_pushvalue(flt_hlua->T, 1); |
| 10817 | lua_remove(flt_hlua->T, 1); |
| 10818 | flt_hlua->nargs++; |
| 10819 | } |
| 10820 | |
| 10821 | /* We must initialize the execution timeouts. */ |
| 10822 | flt_hlua->max_time = hlua_timeout_session; |
| 10823 | |
| 10824 | /* At this point the execution is safe. */ |
| 10825 | RESET_SAFE_LJMP(flt_hlua); |
| 10826 | } |
| 10827 | |
| 10828 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10829 | case HLUA_E_OK: |
| 10830 | /* Catch the return value if it required */ |
| 10831 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10832 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10833 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10834 | } |
| 10835 | |
| 10836 | /* Set timeout in the required channel. */ |
| 10837 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10838 | if (dir == SMP_OPT_DIR_REQ) |
| 10839 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10840 | else |
| 10841 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10842 | } |
| 10843 | break; |
| 10844 | case HLUA_E_AGAIN: |
| 10845 | /* Set timeout in the required channel. */ |
| 10846 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10847 | if (dir == SMP_OPT_DIR_REQ) |
| 10848 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10849 | else |
| 10850 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10851 | } |
| 10852 | /* Some actions can be wake up when a "write" event |
| 10853 | * is detected on a response channel. This is useful |
| 10854 | * only for actions targeted on the requests. |
| 10855 | */ |
| 10856 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10857 | s->res.flags |= CF_WAKE_WRITE; |
| 10858 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10859 | s->req.flags |= CF_WAKE_WRITE; |
| 10860 | ret = 0; |
| 10861 | goto end; |
| 10862 | case HLUA_E_ERRMSG: |
| 10863 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10864 | ret = -1; |
| 10865 | goto end; |
| 10866 | case HLUA_E_ETMOUT: |
| 10867 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10868 | goto end; |
| 10869 | case HLUA_E_NOMEM: |
| 10870 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10871 | goto end; |
| 10872 | case HLUA_E_YIELD: |
| 10873 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10874 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10875 | goto end; |
| 10876 | case HLUA_E_ERR: |
| 10877 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10878 | goto end; |
| 10879 | default: |
| 10880 | goto end; |
| 10881 | } |
| 10882 | |
| 10883 | |
| 10884 | end: |
| 10885 | return ret; |
| 10886 | } |
| 10887 | |
| 10888 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10889 | { |
| 10890 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10891 | |
| 10892 | flt_ctx->flags = 0; |
| 10893 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10894 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10895 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10896 | } |
| 10897 | |
| 10898 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10899 | { |
| 10900 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10901 | |
| 10902 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10903 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10904 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10905 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10906 | } |
| 10907 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10908 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10909 | { |
| 10910 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10911 | |
| 10912 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10913 | return hlua_filter_callback(s, filter, "http_headers", |
| 10914 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10915 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10916 | } |
| 10917 | |
| 10918 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10919 | unsigned int offset, unsigned int len) |
| 10920 | { |
| 10921 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10922 | struct hlua *flt_hlua; |
| 10923 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10924 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10925 | int ret; |
| 10926 | |
| 10927 | flt_hlua = flt_ctx->hlua[idx]; |
| 10928 | flt_ctx->cur_off[idx] = offset; |
| 10929 | flt_ctx->cur_len[idx] = len; |
| 10930 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10931 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10932 | if (ret != -1) { |
| 10933 | ret = flt_ctx->cur_len[idx]; |
| 10934 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10935 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10936 | if (ret > flt_ctx->cur_len[idx]) |
| 10937 | ret = flt_ctx->cur_len[idx]; |
| 10938 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10939 | } |
| 10940 | } |
| 10941 | return ret; |
| 10942 | } |
| 10943 | |
| 10944 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10945 | { |
| 10946 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10947 | |
| 10948 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10949 | return hlua_filter_callback(s, filter, "http_end", |
| 10950 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10951 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10952 | } |
| 10953 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10954 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10955 | unsigned int offset, unsigned int len) |
| 10956 | { |
| 10957 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10958 | struct hlua *flt_hlua; |
| 10959 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10960 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10961 | int ret; |
| 10962 | |
| 10963 | flt_hlua = flt_ctx->hlua[idx]; |
| 10964 | flt_ctx->cur_off[idx] = offset; |
| 10965 | flt_ctx->cur_len[idx] = len; |
| 10966 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10967 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10968 | if (ret != -1) { |
| 10969 | ret = flt_ctx->cur_len[idx]; |
| 10970 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10971 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10972 | if (ret > flt_ctx->cur_len[idx]) |
| 10973 | ret = flt_ctx->cur_len[idx]; |
| 10974 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10975 | } |
| 10976 | } |
| 10977 | return ret; |
| 10978 | } |
| 10979 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10980 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10981 | struct flt_conf *fconf, char **err, void *private) |
| 10982 | { |
| 10983 | struct hlua_reg_filter *reg_flt = private; |
| 10984 | lua_State *L; |
| 10985 | struct hlua_flt_config *conf = NULL; |
| 10986 | const char *flt_id = NULL; |
| 10987 | int state_id, pos, flt_flags = 0; |
| 10988 | struct flt_ops *hlua_flt_ops = NULL; |
| 10989 | |
| 10990 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10991 | L = hlua_states[state_id]; |
| 10992 | |
| 10993 | /* Initialize the filter ops with default callbacks */ |
| 10994 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10995 | if (!hlua_flt_ops) |
| 10996 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10997 | hlua_flt_ops->init = hlua_filter_init; |
| 10998 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10999 | if (state_id) { |
| 11000 | /* Set per-thread callback if script is loaded per-thread */ |
| 11001 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 11002 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 11003 | } |
| 11004 | hlua_flt_ops->attach = hlua_filter_new; |
| 11005 | hlua_flt_ops->detach = hlua_filter_delete; |
| 11006 | |
| 11007 | /* Push the filter class on the stack and resolve all callbacks */ |
| 11008 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 11009 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11010 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 11011 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 11012 | lua_pop(L, 1); |
| 11013 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 11014 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 11015 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 11016 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 11017 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 11018 | lua_pop(L, 1); |
| 11019 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11020 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11021 | lua_pop(L, 1); |
| 11022 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11023 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11024 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11025 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11026 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11027 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11028 | |
| 11029 | /* Get id and flags of the filter class */ |
| 11030 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11031 | flt_id = lua_tostring(L, -1); |
| 11032 | lua_pop(L, 1); |
| 11033 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11034 | flt_flags = lua_tointeger(L, -1); |
| 11035 | lua_pop(L, 1); |
| 11036 | |
| 11037 | /* Create the filter config */ |
| 11038 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11039 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11040 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11041 | conf->reg = reg_flt; |
| 11042 | |
| 11043 | /* duplicate args */ |
| 11044 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11045 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11046 | if (!conf->args) |
| 11047 | goto error; |
| 11048 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11049 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11050 | if (!conf->args[pos]) |
| 11051 | goto error; |
| 11052 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11053 | conf->args[pos] = NULL; |
| 11054 | *cur_arg += pos + 1; |
| 11055 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11056 | if (flt_id) { |
| 11057 | fconf->id = strdup(flt_id); |
| 11058 | if (!fconf->id) |
| 11059 | goto error; |
| 11060 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11061 | fconf->flags = flt_flags; |
| 11062 | fconf->conf = conf; |
| 11063 | fconf->ops = hlua_flt_ops; |
| 11064 | |
| 11065 | lua_settop(L, 0); |
| 11066 | return 0; |
| 11067 | |
| 11068 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11069 | 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] | 11070 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11071 | if (conf && conf->args) { |
| 11072 | for (pos = 0; conf->args[pos]; pos++) |
| 11073 | free(conf->args[pos]); |
| 11074 | free(conf->args); |
| 11075 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11076 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11077 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11078 | lua_settop(L, 0); |
| 11079 | return -1; |
| 11080 | } |
| 11081 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11082 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11083 | { |
| 11084 | struct filter *filter; |
| 11085 | struct channel *chn; |
| 11086 | |
| 11087 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11088 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11089 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11090 | |
| 11091 | lua_getfield(L, 1, "__filter"); |
| 11092 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11093 | filter = lua_touserdata (L, -1); |
| 11094 | lua_pop(L, 1); |
| 11095 | |
| 11096 | register_data_filter(chn_strm(chn), chn, filter); |
| 11097 | return 1; |
| 11098 | } |
| 11099 | |
| 11100 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11101 | { |
| 11102 | struct filter *filter; |
| 11103 | struct channel *chn; |
| 11104 | |
| 11105 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11106 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11107 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11108 | |
| 11109 | lua_getfield(L, 1, "__filter"); |
| 11110 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11111 | filter = lua_touserdata (L, -1); |
| 11112 | lua_pop(L, 1); |
| 11113 | |
| 11114 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11115 | return 1; |
| 11116 | } |
| 11117 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11118 | /* 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] | 11119 | * 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] | 11120 | * parse configuration arguments. |
| 11121 | */ |
| 11122 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11123 | { |
| 11124 | struct buffer *trash; |
| 11125 | struct flt_kw_list *fkl; |
| 11126 | struct flt_kw *fkw; |
| 11127 | const char *name; |
| 11128 | struct hlua_reg_filter *reg_flt= NULL; |
| 11129 | int flt_ref, fun_ref; |
| 11130 | int len; |
| 11131 | |
| 11132 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11133 | |
| 11134 | /* First argument : filter name. */ |
| 11135 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11136 | |
| 11137 | /* Second argument : The filter class */ |
| 11138 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11139 | |
| 11140 | /* Third argument : lua function. */ |
| 11141 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11142 | |
| 11143 | trash = get_trash_chunk(); |
| 11144 | chunk_printf(trash, "lua.%s", name); |
| 11145 | fkw = flt_find_kw(trash->area); |
| 11146 | if (fkw != NULL) { |
| 11147 | reg_flt = fkw->private; |
| 11148 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11149 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11150 | "This will become a hard error in version 2.5.\n", name); |
| 11151 | } |
| 11152 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11153 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11154 | return 0; |
| 11155 | } |
| 11156 | |
| 11157 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11158 | if (!fkl) |
| 11159 | goto alloc_error; |
| 11160 | fkl->scope = "HLUA"; |
| 11161 | |
| 11162 | reg_flt = new_hlua_reg_filter(name); |
| 11163 | if (!reg_flt) |
| 11164 | goto alloc_error; |
| 11165 | |
| 11166 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11167 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11168 | |
| 11169 | /* The filter keyword */ |
| 11170 | len = strlen("lua.") + strlen(name) + 1; |
| 11171 | fkl->kw[0].kw = calloc(1, len); |
| 11172 | if (!fkl->kw[0].kw) |
| 11173 | goto alloc_error; |
| 11174 | |
| 11175 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11176 | |
| 11177 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11178 | fkl->kw[0].private = reg_flt; |
| 11179 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11180 | |
| 11181 | /* Register this new filter */ |
| 11182 | flt_register_keywords(fkl); |
| 11183 | |
| 11184 | return 0; |
| 11185 | |
| 11186 | alloc_error: |
| 11187 | release_hlua_reg_filter(reg_flt); |
| 11188 | ha_free(&fkl); |
| 11189 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11190 | return 0; /* Never reached */ |
| 11191 | } |
| 11192 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11193 | 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] | 11194 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11195 | char **err, unsigned int *timeout) |
| 11196 | { |
| 11197 | const char *error; |
| 11198 | |
| 11199 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11200 | if (error == PARSE_TIME_OVER) { |
| 11201 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11202 | args[1], args[0]); |
| 11203 | return -1; |
| 11204 | } |
| 11205 | else if (error == PARSE_TIME_UNDER) { |
| 11206 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11207 | args[1], args[0]); |
| 11208 | return -1; |
| 11209 | } |
| 11210 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11211 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11212 | return -1; |
| 11213 | } |
| 11214 | return 0; |
| 11215 | } |
| 11216 | |
| 11217 | 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] | 11218 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11219 | char **err) |
| 11220 | { |
| 11221 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11222 | file, line, err, &hlua_timeout_session); |
| 11223 | } |
| 11224 | |
| 11225 | 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] | 11226 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11227 | char **err) |
| 11228 | { |
| 11229 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11230 | file, line, err, &hlua_timeout_task); |
| 11231 | } |
| 11232 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11233 | 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] | 11234 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11235 | char **err) |
| 11236 | { |
| 11237 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11238 | file, line, err, &hlua_timeout_applet); |
| 11239 | } |
| 11240 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11241 | 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] | 11242 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11243 | char **err) |
| 11244 | { |
| 11245 | char *error; |
| 11246 | |
| 11247 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11248 | if (*error != '\0') { |
| 11249 | memprintf(err, "%s: invalid number", args[0]); |
| 11250 | return -1; |
| 11251 | } |
| 11252 | return 0; |
| 11253 | } |
| 11254 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11255 | 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] | 11256 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11257 | char **err) |
| 11258 | { |
| 11259 | char *error; |
| 11260 | |
| 11261 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11262 | 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] | 11263 | return -1; |
| 11264 | } |
| 11265 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11266 | if (*error != '\0') { |
| 11267 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11268 | return -1; |
| 11269 | } |
| 11270 | return 0; |
| 11271 | } |
| 11272 | |
| 11273 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11274 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11275 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11276 | * the main lua entry point. |
| 11277 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11278 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11279 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11280 | * |
| 11281 | * In some error case, LUA set an error message in top of the stack. This function |
| 11282 | * 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] | 11283 | * |
| 11284 | * This function can fail with an abort() due to an Lua critical error. |
| 11285 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11286 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11287 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11288 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11289 | { |
| 11290 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11291 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11292 | |
| 11293 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11294 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11295 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11296 | 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] | 11297 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11298 | return -1; |
| 11299 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11300 | |
| 11301 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11302 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11303 | /* Check stack size. */ |
| 11304 | if (!lua_checkstack(L, 1)) { |
| 11305 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11306 | return -1; |
| 11307 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11308 | lua_pushstring(L, args[nargs]); |
| 11309 | } |
| 11310 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11311 | |
| 11312 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11313 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11314 | switch (error) { |
| 11315 | case LUA_OK: |
| 11316 | break; |
| 11317 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11318 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11319 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11320 | return -1; |
| 11321 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11322 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11323 | return -1; |
| 11324 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11325 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11326 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11327 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11328 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11329 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11330 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11331 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11332 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11333 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11334 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11335 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11336 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11337 | return -1; |
| 11338 | } |
| 11339 | |
| 11340 | return 0; |
| 11341 | } |
| 11342 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11343 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11344 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11345 | char **err) |
| 11346 | { |
| 11347 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11348 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11349 | return -1; |
| 11350 | } |
| 11351 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11352 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11353 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11354 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11355 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11356 | } |
| 11357 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11358 | 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] | 11359 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11360 | char **err) |
| 11361 | { |
| 11362 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11363 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11364 | |
| 11365 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11366 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11367 | return -1; |
| 11368 | } |
| 11369 | |
| 11370 | if (per_thread_load == NULL) { |
| 11371 | /* allocate the first entry large enough to store the final NULL */ |
| 11372 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11373 | if (per_thread_load == NULL) { |
| 11374 | memprintf(err, "out of memory error"); |
| 11375 | return -1; |
| 11376 | } |
| 11377 | } |
| 11378 | |
| 11379 | /* count used entries */ |
| 11380 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11381 | ; |
| 11382 | |
| 11383 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11384 | if (per_thread_load == NULL) { |
| 11385 | memprintf(err, "out of memory error"); |
| 11386 | return -1; |
| 11387 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11388 | per_thread_load[len + 1] = NULL; |
| 11389 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11390 | /* count args excepting the first, allocate array and copy args */ |
| 11391 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11392 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11393 | if (per_thread_load[len] == NULL) { |
| 11394 | memprintf(err, "out of memory error"); |
| 11395 | return -1; |
| 11396 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11397 | for (i = 1; *(args[i]) != 0; i++) { |
| 11398 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11399 | if (per_thread_load[len][i - 1] == NULL) { |
| 11400 | memprintf(err, "out of memory error"); |
| 11401 | return -1; |
| 11402 | } |
| 11403 | } |
| 11404 | per_thread_load[len][i - 1] = strdup(""); |
| 11405 | if (per_thread_load[len][i - 1] == NULL) { |
| 11406 | memprintf(err, "out of memory error"); |
| 11407 | return -1; |
| 11408 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11409 | |
| 11410 | /* loading for thread 1 only */ |
| 11411 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11412 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11413 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11414 | } |
| 11415 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11416 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11417 | * in the given <ctx>. |
| 11418 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11419 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11420 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11421 | lua_getglobal(L, "package"); /* push package variable */ |
| 11422 | lua_pushstring(L, path); /* push given path */ |
| 11423 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11424 | lua_getfield(L, -3, type); /* push old path */ |
| 11425 | lua_concat(L, 3); /* concatenate to new path */ |
| 11426 | lua_setfield(L, -2, type); /* store new path */ |
| 11427 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11428 | |
| 11429 | return 0; |
| 11430 | } |
| 11431 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11432 | 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] | 11433 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11434 | char **err) |
| 11435 | { |
| 11436 | char *path; |
| 11437 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11438 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11439 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11440 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11441 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11442 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11443 | } |
| 11444 | |
| 11445 | if (!(*args[1])) { |
| 11446 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11447 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11448 | } |
| 11449 | path = args[1]; |
| 11450 | |
| 11451 | if (*args[2]) { |
| 11452 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11453 | 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] | 11454 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11455 | } |
| 11456 | type = args[2]; |
| 11457 | } |
| 11458 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11459 | p = calloc(1, sizeof(*p)); |
| 11460 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11461 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11462 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11463 | } |
| 11464 | p->path = strdup(path); |
| 11465 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11466 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11467 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11468 | } |
| 11469 | p->type = strdup(type); |
| 11470 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11471 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11472 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11473 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11474 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11475 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11476 | /* Handle the global state and the per-thread state for the first |
| 11477 | * thread. The remaining threads will be initialized based on |
| 11478 | * prepend_path_list. |
| 11479 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11480 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11481 | lua_State *L = hlua_states[i]; |
| 11482 | const char *error; |
| 11483 | |
| 11484 | if (setjmp(safe_ljmp_env) != 0) { |
| 11485 | lua_atpanic(L, hlua_panic_safe); |
| 11486 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11487 | error = lua_tostring(L, -1); |
| 11488 | else |
| 11489 | error = "critical error"; |
| 11490 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11491 | exit(1); |
| 11492 | } else { |
| 11493 | lua_atpanic(L, hlua_panic_ljmp); |
| 11494 | } |
| 11495 | |
| 11496 | hlua_prepend_path(L, type, path); |
| 11497 | |
| 11498 | lua_atpanic(L, hlua_panic_safe); |
| 11499 | } |
| 11500 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11501 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11502 | |
| 11503 | err2: |
| 11504 | free(p->type); |
| 11505 | free(p->path); |
| 11506 | err: |
| 11507 | free(p); |
| 11508 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11509 | } |
| 11510 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11511 | /* configuration keywords declaration */ |
| 11512 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11513 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11514 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11515 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11516 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11517 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11518 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11519 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11520 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11521 | { 0, NULL, NULL }, |
| 11522 | }}; |
| 11523 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11524 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11525 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11526 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11527 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11528 | /* |
| 11529 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11530 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11531 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11532 | * way. |
| 11533 | */ |
| 11534 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11535 | { |
| 11536 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11537 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11538 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11539 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11540 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11541 | struct hlua *hlua; |
| 11542 | char *err = NULL; |
| 11543 | int y = 1; |
| 11544 | |
| 11545 | hlua = hlua_gethlua(L); |
| 11546 | |
| 11547 | /* get the first ckchi to copy */ |
| 11548 | if (ckchi == NULL) |
| 11549 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11550 | |
| 11551 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11552 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11553 | struct ckch_inst *new_inst; |
| 11554 | |
| 11555 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11556 | if (y % 10 == 0) { |
| 11557 | |
| 11558 | *lua_ckchi = ckchi; |
| 11559 | |
| 11560 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11561 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11562 | } |
| 11563 | |
| 11564 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11565 | goto error; |
| 11566 | |
| 11567 | /* link the new ckch_inst to the duplicate */ |
| 11568 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11569 | y++; |
| 11570 | } |
| 11571 | |
| 11572 | /* The generation is finished, we can insert everything */ |
| 11573 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11574 | |
| 11575 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11576 | |
| 11577 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11578 | |
| 11579 | return 0; |
| 11580 | |
| 11581 | error: |
| 11582 | ckch_store_free(new_ckchs); |
| 11583 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11584 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11585 | free(err); |
| 11586 | |
| 11587 | return 0; |
| 11588 | } |
| 11589 | |
| 11590 | /* |
| 11591 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11592 | * from the table in parameter. |
| 11593 | * |
| 11594 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11595 | * means it does not need to have a transaction since everything is done in the |
| 11596 | * same function. |
| 11597 | * |
| 11598 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11599 | * |
| 11600 | */ |
| 11601 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11602 | { |
| 11603 | struct hlua *hlua; |
| 11604 | struct ckch_inst **lua_ckchi; |
| 11605 | struct ckch_store **lua_ckchs; |
| 11606 | struct ckch_store *old_ckchs = NULL; |
| 11607 | struct ckch_store *new_ckchs = NULL; |
| 11608 | int errcode = 0; |
| 11609 | char *err = NULL; |
| 11610 | struct cert_exts *cert_ext = NULL; |
| 11611 | char *filename; |
| 11612 | struct cert_key_and_chain *ckch; |
| 11613 | int ret; |
| 11614 | |
| 11615 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11616 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11617 | |
| 11618 | hlua = hlua_gethlua(L); |
| 11619 | |
| 11620 | /* FIXME: this should not return an error but should come back later */ |
| 11621 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11622 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11623 | |
| 11624 | ret = lua_getfield(L, -1, "filename"); |
| 11625 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11626 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11627 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11628 | goto end; |
| 11629 | } |
| 11630 | filename = (char *)lua_tostring(L, -1); |
| 11631 | |
| 11632 | |
| 11633 | /* look for the filename in the tree */ |
| 11634 | old_ckchs = ckchs_lookup(filename); |
| 11635 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11636 | 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] | 11637 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11638 | goto end; |
| 11639 | } |
| 11640 | /* TODO: handle extra_files_noext */ |
| 11641 | |
| 11642 | new_ckchs = ckchs_dup(old_ckchs); |
| 11643 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11644 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11645 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11646 | goto end; |
| 11647 | } |
| 11648 | |
| 11649 | ckch = new_ckchs->ckch; |
| 11650 | |
| 11651 | /* loop on the field in the table, which have the same name as the |
| 11652 | * possible extensions of files */ |
| 11653 | lua_pushnil(L); |
| 11654 | while (lua_next(L, 1)) { |
| 11655 | int i; |
| 11656 | const char *field = lua_tostring(L, -2); |
| 11657 | char *payload = (char *)lua_tostring(L, -1); |
| 11658 | |
| 11659 | if (!field || strcmp(field, "filename") == 0) { |
| 11660 | lua_pop(L, 1); |
| 11661 | continue; |
| 11662 | } |
| 11663 | |
| 11664 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11665 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11666 | cert_ext = &cert_exts[i]; |
| 11667 | break; |
| 11668 | } |
| 11669 | } |
| 11670 | |
| 11671 | /* this is the default type, the field is not supported */ |
| 11672 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11673 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11674 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11675 | goto end; |
| 11676 | } |
| 11677 | |
| 11678 | /* appply the change on the duplicate */ |
William Lallemand | d8c195a | 2022-05-26 11:20:13 +0200 | [diff] [blame] | 11679 | if (cert_ext->load(filename, payload, ckch, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11680 | 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] | 11681 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11682 | goto end; |
| 11683 | } |
| 11684 | lua_pop(L, 1); |
| 11685 | } |
| 11686 | |
| 11687 | /* store the pointers on the lua stack */ |
| 11688 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11689 | lua_ckchs[0] = old_ckchs; |
| 11690 | lua_ckchs[1] = new_ckchs; |
| 11691 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11692 | *lua_ckchi = NULL; |
| 11693 | |
| 11694 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11695 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11696 | |
| 11697 | end: |
| 11698 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11699 | |
| 11700 | if (errcode & ERR_CODE) { |
| 11701 | ckch_store_free(new_ckchs); |
| 11702 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11703 | } |
| 11704 | free(err); |
| 11705 | |
| 11706 | return 0; |
| 11707 | } |
| 11708 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11709 | #else |
| 11710 | |
| 11711 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11712 | { |
| 11713 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11714 | |
| 11715 | return 0; |
| 11716 | } |
| 11717 | #endif /* ! USE_OPENSSL */ |
| 11718 | |
| 11719 | |
| 11720 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11721 | /* This function can fail with an abort() due to an Lua critical error. |
| 11722 | * We are in the initialisation process of HAProxy, this abort() is |
| 11723 | * tolerated. |
| 11724 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11725 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11726 | { |
| 11727 | struct hlua_init_function *init; |
| 11728 | const char *msg; |
| 11729 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11730 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11731 | const char *kind; |
| 11732 | const char *trace; |
| 11733 | int return_status = 1; |
| 11734 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11735 | int nres; |
| 11736 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11737 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11738 | /* disable memory limit checks if limit is not set */ |
| 11739 | if (!hlua_global_allocator.limit) |
| 11740 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11741 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11742 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11743 | if (setjmp(safe_ljmp_env) != 0) { |
| 11744 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11745 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11746 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11747 | else |
| 11748 | error = "critical error"; |
| 11749 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11750 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11751 | } else { |
| 11752 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11753 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11754 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11755 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11756 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11757 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11758 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11759 | |
| 11760 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11761 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11762 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11763 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11764 | #endif |
| 11765 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11766 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11767 | |
| 11768 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11769 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11770 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11771 | |
| 11772 | case LUA_ERRERR: |
| 11773 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11774 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11775 | case LUA_ERRRUN: |
| 11776 | if (!kind) |
| 11777 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11778 | msg = lua_tostring(L, -1); |
| 11779 | lua_settop(L, 0); /* Empty the stack. */ |
| 11780 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11781 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11782 | if (msg) |
| 11783 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11784 | else |
| 11785 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11786 | return_status = 0; |
| 11787 | break; |
| 11788 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11789 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11790 | /* Unknown error */ |
| 11791 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11792 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11793 | case LUA_YIELD: |
| 11794 | /* yield is not configured at this step, this state doesn't happen */ |
| 11795 | if (!kind) |
| 11796 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11797 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11798 | case LUA_ERRMEM: |
| 11799 | if (!kind) |
| 11800 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11801 | lua_settop(L, 0); |
| 11802 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11803 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11804 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11805 | return_status = 0; |
| 11806 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11807 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11808 | if (!return_status) |
| 11809 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11810 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11811 | |
| 11812 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11813 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11814 | } |
| 11815 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11816 | int hlua_post_init() |
| 11817 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11818 | int ret; |
| 11819 | int i; |
| 11820 | int errors; |
| 11821 | char *err = NULL; |
| 11822 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11823 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11824 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11825 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11826 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11827 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11828 | int saved_used_backed = global.ssl_used_backend; |
| 11829 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11830 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11831 | global.ssl_used_backend = saved_used_backed; |
| 11832 | } |
| 11833 | #endif |
| 11834 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11835 | /* Perform post init of common thread */ |
| 11836 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11837 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11838 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11839 | if (ret == 0) |
| 11840 | return 0; |
| 11841 | |
| 11842 | /* init remaining lua states and load files */ |
| 11843 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11844 | |
| 11845 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11846 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11847 | |
| 11848 | /* Init lua state */ |
| 11849 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11850 | |
| 11851 | /* Load lua files */ |
| 11852 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11853 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11854 | if (ret != 0) { |
| 11855 | ha_alert("Lua init: %s\n", err); |
| 11856 | return 0; |
| 11857 | } |
| 11858 | } |
| 11859 | } |
| 11860 | |
| 11861 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11862 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11863 | |
| 11864 | /* Execute post init for all states */ |
| 11865 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11866 | |
| 11867 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11868 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11869 | |
| 11870 | /* run post init */ |
| 11871 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11872 | if (ret == 0) |
| 11873 | return 0; |
| 11874 | } |
| 11875 | |
| 11876 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11877 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11878 | |
| 11879 | /* control functions registering. Each function must have: |
| 11880 | * - only the function_ref[0] set positive and all other to -1 |
| 11881 | * - only the function_ref[0] set to -1 and all other positive |
| 11882 | * This ensure a same reference is not used both in shared |
| 11883 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11884 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11885 | * complicated to found for the end user. |
| 11886 | */ |
| 11887 | errors = 0; |
| 11888 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11889 | ret = 0; |
| 11890 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11891 | if (fcn->function_ref[i] == -1) |
| 11892 | ret--; |
| 11893 | else |
| 11894 | ret++; |
| 11895 | } |
| 11896 | if (abs(ret) != global.nbthread) { |
| 11897 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11898 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11899 | errors++; |
| 11900 | continue; |
| 11901 | } |
| 11902 | |
| 11903 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11904 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11905 | "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] | 11906 | "exclusive.\n", fcn->name); |
| 11907 | errors++; |
| 11908 | } |
| 11909 | } |
| 11910 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11911 | /* Do the same with registered filters */ |
| 11912 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11913 | ret = 0; |
| 11914 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11915 | if (reg_flt->flt_ref[i] == -1) |
| 11916 | ret--; |
| 11917 | else |
| 11918 | ret++; |
| 11919 | } |
| 11920 | if (abs(ret) != global.nbthread) { |
| 11921 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11922 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11923 | errors++; |
| 11924 | continue; |
| 11925 | } |
| 11926 | |
| 11927 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11928 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11929 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11930 | "exclusive.\n", fcn->name); |
| 11931 | errors++; |
| 11932 | } |
| 11933 | } |
| 11934 | |
| 11935 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11936 | if (errors > 0) |
| 11937 | return 0; |
| 11938 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11939 | /* 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] | 11940 | * -1 in order to have probably a segfault if someone use it |
| 11941 | */ |
| 11942 | hlua_state_id = -1; |
| 11943 | |
| 11944 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11945 | } |
| 11946 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11947 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11948 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11949 | * 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] | 11950 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11951 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11952 | * zero. This one verifies that the limits are respected but is optimized |
| 11953 | * 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] | 11954 | * |
| 11955 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11956 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11957 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11958 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11959 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11960 | */ |
| 11961 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11962 | { |
| 11963 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11964 | size_t limit, old, new; |
| 11965 | |
| 11966 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11967 | * for accounting anymore. |
| 11968 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11969 | if (likely(~zone->limit == 0)) { |
| 11970 | if (!nsize) |
| 11971 | ha_free(&ptr); |
| 11972 | else |
| 11973 | ptr = realloc(ptr, nsize); |
| 11974 | return ptr; |
| 11975 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11976 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11977 | if (!ptr) |
| 11978 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11979 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11980 | /* enforce strict limits across all threads */ |
| 11981 | limit = zone->limit; |
| 11982 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11983 | do { |
| 11984 | new = old + nsize - osize; |
| 11985 | if (unlikely(nsize && limit && new > limit)) |
| 11986 | return NULL; |
| 11987 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11988 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11989 | if (!nsize) |
| 11990 | ha_free(&ptr); |
| 11991 | else |
| 11992 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11993 | |
| 11994 | if (unlikely(!ptr && nsize)) // failed |
| 11995 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11996 | |
| 11997 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11998 | return ptr; |
| 11999 | } |
| 12000 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12001 | /* 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] | 12002 | * We are in the initialisation process of HAProxy, this abort() is |
| 12003 | * tolerated. |
| 12004 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12005 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12006 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12007 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12008 | int idx; |
| 12009 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12010 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12011 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 12012 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12013 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12014 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12015 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12016 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12017 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12018 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12019 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12020 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12021 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12022 | *context = NULL; |
| 12023 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12024 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12025 | * the Lua function can fail with an abort. We are in the initialisation |
| 12026 | * process of HAProxy, this abort() is tolerated. |
| 12027 | */ |
| 12028 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12029 | /* Call post initialisation function in safe environment. */ |
| 12030 | if (setjmp(safe_ljmp_env) != 0) { |
| 12031 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12032 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12033 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12034 | else |
| 12035 | error_msg = "critical error"; |
| 12036 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12037 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12038 | } else { |
| 12039 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12040 | } |
| 12041 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12042 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12043 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12044 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12045 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12046 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12047 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12048 | #endif |
| 12049 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12050 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12051 | #endif |
| 12052 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12053 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12054 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12055 | /* Apply configured prepend path */ |
| 12056 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12057 | hlua_prepend_path(L, pp->type, pp->path); |
| 12058 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12059 | /* |
| 12060 | * |
| 12061 | * Create "core" object. |
| 12062 | * |
| 12063 | */ |
| 12064 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12065 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12066 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12067 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12068 | /* set the thread id */ |
| 12069 | hlua_class_const_int(L, "thread", thread_num); |
| 12070 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12071 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12072 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12073 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12074 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12075 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12076 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12077 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12078 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12079 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12080 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12081 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12082 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12083 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12084 | hlua_class_function(L, "yield", hlua_yield); |
| 12085 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12086 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12087 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12088 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12089 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12090 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12091 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12092 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12093 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12094 | hlua_class_function(L, "log", hlua_log); |
| 12095 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12096 | hlua_class_function(L, "Info", hlua_log_info); |
| 12097 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12098 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12099 | hlua_class_function(L, "done", hlua_done); |
| 12100 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12101 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12102 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12103 | |
| 12104 | /* |
| 12105 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12106 | * Create "act" object. |
| 12107 | * |
| 12108 | */ |
| 12109 | |
| 12110 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12111 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12112 | |
| 12113 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12114 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12115 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12116 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12117 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12118 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12119 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12120 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12121 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12122 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12123 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12124 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12125 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12126 | |
| 12127 | /* |
| 12128 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12129 | * Create "Filter" object. |
| 12130 | * |
| 12131 | */ |
| 12132 | |
| 12133 | /* This table entry is the object "filter" base. */ |
| 12134 | lua_newtable(L); |
| 12135 | |
| 12136 | /* push flags and constants */ |
| 12137 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12138 | hlua_class_const_int(L, "WAIT", 0); |
| 12139 | hlua_class_const_int(L, "ERROR", -1); |
| 12140 | |
| 12141 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12142 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12143 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12144 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12145 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12146 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12147 | lua_setglobal(L, "filter"); |
| 12148 | |
| 12149 | /* |
| 12150 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12151 | * Register class Map |
| 12152 | * |
| 12153 | */ |
| 12154 | |
| 12155 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12156 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12157 | |
| 12158 | /* register pattern types. */ |
| 12159 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12160 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12161 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12162 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12163 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12164 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12165 | |
| 12166 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12167 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12168 | |
| 12169 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12170 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12171 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12172 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12173 | lua_pushstring(L, "__index"); |
| 12174 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12175 | |
| 12176 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12177 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12178 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12179 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12180 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12181 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12182 | /* Register previous table in the registry with reference and named entry. |
| 12183 | * The function hlua_register_metatable() pops the stack, so we |
| 12184 | * previously create a copy of the table. |
| 12185 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12186 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12187 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12188 | |
| 12189 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12190 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12191 | |
| 12192 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12193 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12194 | |
| 12195 | /* |
| 12196 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12197 | * Register "CertCache" class |
| 12198 | * |
| 12199 | */ |
| 12200 | |
| 12201 | /* Create and fill the metatable. */ |
| 12202 | lua_newtable(L); |
| 12203 | /* Register */ |
| 12204 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12205 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12206 | |
| 12207 | /* |
| 12208 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12209 | * Register class Channel |
| 12210 | * |
| 12211 | */ |
| 12212 | |
| 12213 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12214 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12215 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12216 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12217 | lua_pushstring(L, "__index"); |
| 12218 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12219 | |
| 12220 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12221 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12222 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12223 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12224 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12225 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12226 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12227 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12228 | hlua_class_function(L, "send", hlua_channel_send); |
| 12229 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12230 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12231 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12232 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12233 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12234 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12235 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12236 | /* Deprecated API */ |
| 12237 | hlua_class_function(L, "get", hlua_channel_get); |
| 12238 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12239 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12240 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12241 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12242 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12243 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12244 | |
| 12245 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12246 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12247 | |
| 12248 | /* |
| 12249 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12250 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12251 | * |
| 12252 | */ |
| 12253 | |
| 12254 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12255 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12256 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12257 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12258 | lua_pushstring(L, "__index"); |
| 12259 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12260 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12261 | /* Browse existing fetches and create the associated |
| 12262 | * object method. |
| 12263 | */ |
| 12264 | sf = NULL; |
| 12265 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12266 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12267 | * by an underscore. |
| 12268 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12269 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12270 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12271 | if (*p == '.' || *p == '-' || *p == '+') |
| 12272 | *p = '_'; |
| 12273 | |
| 12274 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12275 | lua_pushstring(L, trash.area); |
| 12276 | lua_pushlightuserdata(L, sf); |
| 12277 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12278 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12279 | } |
| 12280 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12281 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12282 | |
| 12283 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12284 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12285 | |
| 12286 | /* |
| 12287 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12288 | * Register class Converters |
| 12289 | * |
| 12290 | */ |
| 12291 | |
| 12292 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12293 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12294 | |
| 12295 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12296 | lua_pushstring(L, "__index"); |
| 12297 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12298 | |
| 12299 | /* Browse existing converters and create the associated |
| 12300 | * object method. |
| 12301 | */ |
| 12302 | sc = NULL; |
| 12303 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12304 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12305 | * by an underscore. |
| 12306 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12307 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12308 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12309 | if (*p == '.' || *p == '-' || *p == '+') |
| 12310 | *p = '_'; |
| 12311 | |
| 12312 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12313 | lua_pushstring(L, trash.area); |
| 12314 | lua_pushlightuserdata(L, sc); |
| 12315 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12316 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12317 | } |
| 12318 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12319 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12320 | |
| 12321 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12322 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12323 | |
| 12324 | /* |
| 12325 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12326 | * Register class HTTP |
| 12327 | * |
| 12328 | */ |
| 12329 | |
| 12330 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12331 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12332 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12333 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12334 | lua_pushstring(L, "__index"); |
| 12335 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12336 | |
| 12337 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12338 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12339 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12340 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12341 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12342 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12343 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12344 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12345 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12346 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12347 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12348 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12349 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12350 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12351 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12352 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12353 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12354 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12355 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12356 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12357 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12358 | |
| 12359 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12360 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12361 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12362 | /* |
| 12363 | * |
| 12364 | * Register class HTTPMessage |
| 12365 | * |
| 12366 | */ |
| 12367 | |
| 12368 | /* Create and fill the metatable. */ |
| 12369 | lua_newtable(L); |
| 12370 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12371 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12372 | lua_pushstring(L, "__index"); |
| 12373 | lua_newtable(L); |
| 12374 | |
| 12375 | /* Register Lua functions. */ |
| 12376 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12377 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12378 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12379 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12380 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12381 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12382 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12383 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12384 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12385 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12386 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12387 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12388 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12389 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12390 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12391 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12392 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12393 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12394 | |
| 12395 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12396 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12397 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12398 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12399 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12400 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12401 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12402 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12403 | |
| 12404 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12405 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12406 | |
| 12407 | lua_rawset(L, -3); |
| 12408 | |
| 12409 | /* Register previous table in the registry with reference and named entry. */ |
| 12410 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12411 | |
| 12412 | /* |
| 12413 | * |
| 12414 | * Register class HTTPClient |
| 12415 | * |
| 12416 | */ |
| 12417 | |
| 12418 | /* Create and fill the metatable. */ |
| 12419 | lua_newtable(L); |
| 12420 | lua_pushstring(L, "__index"); |
| 12421 | lua_newtable(L); |
| 12422 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12423 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12424 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12425 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12426 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12427 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12428 | /* Register the garbage collector entry. */ |
| 12429 | lua_pushstring(L, "__gc"); |
| 12430 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12431 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12432 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12433 | |
| 12434 | |
| 12435 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12436 | /* |
| 12437 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12438 | * Register class AppletTCP |
| 12439 | * |
| 12440 | */ |
| 12441 | |
| 12442 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12443 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12444 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12445 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12446 | lua_pushstring(L, "__index"); |
| 12447 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12448 | |
| 12449 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12450 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12451 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12452 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12453 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12454 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12455 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12456 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12457 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12458 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12459 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12460 | |
| 12461 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12462 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12463 | |
| 12464 | /* |
| 12465 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12466 | * Register class AppletHTTP |
| 12467 | * |
| 12468 | */ |
| 12469 | |
| 12470 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12471 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12472 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12473 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12474 | lua_pushstring(L, "__index"); |
| 12475 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12476 | |
| 12477 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12478 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12479 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12480 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12481 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12482 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12483 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12484 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12485 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12486 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12487 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12488 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12489 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12490 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12491 | |
| 12492 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12493 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12494 | |
| 12495 | /* |
| 12496 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12497 | * Register class TXN |
| 12498 | * |
| 12499 | */ |
| 12500 | |
| 12501 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12502 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12503 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12504 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12505 | lua_pushstring(L, "__index"); |
| 12506 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12507 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12508 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12509 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12510 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12511 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12512 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12513 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12514 | hlua_class_function(L, "done", hlua_txn_done); |
| 12515 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12516 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12517 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12518 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12519 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12520 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12521 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12522 | hlua_class_function(L, "log", hlua_txn_log); |
| 12523 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12524 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12525 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12526 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12527 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12528 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12529 | |
| 12530 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12531 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12532 | |
| 12533 | /* |
| 12534 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12535 | * Register class reply |
| 12536 | * |
| 12537 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12538 | lua_newtable(L); |
| 12539 | lua_pushstring(L, "__index"); |
| 12540 | lua_newtable(L); |
| 12541 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12542 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12543 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12544 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12545 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12546 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12547 | |
| 12548 | |
| 12549 | /* |
| 12550 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12551 | * Register class Socket |
| 12552 | * |
| 12553 | */ |
| 12554 | |
| 12555 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12556 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12557 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12558 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12559 | lua_pushstring(L, "__index"); |
| 12560 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12561 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12562 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12563 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12564 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12565 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12566 | hlua_class_function(L, "send", hlua_socket_send); |
| 12567 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12568 | hlua_class_function(L, "close", hlua_socket_close); |
| 12569 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12570 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12571 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12572 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12573 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12574 | 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] | 12575 | |
| 12576 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12577 | lua_pushstring(L, "__gc"); |
| 12578 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12579 | 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] | 12580 | |
| 12581 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12582 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12583 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12584 | lua_atpanic(L, hlua_panic_safe); |
| 12585 | |
| 12586 | return L; |
| 12587 | } |
| 12588 | |
| 12589 | void hlua_init(void) { |
| 12590 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12591 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12592 | #ifdef USE_OPENSSL |
| 12593 | struct srv_kw *kw; |
| 12594 | int tmp_error; |
| 12595 | char *error; |
| 12596 | char *args[] = { /* SSL client configuration. */ |
| 12597 | "ssl", |
| 12598 | "verify", |
| 12599 | "none", |
| 12600 | NULL |
| 12601 | }; |
| 12602 | #endif |
| 12603 | |
| 12604 | /* Init post init function list head */ |
| 12605 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12606 | LIST_INIT(&hlua_init_functions[i]); |
| 12607 | |
| 12608 | /* Init state for common/shared lua parts */ |
| 12609 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12610 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12611 | hlua_states[0] = hlua_init_state(0); |
| 12612 | |
| 12613 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12614 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12615 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12616 | hlua_states[1] = hlua_init_state(1); |
| 12617 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12618 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12619 | 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] | 12620 | if (!socket_proxy) { |
| 12621 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12622 | exit(1); |
| 12623 | } |
| 12624 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12625 | |
| 12626 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12627 | socket_tcp = new_server(socket_proxy); |
| 12628 | if (!socket_tcp) { |
| 12629 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12630 | exit(1); |
| 12631 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12632 | |
| 12633 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12634 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12635 | socket_ssl = new_server(socket_proxy); |
| 12636 | if (!socket_ssl) { |
| 12637 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12638 | exit(1); |
| 12639 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12640 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12641 | socket_ssl->use_ssl = 1; |
| 12642 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12643 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12644 | for (i = 0; args[i] != NULL; i++) { |
| 12645 | 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] | 12646 | /* |
| 12647 | * |
| 12648 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12649 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12650 | * features like client certificates and ssl_verify. |
| 12651 | * |
| 12652 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12653 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12654 | if (tmp_error != 0) { |
| 12655 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12656 | abort(); /* This must be never arrives because the command line |
| 12657 | not editable by the user. */ |
| 12658 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12659 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12660 | } |
| 12661 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12662 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12663 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12664 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12665 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12666 | static void hlua_deinit() |
| 12667 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12668 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12669 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12670 | |
| 12671 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12672 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12673 | |
| 12674 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12675 | if (hlua_states[thr]) |
| 12676 | lua_close(hlua_states[thr]); |
| 12677 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12678 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12679 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12680 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12681 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12682 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12683 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12684 | |
| 12685 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12686 | } |
| 12687 | |
| 12688 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12689 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12690 | static void hlua_register_build_options(void) |
| 12691 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12692 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12693 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12694 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12695 | hap_register_build_opts(ptr, 1); |
| 12696 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12697 | |
| 12698 | INITCALL0(STG_REGISTER, hlua_register_build_options); |