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); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1251 | if (!already_safe) { |
| 1252 | if (!SET_SAFE_LJMP_PARENT(lua)) { |
| 1253 | lua->Tref = LUA_REFNIL; |
| 1254 | return 0; |
| 1255 | } |
| 1256 | } |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1257 | lua->T = lua_newthread(hlua_states[state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1258 | if (!lua->T) { |
| 1259 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1260 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1261 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1262 | return 0; |
| 1263 | } |
| 1264 | hlua_sethlua(lua); |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1265 | lua->Tref = luaL_ref(hlua_states[state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1266 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 1267 | if (!already_safe) |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1268 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1269 | return 1; |
| 1270 | } |
| 1271 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1272 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1273 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 1274 | * is not freed. |
| 1275 | */ |
| 1276 | void hlua_ctx_destroy(struct hlua *lua) |
| 1277 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1278 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 1279 | return; |
| 1280 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1281 | if (!lua->T) |
| 1282 | goto end; |
| 1283 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1284 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1285 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 1286 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1287 | if (!SET_SAFE_LJMP(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1288 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1289 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1290 | RESET_SAFE_LJMP(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1291 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1292 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1293 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1294 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1295 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1296 | /* Forces a garbage collecting process. If the Lua program is finished |
| 1297 | * without error, we run the GC on the thread pointer. Its freed all |
| 1298 | * the unused memory. |
| 1299 | * If the thread is finnish with an error or is currently yielded, |
| 1300 | * it seems that the GC applied on the thread doesn't clean anything, |
| 1301 | * so e run the GC on the main thread. |
| 1302 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 1303 | * the garbage collection. |
| 1304 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1305 | if (lua->gc_count) { |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1306 | if (!SET_SAFE_LJMP_PARENT(lua)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 1307 | return; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1308 | lua_gc(hlua_states[lua->state_id], LUA_GCCOLLECT, 0); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 1309 | RESET_SAFE_LJMP_PARENT(lua); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1310 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 1311 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 1312 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 1313 | |
| 1314 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 1315 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1316 | } |
| 1317 | |
| 1318 | /* This function is used to restore the Lua context when a coroutine |
| 1319 | * fails. This function copy the common memory between old coroutine |
| 1320 | * and the new coroutine. The old coroutine is destroyed, and its |
| 1321 | * replaced by the new coroutine. |
| 1322 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 1323 | * as string error message and it is copied in the new stack. |
| 1324 | */ |
| 1325 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 1326 | { |
| 1327 | lua_State *T; |
| 1328 | int new_ref; |
| 1329 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1330 | /* New Lua coroutine. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1331 | T = lua_newthread(hlua_states[lua->state_id]); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1332 | if (!T) |
| 1333 | return 0; |
| 1334 | |
| 1335 | /* Copy last error message. */ |
| 1336 | if (keep_msg) |
| 1337 | lua_xmove(lua->T, T, 1); |
| 1338 | |
| 1339 | /* Copy data between the coroutines. */ |
| 1340 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1341 | lua_xmove(lua->T, T, 1); |
Ilya Shipitsin | 46a030c | 2020-07-05 16:36:08 +0500 | [diff] [blame] | 1342 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Value popped. */ |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1343 | |
| 1344 | /* Destroy old data. */ |
| 1345 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1346 | |
| 1347 | /* The thread is garbage collected by Lua. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1348 | luaL_unref(hlua_states[lua->state_id], LUA_REGISTRYINDEX, lua->Tref); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1349 | |
| 1350 | /* Fill the struct with the new coroutine values. */ |
| 1351 | lua->Mref = new_ref; |
| 1352 | lua->T = T; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1353 | lua->Tref = luaL_ref(hlua_states[lua->state_id], LUA_REGISTRYINDEX); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1354 | |
| 1355 | /* Set context. */ |
| 1356 | hlua_sethlua(lua); |
| 1357 | |
| 1358 | return 1; |
| 1359 | } |
| 1360 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1361 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1362 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1363 | struct hlua *hlua; |
| 1364 | |
| 1365 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1366 | hlua = hlua_gethlua(L); |
| 1367 | if (!hlua) |
| 1368 | return; |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1369 | |
| 1370 | /* Lua cannot yield when its returning from a function, |
| 1371 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1372 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1373 | */ |
| 1374 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1375 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1376 | return; |
| 1377 | } |
| 1378 | |
| 1379 | /* restore the interrupt condition. */ |
| 1380 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1381 | |
| 1382 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1383 | * If the state is not yieldable, trying yield causes an error. |
| 1384 | */ |
| 1385 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1386 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1387 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1388 | /* If we cannot yield, update the clock and check the timeout. */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1389 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1390 | hlua->run_time += now_ms - hlua->start_time; |
| 1391 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1392 | lua_pushfstring(L, "execution timeout"); |
| 1393 | WILL_LJMP(lua_error(L)); |
| 1394 | } |
| 1395 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1396 | /* Update the start time. */ |
| 1397 | hlua->start_time = now_ms; |
| 1398 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1399 | /* Try to interrupt the process at the end of the current |
| 1400 | * unyieldable function. |
| 1401 | */ |
| 1402 | 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] | 1403 | } |
| 1404 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1405 | /* This function start or resumes the Lua stack execution. If the flag |
| 1406 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1407 | * The function return an error. |
| 1408 | * |
| 1409 | * The function can returns 4 values: |
| 1410 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1411 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1412 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1413 | * - 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] | 1414 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1415 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1416 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1417 | * 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] | 1418 | * LUA code. |
| 1419 | */ |
| 1420 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1421 | { |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1422 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 1423 | int nres; |
| 1424 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1425 | int ret; |
| 1426 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1427 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1428 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1429 | /* Initialise run time counter. */ |
| 1430 | if (!HLUA_IS_RUNNING(lua)) |
| 1431 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1432 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1433 | /* Lock the whole Lua execution. This lock must be before the |
| 1434 | * label "resume_execution". |
| 1435 | */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1436 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1437 | lua_take_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1438 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1439 | resume_execution: |
| 1440 | |
| 1441 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1442 | * instructions. it is used for preventing infinite loops. |
| 1443 | */ |
| 1444 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1445 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1446 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1447 | HLUA_SET_RUN(lua); |
| 1448 | HLUA_CLR_CTRLYIELD(lua); |
| 1449 | HLUA_CLR_WAKERESWR(lua); |
| 1450 | HLUA_CLR_WAKEREQWR(lua); |
Christopher Faulet | 1f43a34 | 2021-08-04 17:58:21 +0200 | [diff] [blame] | 1451 | HLUA_CLR_NOYIELD(lua); |
| 1452 | if (!yield_allowed) |
| 1453 | HLUA_SET_NOYIELD(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1454 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1455 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1456 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1457 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1458 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1459 | /* Call the function. */ |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1460 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1461 | 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] | 1462 | #else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1463 | ret = lua_resume(lua->T, hlua_states[lua->state_id], lua->nargs); |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 1464 | #endif |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1465 | switch (ret) { |
| 1466 | |
| 1467 | case LUA_OK: |
| 1468 | ret = HLUA_E_OK; |
| 1469 | break; |
| 1470 | |
| 1471 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1472 | /* Check if the execution timeout is expired. It it is the case, we |
| 1473 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1474 | */ |
Willy Tarreau | 5554264 | 2021-10-08 09:33:24 +0200 | [diff] [blame] | 1475 | clock_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1476 | lua->run_time += now_ms - lua->start_time; |
| 1477 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1478 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1479 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1480 | break; |
| 1481 | } |
| 1482 | /* Process the forced yield. if the general yield is not allowed or |
| 1483 | * if no task were associated this the current Lua execution |
| 1484 | * coroutine, we resume the execution. Else we want to return in the |
| 1485 | * scheduler and we want to be waked up again, to continue the |
| 1486 | * current Lua execution. So we schedule our own task. |
| 1487 | */ |
| 1488 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1489 | if (!yield_allowed || !lua->task) |
| 1490 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1491 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1492 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1493 | if (!yield_allowed) { |
| 1494 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1495 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1496 | break; |
| 1497 | } |
| 1498 | ret = HLUA_E_AGAIN; |
| 1499 | break; |
| 1500 | |
| 1501 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1502 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1503 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1504 | * because the errors ares the only one mean to return immediately |
| 1505 | * from and lua execution. |
| 1506 | */ |
| 1507 | if (lua->flags & HLUA_EXIT) { |
| 1508 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1509 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1510 | break; |
| 1511 | } |
| 1512 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1513 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1514 | if (!lua_checkstack(lua->T, 1)) { |
| 1515 | ret = HLUA_E_ERR; |
| 1516 | break; |
| 1517 | } |
| 1518 | msg = lua_tostring(lua->T, -1); |
| 1519 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1520 | lua_pop(lua->T, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 1521 | trace = hlua_traceback(lua->T, ", "); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1522 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1523 | 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] | 1524 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1525 | 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] | 1526 | ret = HLUA_E_ERRMSG; |
| 1527 | break; |
| 1528 | |
| 1529 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1530 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1531 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1532 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1533 | break; |
| 1534 | |
| 1535 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1536 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1537 | if (!lua_checkstack(lua->T, 1)) { |
| 1538 | ret = HLUA_E_ERR; |
| 1539 | break; |
| 1540 | } |
| 1541 | msg = lua_tostring(lua->T, -1); |
| 1542 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1543 | lua_pop(lua->T, 1); |
| 1544 | if (msg) |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1545 | 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] | 1546 | else |
Thierry Fournier | 46278ff | 2020-11-29 11:48:12 +0100 | [diff] [blame] | 1547 | 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] | 1548 | ret = HLUA_E_ERRMSG; |
| 1549 | break; |
| 1550 | |
| 1551 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1552 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1553 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1554 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1555 | break; |
| 1556 | } |
| 1557 | |
| 1558 | switch (ret) { |
| 1559 | case HLUA_E_AGAIN: |
| 1560 | break; |
| 1561 | |
| 1562 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1563 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1564 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1565 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1566 | break; |
| 1567 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1568 | case HLUA_E_ETMOUT: |
| 1569 | case HLUA_E_NOMEM: |
| 1570 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1571 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1572 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1573 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1574 | hlua_ctx_renew(lua, 0); |
| 1575 | break; |
| 1576 | |
| 1577 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1578 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1579 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1580 | break; |
| 1581 | } |
| 1582 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1583 | /* This is the main exit point, remove the Lua lock. */ |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 1584 | if (lua->state_id == 0) |
Willy Tarreau | 1e7bef1 | 2021-08-20 15:47:25 +0200 | [diff] [blame] | 1585 | lua_drop_global_lock(); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1586 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1587 | return ret; |
| 1588 | } |
| 1589 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1590 | /* This function exit the current code. */ |
| 1591 | __LJMP static int hlua_done(lua_State *L) |
| 1592 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 1593 | struct hlua *hlua; |
| 1594 | |
| 1595 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 1596 | hlua = hlua_gethlua(L); |
| 1597 | if (!hlua) |
| 1598 | return 0; |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1599 | |
| 1600 | hlua->flags |= HLUA_EXIT; |
| 1601 | WILL_LJMP(lua_error(L)); |
| 1602 | |
| 1603 | return 0; |
| 1604 | } |
| 1605 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1606 | /* This function is an LUA binding. It provides a function |
| 1607 | * for deleting ACL from a referenced ACL file. |
| 1608 | */ |
| 1609 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1610 | { |
| 1611 | const char *name; |
| 1612 | const char *key; |
| 1613 | struct pat_ref *ref; |
| 1614 | |
| 1615 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1616 | |
| 1617 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1618 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1619 | |
| 1620 | ref = pat_ref_lookup(name); |
| 1621 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1622 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1623 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1624 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1625 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1626 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1627 | return 0; |
| 1628 | } |
| 1629 | |
| 1630 | /* This function is an LUA binding. It provides a function |
| 1631 | * for deleting map entry from a referenced map file. |
| 1632 | */ |
| 1633 | static int hlua_del_map(lua_State *L) |
| 1634 | { |
| 1635 | const char *name; |
| 1636 | const char *key; |
| 1637 | struct pat_ref *ref; |
| 1638 | |
| 1639 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1640 | |
| 1641 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1642 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1643 | |
| 1644 | ref = pat_ref_lookup(name); |
| 1645 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1646 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1647 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1648 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1649 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1650 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1651 | return 0; |
| 1652 | } |
| 1653 | |
| 1654 | /* This function is an LUA binding. It provides a function |
| 1655 | * for adding ACL pattern from a referenced ACL file. |
| 1656 | */ |
| 1657 | static int hlua_add_acl(lua_State *L) |
| 1658 | { |
| 1659 | const char *name; |
| 1660 | const char *key; |
| 1661 | struct pat_ref *ref; |
| 1662 | |
| 1663 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1664 | |
| 1665 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1666 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1667 | |
| 1668 | ref = pat_ref_lookup(name); |
| 1669 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1670 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1671 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1672 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1673 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1674 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1675 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1676 | return 0; |
| 1677 | } |
| 1678 | |
| 1679 | /* This function is an LUA binding. It provides a function |
| 1680 | * for setting map pattern and sample from a referenced map |
| 1681 | * file. |
| 1682 | */ |
| 1683 | static int hlua_set_map(lua_State *L) |
| 1684 | { |
| 1685 | const char *name; |
| 1686 | const char *key; |
| 1687 | const char *value; |
| 1688 | struct pat_ref *ref; |
| 1689 | |
| 1690 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1691 | |
| 1692 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1693 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1694 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1695 | |
| 1696 | ref = pat_ref_lookup(name); |
| 1697 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1698 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1699 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1700 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1701 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1702 | pat_ref_set(ref, key, value, NULL); |
| 1703 | else |
| 1704 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1705 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1706 | return 0; |
| 1707 | } |
| 1708 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1709 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1710 | * 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] | 1711 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1712 | * the name of the object (_G[<name>] = <metable> ). |
| 1713 | * |
| 1714 | * A metable is a table that modify the standard behavior of a standard |
| 1715 | * access to the associated data. The entries of this new metatable are |
| 1716 | * defined as is: |
| 1717 | * |
| 1718 | * http://lua-users.org/wiki/MetatableEvents |
| 1719 | * |
| 1720 | * __index |
| 1721 | * |
| 1722 | * we access an absent field in a table, the result is nil. This is |
| 1723 | * true, but it is not the whole truth. Actually, such access triggers |
| 1724 | * the interpreter to look for an __index metamethod: If there is no |
| 1725 | * such method, as usually happens, then the access results in nil; |
| 1726 | * otherwise, the metamethod will provide the result. |
| 1727 | * |
| 1728 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1729 | * the key does not appear in the table, but the metatable has an __index |
| 1730 | * property: |
| 1731 | * |
| 1732 | * - if the value is a function, the function is called, passing in the |
| 1733 | * table and the key; the return value of that function is returned as |
| 1734 | * the result. |
| 1735 | * |
| 1736 | * - if the value is another table, the value of the key in that table is |
| 1737 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1738 | * table's metatable has an __index property, then it continues on up) |
| 1739 | * |
| 1740 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1741 | * |
| 1742 | * http://www.lua.org/pil/13.4.1.html |
| 1743 | * |
| 1744 | * __newindex |
| 1745 | * |
| 1746 | * Like __index, but control property assignment. |
| 1747 | * |
| 1748 | * __mode - Control weak references. A string value with one or both |
| 1749 | * of the characters 'k' and 'v' which specifies that the the |
| 1750 | * keys and/or values in the table are weak references. |
| 1751 | * |
| 1752 | * __call - Treat a table like a function. When a table is followed by |
| 1753 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1754 | * a __call key pointing to a function, that function is invoked |
| 1755 | * (passing any specified arguments) and the return value is |
| 1756 | * returned. |
| 1757 | * |
| 1758 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1759 | * called, if the metatable for myTable has a __metatable |
| 1760 | * key, the value of that key is returned instead of the |
| 1761 | * actual metatable. |
| 1762 | * |
| 1763 | * __tostring - Control string representation. When the builtin |
| 1764 | * "tostring( myTable )" function is called, if the metatable |
| 1765 | * for myTable has a __tostring property set to a function, |
| 1766 | * that function is invoked (passing myTable to it) and the |
| 1767 | * return value is used as the string representation. |
| 1768 | * |
| 1769 | * __len - Control table length. When the table length is requested using |
| 1770 | * the length operator ( '#' ), if the metatable for myTable has |
| 1771 | * a __len key pointing to a function, that function is invoked |
| 1772 | * (passing myTable to it) and the return value used as the value |
| 1773 | * of "#myTable". |
| 1774 | * |
| 1775 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1776 | * collected, if the metatable has a __gc field pointing to a |
| 1777 | * function, that function is first invoked, passing the userdata |
| 1778 | * to it. The __gc metamethod is not called for tables. |
| 1779 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1780 | * |
| 1781 | * Special metamethods for redefining standard operators: |
| 1782 | * http://www.lua.org/pil/13.1.html |
| 1783 | * |
| 1784 | * __add "+" |
| 1785 | * __sub "-" |
| 1786 | * __mul "*" |
| 1787 | * __div "/" |
| 1788 | * __unm "!" |
| 1789 | * __pow "^" |
| 1790 | * __concat ".." |
| 1791 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1792 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1793 | * http://www.lua.org/pil/13.2.html |
| 1794 | * |
| 1795 | * __eq "==" |
| 1796 | * __lt "<" |
| 1797 | * __le "<=" |
| 1798 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | |
| 1800 | /* |
| 1801 | * |
| 1802 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1803 | * Class Map |
| 1804 | * |
| 1805 | * |
| 1806 | */ |
| 1807 | |
| 1808 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1809 | * a class session, otherwise it throws an error. |
| 1810 | */ |
| 1811 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1812 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1813 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | /* This function is the map constructor. It don't need |
| 1817 | * the class Map object. It creates and return a new Map |
| 1818 | * object. It must be called only during "body" or "init" |
| 1819 | * context because it process some filesystem accesses. |
| 1820 | */ |
| 1821 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1822 | { |
| 1823 | const char *fn; |
| 1824 | int match = PAT_MATCH_STR; |
| 1825 | struct sample_conv conv; |
| 1826 | const char *file = ""; |
| 1827 | int line = 0; |
| 1828 | lua_Debug ar; |
| 1829 | char *err = NULL; |
| 1830 | struct arg args[2]; |
| 1831 | |
| 1832 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1833 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1834 | |
| 1835 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1836 | |
| 1837 | if (lua_gettop(L) >= 2) { |
| 1838 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1839 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1840 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1841 | } |
| 1842 | |
| 1843 | /* Get Lua filename and line number. */ |
| 1844 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1845 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1846 | if (ar.currentline > 0) { /* is there info? */ |
| 1847 | file = ar.short_src; |
| 1848 | line = ar.currentline; |
| 1849 | } |
| 1850 | } |
| 1851 | |
| 1852 | /* fill fake sample_conv struct. */ |
| 1853 | conv.kw = ""; /* unused. */ |
| 1854 | conv.process = NULL; /* unused. */ |
| 1855 | conv.arg_mask = 0; /* unused. */ |
| 1856 | conv.val_args = NULL; /* unused. */ |
| 1857 | conv.out_type = SMP_T_STR; |
| 1858 | conv.private = (void *)(long)match; |
| 1859 | switch (match) { |
| 1860 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1861 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1862 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1863 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1864 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1865 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1866 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1867 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1868 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1869 | default: |
| 1870 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1871 | } |
| 1872 | |
| 1873 | /* fill fake args. */ |
| 1874 | args[0].type = ARGT_STR; |
Christopher Faulet | 73292e9 | 2020-08-06 08:40:09 +0200 | [diff] [blame] | 1875 | args[0].data.str.area = strdup(fn); |
| 1876 | args[0].data.str.data = strlen(fn); |
| 1877 | args[0].data.str.size = args[0].data.str.data+1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1878 | args[1].type = ARGT_STOP; |
| 1879 | |
| 1880 | /* load the map. */ |
| 1881 | if (!sample_load_map(args, &conv, file, line, &err)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1882 | /* error case: we can't use luaL_error because we must |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1883 | * free the err variable. |
| 1884 | */ |
| 1885 | luaL_where(L, 1); |
| 1886 | lua_pushfstring(L, "'new': %s.", err); |
| 1887 | lua_concat(L, 2); |
| 1888 | free(err); |
Christopher Faulet | 6ad7df4 | 2020-08-07 11:45:18 +0200 | [diff] [blame] | 1889 | chunk_destroy(&args[0].data.str); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1890 | WILL_LJMP(lua_error(L)); |
| 1891 | } |
| 1892 | |
| 1893 | /* create the lua object. */ |
| 1894 | lua_newtable(L); |
| 1895 | lua_pushlightuserdata(L, args[0].data.map); |
| 1896 | lua_rawseti(L, -2, 0); |
| 1897 | |
| 1898 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1899 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1900 | lua_setmetatable(L, -2); |
| 1901 | |
| 1902 | |
| 1903 | return 1; |
| 1904 | } |
| 1905 | |
| 1906 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1907 | { |
| 1908 | struct map_descriptor *desc; |
| 1909 | struct pattern *pat; |
| 1910 | struct sample smp; |
| 1911 | |
| 1912 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1913 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1914 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1915 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1916 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1917 | } |
| 1918 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1919 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1920 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1921 | 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] | 1922 | smp.data.u.str.size = smp.data.u.str.data + 1; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1923 | } |
| 1924 | |
| 1925 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1926 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1927 | if (str) |
| 1928 | lua_pushstring(L, ""); |
| 1929 | else |
| 1930 | lua_pushnil(L); |
| 1931 | return 1; |
| 1932 | } |
| 1933 | |
| 1934 | /* 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] | 1935 | 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] | 1936 | return 1; |
| 1937 | } |
| 1938 | |
| 1939 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1940 | { |
| 1941 | return _hlua_map_lookup(L, 0); |
| 1942 | } |
| 1943 | |
| 1944 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1945 | { |
| 1946 | return _hlua_map_lookup(L, 1); |
| 1947 | } |
| 1948 | |
| 1949 | /* |
| 1950 | * |
| 1951 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1952 | * Class Socket |
| 1953 | * |
| 1954 | * |
| 1955 | */ |
| 1956 | |
| 1957 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1958 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1959 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1960 | } |
| 1961 | |
| 1962 | /* 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] | 1963 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1964 | * received. |
| 1965 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1966 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1968 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 1969 | struct stconn *sc = appctx_sc(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1971 | if (ctx->die) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1972 | sc_shutw(sc); |
| 1973 | sc_shutr(sc); |
| 1974 | sc_ic(sc)->flags |= CF_READ_NULL; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1975 | notification_wake(&ctx->wake_on_read); |
| 1976 | notification_wake(&ctx->wake_on_write); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1977 | stream_shutdown(__sc_strm(sc), SF_ERR_KILLED); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1980 | /* If we can't write, wakeup the pending write signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1981 | if (channel_output_closed(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1982 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1983 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 1984 | /* If we can't read, wakeup the pending read signals. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1985 | if (channel_input_closed(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1986 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1987 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1988 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1989 | * to be notified whenever the connection completes. |
| 1990 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 1991 | if (sc_opposite(sc)->state < SC_ST_EST) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 1992 | applet_need_more_data(appctx); |
Willy Tarreau | b23edc8 | 2022-05-24 16:49:03 +0200 | [diff] [blame] | 1993 | se_need_remote_conn(appctx->sedesc); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 1994 | applet_have_more_data(appctx); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1995 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1996 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1997 | |
| 1998 | /* This function is called after the connect. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 1999 | ctx->connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2001 | /* 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] | 2002 | if (channel_may_recv(sc_ic(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2003 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2004 | |
| 2005 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2006 | if (!channel_is_empty(sc_oc(sc))) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2007 | notification_wake(&ctx->wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2008 | |
| 2009 | /* Some data were injected in the buffer, notify the stream |
| 2010 | * interface. |
| 2011 | */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2012 | if (!channel_is_empty(sc_ic(sc))) |
| 2013 | sc_update(sc); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2014 | |
| 2015 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2016 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2017 | */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2018 | if (notification_registered(&ctx->wake_on_write)) |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2019 | applet_have_more_data(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | } |
| 2021 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2022 | static int hlua_socket_init(struct appctx *appctx) |
| 2023 | { |
| 2024 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
| 2025 | struct stream *s; |
| 2026 | |
| 2027 | if (appctx_finalize_startup(appctx, socket_proxy, &BUF_NULL) == -1) |
| 2028 | goto error; |
| 2029 | |
| 2030 | s = appctx_strm(appctx); |
| 2031 | |
Willy Tarreau | 4596fe2 | 2022-05-17 19:07:51 +0200 | [diff] [blame] | 2032 | /* Configure "right" stream connector. This stconn is used to connect |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2033 | * and retrieve data from the server. The connection is initialized |
| 2034 | * with the "struct server". |
| 2035 | */ |
Willy Tarreau | 74568cf | 2022-05-27 09:03:30 +0200 | [diff] [blame] | 2036 | sc_set_state(s->scb, SC_ST_ASS); |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2037 | |
| 2038 | /* Force destination server. */ |
| 2039 | s->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
| 2040 | s->target = &socket_tcp->obj_type; |
| 2041 | |
| 2042 | ctx->appctx = appctx; |
| 2043 | return 0; |
| 2044 | |
| 2045 | error: |
| 2046 | return -1; |
| 2047 | } |
| 2048 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2049 | /* This function is called when the "struct stream" is destroyed. |
| 2050 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | * Wake all the pending signals. |
| 2052 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 2053 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2054 | { |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2055 | struct hlua_csk_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2056 | struct xref *peer; |
| 2057 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2058 | /* Remove my link in the original objects. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2059 | peer = xref_get_peer_and_lock(&ctx->xref); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2060 | if (peer) |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2061 | xref_disconnect(&ctx->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | |
| 2063 | /* Wake all the task waiting for me. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2064 | notification_wake(&ctx->wake_on_read); |
| 2065 | notification_wake(&ctx->wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | } |
| 2067 | |
| 2068 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2069 | * uses this object. If the stream does not exists, just quit. |
| 2070 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2071 | * pending signal can rest in the read and write lists. destroy |
| 2072 | * it. |
| 2073 | */ |
| 2074 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 2075 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2076 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2077 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2078 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2080 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 2081 | |
| 2082 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2084 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | return 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2086 | |
| 2087 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2089 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2090 | ctx->die = 1; |
| 2091 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2092 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2093 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2094 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2099 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2100 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2101 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2102 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2103 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2104 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2105 | struct xref *peer; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2106 | struct hlua *hlua; |
| 2107 | |
| 2108 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2109 | hlua = hlua_gethlua(L); |
| 2110 | if (!hlua) |
| 2111 | return 0; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2112 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2113 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2114 | |
| 2115 | /* Check if we run on the same thread than the xreator thread. |
| 2116 | * We cannot access to the socket if the thread is different. |
| 2117 | */ |
| 2118 | if (socket->tid != tid) |
| 2119 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2120 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2121 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2122 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2123 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2124 | |
| 2125 | hlua->gc_count--; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2126 | ctx = container_of(peer, struct hlua_csk_ctx, xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2127 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2128 | /* Set the flag which destroy the session. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2129 | ctx->die = 1; |
| 2130 | appctx_wakeup(ctx->appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2131 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2132 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2133 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2134 | return 0; |
| 2135 | } |
| 2136 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2137 | /* The close function calls close_helper. |
| 2138 | */ |
| 2139 | __LJMP static int hlua_socket_close(lua_State *L) |
| 2140 | { |
| 2141 | MAY_LJMP(check_args(L, 1, "close")); |
| 2142 | return hlua_socket_close_helper(L); |
| 2143 | } |
| 2144 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2145 | /* This Lua function assumes that the stack contain three parameters. |
| 2146 | * 1 - USERDATA containing a struct socket |
| 2147 | * 2 - INTEGER with values of the macro defined below |
| 2148 | * If the integer is -1, we must read at most one line. |
| 2149 | * If the integer is -2, we ust read all the data until the |
| 2150 | * end of the stream. |
| 2151 | * If the integer is positive value, we must read a number of |
| 2152 | * bytes corresponding to this value. |
| 2153 | */ |
| 2154 | #define HLSR_READ_LINE (-1) |
| 2155 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2156 | __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] | 2157 | { |
| 2158 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2159 | int wanted = lua_tointeger(L, 2); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2160 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2161 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2162 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2163 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2165 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2166 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 2167 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2168 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2169 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 2170 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2171 | struct stream *s; |
| 2172 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2173 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2175 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2176 | hlua = hlua_gethlua(L); |
| 2177 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2178 | /* Check if this lua stack is schedulable. */ |
| 2179 | if (!hlua || !hlua->task) |
| 2180 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 2181 | "'frontend', 'backend' or 'task'")); |
| 2182 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2183 | /* Check if we run on the same thread than the xreator thread. |
| 2184 | * We cannot access to the socket if the thread is different. |
| 2185 | */ |
| 2186 | if (socket->tid != tid) |
| 2187 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2188 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2189 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2190 | peer = xref_get_peer_and_lock(&socket->xref); |
| 2191 | if (!peer) |
| 2192 | goto no_peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2193 | |
| 2194 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2195 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2196 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2197 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2198 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2199 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2200 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2201 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2202 | if (nblk < 0) /* Connection close. */ |
| 2203 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2204 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2205 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 2206 | |
| 2207 | /* remove final \r\n. */ |
| 2208 | if (nblk == 1) { |
| 2209 | if (blk1[len1-1] == '\n') { |
| 2210 | len1--; |
| 2211 | skip_at_end++; |
| 2212 | if (blk1[len1-1] == '\r') { |
| 2213 | len1--; |
| 2214 | skip_at_end++; |
| 2215 | } |
| 2216 | } |
| 2217 | } |
| 2218 | else { |
| 2219 | if (blk2[len2-1] == '\n') { |
| 2220 | len2--; |
| 2221 | skip_at_end++; |
| 2222 | if (blk2[len2-1] == '\r') { |
| 2223 | len2--; |
| 2224 | skip_at_end++; |
| 2225 | } |
| 2226 | } |
| 2227 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | } |
| 2229 | |
| 2230 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2232 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2233 | if (nblk < 0) /* Connection close. */ |
| 2234 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2235 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | goto connection_empty; |
| 2237 | } |
| 2238 | |
| 2239 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2240 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2241 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2242 | if (nblk < 0) /* Connection close. */ |
| 2243 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2244 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | goto connection_empty; |
| 2246 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2247 | missing_bytes = wanted - socket->b.n; |
| 2248 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2250 | len1 = missing_bytes; |
| 2251 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 2252 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | } |
| 2254 | |
| 2255 | len = len1; |
| 2256 | |
| 2257 | luaL_addlstring(&socket->b, blk1, len1); |
| 2258 | if (nblk == 2) { |
| 2259 | len += len2; |
| 2260 | luaL_addlstring(&socket->b, blk2, len2); |
| 2261 | } |
| 2262 | |
| 2263 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2264 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | |
| 2266 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 2267 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | |
| 2269 | /* If the pattern reclaim to read all the data |
| 2270 | * in the connection, got out. |
| 2271 | */ |
| 2272 | if (wanted == HLSR_READ_ALL) |
| 2273 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 2274 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2275 | goto connection_empty; |
| 2276 | |
| 2277 | /* Return result. */ |
| 2278 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | return 1; |
| 2281 | |
| 2282 | connection_closed: |
| 2283 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2284 | xref_unlock(&socket->xref, peer); |
| 2285 | |
| 2286 | no_peer: |
| 2287 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2288 | /* If the buffer containds data. */ |
| 2289 | if (socket->b.n > 0) { |
| 2290 | luaL_pushresult(&socket->b); |
| 2291 | return 1; |
| 2292 | } |
| 2293 | lua_pushnil(L); |
| 2294 | lua_pushstring(L, "connection closed."); |
| 2295 | return 2; |
| 2296 | |
| 2297 | connection_empty: |
| 2298 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2299 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_read, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2300 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2302 | } |
| 2303 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2304 | 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] | 2305 | return 0; |
| 2306 | } |
| 2307 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2308 | /* This Lua function gets two parameters. The first one can be string |
| 2309 | * or a number. If the string is "*l", the user requires one line. If |
| 2310 | * 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] | 2311 | * If the value is a number, the user require a number of bytes equal |
| 2312 | * to the value. The default value is "*l" (a line). |
| 2313 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2314 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | * integer takes this values: |
| 2316 | * -1 : read a line |
| 2317 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2318 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2320 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2321 | * concatenated with the read data. |
| 2322 | */ |
| 2323 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 2324 | { |
| 2325 | int wanted = HLSR_READ_LINE; |
| 2326 | const char *pattern; |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2327 | int lastarg, type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2328 | char *error; |
| 2329 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2330 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2331 | |
| 2332 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 2333 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 2334 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2335 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2336 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2337 | /* Check if we run on the same thread than the xreator thread. |
| 2338 | * We cannot access to the socket if the thread is different. |
| 2339 | */ |
| 2340 | if (socket->tid != tid) |
| 2341 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2342 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | /* check for pattern. */ |
| 2344 | if (lua_gettop(L) >= 2) { |
| 2345 | type = lua_type(L, 2); |
| 2346 | if (type == LUA_TSTRING) { |
| 2347 | pattern = lua_tostring(L, 2); |
| 2348 | if (strcmp(pattern, "*a") == 0) |
| 2349 | wanted = HLSR_READ_ALL; |
| 2350 | else if (strcmp(pattern, "*l") == 0) |
| 2351 | wanted = HLSR_READ_LINE; |
| 2352 | else { |
| 2353 | wanted = strtoll(pattern, &error, 10); |
| 2354 | if (*error != '\0') |
| 2355 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 2356 | } |
| 2357 | } |
| 2358 | else if (type == LUA_TNUMBER) { |
| 2359 | wanted = lua_tointeger(L, 2); |
| 2360 | if (wanted < 0) |
| 2361 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 2362 | } |
| 2363 | } |
| 2364 | |
| 2365 | /* Set pattern. */ |
| 2366 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 2367 | |
| 2368 | /* Check if we would replace the top by itself. */ |
| 2369 | if (lua_gettop(L) != 2) |
| 2370 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2372 | /* Save index of the top of the stack because since buffers are used, it |
| 2373 | * may change |
| 2374 | */ |
| 2375 | lastarg = lua_gettop(L); |
| 2376 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 2377 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2378 | luaL_buffinit(L, &socket->b); |
| 2379 | |
| 2380 | /* Check prefix. */ |
Christopher Faulet | c31b200 | 2021-05-03 10:11:13 +0200 | [diff] [blame] | 2381 | if (lastarg >= 3) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | if (lua_type(L, 3) != LUA_TSTRING) |
| 2383 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 2384 | pattern = lua_tolstring(L, 3, &len); |
| 2385 | luaL_addlstring(&socket->b, pattern, len); |
| 2386 | } |
| 2387 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2388 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2389 | } |
| 2390 | |
| 2391 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 2392 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2394 | 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] | 2395 | { |
| 2396 | struct hlua_socket *socket; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2397 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2398 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2399 | struct appctx *appctx; |
| 2400 | size_t buf_len; |
| 2401 | const char *buf; |
| 2402 | int len; |
| 2403 | int send_len; |
| 2404 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2405 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2406 | struct stream *s; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2407 | struct stconn *sc; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2409 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2410 | hlua = hlua_gethlua(L); |
| 2411 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2412 | /* Check if this lua stack is schedulable. */ |
| 2413 | if (!hlua || !hlua->task) |
| 2414 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2415 | "'frontend', 'backend' or 'task'")); |
| 2416 | |
| 2417 | /* Get object */ |
| 2418 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2419 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2420 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2421 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2422 | /* Check if we run on the same thread than the xreator thread. |
| 2423 | * We cannot access to the socket if the thread is different. |
| 2424 | */ |
| 2425 | if (socket->tid != tid) |
| 2426 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2427 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2428 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2430 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2431 | lua_pushinteger(L, -1); |
| 2432 | return 1; |
| 2433 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2434 | |
| 2435 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2436 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2437 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2438 | s = __sc_strm(sc); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2439 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2440 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2441 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2442 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2443 | lua_pushinteger(L, -1); |
| 2444 | return 1; |
| 2445 | } |
| 2446 | |
| 2447 | /* Update the input buffer data. */ |
| 2448 | buf += sent; |
| 2449 | send_len = buf_len - sent; |
| 2450 | |
| 2451 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2452 | if (sent >= buf_len) { |
| 2453 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2454 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2455 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2457 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2458 | * the request buffer if its not required. |
| 2459 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2460 | if (s->req.buf.size == 0) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2461 | if (!sc_alloc_ibuf(sc, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2462 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2463 | } |
| 2464 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2465 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2466 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2467 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2468 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2469 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* send data */ |
| 2472 | if (len < send_len) |
| 2473 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2474 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2475 | |
| 2476 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2477 | * the data" (-2) are not expected because the available length |
| 2478 | * is tested. |
| 2479 | * Other unknown error are also not expected. |
| 2480 | */ |
| 2481 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2482 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2483 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2484 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2485 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2486 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2487 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2488 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | return 1; |
| 2490 | } |
| 2491 | |
| 2492 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2493 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2494 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2495 | s->req.rex = TICK_ETERNITY; |
| 2496 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2497 | |
| 2498 | /* Update length sent. */ |
| 2499 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2500 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2501 | |
| 2502 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2503 | if (sent + len >= buf_len) { |
| 2504 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2506 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2507 | |
| 2508 | hlua_socket_write_yield_return: |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2509 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2510 | xref_unlock(&socket->xref, peer); |
| 2511 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2512 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2513 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2514 | 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] | 2515 | return 0; |
| 2516 | } |
| 2517 | |
| 2518 | /* This function initiate the send of data. It just check the input |
| 2519 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2520 | * 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] | 2521 | * "hlua_socket_write_yield" that can yield. |
| 2522 | * |
| 2523 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2524 | * the associated object. The second is a string buffer. The third is |
| 2525 | * a facultative integer that represents where is the buffer position |
| 2526 | * of the start of the data that can send. The first byte is the |
| 2527 | * position "1". The default value is "1". The fourth argument is a |
| 2528 | * facultative integer that represents where is the buffer position |
| 2529 | * of the end of the data that can send. The default is the last byte. |
| 2530 | */ |
| 2531 | static int hlua_socket_send(struct lua_State *L) |
| 2532 | { |
| 2533 | int i; |
| 2534 | int j; |
| 2535 | const char *buf; |
| 2536 | size_t buf_len; |
| 2537 | |
| 2538 | /* Check number of arguments. */ |
| 2539 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2540 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2541 | |
| 2542 | /* Get the string. */ |
| 2543 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2544 | |
| 2545 | /* Get and check j. */ |
| 2546 | if (lua_gettop(L) == 4) { |
| 2547 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2548 | if (j < 0) |
| 2549 | j = buf_len + j + 1; |
| 2550 | if (j > buf_len) |
| 2551 | j = buf_len + 1; |
| 2552 | lua_pop(L, 1); |
| 2553 | } |
| 2554 | else |
| 2555 | j = buf_len; |
| 2556 | |
| 2557 | /* Get and check i. */ |
| 2558 | if (lua_gettop(L) == 3) { |
| 2559 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2560 | if (i < 0) |
| 2561 | i = buf_len + i + 1; |
| 2562 | if (i > buf_len) |
| 2563 | i = buf_len + 1; |
| 2564 | lua_pop(L, 1); |
| 2565 | } else |
| 2566 | i = 1; |
| 2567 | |
| 2568 | /* Check bth i and j. */ |
| 2569 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2570 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2571 | return 1; |
| 2572 | } |
| 2573 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2574 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2575 | return 1; |
| 2576 | } |
| 2577 | if (i == 0) |
| 2578 | i = 1; |
| 2579 | if (j == 0) |
| 2580 | j = 1; |
| 2581 | |
| 2582 | /* Pop the string. */ |
| 2583 | lua_pop(L, 1); |
| 2584 | |
| 2585 | /* Update the buffer length. */ |
| 2586 | buf += i - 1; |
| 2587 | buf_len = j - i + 1; |
| 2588 | lua_pushlstring(L, buf, buf_len); |
| 2589 | |
| 2590 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2591 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2592 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2593 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | } |
| 2595 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2596 | #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] | 2597 | __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] | 2598 | { |
| 2599 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2600 | int ret; |
| 2601 | int len; |
| 2602 | char *p; |
| 2603 | |
| 2604 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2605 | if (ret <= 0) { |
| 2606 | lua_pushnil(L); |
| 2607 | return 1; |
| 2608 | } |
| 2609 | |
| 2610 | if (ret == AF_UNIX) { |
| 2611 | lua_pushstring(L, buffer+1); |
| 2612 | return 1; |
| 2613 | } |
| 2614 | else if (ret == AF_INET6) { |
| 2615 | buffer[0] = '['; |
| 2616 | len = strlen(buffer); |
| 2617 | buffer[len] = ']'; |
| 2618 | len++; |
| 2619 | buffer[len] = ':'; |
| 2620 | len++; |
| 2621 | p = buffer; |
| 2622 | } |
| 2623 | else if (ret == AF_INET) { |
| 2624 | p = buffer + 1; |
| 2625 | len = strlen(p); |
| 2626 | p[len] = ':'; |
| 2627 | len++; |
| 2628 | } |
| 2629 | else { |
| 2630 | lua_pushnil(L); |
| 2631 | return 1; |
| 2632 | } |
| 2633 | |
| 2634 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2635 | lua_pushnil(L); |
| 2636 | return 1; |
| 2637 | } |
| 2638 | |
| 2639 | lua_pushstring(L, p); |
| 2640 | return 1; |
| 2641 | } |
| 2642 | |
| 2643 | /* Returns information about the peer of the connection. */ |
| 2644 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2645 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2646 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2647 | struct xref *peer; |
| 2648 | struct appctx *appctx; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2649 | struct stconn *sc; |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2650 | const struct sockaddr_storage *dst; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2651 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2652 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2654 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2655 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2657 | /* Check if we run on the same thread than the xreator thread. |
| 2658 | * We cannot access to the socket if the thread is different. |
| 2659 | */ |
| 2660 | if (socket->tid != tid) |
| 2661 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2662 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2663 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2664 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2665 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2666 | lua_pushnil(L); |
| 2667 | return 1; |
| 2668 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2669 | |
| 2670 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2671 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2672 | dst = sc_dst(sc_opposite(sc)); |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2673 | if (!dst) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2674 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2675 | lua_pushnil(L); |
| 2676 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2677 | } |
| 2678 | |
Christopher Faulet | 16f16af | 2021-10-27 09:34:56 +0200 | [diff] [blame] | 2679 | ret = MAY_LJMP(hlua_socket_info(L, dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2680 | xref_unlock(&socket->xref, peer); |
| 2681 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
| 2684 | /* Returns information about my connection side. */ |
| 2685 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2686 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2687 | struct hlua_socket *socket; |
| 2688 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2689 | struct appctx *appctx; |
| 2690 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2691 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2692 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2693 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2694 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2695 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2696 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2697 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2698 | /* Check if we run on the same thread than the xreator thread. |
| 2699 | * We cannot access to the socket if the thread is different. |
| 2700 | */ |
| 2701 | if (socket->tid != tid) |
| 2702 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2703 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2704 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2705 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2706 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2707 | lua_pushnil(L); |
| 2708 | return 1; |
| 2709 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2710 | |
| 2711 | appctx = container_of(peer, struct hlua_csk_ctx, xref)->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2712 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2713 | |
Willy Tarreau | fd9417b | 2022-05-18 16:23:22 +0200 | [diff] [blame] | 2714 | conn = sc_conn(s->scb); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2715 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2716 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2717 | lua_pushnil(L); |
| 2718 | return 1; |
| 2719 | } |
| 2720 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2721 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2722 | xref_unlock(&socket->xref, peer); |
| 2723 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2724 | } |
| 2725 | |
| 2726 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2727 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2728 | .obj_type = OBJ_TYPE_APPLET, |
| 2729 | .name = "<LUA_TCP>", |
| 2730 | .fct = hlua_socket_handler, |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 2731 | .init = hlua_socket_init, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2732 | .release = hlua_socket_release, |
| 2733 | }; |
| 2734 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2735 | __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] | 2736 | { |
| 2737 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2738 | struct hlua *hlua; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2739 | struct xref *peer; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2740 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2741 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2742 | struct stream *s; |
| 2743 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2744 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 2745 | hlua = hlua_gethlua(L); |
| 2746 | if (!hlua) |
| 2747 | return 0; |
| 2748 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2749 | /* Check if we run on the same thread than the xreator thread. |
| 2750 | * We cannot access to the socket if the thread is different. |
| 2751 | */ |
| 2752 | if (socket->tid != tid) |
| 2753 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2754 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2755 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2756 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2757 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2758 | lua_pushnil(L); |
| 2759 | lua_pushstring(L, "Can't connect"); |
| 2760 | return 2; |
| 2761 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2762 | |
| 2763 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2764 | appctx = csk_ctx->appctx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2765 | s = appctx_strm(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 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 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2770 | if (socket->tid != tid) { |
| 2771 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2772 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2773 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2774 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2775 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2776 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2777 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2778 | lua_pushnil(L); |
| 2779 | lua_pushstring(L, "Can't connect"); |
| 2780 | return 2; |
| 2781 | } |
| 2782 | |
Willy Tarreau | 8e7c6e6 | 2022-05-18 17:58:02 +0200 | [diff] [blame] | 2783 | appctx = __sc_appctx(s->scf); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2784 | |
| 2785 | /* Check for connection established. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2786 | if (csk_ctx->connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2787 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2788 | lua_pushinteger(L, 1); |
| 2789 | return 1; |
| 2790 | } |
| 2791 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2792 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2793 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2794 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2795 | } |
| 2796 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2797 | 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] | 2798 | return 0; |
| 2799 | } |
| 2800 | |
| 2801 | /* This function fail or initite the connection. */ |
| 2802 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2803 | { |
| 2804 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2805 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2806 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2807 | struct hlua *hlua; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2808 | struct hlua_csk_ctx *csk_ctx; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2809 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2810 | int low, high; |
| 2811 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2812 | struct xref *peer; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2813 | struct stconn *sc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2814 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2815 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2816 | if (lua_gettop(L) < 2) |
| 2817 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2818 | |
| 2819 | /* Get args. */ |
| 2820 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2821 | |
| 2822 | /* Check if we run on the same thread than the xreator thread. |
| 2823 | * We cannot access to the socket if the thread is different. |
| 2824 | */ |
| 2825 | if (socket->tid != tid) |
| 2826 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2827 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2828 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2829 | if (lua_gettop(L) >= 3) { |
| 2830 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2831 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2832 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2833 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2834 | * that are not enclosed within square brackets. |
| 2835 | */ |
| 2836 | if (port > 0) { |
| 2837 | luaL_buffinit(L, &b); |
| 2838 | luaL_addstring(&b, ip); |
| 2839 | luaL_addchar(&b, ':'); |
| 2840 | luaL_pushresult(&b); |
| 2841 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2842 | } |
| 2843 | } |
| 2844 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2845 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2846 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2847 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2848 | lua_pushnil(L); |
| 2849 | return 1; |
| 2850 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2851 | |
| 2852 | /* Parse ip address. */ |
Willy Tarreau | 5fc9328 | 2020-09-16 18:25:03 +0200 | [diff] [blame] | 2853 | 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] | 2854 | if (!addr) { |
| 2855 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2856 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2857 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2858 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2859 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2860 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2861 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2862 | if (port == -1) { |
| 2863 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2864 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2865 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2866 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2867 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2868 | if (port == -1) { |
| 2869 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2870 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2871 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2872 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2873 | } |
| 2874 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2875 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2876 | csk_ctx = container_of(peer, struct hlua_csk_ctx, xref); |
| 2877 | appctx = csk_ctx->appctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 2878 | sc = appctx_sc(appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2879 | s = __sc_strm(sc); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2880 | |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 2881 | if (!sockaddr_alloc(&sc_opposite(sc)->dst, addr, sizeof(*addr))) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2882 | xref_unlock(&socket->xref, peer); |
| 2883 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2884 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2885 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2886 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2887 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 2888 | if (!hlua) |
| 2889 | return 0; |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2890 | |
| 2891 | /* inform the stream that we want to be notified whenever the |
| 2892 | * connection completes. |
| 2893 | */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 2894 | applet_need_more_data(appctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 2895 | applet_have_more_data(appctx); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2896 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2897 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2898 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2899 | |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2900 | if (!notification_new(&hlua->com, &csk_ctx->wake_on_write, hlua->task)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2901 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2902 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2903 | } |
| 2904 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2905 | |
| 2906 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2907 | /* Return yield waiting for connection. */ |
| 2908 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2909 | 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] | 2910 | |
| 2911 | return 0; |
| 2912 | } |
| 2913 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2914 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2915 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2916 | { |
| 2917 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2918 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2919 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2920 | |
| 2921 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2922 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2923 | |
| 2924 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2925 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2926 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2927 | lua_pushnil(L); |
| 2928 | return 1; |
| 2929 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2930 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2931 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2932 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 2933 | s->target = &socket_ssl->obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2934 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2935 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2936 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2937 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2938 | |
| 2939 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2940 | { |
| 2941 | return 0; |
| 2942 | } |
| 2943 | |
| 2944 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2945 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2946 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2947 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2948 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2949 | struct xref *peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2950 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2951 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2952 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2953 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2954 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2955 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2956 | /* convert the timeout to millis */ |
| 2957 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2958 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2959 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2960 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2961 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2962 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2963 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2964 | 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] | 2965 | |
| 2966 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2967 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2968 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2969 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2970 | /* Check if we run on the same thread than the xreator thread. |
| 2971 | * We cannot access to the socket if the thread is different. |
| 2972 | */ |
| 2973 | if (socket->tid != tid) |
| 2974 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2975 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2976 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2977 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2978 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2979 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2980 | WILL_LJMP(lua_error(L)); |
| 2981 | return 0; |
| 2982 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 2983 | |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 2984 | s = appctx_strm(container_of(peer, struct hlua_csk_ctx, xref)->appctx); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2985 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2986 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2987 | s->req.rto = tmout; |
| 2988 | s->req.wto = tmout; |
| 2989 | s->res.rto = tmout; |
| 2990 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2991 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2992 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2993 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2994 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2995 | |
| 2996 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2997 | task_queue(s->task); |
| 2998 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2999 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3000 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 3001 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 3002 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3003 | } |
| 3004 | |
| 3005 | __LJMP static int hlua_socket_new(lua_State *L) |
| 3006 | { |
| 3007 | struct hlua_socket *socket; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3008 | struct hlua_csk_ctx *ctx; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3009 | struct appctx *appctx; |
| 3010 | |
| 3011 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3012 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3013 | hlua_pusherror(L, "socket: full stack"); |
| 3014 | goto out_fail_conf; |
| 3015 | } |
| 3016 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3017 | /* Create the object: obj[0] = userdata. */ |
| 3018 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3019 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3020 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3021 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 3022 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3023 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3024 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 3025 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 3026 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 3027 | goto out_fail_conf; |
| 3028 | } |
| 3029 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3030 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3031 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 3032 | lua_setmetatable(L, -2); |
| 3033 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3034 | /* Create the applet context */ |
Christopher Faulet | 6095d57 | 2022-05-16 17:09:48 +0200 | [diff] [blame] | 3035 | appctx = appctx_new_here(&update_applet, NULL); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3036 | if (!appctx) { |
| 3037 | hlua_pusherror(L, "socket: out of memory"); |
Christopher Faulet | a9e8b39 | 2022-03-23 11:01:09 +0100 | [diff] [blame] | 3038 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 3039 | } |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3040 | ctx = applet_reserve_svcctx(appctx, sizeof(*ctx)); |
| 3041 | ctx->connected = 0; |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3042 | ctx->die = 0; |
| 3043 | LIST_INIT(&ctx->wake_on_write); |
| 3044 | LIST_INIT(&ctx->wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 3045 | |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3046 | if (appctx_init(appctx) == -1) { |
| 3047 | hlua_pusherror(L, "socket: fail to init applet."); |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3048 | goto out_fail_appctx; |
| 3049 | } |
| 3050 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 3051 | /* Initialise cross reference between stream and Lua socket object. */ |
Willy Tarreau | 5321da9 | 2022-05-06 11:57:34 +0200 | [diff] [blame] | 3052 | xref_create(&socket->xref, &ctx->xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3053 | return 1; |
| 3054 | |
Christopher Faulet | 13a35e5 | 2021-12-20 15:34:16 +0100 | [diff] [blame] | 3055 | out_fail_appctx: |
Christopher Faulet | 5f35a3e | 2022-05-12 15:31:05 +0200 | [diff] [blame] | 3056 | appctx_free_on_early_error(appctx); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 3057 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 3058 | WILL_LJMP(lua_error(L)); |
| 3059 | return 0; |
| 3060 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 3061 | |
| 3062 | /* |
| 3063 | * |
| 3064 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | * Class Channel |
| 3066 | * |
| 3067 | * |
| 3068 | */ |
| 3069 | |
| 3070 | /* Returns the struct hlua_channel join to the class channel in the |
| 3071 | * stack entry "ud" or throws an argument error. |
| 3072 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3075 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3076 | } |
| 3077 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3078 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 3079 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3080 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 3081 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3083 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3084 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | return 0; |
| 3086 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3087 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3088 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 3089 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3090 | |
| 3091 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3092 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 3093 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3094 | return 1; |
| 3095 | } |
| 3096 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3097 | /* Helper function returning a filter attached to a channel at the position <ud> |
| 3098 | * 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] | 3099 | * filter is attached, NULL is returned and <offset> and <len> are not |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 3100 | * initialized. |
| 3101 | */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3102 | 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] | 3103 | { |
| 3104 | struct filter *filter = NULL; |
| 3105 | |
| 3106 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 3107 | struct hlua_flt_ctx *flt_ctx; |
| 3108 | |
| 3109 | filter = lua_touserdata (L, -1); |
| 3110 | flt_ctx = filter->ctx; |
| 3111 | if (hlua_filter_from_payload(filter)) { |
| 3112 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 3113 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 3114 | } |
| 3115 | } |
| 3116 | |
| 3117 | lua_pop(L, 1); |
| 3118 | return filter; |
| 3119 | } |
| 3120 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3121 | /* Copies <len> bytes of data present in the channel's buffer, starting at the |
| 3122 | * 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] | 3123 | * responsibility to ensure <len> and <offset> are valid. It always return the |
| 3124 | * length of the built string. <len> may be 0, in this case, an empty string is |
| 3125 | * created and 0 is returned. |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3126 | */ |
| 3127 | 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] | 3128 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3129 | size_t block1, block2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | luaL_Buffer b; |
| 3131 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3132 | block1 = len; |
| 3133 | if (block1 > b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset))) |
| 3134 | block1 = b_contig_data(&chn->buf, b_peek_ofs(&chn->buf, offset)); |
| 3135 | block2 = len - block1; |
| 3136 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3137 | luaL_buffinit(L, &b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3138 | luaL_addlstring(&b, b_peek(&chn->buf, offset), block1); |
| 3139 | if (block2) |
| 3140 | luaL_addlstring(&b, b_orig(&chn->buf), block2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3141 | luaL_pushresult(&b); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3142 | return len; |
| 3143 | } |
| 3144 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3145 | /* Inserts the string <str> to the channel's buffer at the offset <offset>. This |
| 3146 | * function returns -1 if data cannot be copied. Otherwise, it returns the |
| 3147 | * number of bytes copied. |
| 3148 | */ |
| 3149 | static int _hlua_channel_insert(struct channel *chn, lua_State *L, struct ist str, size_t offset) |
| 3150 | { |
| 3151 | int ret = 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3152 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3153 | /* Nothing to do, just return */ |
| 3154 | if (unlikely(istlen(str) == 0)) |
| 3155 | goto end; |
| 3156 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3157 | if (istlen(str) > c_room(chn)) { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3158 | ret = -1; |
| 3159 | goto end; |
| 3160 | } |
| 3161 | ret = b_insert_blk(&chn->buf, offset, istptr(str), istlen(str)); |
| 3162 | |
| 3163 | end: |
| 3164 | return ret; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | } |
| 3166 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3167 | /* Removes <len> bytes of data at the absolute position <offset>. |
| 3168 | */ |
| 3169 | static void _hlua_channel_delete(struct channel *chn, size_t offset, size_t len) |
| 3170 | { |
| 3171 | size_t end = offset + len; |
| 3172 | |
| 3173 | if (b_peek(&chn->buf, end) != b_tail(&chn->buf)) |
| 3174 | b_move(&chn->buf, b_peek_ofs(&chn->buf, end), |
| 3175 | b_data(&chn->buf) - end, -len); |
| 3176 | b_sub(&chn->buf, len); |
| 3177 | } |
| 3178 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3179 | /* Copies input data in the channel's buffer. It is possible to set a specific |
| 3180 | * offset (0 by default) and a length (all remaining input data starting for the |
| 3181 | * offset by default). If there is not enough input data and more data can be |
| 3182 | * received, this function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3183 | * |
| 3184 | * From an action, All input data are considered. For a filter, the offset and |
| 3185 | * 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] | 3186 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3187 | __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] | 3188 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3189 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3190 | struct filter *filter; |
| 3191 | size_t input, output; |
| 3192 | int offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3193 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3194 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3195 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3196 | output = co_data(chn); |
| 3197 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3198 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3199 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3200 | if (filter && !hlua_filter_from_payload(filter)) |
| 3201 | WILL_LJMP(lua_error(L)); |
| 3202 | |
| 3203 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3204 | if (lua_gettop(L) > 1) { |
| 3205 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3206 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3207 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3208 | offset += output; |
| 3209 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3210 | lua_pushfstring(L, "offset out of range."); |
| 3211 | WILL_LJMP(lua_error(L)); |
| 3212 | } |
| 3213 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3214 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3215 | if (lua_gettop(L) == 3) { |
| 3216 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3217 | if (!len) |
| 3218 | goto dup; |
| 3219 | if (len == -1) |
| 3220 | len = global.tune.bufsize; |
| 3221 | if (len < 0) { |
| 3222 | lua_pushfstring(L, "length out of range."); |
| 3223 | WILL_LJMP(lua_error(L)); |
| 3224 | } |
| 3225 | } |
| 3226 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3227 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3228 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3229 | /* Yield waiting for more data, as requested */ |
| 3230 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_data_yield, TICK_ETERNITY, 0)); |
| 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 | } |
| 3234 | |
| 3235 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3236 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3237 | return 1; |
| 3238 | } |
| 3239 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3240 | /* Copies the first line (including the trailing LF) of input data in the |
| 3241 | * channel's buffer. It is possible to set a specific offset (0 by default) and |
| 3242 | * a length (all remaining input data starting for the offset by default). If |
| 3243 | * there is not enough input data and more data can be received, the function |
| 3244 | * yields. If a length is explicitly specified, no more data are |
| 3245 | * copied. Otherwise, if no LF is found and more data can be received, this |
| 3246 | * function yields. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3247 | * |
| 3248 | * From an action, All input data are considered. For a filter, the offset and |
| 3249 | * 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] | 3250 | */ |
| 3251 | __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] | 3252 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3253 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3254 | struct filter *filter; |
| 3255 | size_t l, input, output; |
| 3256 | int offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3257 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3258 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3259 | output = co_data(chn); |
| 3260 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3261 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3262 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3263 | if (filter && !hlua_filter_from_payload(filter)) |
| 3264 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3265 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3266 | offset = output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3267 | if (lua_gettop(L) > 1) { |
| 3268 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3269 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3270 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3271 | offset += output; |
| 3272 | if (offset < output || offset > input + output) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3273 | lua_pushfstring(L, "offset out of range."); |
| 3274 | WILL_LJMP(lua_error(L)); |
| 3275 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3276 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3277 | |
| 3278 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3279 | if (lua_gettop(L) == 3) { |
| 3280 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3281 | if (!len) |
| 3282 | goto dup; |
| 3283 | if (len == -1) |
| 3284 | len = global.tune.bufsize; |
| 3285 | if (len < 0) { |
| 3286 | lua_pushfstring(L, "length out of range."); |
| 3287 | WILL_LJMP(lua_error(L)); |
| 3288 | } |
| 3289 | } |
| 3290 | |
| 3291 | for (l = 0; l < len; l++) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3292 | if (l + offset >= output + input) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3293 | break; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3294 | if (*(b_peek(&chn->buf, offset + l)) == '\n') { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3295 | len = l+1; |
| 3296 | goto dup; |
| 3297 | } |
| 3298 | } |
| 3299 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3300 | if (offset + len > output + input) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3301 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3302 | /* Yield waiting for more data */ |
| 3303 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_line_yield, TICK_ETERNITY, 0)); |
| 3304 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3305 | len = output + input - offset; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3306 | } |
| 3307 | |
| 3308 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3309 | _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3310 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3311 | } |
| 3312 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3313 | /* [ DEPRECATED ] |
| 3314 | * |
| 3315 | * Duplicate all input data foud in the channel's buffer. The data are not |
| 3316 | * removed from the buffer. This function relies on _hlua_channel_dup(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3317 | * |
| 3318 | * From an action, All input data are considered. For a filter, the offset and |
| 3319 | * 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] | 3320 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3321 | __LJMP static int hlua_channel_dup(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3322 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3323 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3324 | struct filter *filter; |
| 3325 | size_t offset, len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3326 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3327 | MAY_LJMP(check_args(L, 1, "dup")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3328 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3329 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3330 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3331 | WILL_LJMP(lua_error(L)); |
| 3332 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3333 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3334 | offset = co_data(chn); |
| 3335 | len = ci_data(chn); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3336 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3337 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3338 | if (filter && !hlua_filter_from_payload(filter)) |
| 3339 | WILL_LJMP(lua_error(L)); |
| 3340 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3341 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3342 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3343 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3344 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3345 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3346 | _hlua_channel_dup(chn, L, offset, len); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3347 | return 1; |
| 3348 | } |
| 3349 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3350 | /* [ DEPRECATED ] |
| 3351 | * |
| 3352 | * Get all input data foud in the channel's buffer. The data are removed from |
| 3353 | * the buffer after the copy. This function relies on _hlua_channel_dup() and |
| 3354 | * _hlua_channel_delete(). |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3355 | * |
| 3356 | * From an action, All input data are considered. For a filter, the offset and |
| 3357 | * 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] | 3358 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3359 | __LJMP static int hlua_channel_get(lua_State *L) |
| 3360 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3361 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3362 | struct filter *filter; |
| 3363 | size_t offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3364 | int ret; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3365 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3366 | MAY_LJMP(check_args(L, 1, "get")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3367 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3368 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3369 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3370 | WILL_LJMP(lua_error(L)); |
| 3371 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3372 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3373 | offset = co_data(chn); |
| 3374 | len = ci_data(chn); |
| 3375 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3376 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3377 | if (filter && !hlua_filter_from_payload(filter)) |
| 3378 | WILL_LJMP(lua_error(L)); |
| 3379 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3380 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3381 | lua_pushnil(L); |
| 3382 | return 1; |
| 3383 | } |
| 3384 | |
| 3385 | ret = _hlua_channel_dup(chn, L, offset, len); |
| 3386 | _hlua_channel_delete(chn, offset, ret); |
| 3387 | return 1; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3388 | } |
| 3389 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3390 | /* This functions consumes and returns one line. If the channel is closed, |
| 3391 | * 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] | 3392 | * 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] | 3393 | * value. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3394 | * |
| 3395 | * From an action, All input data are considered. For a filter, the offset and |
| 3396 | * 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] | 3397 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3398 | __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] | 3399 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3400 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3401 | struct filter *filter; |
| 3402 | size_t l, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3403 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3404 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3405 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3406 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3407 | offset = co_data(chn); |
| 3408 | len = ci_data(chn); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3409 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3410 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3411 | if (filter && !hlua_filter_from_payload(filter)) |
| 3412 | WILL_LJMP(lua_error(L)); |
| 3413 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3414 | if (!ci_data(chn) && channel_input_closed(chn)) { |
| 3415 | lua_pushnil(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3416 | return 1; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3417 | } |
| 3418 | |
| 3419 | for (l = 0; l < len; l++) { |
| 3420 | if (*(b_peek(&chn->buf, offset+l)) == '\n') { |
| 3421 | len = l+1; |
| 3422 | goto dup; |
| 3423 | } |
| 3424 | } |
| 3425 | |
| 3426 | if (!HLUA_CANT_YIELD(hlua_gethlua(L)) && !channel_input_closed(chn) && channel_may_recv(chn)) { |
| 3427 | /* Yield waiting for more data */ |
| 3428 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
| 3429 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3430 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3431 | dup: |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3432 | ret = _hlua_channel_dup(chn, L, offset, len); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3433 | _hlua_channel_delete(chn, offset, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3434 | return 1; |
| 3435 | } |
| 3436 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3437 | /* [ DEPRECATED ] |
| 3438 | * |
| 3439 | * Check arguments for the function "hlua_channel_getline_yield". |
| 3440 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3441 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 3442 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3443 | struct channel *chn; |
| 3444 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3445 | MAY_LJMP(check_args(L, 1, "getline")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3446 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3447 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3448 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3449 | WILL_LJMP(lua_error(L)); |
| 3450 | } |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3451 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 3452 | } |
| 3453 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3454 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3455 | * available input data are returned. The offset may be negactive to start from |
| 3456 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3457 | * size. |
| 3458 | */ |
| 3459 | __LJMP static int hlua_channel_get_data(lua_State *L) |
| 3460 | { |
| 3461 | struct channel *chn; |
| 3462 | |
| 3463 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3464 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 3465 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3466 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3467 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3468 | WILL_LJMP(lua_error(L)); |
| 3469 | } |
| 3470 | return MAY_LJMP(hlua_channel_get_data_yield(L, 0, 0)); |
| 3471 | } |
| 3472 | |
| 3473 | /* Retrieves a given amount of input data at the given offset. By default all |
| 3474 | * available input data are returned. The offset may be negactive to start from |
| 3475 | * the end of input data. The length may be -1 to set it to the maximum buffer |
| 3476 | * size. |
| 3477 | */ |
| 3478 | __LJMP static int hlua_channel_get_line(lua_State *L) |
| 3479 | { |
| 3480 | struct channel *chn; |
| 3481 | |
| 3482 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3483 | WILL_LJMP(luaL_error(L, "'line' expects at most 2 arguments")); |
| 3484 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3485 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3486 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3487 | WILL_LJMP(lua_error(L)); |
| 3488 | } |
| 3489 | return MAY_LJMP(hlua_channel_get_line_yield(L, 0, 0)); |
| 3490 | } |
| 3491 | |
| 3492 | /* Appends a string into the input side of channel. It returns the length of the |
| 3493 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3494 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3495 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3496 | * |
| 3497 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3498 | */ |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3499 | __LJMP static int hlua_channel_append(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3500 | { |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3501 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3502 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3503 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3504 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3505 | int ret; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3506 | |
| 3507 | MAY_LJMP(check_args(L, 2, "append")); |
| 3508 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3509 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3510 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3511 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3512 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3513 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3514 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3515 | offset = co_data(chn); |
| 3516 | len = ci_data(chn); |
| 3517 | |
| 3518 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3519 | if (filter && !hlua_filter_from_payload(filter)) |
| 3520 | WILL_LJMP(lua_error(L)); |
| 3521 | |
| 3522 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3523 | if (ret > 0 && filter) { |
| 3524 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3525 | |
| 3526 | flt_update_offsets(filter, chn, ret); |
| 3527 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3528 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3529 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3530 | return 1; |
| 3531 | } |
| 3532 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3533 | /* Prepends a string into the input side of channel. It returns the length of the |
| 3534 | * written string, or -1 if the channel is closed or if the buffer size is too |
| 3535 | * little for the data. 0 may be returned if nothing is copied. This function |
| 3536 | * does not yield. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3537 | * |
| 3538 | * For a filter, the context is updated on success. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3539 | */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3540 | __LJMP static int hlua_channel_prepend(lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3541 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3542 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3543 | struct filter *filter; |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3544 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3545 | size_t sz, offset, len; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3546 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3547 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3548 | MAY_LJMP(check_args(L, 2, "prepend")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3549 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3550 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3551 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3552 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3553 | WILL_LJMP(lua_error(L)); |
| 3554 | } |
| 3555 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3556 | offset = co_data(chn); |
| 3557 | len = ci_data(chn); |
| 3558 | |
| 3559 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3560 | if (filter && !hlua_filter_from_payload(filter)) |
| 3561 | WILL_LJMP(lua_error(L)); |
| 3562 | |
| 3563 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3564 | if (ret > 0 && filter) { |
| 3565 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3566 | |
| 3567 | flt_update_offsets(filter, chn, ret); |
| 3568 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
| 3569 | } |
| 3570 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3571 | lua_pushinteger(L, ret); |
| 3572 | return 1; |
| 3573 | } |
| 3574 | |
| 3575 | /* 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^] | 3576 | * 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] | 3577 | * returns the length of the written string, or -1 if the channel is closed or |
| 3578 | * if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3579 | * |
| 3580 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3581 | */ |
| 3582 | __LJMP static int hlua_channel_insert_data(lua_State *L) |
| 3583 | { |
| 3584 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3585 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3586 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3587 | size_t sz, input, output; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3588 | int ret, offset; |
| 3589 | |
| 3590 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 3591 | WILL_LJMP(luaL_error(L, "'insert' expects at least 1 argument and at most 2 arguments")); |
| 3592 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3593 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3594 | |
| 3595 | output = co_data(chn); |
| 3596 | input = ci_data(chn); |
| 3597 | |
| 3598 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3599 | if (filter && !hlua_filter_from_payload(filter)) |
| 3600 | WILL_LJMP(lua_error(L)); |
| 3601 | |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame^] | 3602 | offset = output; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3603 | if (lua_gettop(L) > 2) { |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3604 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3605 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3606 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3607 | offset += output; |
Aurelien DARRAGON | afb7daf | 2022-10-04 12:16:05 +0200 | [diff] [blame^] | 3608 | if (offset > output + input) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3609 | lua_pushfstring(L, "offset out of range."); |
| 3610 | WILL_LJMP(lua_error(L)); |
| 3611 | } |
| 3612 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3613 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3614 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3615 | WILL_LJMP(lua_error(L)); |
| 3616 | } |
| 3617 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3618 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3619 | if (ret > 0 && filter) { |
| 3620 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3621 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3622 | flt_update_offsets(filter, chn, ret); |
| 3623 | flt_ctx->cur_len[CHN_IDX(chn)] += ret; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3624 | } |
| 3625 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3626 | lua_pushinteger(L, ret); |
| 3627 | return 1; |
| 3628 | } |
| 3629 | /* Replaces a given amount of input data at the given offset by a string |
| 3630 | * content. By default all remaining data are removed (offset = 0 and len = |
| 3631 | * -1). It returns the length of the written string, or -1 if the channel is |
| 3632 | * closed or if the buffer size is too little for the data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3633 | * |
| 3634 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3635 | */ |
| 3636 | __LJMP static int hlua_channel_set_data(lua_State *L) |
| 3637 | { |
| 3638 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3639 | struct filter *filter; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3640 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3641 | size_t sz, input, output; |
| 3642 | int ret, offset, len; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3643 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3644 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 3645 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 3646 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3647 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3648 | |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 3649 | if (IS_HTX_STRM(chn_strm(chn))) { |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3650 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3651 | WILL_LJMP(lua_error(L)); |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3652 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3653 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3654 | output = co_data(chn); |
| 3655 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3656 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3657 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3658 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3659 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3660 | |
| 3661 | offset = output; |
| 3662 | if (lua_gettop(L) > 2) { |
| 3663 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3664 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3665 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3666 | offset += output; |
| 3667 | if (offset < output || offset > input + output) { |
| 3668 | lua_pushfstring(L, "offset out of range."); |
| 3669 | WILL_LJMP(lua_error(L)); |
| 3670 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3671 | } |
| 3672 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3673 | len = output + input - offset; |
| 3674 | if (lua_gettop(L) == 4) { |
| 3675 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 3676 | if (!len) |
| 3677 | goto set; |
| 3678 | if (len == -1) |
| 3679 | len = output + input - offset; |
| 3680 | if (len < 0 || offset + len > output + input) { |
| 3681 | lua_pushfstring(L, "length out of range."); |
| 3682 | WILL_LJMP(lua_error(L)); |
| 3683 | } |
| 3684 | } |
| 3685 | |
| 3686 | set: |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3687 | /* 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] | 3688 | if (sz > c_room(chn) + len) |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3689 | lua_pushinteger(L, -1); |
| 3690 | else { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3691 | _hlua_channel_delete(chn, offset, len); |
| 3692 | ret = _hlua_channel_insert(chn, L, ist2(str, sz), offset); |
| 3693 | if (filter) { |
| 3694 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3695 | |
| 3696 | len -= (ret > 0 ? ret : 0); |
| 3697 | flt_update_offsets(filter, chn, -len); |
| 3698 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3699 | } |
| 3700 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3701 | lua_pushinteger(L, ret); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3702 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3703 | return 1; |
| 3704 | } |
| 3705 | |
| 3706 | /* 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] | 3707 | * 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] | 3708 | * the removed data. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3709 | * |
| 3710 | * For a filter, the context is updated on success. |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3711 | */ |
| 3712 | __LJMP static int hlua_channel_del_data(lua_State *L) |
| 3713 | { |
| 3714 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3715 | struct filter *filter; |
| 3716 | size_t input, output; |
| 3717 | int offset, len; |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3718 | |
| 3719 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 3720 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
| 3721 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3722 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3723 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3724 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3725 | WILL_LJMP(lua_error(L)); |
| 3726 | } |
| 3727 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3728 | output = co_data(chn); |
| 3729 | input = ci_data(chn); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3730 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3731 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3732 | if (filter && !hlua_filter_from_payload(filter)) |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3733 | WILL_LJMP(lua_error(L)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3734 | |
| 3735 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3736 | if (lua_gettop(L) > 1) { |
| 3737 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3738 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 3739 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3740 | offset += output; |
| 3741 | if (offset < output || offset > input + output) { |
| 3742 | lua_pushfstring(L, "offset out of range."); |
| 3743 | WILL_LJMP(lua_error(L)); |
| 3744 | } |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3745 | } |
| 3746 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3747 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 3748 | if (lua_gettop(L) == 3) { |
| 3749 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3750 | if (!len) |
| 3751 | goto end; |
| 3752 | if (len == -1) |
| 3753 | len = output + input - offset; |
| 3754 | if (len < 0 || offset + len > output + input) { |
| 3755 | lua_pushfstring(L, "length out of range."); |
| 3756 | WILL_LJMP(lua_error(L)); |
| 3757 | } |
| 3758 | } |
| 3759 | |
| 3760 | _hlua_channel_delete(chn, offset, len); |
| 3761 | if (filter) { |
| 3762 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3763 | |
| 3764 | flt_update_offsets(filter, chn, -len); |
| 3765 | flt_ctx->cur_len[CHN_IDX(chn)] -= len; |
| 3766 | } |
| 3767 | |
| 3768 | end: |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3769 | lua_pushinteger(L, len); |
Christopher Faulet | 23976d9 | 2021-08-06 09:59:49 +0200 | [diff] [blame] | 3770 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3771 | } |
| 3772 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3773 | /* Append data in the output side of the buffer. This data is immediately |
| 3774 | * sent. The function returns the amount of data written. If the buffer |
| 3775 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3776 | * if the channel is closed. |
| 3777 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3778 | __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] | 3779 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3780 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3781 | struct filter *filter; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3782 | const char *str; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3783 | size_t offset, len, sz; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3784 | int l, ret; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3785 | struct hlua *hlua; |
| 3786 | |
| 3787 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3788 | hlua = hlua_gethlua(L); |
| 3789 | if (!hlua) { |
| 3790 | lua_pushnil(L); |
| 3791 | return 1; |
| 3792 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3793 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3794 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3795 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 3796 | l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3797 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3798 | offset = co_data(chn); |
| 3799 | len = ci_data(chn); |
| 3800 | |
| 3801 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3802 | if (filter && !hlua_filter_from_payload(filter)) |
| 3803 | WILL_LJMP(lua_error(L)); |
| 3804 | |
| 3805 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3806 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3807 | lua_pushinteger(L, -1); |
| 3808 | return 1; |
| 3809 | } |
| 3810 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3811 | len = c_room(chn); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3812 | if (len > sz -l) { |
| 3813 | if (filter) { |
| 3814 | lua_pushinteger(L, -1); |
| 3815 | return 1; |
| 3816 | } |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3817 | len = sz - l; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3818 | } |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3819 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3820 | ret = _hlua_channel_insert(chn, L, ist2(str, len), offset); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3821 | if (ret == -1) { |
| 3822 | lua_pop(L, 1); |
| 3823 | lua_pushinteger(L, -1); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3824 | return 1; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3825 | } |
| 3826 | if (ret) { |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3827 | if (filter) { |
| 3828 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3829 | |
| 3830 | |
| 3831 | flt_update_offsets(filter, chn, ret); |
| 3832 | FLT_OFF(filter, chn) += ret; |
| 3833 | flt_ctx->cur_off[CHN_IDX(chn)] += ret; |
| 3834 | } |
| 3835 | else |
| 3836 | c_adv(chn, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3837 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3838 | l += ret; |
| 3839 | lua_pop(L, 1); |
| 3840 | lua_pushinteger(L, l); |
| 3841 | } |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3842 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3843 | if (l < sz) { |
| 3844 | /* Yield only if the channel's output is not empty. |
| 3845 | * Otherwise it means we cannot add more data. */ |
| 3846 | if (co_data(chn) == 0 || HLUA_CANT_YIELD(hlua_gethlua(L))) |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3847 | return 1; |
| 3848 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3849 | /* If we are waiting for space in the response buffer, we |
| 3850 | * must set the flag WAKERESWR. This flag required the task |
| 3851 | * wake up if any activity is detected on the response buffer. |
| 3852 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3853 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3854 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3855 | else |
| 3856 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3857 | 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] | 3858 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3859 | |
| 3860 | return 1; |
| 3861 | } |
| 3862 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3863 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3864 | * yield the LUA process, and resume it without checking the |
| 3865 | * input arguments. |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3866 | * |
| 3867 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3868 | */ |
| 3869 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3870 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3871 | struct channel *chn; |
| 3872 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3873 | MAY_LJMP(check_args(L, 2, "send")); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3874 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3875 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3876 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3877 | WILL_LJMP(lua_error(L)); |
| 3878 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3879 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3880 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3881 | } |
| 3882 | |
| 3883 | /* This function forward and amount of butes. The data pass from |
| 3884 | * the input side of the buffer to the output side, and can be |
| 3885 | * forwarded. This function never fails. |
| 3886 | * |
| 3887 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3888 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3889 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3890 | __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] | 3891 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3892 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3893 | struct filter *filter; |
| 3894 | size_t offset, len, fwd; |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3895 | int l, max; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3896 | struct hlua *hlua; |
| 3897 | |
| 3898 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 3899 | hlua = hlua_gethlua(L); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3900 | if (!hlua) { |
| 3901 | lua_pushnil(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 3902 | return 1; |
Thierry Fournier | 77016da | 2020-08-15 14:35:51 +0200 | [diff] [blame] | 3903 | } |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3904 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3905 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3906 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3907 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3908 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3909 | offset = co_data(chn); |
| 3910 | len = ci_data(chn); |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3911 | |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3912 | filter = hlua_channel_filter(L, 1, chn, &offset, &len); |
| 3913 | if (filter && !hlua_filter_from_payload(filter)) |
| 3914 | WILL_LJMP(lua_error(L)); |
| 3915 | |
| 3916 | max = fwd - l; |
| 3917 | if (max > len) |
| 3918 | max = len; |
| 3919 | |
| 3920 | if (filter) { |
| 3921 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 3922 | |
| 3923 | FLT_OFF(filter, chn) += max; |
| 3924 | flt_ctx->cur_off[CHN_IDX(chn)] += max; |
| 3925 | flt_ctx->cur_len[CHN_IDX(chn)] -= max; |
| 3926 | } |
| 3927 | else |
| 3928 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3929 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3930 | l += max; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3931 | lua_pop(L, 1); |
| 3932 | lua_pushinteger(L, l); |
| 3933 | |
| 3934 | /* Check if it miss bytes to forward. */ |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3935 | if (l < fwd) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3936 | /* The the input channel or the output channel are closed, we |
| 3937 | * must return the amount of data forwarded. |
| 3938 | */ |
Christopher Faulet | 2e60aa4 | 2021-08-05 11:58:37 +0200 | [diff] [blame] | 3939 | 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] | 3940 | return 1; |
| 3941 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3942 | /* If we are waiting for space data in the response buffer, we |
| 3943 | * must set the flag WAKERESWR. This flag required the task |
| 3944 | * wake up if any activity is detected on the response buffer. |
| 3945 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3946 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3947 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3948 | else |
| 3949 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3950 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3951 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3952 | 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] | 3953 | } |
| 3954 | |
| 3955 | return 1; |
| 3956 | } |
| 3957 | |
| 3958 | /* Just check the input and prepare the stack for the previous |
| 3959 | * function "hlua_channel_forward_yield" |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3960 | * |
| 3961 | * This function cannot be called from a filter. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3962 | */ |
| 3963 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3964 | { |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3965 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3966 | |
Christopher Faulet | 9a6ffda | 2021-08-06 13:49:54 +0200 | [diff] [blame] | 3967 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3968 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3969 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3970 | lua_pushfstring(L, "Cannot manipulate HAProxy channels in HTTP mode."); |
| 3971 | WILL_LJMP(lua_error(L)); |
| 3972 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3973 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3974 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3975 | } |
| 3976 | |
| 3977 | /* Just returns the number of bytes available in the input |
| 3978 | * side of the buffer. This function never fails. |
| 3979 | */ |
| 3980 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3981 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3982 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3983 | struct filter *filter; |
| 3984 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3985 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 3986 | MAY_LJMP(check_args(L, 1, "input")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3987 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3988 | |
| 3989 | output = co_data(chn); |
| 3990 | input = ci_data(chn); |
| 3991 | filter = hlua_channel_filter(L, 1, chn, &output, &input); |
| 3992 | if (filter || !IS_HTX_STRM(chn_strm(chn))) |
| 3993 | lua_pushinteger(L, input); |
| 3994 | else { |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3995 | struct htx *htx = htxbuf(&chn->buf); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 3996 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3997 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3998 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3999 | return 1; |
| 4000 | } |
| 4001 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4002 | /* Returns true if the channel is full. */ |
| 4003 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 4004 | { |
| 4005 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4006 | |
| 4007 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 4008 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 4009 | /* ignore the reserve, we are not on a producer side (ie in an |
| 4010 | * applet). |
| 4011 | */ |
| 4012 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 4013 | return 1; |
| 4014 | } |
| 4015 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4016 | /* Returns true if the channel may still receive data. */ |
| 4017 | __LJMP static int hlua_channel_may_recv(lua_State *L) |
| 4018 | { |
| 4019 | struct channel *chn; |
| 4020 | |
| 4021 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 4022 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4023 | lua_pushboolean(L, (!channel_input_closed(chn) && channel_may_recv(chn))); |
| 4024 | return 1; |
| 4025 | } |
| 4026 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 4027 | /* Returns true if the channel is the response channel. */ |
| 4028 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 4029 | { |
| 4030 | struct channel *chn; |
| 4031 | |
| 4032 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 4033 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 4034 | |
| 4035 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 4036 | return 1; |
| 4037 | } |
| 4038 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4039 | /* Just returns the number of bytes available in the output |
| 4040 | * side of the buffer. This function never fails. |
| 4041 | */ |
| 4042 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 4043 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 4044 | struct channel *chn; |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4045 | size_t output, input; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4046 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 4047 | MAY_LJMP(check_args(L, 1, "output")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 4048 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a1ac5fb | 2021-08-09 10:22:46 +0200 | [diff] [blame] | 4049 | |
| 4050 | output = co_data(chn); |
| 4051 | input = ci_data(chn); |
| 4052 | hlua_channel_filter(L, 1, chn, &output, &input); |
| 4053 | |
| 4054 | lua_pushinteger(L, output); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4055 | return 1; |
| 4056 | } |
| 4057 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4058 | /* |
| 4059 | * |
| 4060 | * |
| 4061 | * Class Fetches |
| 4062 | * |
| 4063 | * |
| 4064 | */ |
| 4065 | |
| 4066 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4067 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4068 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4069 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4070 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4071 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4072 | } |
| 4073 | |
| 4074 | /* This function creates and push in the stack a fetch object according |
| 4075 | * with a current TXN. |
| 4076 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4077 | 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] | 4078 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4079 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4080 | |
| 4081 | /* Check stack size. */ |
| 4082 | if (!lua_checkstack(L, 3)) |
| 4083 | return 0; |
| 4084 | |
| 4085 | /* Create the object: obj[0] = userdata. |
| 4086 | * Note that the base of the Fetches object is the |
| 4087 | * transaction object. |
| 4088 | */ |
| 4089 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4090 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4091 | lua_rawseti(L, -2, 0); |
| 4092 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4093 | hsmp->s = txn->s; |
| 4094 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4095 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4096 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4097 | |
| 4098 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 4099 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 4100 | lua_setmetatable(L, -2); |
| 4101 | |
| 4102 | return 1; |
| 4103 | } |
| 4104 | |
| 4105 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 4106 | * It uses closure argument to store the associated sample-fetch. It |
| 4107 | * returns only one argument or throws an error. An error is thrown |
| 4108 | * only if an error is encountered during the argument parsing. If |
| 4109 | * the "sample-fetch" function fails, nil is returned. |
| 4110 | */ |
| 4111 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 4112 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4113 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4114 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4115 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4116 | int i; |
| 4117 | struct sample smp; |
| 4118 | |
| 4119 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4120 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4121 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4122 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4123 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4124 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 4125 | /* Check execution authorization. */ |
| 4126 | if (f->use & SMP_USE_HTTP_ANY && |
| 4127 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 4128 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 4129 | "is not available in Lua services", f->kw); |
| 4130 | WILL_LJMP(lua_error(L)); |
| 4131 | } |
| 4132 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4133 | /* Get extra arguments. */ |
| 4134 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 4135 | if (i >= ARGM_NBARGS) |
| 4136 | break; |
| 4137 | hlua_lua2arg(L, i + 2, &args[i]); |
| 4138 | } |
| 4139 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4140 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4141 | |
| 4142 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 4143 | 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] | 4144 | |
| 4145 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 4146 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4147 | lua_pushfstring(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4148 | goto error; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4149 | } |
| 4150 | |
| 4151 | /* Initialise the sample. */ |
| 4152 | memset(&smp, 0, sizeof(smp)); |
| 4153 | |
| 4154 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4155 | 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] | 4156 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4157 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4158 | lua_pushstring(L, ""); |
| 4159 | else |
| 4160 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4161 | goto end; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4162 | } |
| 4163 | |
| 4164 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4165 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4166 | hlua_smp2lua_str(L, &smp); |
| 4167 | else |
| 4168 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4169 | |
| 4170 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4171 | free_args(args); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4172 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4173 | |
| 4174 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4175 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4176 | WILL_LJMP(lua_error(L)); |
| 4177 | return 0; /* Never reached */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 4178 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 4179 | |
| 4180 | /* |
| 4181 | * |
| 4182 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4183 | * Class Converters |
| 4184 | * |
| 4185 | * |
| 4186 | */ |
| 4187 | |
| 4188 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4189 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4190 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4191 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4192 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4193 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4194 | } |
| 4195 | |
| 4196 | /* This function creates and push in the stack a Converters object |
| 4197 | * according with a current TXN. |
| 4198 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4199 | 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] | 4200 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4201 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4202 | |
| 4203 | /* Check stack size. */ |
| 4204 | if (!lua_checkstack(L, 3)) |
| 4205 | return 0; |
| 4206 | |
| 4207 | /* Create the object: obj[0] = userdata. |
| 4208 | * Note that the base of the Converters object is the |
| 4209 | * same than the TXN object. |
| 4210 | */ |
| 4211 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4212 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4213 | lua_rawseti(L, -2, 0); |
| 4214 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 4215 | hsmp->s = txn->s; |
| 4216 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 4217 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4218 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4219 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4220 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4221 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 4222 | lua_setmetatable(L, -2); |
| 4223 | |
| 4224 | return 1; |
| 4225 | } |
| 4226 | |
| 4227 | /* This function is an LUA binding. It is called with each converter. |
| 4228 | * It uses closure argument to store the associated converter. It |
| 4229 | * returns only one argument or throws an error. An error is thrown |
| 4230 | * only if an error is encountered during the argument parsing. If |
| 4231 | * the converter function function fails, nil is returned. |
| 4232 | */ |
| 4233 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 4234 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4235 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4236 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 4237 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4238 | int i; |
| 4239 | struct sample smp; |
| 4240 | |
| 4241 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4242 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4243 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4244 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4245 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4246 | |
| 4247 | /* Get extra arguments. */ |
| 4248 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 4249 | if (i >= ARGM_NBARGS) |
| 4250 | break; |
| 4251 | hlua_lua2arg(L, i + 3, &args[i]); |
| 4252 | } |
| 4253 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4254 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4255 | |
| 4256 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 4257 | 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] | 4258 | |
| 4259 | /* Run the special args checker. */ |
| 4260 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 4261 | hlua_pusherror(L, "error in arguments"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4262 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4263 | } |
| 4264 | |
| 4265 | /* Initialise the sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4266 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4267 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 4268 | hlua_pusherror(L, "error in the input argument"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4269 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4270 | } |
| 4271 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 4272 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 4273 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4274 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4275 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4276 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4277 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4278 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4279 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 4280 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 4281 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4282 | hlua_pusherror(L, "error during the input argument casting"); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4283 | goto error; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 4287 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4288 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4289 | lua_pushstring(L, ""); |
| 4290 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4291 | lua_pushnil(L); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4292 | goto end; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4293 | } |
| 4294 | |
| 4295 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 4296 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 4297 | hlua_smp2lua_str(L, &smp); |
| 4298 | else |
| 4299 | hlua_smp2lua(L, &smp); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4300 | end: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4301 | free_args(args); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 4302 | return 1; |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4303 | |
| 4304 | error: |
Willy Tarreau | ee0d727 | 2021-07-16 10:26:56 +0200 | [diff] [blame] | 4305 | free_args(args); |
Christopher Faulet | aec27ef | 2020-08-06 08:54:25 +0200 | [diff] [blame] | 4306 | WILL_LJMP(lua_error(L)); |
| 4307 | return 0; /* Never reached */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4308 | } |
| 4309 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4310 | /* |
| 4311 | * |
| 4312 | * |
| 4313 | * Class AppletTCP |
| 4314 | * |
| 4315 | * |
| 4316 | */ |
| 4317 | |
| 4318 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4319 | * a class stream, otherwise it throws an error. |
| 4320 | */ |
| 4321 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 4322 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4323 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4324 | } |
| 4325 | |
| 4326 | /* This function creates and push in the stack an Applet object |
| 4327 | * according with a current TXN. |
| 4328 | */ |
| 4329 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 4330 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4331 | struct hlua_appctx *luactx; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4332 | struct stream *s = appctx_strm(ctx); |
Christopher Faulet | 2da02ae | 2022-02-24 13:45:27 +0100 | [diff] [blame] | 4333 | struct proxy *p; |
| 4334 | |
| 4335 | ALREADY_CHECKED(s); |
| 4336 | p = s->be; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4337 | |
| 4338 | /* Check stack size. */ |
| 4339 | if (!lua_checkstack(L, 3)) |
| 4340 | return 0; |
| 4341 | |
| 4342 | /* Create the object: obj[0] = userdata. |
| 4343 | * Note that the base of the Converters object is the |
| 4344 | * same than the TXN object. |
| 4345 | */ |
| 4346 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4347 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4348 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4349 | luactx->appctx = ctx; |
| 4350 | luactx->htxn.s = s; |
| 4351 | luactx->htxn.p = p; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4352 | |
| 4353 | /* Create the "f" field that contains a list of fetches. */ |
| 4354 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4355 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4356 | return 0; |
| 4357 | lua_settable(L, -3); |
| 4358 | |
| 4359 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4360 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4361 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4362 | return 0; |
| 4363 | lua_settable(L, -3); |
| 4364 | |
| 4365 | /* Create the "c" field that contains a list of converters. */ |
| 4366 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4367 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4368 | return 0; |
| 4369 | lua_settable(L, -3); |
| 4370 | |
| 4371 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 4372 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4373 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4374 | return 0; |
| 4375 | lua_settable(L, -3); |
| 4376 | |
| 4377 | /* Pop a class stream metatable and affect it to the table. */ |
| 4378 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 4379 | lua_setmetatable(L, -2); |
| 4380 | |
| 4381 | return 1; |
| 4382 | } |
| 4383 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4384 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 4385 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4386 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4387 | struct stream *s; |
| 4388 | const char *name; |
| 4389 | size_t len; |
| 4390 | struct sample smp; |
| 4391 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4392 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4393 | 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] | 4394 | |
| 4395 | /* It is useles to retrieve the stream, but this function |
| 4396 | * runs only in a stream context. |
| 4397 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4398 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4399 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4400 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4401 | |
| 4402 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4403 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4404 | hlua_lua2smp(L, 3, &smp); |
| 4405 | |
| 4406 | /* Store the sample in a variable. */ |
| 4407 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4408 | |
| 4409 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4410 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4411 | else |
| 4412 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4413 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4414 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4415 | } |
| 4416 | |
| 4417 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 4418 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4419 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4420 | struct stream *s; |
| 4421 | const char *name; |
| 4422 | size_t len; |
| 4423 | struct sample smp; |
| 4424 | |
| 4425 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4426 | |
| 4427 | /* It is useles to retrieve the stream, but this function |
| 4428 | * runs only in a stream context. |
| 4429 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4430 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4431 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4432 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4433 | |
| 4434 | /* Unset the variable. */ |
| 4435 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4436 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4437 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4438 | } |
| 4439 | |
| 4440 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 4441 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4442 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4443 | struct stream *s; |
| 4444 | const char *name; |
| 4445 | size_t len; |
| 4446 | struct sample smp; |
| 4447 | |
| 4448 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4449 | |
| 4450 | /* It is useles to retrieve the stream, but this function |
| 4451 | * runs only in a stream context. |
| 4452 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4453 | luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4454 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4455 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4456 | |
| 4457 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4458 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4459 | lua_pushnil(L); |
| 4460 | return 1; |
| 4461 | } |
| 4462 | |
| 4463 | return hlua_smp2lua(L, &smp); |
| 4464 | } |
| 4465 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4466 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 4467 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4468 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4469 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4470 | struct hlua *hlua; |
| 4471 | |
| 4472 | /* 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] | 4473 | if (!s->hlua) |
| 4474 | return 0; |
| 4475 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4476 | |
| 4477 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4478 | |
| 4479 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4480 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4481 | |
| 4482 | /* Get and store new value. */ |
| 4483 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4484 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4485 | |
| 4486 | return 0; |
| 4487 | } |
| 4488 | |
| 4489 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 4490 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4491 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 4492 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4493 | struct hlua *hlua; |
| 4494 | |
| 4495 | /* 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] | 4496 | if (!s->hlua) { |
| 4497 | lua_pushnil(L); |
| 4498 | return 1; |
| 4499 | } |
| 4500 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4501 | |
| 4502 | /* Push configuration index in the stack. */ |
| 4503 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4504 | |
| 4505 | return 1; |
| 4506 | } |
| 4507 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4508 | /* If expected data not yet available, it returns a yield. This function |
| 4509 | * consumes the data in the buffer. It returns a string containing the |
| 4510 | * data. This string can be empty. |
| 4511 | */ |
| 4512 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 4513 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4514 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4515 | struct stconn *sc = appctx_sc(luactx->appctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4516 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4517 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4518 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4519 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4520 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4521 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4522 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4523 | ret = co_getline_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4524 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4525 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4526 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4527 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4528 | 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] | 4529 | } |
| 4530 | |
| 4531 | /* End of data: commit the total strings and return. */ |
| 4532 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4533 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4534 | return 1; |
| 4535 | } |
| 4536 | |
| 4537 | /* Ensure that the block 2 length is usable. */ |
| 4538 | if (ret == 1) |
| 4539 | len2 = 0; |
| 4540 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 4541 | /* don't check the max length read and don't check. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4542 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4543 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4544 | |
| 4545 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4546 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4547 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4548 | return 1; |
| 4549 | } |
| 4550 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4551 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4552 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 4553 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4554 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4555 | |
| 4556 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4557 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4558 | |
| 4559 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 4560 | } |
| 4561 | |
| 4562 | /* If expected data not yet available, it returns a yield. This function |
| 4563 | * consumes the data in the buffer. It returns a string containing the |
| 4564 | * data. This string can be empty. |
| 4565 | */ |
| 4566 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 4567 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4568 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4569 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4570 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4571 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4572 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4573 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 4574 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 4575 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4576 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4577 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4578 | ret = co_getblk_nc(sc_oc(sc), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4579 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4580 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4581 | if (ret == 0) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4582 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4583 | 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] | 4584 | } |
| 4585 | |
| 4586 | /* End of data: commit the total strings and return. */ |
| 4587 | if (ret < 0) { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4588 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4589 | return 1; |
| 4590 | } |
| 4591 | |
| 4592 | /* Ensure that the block 2 length is usable. */ |
| 4593 | if (ret == 1) |
| 4594 | len2 = 0; |
| 4595 | |
| 4596 | if (len == -1) { |
| 4597 | |
| 4598 | /* If len == -1, catenate all the data avalaile and |
| 4599 | * yield because we want to get all the data until |
| 4600 | * the end of data stream. |
| 4601 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4602 | luaL_addlstring(&luactx->b, blk1, len1); |
| 4603 | luaL_addlstring(&luactx->b, blk2, len2); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4604 | co_skip(sc_oc(sc), len1 + len2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4605 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4606 | 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] | 4607 | |
| 4608 | } else { |
| 4609 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4610 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4611 | if (len1 > len) |
| 4612 | len1 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4613 | luaL_addlstring(&luactx->b, blk1, len1); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4614 | len -= len1; |
| 4615 | |
| 4616 | /* Copy the second block. */ |
| 4617 | if (len2 > len) |
| 4618 | len2 = len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4619 | luaL_addlstring(&luactx->b, blk2, len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4620 | len -= len2; |
| 4621 | |
| 4622 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4623 | co_skip(sc_oc(sc), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4625 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4626 | if (len > 0) { |
| 4627 | lua_pushinteger(L, len); |
| 4628 | lua_replace(L, 2); |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 4629 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4630 | 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] | 4631 | } |
| 4632 | |
| 4633 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4634 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4635 | return 1; |
| 4636 | } |
| 4637 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4638 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4639 | hlua_pusherror(L, "Lua: internal error"); |
| 4640 | WILL_LJMP(lua_error(L)); |
| 4641 | return 0; |
| 4642 | } |
| 4643 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4644 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4645 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 4646 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4647 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4648 | int len = -1; |
| 4649 | |
| 4650 | if (lua_gettop(L) > 2) |
| 4651 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4652 | if (lua_gettop(L) >= 2) { |
| 4653 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4654 | lua_pop(L, 1); |
| 4655 | } |
| 4656 | |
| 4657 | /* Confirm or set the required length */ |
| 4658 | lua_pushinteger(L, len); |
| 4659 | |
| 4660 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4661 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4662 | |
| 4663 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 4664 | } |
| 4665 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4666 | /* Append data in the output side of the buffer. This data is immediately |
| 4667 | * sent. The function returns the amount of data written. If the buffer |
| 4668 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4669 | * if the channel is closed. |
| 4670 | */ |
| 4671 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 4672 | { |
| 4673 | size_t len; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4674 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4675 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4676 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 4677 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4678 | struct channel *chn = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4679 | int max; |
| 4680 | |
| 4681 | /* Get the max amount of data which can write as input in the channel. */ |
| 4682 | max = channel_recv_max(chn); |
| 4683 | if (max > (len - l)) |
| 4684 | max = len - l; |
| 4685 | |
| 4686 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 4687 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4688 | |
| 4689 | /* update counters. */ |
| 4690 | l += max; |
| 4691 | lua_pop(L, 1); |
| 4692 | lua_pushinteger(L, l); |
| 4693 | |
| 4694 | /* If some data is not send, declares the situation to the |
| 4695 | * applet, and returns a yield. |
| 4696 | */ |
| 4697 | if (l < len) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 4698 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4699 | 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] | 4700 | } |
| 4701 | |
| 4702 | return 1; |
| 4703 | } |
| 4704 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4705 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 4706 | * yield the LUA process, and resume it without checking the |
| 4707 | * input arguments. |
| 4708 | */ |
| 4709 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 4710 | { |
| 4711 | MAY_LJMP(check_args(L, 2, "send")); |
| 4712 | lua_pushinteger(L, 0); |
| 4713 | |
| 4714 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 4715 | } |
| 4716 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 4717 | /* |
| 4718 | * |
| 4719 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4720 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4721 | * |
| 4722 | * |
| 4723 | */ |
| 4724 | |
| 4725 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 4726 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4727 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4728 | __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] | 4729 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 4730 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4731 | } |
| 4732 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4733 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4734 | * according with a current TXN. |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4735 | * It relies on the caller to have already reserved the room in ctx->svcctx |
| 4736 | * for the local storage of hlua_http_ctx. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4737 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4738 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4739 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4740 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4741 | struct hlua_appctx *luactx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 4742 | struct hlua_txn htxn; |
Willy Tarreau | 0698c80 | 2022-05-11 14:09:57 +0200 | [diff] [blame] | 4743 | struct stream *s = appctx_strm(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4744 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4745 | struct htx *htx; |
| 4746 | struct htx_blk *blk; |
| 4747 | struct htx_sl *sl; |
| 4748 | struct ist path; |
| 4749 | unsigned long long len = 0; |
| 4750 | int32_t pos; |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4751 | struct http_uri_parser parser; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4752 | |
| 4753 | /* Check stack size. */ |
| 4754 | if (!lua_checkstack(L, 3)) |
| 4755 | return 0; |
| 4756 | |
| 4757 | /* Create the object: obj[0] = userdata. |
| 4758 | * Note that the base of the Converters object is the |
| 4759 | * same than the TXN object. |
| 4760 | */ |
| 4761 | lua_newtable(L); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4762 | luactx = lua_newuserdata(L, sizeof(*luactx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4763 | lua_rawseti(L, -2, 0); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4764 | luactx->appctx = ctx; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 4765 | http_ctx->status = 200; /* Default status code returned. */ |
| 4766 | http_ctx->reason = NULL; /* Use default reason based on status */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4767 | luactx->htxn.s = s; |
| 4768 | luactx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4769 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4770 | /* Create the "f" field that contains a list of fetches. */ |
| 4771 | lua_pushstring(L, "f"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4772 | if (!hlua_fetches_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4773 | return 0; |
| 4774 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4775 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4776 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 4777 | lua_pushstring(L, "sf"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4778 | if (!hlua_fetches_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4779 | return 0; |
| 4780 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4781 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4782 | /* Create the "c" field that contains a list of converters. */ |
| 4783 | lua_pushstring(L, "c"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4784 | if (!hlua_converters_new(L, &luactx->htxn, 0)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4785 | return 0; |
| 4786 | lua_settable(L, -3); |
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 "sc" field that contains a list of stringsafe converters. */ |
| 4789 | lua_pushstring(L, "sc"); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4790 | if (!hlua_converters_new(L, &luactx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | return 0; |
| 4792 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4793 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4794 | htx = htxbuf(&s->req.buf); |
| 4795 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 4796 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4797 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4798 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4799 | /* Stores the request method. */ |
| 4800 | lua_pushstring(L, "method"); |
| 4801 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 4802 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4803 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4804 | /* Stores the http version. */ |
| 4805 | lua_pushstring(L, "version"); |
| 4806 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 4807 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4808 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4809 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 4810 | * the array on the top of the stack. |
| 4811 | */ |
| 4812 | lua_pushstring(L, "headers"); |
| 4813 | htxn.s = s; |
| 4814 | htxn.p = px; |
| 4815 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4816 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4817 | return 0; |
| 4818 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4819 | |
Amaury Denoyelle | c453f95 | 2021-07-06 11:40:12 +0200 | [diff] [blame] | 4820 | parser = http_uri_parser_init(htx_sl_req_uri(sl)); |
| 4821 | path = http_parse_path(&parser); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 4822 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4823 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4824 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4825 | p = path.ptr; |
Tim Duesterhus | 4c8f75f | 2021-11-06 15:14:44 +0100 | [diff] [blame] | 4826 | end = istend(path); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4827 | q = p; |
| 4828 | while (q < end && *q != '?') |
| 4829 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4830 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4831 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4832 | lua_pushstring(L, "path"); |
| 4833 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 4834 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4835 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4836 | /* Stores the query string. */ |
| 4837 | lua_pushstring(L, "qs"); |
| 4838 | if (*q == '?') |
| 4839 | q++; |
| 4840 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4841 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4842 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4843 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4844 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4845 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4846 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4847 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 4848 | if (type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4849 | break; |
| 4850 | if (type == HTX_BLK_DATA) |
| 4851 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4852 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4853 | if (htx->extra != ULLONG_MAX) |
| 4854 | len += htx->extra; |
| 4855 | |
| 4856 | /* Stores the request path. */ |
| 4857 | lua_pushstring(L, "length"); |
| 4858 | lua_pushinteger(L, len); |
| 4859 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4860 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4861 | /* Create an empty array of HTTP request headers. */ |
| 4862 | lua_pushstring(L, "response"); |
| 4863 | lua_newtable(L); |
| 4864 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4866 | /* Pop a class stream metatable and affect it to the table. */ |
| 4867 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4868 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4869 | |
| 4870 | return 1; |
| 4871 | } |
| 4872 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4873 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4874 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4875 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4876 | struct stream *s; |
| 4877 | const char *name; |
| 4878 | size_t len; |
| 4879 | struct sample smp; |
| 4880 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4881 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 4882 | 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] | 4883 | |
| 4884 | /* It is useles to retrieve the stream, but this function |
| 4885 | * runs only in a stream context. |
| 4886 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4887 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4888 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4889 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4890 | |
| 4891 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 4892 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4893 | hlua_lua2smp(L, 3, &smp); |
| 4894 | |
| 4895 | /* Store the sample in a variable. */ |
| 4896 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 4897 | |
| 4898 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 4899 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 4900 | else |
| 4901 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 4902 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4903 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4904 | } |
| 4905 | |
| 4906 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4907 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4908 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4909 | struct stream *s; |
| 4910 | const char *name; |
| 4911 | size_t len; |
| 4912 | struct sample smp; |
| 4913 | |
| 4914 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4915 | |
| 4916 | /* It is useles to retrieve the stream, but this function |
| 4917 | * runs only in a stream context. |
| 4918 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4919 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4920 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4921 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4922 | |
| 4923 | /* Unset the variable. */ |
| 4924 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4925 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4926 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4927 | } |
| 4928 | |
| 4929 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4930 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4931 | struct hlua_appctx *luactx; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4932 | struct stream *s; |
| 4933 | const char *name; |
| 4934 | size_t len; |
| 4935 | struct sample smp; |
| 4936 | |
| 4937 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4938 | |
| 4939 | /* It is useles to retrieve the stream, but this function |
| 4940 | * runs only in a stream context. |
| 4941 | */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4942 | luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4943 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4944 | s = luactx->htxn.s; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4945 | |
| 4946 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Willy Tarreau | e352b9d | 2021-09-03 11:52:38 +0200 | [diff] [blame] | 4947 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4948 | lua_pushnil(L); |
| 4949 | return 1; |
| 4950 | } |
| 4951 | |
| 4952 | return hlua_smp2lua(L, &smp); |
| 4953 | } |
| 4954 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4955 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4956 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4957 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4958 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4959 | struct hlua *hlua; |
| 4960 | |
| 4961 | /* 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] | 4962 | if (!s->hlua) |
| 4963 | return 0; |
| 4964 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4965 | |
| 4966 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4967 | |
| 4968 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4969 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4970 | |
| 4971 | /* Get and store new value. */ |
| 4972 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4973 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4974 | |
| 4975 | return 0; |
| 4976 | } |
| 4977 | |
| 4978 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4979 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 4980 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4981 | struct stream *s = luactx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4982 | struct hlua *hlua; |
| 4983 | |
| 4984 | /* 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] | 4985 | if (!s->hlua) { |
| 4986 | lua_pushnil(L); |
| 4987 | return 1; |
| 4988 | } |
| 4989 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4990 | |
| 4991 | /* Push configuration index in the stack. */ |
| 4992 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4993 | |
| 4994 | return 1; |
| 4995 | } |
| 4996 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4997 | /* If expected data not yet available, it returns a yield. This function |
| 4998 | * consumes the data in the buffer. It returns a string containing the |
| 4999 | * data. This string can be empty. |
| 5000 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5001 | __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] | 5002 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5003 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5004 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5005 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5006 | struct htx *htx; |
| 5007 | struct htx_blk *blk; |
| 5008 | size_t count; |
| 5009 | int stop = 0; |
| 5010 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5011 | htx = htx_from_buf(&req->buf); |
| 5012 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 5013 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 5014 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5015 | while (count && !stop && blk) { |
| 5016 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5017 | uint32_t sz = htx_get_blksz(blk); |
| 5018 | struct ist v; |
| 5019 | uint32_t vlen; |
| 5020 | char *nl; |
| 5021 | |
| 5022 | vlen = sz; |
| 5023 | if (vlen > count) { |
| 5024 | if (type != HTX_BLK_DATA) |
| 5025 | break; |
| 5026 | vlen = count; |
| 5027 | } |
| 5028 | |
| 5029 | switch (type) { |
| 5030 | case HTX_BLK_UNUSED: |
| 5031 | break; |
| 5032 | |
| 5033 | case HTX_BLK_DATA: |
| 5034 | v = htx_get_blk_value(htx, blk); |
| 5035 | v.len = vlen; |
| 5036 | nl = istchr(v, '\n'); |
| 5037 | if (nl != NULL) { |
| 5038 | stop = 1; |
| 5039 | vlen = nl - v.ptr + 1; |
| 5040 | } |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5041 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5042 | break; |
| 5043 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5044 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5045 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5046 | stop = 1; |
| 5047 | break; |
| 5048 | |
| 5049 | default: |
| 5050 | break; |
| 5051 | } |
| 5052 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5053 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5054 | count -= vlen; |
| 5055 | if (sz == vlen) |
| 5056 | blk = htx_remove_blk(htx, blk); |
| 5057 | else { |
| 5058 | htx_cut_data_blk(htx, blk, vlen); |
| 5059 | break; |
| 5060 | } |
| 5061 | } |
| 5062 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5063 | /* The message was fully consumed and no more data are expected |
| 5064 | * (EOM flag set). |
| 5065 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5066 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5067 | stop = 1; |
| 5068 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5069 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5070 | if (!stop) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5071 | applet_need_more_data(luactx->appctx); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5072 | 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] | 5073 | } |
| 5074 | |
| 5075 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5076 | luaL_pushresult(&luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5077 | return 1; |
| 5078 | } |
| 5079 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5080 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5081 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5082 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5083 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5084 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5085 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5086 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5087 | luaL_buffinit(L, &luactx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5088 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5089 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5090 | } |
| 5091 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5092 | /* If expected data not yet available, it returns a yield. This function |
| 5093 | * consumes the data in the buffer. It returns a string containing the |
| 5094 | * data. This string can be empty. |
| 5095 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5096 | __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] | 5097 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5098 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5099 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5100 | struct channel *req = sc_oc(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5101 | struct htx *htx; |
| 5102 | struct htx_blk *blk; |
| 5103 | size_t count; |
| 5104 | int len; |
| 5105 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5106 | htx = htx_from_buf(&req->buf); |
| 5107 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5108 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 5109 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5110 | while (count && len && blk) { |
| 5111 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5112 | uint32_t sz = htx_get_blksz(blk); |
| 5113 | struct ist v; |
| 5114 | uint32_t vlen; |
| 5115 | |
| 5116 | vlen = sz; |
| 5117 | if (len > 0 && vlen > len) |
| 5118 | vlen = len; |
| 5119 | if (vlen > count) { |
| 5120 | if (type != HTX_BLK_DATA) |
| 5121 | break; |
| 5122 | vlen = count; |
| 5123 | } |
| 5124 | |
| 5125 | switch (type) { |
| 5126 | case HTX_BLK_UNUSED: |
| 5127 | break; |
| 5128 | |
| 5129 | case HTX_BLK_DATA: |
| 5130 | v = htx_get_blk_value(htx, blk); |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5131 | luaL_addlstring(&luactx->b, v.ptr, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5132 | break; |
| 5133 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5134 | case HTX_BLK_TLR: |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 5135 | case HTX_BLK_EOT: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5136 | len = 0; |
| 5137 | break; |
| 5138 | |
| 5139 | default: |
| 5140 | break; |
| 5141 | } |
| 5142 | |
Willy Tarreau | 8424004 | 2022-02-28 16:51:23 +0100 | [diff] [blame] | 5143 | c_rew(req, vlen); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5144 | count -= vlen; |
| 5145 | if (len > 0) |
| 5146 | len -= vlen; |
| 5147 | if (sz == vlen) |
| 5148 | blk = htx_remove_blk(htx, blk); |
| 5149 | else { |
| 5150 | htx_cut_data_blk(htx, blk, vlen); |
| 5151 | break; |
| 5152 | } |
| 5153 | } |
| 5154 | |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5155 | /* The message was fully consumed and no more data are expected |
| 5156 | * (EOM flag set). |
| 5157 | */ |
Christopher Faulet | 4a20972 | 2022-08-29 15:37:16 +0200 | [diff] [blame] | 5158 | if (htx_is_empty(htx) && (req->flags & CF_EOI)) |
Christopher Faulet | eccb31c | 2021-04-02 14:24:56 +0200 | [diff] [blame] | 5159 | len = 0; |
| 5160 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5161 | htx_to_buf(htx, &req->buf); |
| 5162 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5163 | /* If we are no other data available, yield waiting for new data. */ |
| 5164 | if (len) { |
| 5165 | if (len > 0) { |
| 5166 | lua_pushinteger(L, len); |
| 5167 | lua_replace(L, 2); |
| 5168 | } |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 5169 | applet_need_more_data(luactx->appctx); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5170 | 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] | 5171 | } |
| 5172 | |
| 5173 | /* return the result. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5174 | luaL_pushresult(&luactx->b); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5175 | return 1; |
| 5176 | } |
| 5177 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5178 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5179 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 5180 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5181 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5182 | int len = -1; |
| 5183 | |
| 5184 | /* Check arguments. */ |
| 5185 | if (lua_gettop(L) > 2) |
| 5186 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 5187 | if (lua_gettop(L) >= 2) { |
| 5188 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5189 | lua_pop(L, 1); |
| 5190 | } |
| 5191 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5192 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5193 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5194 | /* Initialise the string catenation. */ |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5195 | luaL_buffinit(L, &luactx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5196 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5197 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5198 | } |
| 5199 | |
| 5200 | /* Append data in the output side of the buffer. This data is immediately |
| 5201 | * sent. The function returns the amount of data written. If the buffer |
| 5202 | * cannot contain the data, the function yields. The function returns -1 |
| 5203 | * if the channel is closed. |
| 5204 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5205 | __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] | 5206 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5207 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5208 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5209 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5210 | struct htx *htx = htx_from_buf(&res->buf); |
| 5211 | const char *data; |
| 5212 | size_t len; |
| 5213 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 5214 | int max; |
| 5215 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 5216 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5217 | if (!max) |
| 5218 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5219 | |
| 5220 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5221 | |
| 5222 | /* Get the max amount of data which can write as input in the channel. */ |
| 5223 | if (max > (len - l)) |
| 5224 | max = len - l; |
| 5225 | |
| 5226 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 5227 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5228 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5229 | |
| 5230 | /* update counters. */ |
| 5231 | l += max; |
| 5232 | lua_pop(L, 1); |
| 5233 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5234 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5235 | /* If some data is not send, declares the situation to the |
| 5236 | * applet, and returns a yield. |
| 5237 | */ |
| 5238 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 5239 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 5240 | htx_to_buf(htx, &res->buf); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5241 | sc_need_room(sc); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5242 | 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] | 5243 | } |
| 5244 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5245 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5246 | return 1; |
| 5247 | } |
| 5248 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5249 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5250 | * yield the LUA process, and resume it without checking the |
| 5251 | * input arguments. |
| 5252 | */ |
| 5253 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 5254 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5255 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5256 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5257 | |
| 5258 | /* We want to send some data. Headers must be sent. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5259 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5260 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 5261 | WILL_LJMP(lua_error(L)); |
| 5262 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5263 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5264 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5265 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5266 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5267 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5268 | } |
| 5269 | |
| 5270 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 5271 | { |
| 5272 | const char *name; |
| 5273 | int ret; |
| 5274 | |
| 5275 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 5276 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5277 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 5278 | |
| 5279 | /* Push in the stack the "response" entry. */ |
| 5280 | ret = lua_getfield(L, 1, "response"); |
| 5281 | if (ret != LUA_TTABLE) { |
| 5282 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 5283 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 5284 | WILL_LJMP(lua_error(L)); |
| 5285 | } |
| 5286 | |
| 5287 | /* check if the header is already registered if it is not |
| 5288 | * the case, register it. |
| 5289 | */ |
| 5290 | ret = lua_getfield(L, -1, name); |
| 5291 | if (ret == LUA_TNIL) { |
| 5292 | |
| 5293 | /* Entry not found. */ |
| 5294 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 5295 | |
| 5296 | /* Insert the new header name in the array in the top of the stack. |
| 5297 | * It left the new array in the top of the stack. |
| 5298 | */ |
| 5299 | lua_newtable(L); |
| 5300 | lua_pushvalue(L, 2); |
| 5301 | lua_pushvalue(L, -2); |
| 5302 | lua_settable(L, -4); |
| 5303 | |
| 5304 | } else if (ret != LUA_TTABLE) { |
| 5305 | |
| 5306 | /* corruption error. */ |
| 5307 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 5308 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 5309 | WILL_LJMP(lua_error(L)); |
| 5310 | } |
| 5311 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5312 | /* Now the top of thestack is an array of values. We push |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5313 | * the header value as new entry. |
| 5314 | */ |
| 5315 | lua_pushvalue(L, 3); |
| 5316 | ret = lua_rawlen(L, -2); |
| 5317 | lua_rawseti(L, -2, ret + 1); |
| 5318 | lua_pushboolean(L, 1); |
| 5319 | return 1; |
| 5320 | } |
| 5321 | |
| 5322 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 5323 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5324 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5325 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5326 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5327 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5328 | |
| 5329 | if (status < 100 || status > 599) { |
| 5330 | lua_pushboolean(L, 0); |
| 5331 | return 1; |
| 5332 | } |
| 5333 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5334 | http_ctx->status = status; |
| 5335 | http_ctx->reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5336 | lua_pushboolean(L, 1); |
| 5337 | return 1; |
| 5338 | } |
| 5339 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5340 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5341 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5342 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5343 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5344 | struct hlua_http_ctx *http_ctx = luactx->appctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5345 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5346 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5347 | struct htx *htx; |
| 5348 | struct htx_sl *sl; |
| 5349 | struct h1m h1m; |
| 5350 | const char *status, *reason; |
| 5351 | const char *name, *value; |
| 5352 | size_t nlen, vlen; |
| 5353 | unsigned int flags; |
| 5354 | |
| 5355 | /* Send the message at once. */ |
| 5356 | htx = htx_from_buf(&res->buf); |
| 5357 | h1m_init_res(&h1m); |
| 5358 | |
| 5359 | /* Use the same http version than the request. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5360 | status = ultoa_r(http_ctx->status, trash.area, trash.size); |
| 5361 | reason = http_ctx->reason; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5362 | if (reason == NULL) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5363 | reason = http_get_reason(http_ctx->status); |
| 5364 | if (http_ctx->flags & APPLET_HTTP11) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5365 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5366 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 5367 | } |
| 5368 | else { |
| 5369 | flags = HTX_SL_F_IS_RESP; |
| 5370 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 5371 | } |
| 5372 | if (!sl) { |
| 5373 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5374 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5375 | WILL_LJMP(lua_error(L)); |
| 5376 | } |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5377 | sl->info.res.status = http_ctx->status; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5378 | |
| 5379 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
Aurelien DARRAGON | cd341d5 | 2022-09-29 12:00:04 +0200 | [diff] [blame] | 5380 | /* FIXME: according to Lua doc, 0 is considered to be invalid index? */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5381 | lua_pushvalue(L, 0); |
| 5382 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 5383 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5384 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5385 | WILL_LJMP(lua_error(L)); |
| 5386 | } |
| 5387 | |
| 5388 | /* Browse the list of headers. */ |
| 5389 | lua_pushnil(L); |
| 5390 | while(lua_next(L, -2) != 0) { |
| 5391 | /* We expect a string as -2. */ |
| 5392 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 5393 | 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] | 5394 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5395 | lua_typename(L, lua_type(L, -2))); |
| 5396 | WILL_LJMP(lua_error(L)); |
| 5397 | } |
| 5398 | name = lua_tolstring(L, -2, &nlen); |
| 5399 | |
| 5400 | /* We expect an array as -1. */ |
| 5401 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 5402 | 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] | 5403 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5404 | name, |
| 5405 | lua_typename(L, lua_type(L, -1))); |
| 5406 | WILL_LJMP(lua_error(L)); |
| 5407 | } |
| 5408 | |
| 5409 | /* Browse the table who is on the top of the stack. */ |
| 5410 | lua_pushnil(L); |
| 5411 | while(lua_next(L, -2) != 0) { |
| 5412 | int id; |
| 5413 | |
| 5414 | /* We expect a number as -2. */ |
| 5415 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 5416 | 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] | 5417 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5418 | name, |
| 5419 | lua_typename(L, lua_type(L, -2))); |
| 5420 | WILL_LJMP(lua_error(L)); |
| 5421 | } |
| 5422 | id = lua_tointeger(L, -2); |
| 5423 | |
| 5424 | /* We expect a string as -2. */ |
| 5425 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 5426 | 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] | 5427 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5428 | name, id, |
| 5429 | lua_typename(L, lua_type(L, -1))); |
| 5430 | WILL_LJMP(lua_error(L)); |
| 5431 | } |
| 5432 | value = lua_tolstring(L, -1, &vlen); |
| 5433 | |
| 5434 | /* Simple Protocol checks. */ |
Christopher Faulet | 545fbba | 2021-09-28 09:36:25 +0200 | [diff] [blame] | 5435 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) { |
| 5436 | int ret; |
| 5437 | |
| 5438 | ret = h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
| 5439 | if (ret < 0) { |
| 5440 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 5441 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
| 5442 | name); |
| 5443 | WILL_LJMP(lua_error(L)); |
| 5444 | } |
| 5445 | else if (ret == 0) |
| 5446 | goto next; /* Skip it */ |
| 5447 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5448 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 5449 | struct ist v = ist2(value, vlen); |
| 5450 | int ret; |
| 5451 | |
| 5452 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 5453 | if (ret < 0) { |
| 5454 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5455 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5456 | name); |
| 5457 | WILL_LJMP(lua_error(L)); |
| 5458 | } |
| 5459 | else if (ret == 0) |
| 5460 | goto next; /* Skip it */ |
| 5461 | } |
| 5462 | |
| 5463 | /* Add a new header */ |
| 5464 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 5465 | 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] | 5466 | luactx->appctx->rule->arg.hlua_rule->fcn->name, |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5467 | name); |
| 5468 | WILL_LJMP(lua_error(L)); |
| 5469 | } |
| 5470 | next: |
| 5471 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5472 | lua_pop(L, 1); |
| 5473 | } |
| 5474 | |
| 5475 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 5476 | lua_pop(L, 1); |
| 5477 | } |
| 5478 | |
| 5479 | if (h1m.flags & H1_MF_CHNK) |
| 5480 | h1m.flags &= ~H1_MF_CLEN; |
| 5481 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5482 | h1m.flags |= H1_MF_XFER_LEN; |
| 5483 | |
| 5484 | /* Uset HTX start-line flags */ |
| 5485 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5486 | flags |= HTX_SL_F_XFER_ENC; |
| 5487 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5488 | flags |= HTX_SL_F_XFER_LEN; |
| 5489 | if (h1m.flags & H1_MF_CHNK) |
| 5490 | flags |= HTX_SL_F_CHNK; |
| 5491 | else if (h1m.flags & H1_MF_CLEN) |
| 5492 | flags |= HTX_SL_F_CLEN; |
| 5493 | if (h1m.body_len == 0) |
| 5494 | flags |= HTX_SL_F_BODYLESS; |
| 5495 | } |
| 5496 | sl->flags |= flags; |
| 5497 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 5498 | /* 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] | 5499 | * and the status code implies the presence of a message body, we must |
| 5500 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5501 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 5502 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5503 | */ |
| 5504 | 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] | 5505 | http_ctx->status >= 200 && http_ctx->status != 204 && http_ctx->status != 304) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5506 | /* Add a new header */ |
| 5507 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 5508 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 5509 | 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] | 5510 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5511 | WILL_LJMP(lua_error(L)); |
| 5512 | } |
| 5513 | } |
| 5514 | |
| 5515 | /* Finalize headers. */ |
| 5516 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 5517 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5518 | luactx->appctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5519 | WILL_LJMP(lua_error(L)); |
| 5520 | } |
| 5521 | |
| 5522 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 5523 | b_reset(&res->buf); |
| 5524 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 5525 | WILL_LJMP(lua_error(L)); |
| 5526 | } |
| 5527 | |
| 5528 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 5529 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5530 | |
| 5531 | /* Headers sent, set the flag. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 5532 | http_ctx->flags |= APPLET_HDR_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5533 | return 0; |
| 5534 | |
| 5535 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5536 | /* We will build the status line and the headers of the HTTP response. |
| 5537 | * We will try send at once if its not possible, we give back the hand |
| 5538 | * waiting for more room. |
| 5539 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5540 | __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] | 5541 | { |
Willy Tarreau | 7e702d1 | 2021-04-28 17:59:21 +0200 | [diff] [blame] | 5542 | struct hlua_appctx *luactx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 5543 | struct stconn *sc = appctx_sc(luactx->appctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5544 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5545 | |
| 5546 | if (co_data(res)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 5547 | sc_need_room(sc); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5548 | 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] | 5549 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5550 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5551 | } |
| 5552 | |
| 5553 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5554 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5555 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5556 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 5557 | } |
| 5558 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5559 | /* |
| 5560 | * |
| 5561 | * |
| 5562 | * Class HTTP |
| 5563 | * |
| 5564 | * |
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 | |
| 5567 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 5568 | * a class stream, otherwise it throws an error. |
| 5569 | */ |
| 5570 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5571 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5572 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 5573 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5575 | /* This function creates and push in the stack a HTTP object |
| 5576 | * according with a current TXN. |
| 5577 | */ |
| 5578 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 5579 | { |
| 5580 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5581 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5582 | /* Check stack size. */ |
| 5583 | if (!lua_checkstack(L, 3)) |
| 5584 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5585 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5586 | /* Create the object: obj[0] = userdata. |
| 5587 | * Note that the base of the Converters object is the |
| 5588 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5589 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5590 | lua_newtable(L); |
| 5591 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 5592 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5593 | |
| 5594 | htxn->s = txn->s; |
| 5595 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 5596 | htxn->dir = txn->dir; |
| 5597 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5598 | |
| 5599 | /* Pop a class stream metatable and affect it to the table. */ |
| 5600 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 5601 | lua_setmetatable(L, -2); |
| 5602 | |
| 5603 | return 1; |
| 5604 | } |
| 5605 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5606 | /* This function creates and returns an array containing the status-line |
| 5607 | * elements. This function does not fails. |
| 5608 | */ |
| 5609 | __LJMP static int hlua_http_get_stline(lua_State *L, struct htx_sl *sl) |
| 5610 | { |
| 5611 | /* Create the table. */ |
| 5612 | lua_newtable(L); |
| 5613 | |
| 5614 | if (sl->flags & HTX_SL_F_IS_RESP) { |
| 5615 | lua_pushstring(L, "version"); |
| 5616 | lua_pushlstring(L, HTX_SL_RES_VPTR(sl), HTX_SL_RES_VLEN(sl)); |
| 5617 | lua_settable(L, -3); |
| 5618 | lua_pushstring(L, "code"); |
| 5619 | lua_pushlstring(L, HTX_SL_RES_CPTR(sl), HTX_SL_RES_CLEN(sl)); |
| 5620 | lua_settable(L, -3); |
| 5621 | lua_pushstring(L, "reason"); |
| 5622 | lua_pushlstring(L, HTX_SL_RES_RPTR(sl), HTX_SL_RES_RLEN(sl)); |
| 5623 | lua_settable(L, -3); |
| 5624 | } |
| 5625 | else { |
| 5626 | lua_pushstring(L, "method"); |
| 5627 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 5628 | lua_settable(L, -3); |
| 5629 | lua_pushstring(L, "uri"); |
| 5630 | lua_pushlstring(L, HTX_SL_REQ_UPTR(sl), HTX_SL_REQ_ULEN(sl)); |
| 5631 | lua_settable(L, -3); |
| 5632 | lua_pushstring(L, "version"); |
| 5633 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 5634 | lua_settable(L, -3); |
| 5635 | } |
| 5636 | return 1; |
| 5637 | } |
| 5638 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5639 | /* This function creates ans returns an array of HTTP headers. |
| 5640 | * This function does not fails. It is used as wrapper with the |
| 5641 | * 2 following functions. |
| 5642 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5643 | __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] | 5644 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5645 | struct htx *htx; |
| 5646 | int32_t pos; |
| 5647 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5648 | /* Create the table. */ |
| 5649 | lua_newtable(L); |
| 5650 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5651 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5652 | htx = htxbuf(&msg->chn->buf); |
| 5653 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 5654 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 5655 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 5656 | struct ist n, v; |
| 5657 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5658 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5659 | if (type == HTX_BLK_HDR) { |
| 5660 | n = htx_get_blk_name(htx,blk); |
| 5661 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5662 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5663 | else if (type == HTX_BLK_EOH) |
| 5664 | break; |
| 5665 | else |
| 5666 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5667 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5668 | /* Check for existing entry: |
| 5669 | * assume that the table is on the top of the stack, and |
| 5670 | * push the key in the stack, the function lua_gettable() |
| 5671 | * perform the lookup. |
| 5672 | */ |
| 5673 | lua_pushlstring(L, n.ptr, n.len); |
| 5674 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5675 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5676 | switch (lua_type(L, -1)) { |
| 5677 | case LUA_TNIL: |
| 5678 | /* Table not found, create it. */ |
| 5679 | lua_pop(L, 1); /* remove the nil value. */ |
| 5680 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 5681 | lua_newtable(L); /* create and push empty table. */ |
| 5682 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5683 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 5684 | 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] | 5685 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 5686 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5687 | case LUA_TTABLE: |
| 5688 | /* Entry found: push the value in the table. */ |
| 5689 | len = lua_rawlen(L, -1); |
| 5690 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 5691 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 5692 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 5693 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5694 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5695 | default: |
| 5696 | /* Other cases are errors. */ |
| 5697 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 5698 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5699 | } |
| 5700 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5701 | return 1; |
| 5702 | } |
| 5703 | |
| 5704 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 5705 | { |
| 5706 | struct hlua_txn *htxn; |
| 5707 | |
| 5708 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 5709 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5710 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5711 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5712 | WILL_LJMP(lua_error(L)); |
| 5713 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5714 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5715 | } |
| 5716 | |
| 5717 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 5718 | { |
| 5719 | struct hlua_txn *htxn; |
| 5720 | |
| 5721 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 5722 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5723 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5724 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5725 | WILL_LJMP(lua_error(L)); |
| 5726 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 5727 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5728 | } |
| 5729 | |
| 5730 | /* This function replace full header, or just a value in |
| 5731 | * the request or in the response. It is a wrapper fir the |
| 5732 | * 4 following functions. |
| 5733 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5734 | __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] | 5735 | { |
| 5736 | size_t name_len; |
| 5737 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5738 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5739 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5740 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5741 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5742 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 5743 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5744 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 5745 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5746 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5747 | 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] | 5748 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 5749 | return 0; |
| 5750 | } |
| 5751 | |
| 5752 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 5753 | { |
| 5754 | struct hlua_txn *htxn; |
| 5755 | |
| 5756 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5757 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5758 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5759 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5760 | WILL_LJMP(lua_error(L)); |
| 5761 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5762 | 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] | 5763 | } |
| 5764 | |
| 5765 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 5766 | { |
| 5767 | struct hlua_txn *htxn; |
| 5768 | |
| 5769 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 5770 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5771 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5772 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5773 | WILL_LJMP(lua_error(L)); |
| 5774 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5775 | 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] | 5776 | } |
| 5777 | |
| 5778 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 5779 | { |
| 5780 | struct hlua_txn *htxn; |
| 5781 | |
| 5782 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 5783 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5784 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5785 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5786 | WILL_LJMP(lua_error(L)); |
| 5787 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5788 | 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] | 5789 | } |
| 5790 | |
| 5791 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 5792 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5793 | struct hlua_txn *htxn; |
| 5794 | |
| 5795 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 5796 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5797 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5798 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5799 | WILL_LJMP(lua_error(L)); |
| 5800 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 5801 | 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] | 5802 | } |
| 5803 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5804 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5805 | * It is a wrapper for the 2 following functions. |
| 5806 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5807 | __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] | 5808 | { |
| 5809 | size_t len; |
| 5810 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5811 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 5812 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5813 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5814 | ctx.blk = NULL; |
| 5815 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 5816 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5817 | return 0; |
| 5818 | } |
| 5819 | |
| 5820 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 5821 | { |
| 5822 | struct hlua_txn *htxn; |
| 5823 | |
| 5824 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 5825 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5826 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5827 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5828 | WILL_LJMP(lua_error(L)); |
| 5829 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5830 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5831 | } |
| 5832 | |
| 5833 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 5834 | { |
| 5835 | struct hlua_txn *htxn; |
| 5836 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5837 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5838 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5839 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5840 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5841 | WILL_LJMP(lua_error(L)); |
| 5842 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5843 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5844 | } |
| 5845 | |
| 5846 | /* This function adds an header. It is a wrapper used by |
| 5847 | * the 2 following functions. |
| 5848 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 5849 | __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] | 5850 | { |
| 5851 | size_t name_len; |
| 5852 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5853 | size_t value_len; |
| 5854 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5855 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5856 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 5857 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 5858 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5859 | return 0; |
| 5860 | } |
| 5861 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 5862 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 5863 | { |
| 5864 | struct hlua_txn *htxn; |
| 5865 | |
| 5866 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 5867 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5868 | |
| 5869 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5870 | WILL_LJMP(lua_error(L)); |
| 5871 | |
| 5872 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5873 | } |
| 5874 | |
| 5875 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 5876 | { |
| 5877 | struct hlua_txn *htxn; |
| 5878 | |
| 5879 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 5880 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5881 | |
| 5882 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5883 | WILL_LJMP(lua_error(L)); |
| 5884 | |
| 5885 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5886 | } |
| 5887 | |
| 5888 | static int hlua_http_req_set_hdr(lua_State *L) |
| 5889 | { |
| 5890 | struct hlua_txn *htxn; |
| 5891 | |
| 5892 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 5893 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5894 | |
| 5895 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5896 | WILL_LJMP(lua_error(L)); |
| 5897 | |
| 5898 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 5899 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
| 5900 | } |
| 5901 | |
| 5902 | static int hlua_http_res_set_hdr(lua_State *L) |
| 5903 | { |
| 5904 | struct hlua_txn *htxn; |
| 5905 | |
| 5906 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 5907 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5908 | |
| 5909 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5910 | WILL_LJMP(lua_error(L)); |
| 5911 | |
| 5912 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 5913 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
| 5914 | } |
| 5915 | |
| 5916 | /* This function set the method. */ |
| 5917 | static int hlua_http_req_set_meth(lua_State *L) |
| 5918 | { |
| 5919 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5920 | size_t name_len; |
| 5921 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5922 | |
| 5923 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5924 | WILL_LJMP(lua_error(L)); |
| 5925 | |
| 5926 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
| 5927 | return 1; |
| 5928 | } |
| 5929 | |
| 5930 | /* This function set the method. */ |
| 5931 | static int hlua_http_req_set_path(lua_State *L) |
| 5932 | { |
| 5933 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5934 | size_t name_len; |
| 5935 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5936 | |
| 5937 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5938 | WILL_LJMP(lua_error(L)); |
| 5939 | |
| 5940 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
| 5941 | return 1; |
| 5942 | } |
| 5943 | |
| 5944 | /* This function set the query-string. */ |
| 5945 | static int hlua_http_req_set_query(lua_State *L) |
| 5946 | { |
| 5947 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5948 | size_t name_len; |
| 5949 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5950 | |
| 5951 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5952 | WILL_LJMP(lua_error(L)); |
| 5953 | |
| 5954 | /* Check length. */ |
| 5955 | if (name_len > trash.size - 1) { |
| 5956 | lua_pushboolean(L, 0); |
| 5957 | return 1; |
| 5958 | } |
| 5959 | |
| 5960 | /* Add the mark question as prefix. */ |
| 5961 | chunk_reset(&trash); |
| 5962 | trash.area[trash.data++] = '?'; |
| 5963 | memcpy(trash.area + trash.data, name, name_len); |
| 5964 | trash.data += name_len; |
| 5965 | |
| 5966 | lua_pushboolean(L, |
| 5967 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
| 5968 | return 1; |
| 5969 | } |
| 5970 | |
| 5971 | /* This function set the uri. */ |
| 5972 | static int hlua_http_req_set_uri(lua_State *L) |
| 5973 | { |
| 5974 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5975 | size_t name_len; |
| 5976 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 5977 | |
| 5978 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
| 5979 | WILL_LJMP(lua_error(L)); |
| 5980 | |
| 5981 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
| 5982 | return 1; |
| 5983 | } |
| 5984 | |
| 5985 | /* This function set the response code & optionally reason. */ |
| 5986 | static int hlua_http_res_set_status(lua_State *L) |
| 5987 | { |
| 5988 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5989 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5990 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5991 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
| 5992 | |
| 5993 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
| 5994 | WILL_LJMP(lua_error(L)); |
| 5995 | |
| 5996 | http_res_set_status(code, reason, htxn->s); |
| 5997 | return 0; |
| 5998 | } |
| 5999 | |
| 6000 | /* |
| 6001 | * |
| 6002 | * |
| 6003 | * Class HTTPMessage |
| 6004 | * |
| 6005 | * |
| 6006 | */ |
| 6007 | |
| 6008 | /* Returns a struct http_msg if the stack entry "ud" is a class HTTPMessage, |
| 6009 | * otherwise it throws an error. |
| 6010 | */ |
| 6011 | __LJMP static struct http_msg *hlua_checkhttpmsg(lua_State *L, int ud) |
| 6012 | { |
| 6013 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_msg_ref)); |
| 6014 | } |
| 6015 | |
| 6016 | /* Creates and pushes on the stack a HTTP object according with a current TXN. |
| 6017 | */ |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 6018 | 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] | 6019 | { |
| 6020 | /* Check stack size. */ |
| 6021 | if (!lua_checkstack(L, 3)) |
| 6022 | return 0; |
| 6023 | |
| 6024 | lua_newtable(L); |
| 6025 | lua_pushlightuserdata(L, msg); |
| 6026 | lua_rawseti(L, -2, 0); |
| 6027 | |
| 6028 | /* Create the "channel" field that contains the request channel object. */ |
| 6029 | lua_pushstring(L, "channel"); |
| 6030 | if (!hlua_channel_new(L, msg->chn)) |
| 6031 | return 0; |
| 6032 | lua_rawset(L, -3); |
| 6033 | |
| 6034 | /* Pop a class stream metatable and affect it to the table. */ |
| 6035 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_msg_ref); |
| 6036 | lua_setmetatable(L, -2); |
| 6037 | |
| 6038 | return 1; |
| 6039 | } |
| 6040 | |
| 6041 | /* Helper function returning a filter attached to the HTTP message at the |
| 6042 | * 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] | 6043 | * 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] | 6044 | * filled with output and input length respectively. |
| 6045 | */ |
| 6046 | static struct filter *hlua_http_msg_filter(lua_State *L, int ud, struct http_msg *msg, size_t *offset, size_t *len) |
| 6047 | { |
| 6048 | struct channel *chn = msg->chn; |
| 6049 | struct htx *htx = htxbuf(&chn->buf); |
| 6050 | struct filter *filter = NULL; |
| 6051 | |
| 6052 | *offset = co_data(msg->chn); |
| 6053 | *len = htx->data - co_data(msg->chn); |
| 6054 | |
| 6055 | if (lua_getfield(L, ud, "__filter") == LUA_TLIGHTUSERDATA) { |
| 6056 | filter = lua_touserdata (L, -1); |
| 6057 | if (msg->msg_state >= HTTP_MSG_DATA) { |
| 6058 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6059 | |
| 6060 | *offset = flt_ctx->cur_off[CHN_IDX(chn)]; |
| 6061 | *len = flt_ctx->cur_len[CHN_IDX(chn)]; |
| 6062 | } |
| 6063 | } |
| 6064 | |
| 6065 | lua_pop(L, 1); |
| 6066 | return filter; |
| 6067 | } |
| 6068 | |
| 6069 | /* Returns true if the channel attached to the HTTP message is the response |
| 6070 | * channel. |
| 6071 | */ |
| 6072 | __LJMP static int hlua_http_msg_is_resp(lua_State *L) |
| 6073 | { |
| 6074 | struct http_msg *msg; |
| 6075 | |
| 6076 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 6077 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6078 | |
| 6079 | lua_pushboolean(L, !!(msg->chn->flags & CF_ISRESP)); |
| 6080 | return 1; |
| 6081 | } |
| 6082 | |
| 6083 | /* Returns an array containing the elements status-line of the HTTP message. It relies |
| 6084 | * on hlua_http_get_stline(). |
| 6085 | */ |
| 6086 | __LJMP static int hlua_http_msg_get_stline(lua_State *L) |
| 6087 | { |
| 6088 | struct http_msg *msg; |
| 6089 | struct htx *htx; |
| 6090 | struct htx_sl *sl; |
| 6091 | |
| 6092 | MAY_LJMP(check_args(L, 1, "get_stline")); |
| 6093 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6094 | |
| 6095 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6096 | WILL_LJMP(lua_error(L)); |
| 6097 | |
| 6098 | htx = htxbuf(&msg->chn->buf); |
| 6099 | sl = http_get_stline(htx); |
| 6100 | if (!sl) |
| 6101 | return 0; |
| 6102 | return hlua_http_get_stline(L, sl); |
| 6103 | } |
| 6104 | |
| 6105 | /* Returns an array containing all headers of the HTTP message. it relies on |
| 6106 | * hlua_http_get_headers(). |
| 6107 | */ |
| 6108 | __LJMP static int hlua_http_msg_get_headers(lua_State *L) |
| 6109 | { |
| 6110 | struct http_msg *msg; |
| 6111 | |
| 6112 | MAY_LJMP(check_args(L, 1, "get_headers")); |
| 6113 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6114 | |
| 6115 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6116 | WILL_LJMP(lua_error(L)); |
| 6117 | |
| 6118 | return hlua_http_get_headers(L, msg); |
| 6119 | } |
| 6120 | |
| 6121 | /* Deletes all occurrences of an header in the HTTP message matching on its |
| 6122 | * name. It relies on hlua_http_del_hdr(). |
| 6123 | */ |
| 6124 | __LJMP static int hlua_http_msg_del_hdr(lua_State *L) |
| 6125 | { |
| 6126 | struct http_msg *msg; |
| 6127 | |
| 6128 | MAY_LJMP(check_args(L, 2, "del_header")); |
| 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_del_hdr(L, msg); |
| 6135 | } |
| 6136 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6137 | /* 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] | 6138 | * message given its name against a regex and replaces it if it matches. It |
| 6139 | * relies on hlua_http_rep_hdr(). |
| 6140 | */ |
| 6141 | __LJMP static int hlua_http_msg_rep_hdr(lua_State *L) |
| 6142 | { |
| 6143 | struct http_msg *msg; |
| 6144 | |
| 6145 | MAY_LJMP(check_args(L, 4, "rep_header")); |
| 6146 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6147 | |
| 6148 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6149 | WILL_LJMP(lua_error(L)); |
| 6150 | |
| 6151 | return hlua_http_rep_hdr(L, msg, 1); |
| 6152 | } |
| 6153 | |
Ilya Shipitsin | bd6b4be | 2021-10-15 16:18:21 +0500 | [diff] [blame] | 6154 | /* 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] | 6155 | * message given its name against a regex and replaces it if it matches. It |
| 6156 | * relies on hlua_http_rep_hdr(). |
| 6157 | */ |
| 6158 | __LJMP static int hlua_http_msg_rep_val(lua_State *L) |
| 6159 | { |
| 6160 | struct http_msg *msg; |
| 6161 | |
| 6162 | MAY_LJMP(check_args(L, 4, "rep_value")); |
| 6163 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6164 | |
| 6165 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6166 | WILL_LJMP(lua_error(L)); |
| 6167 | |
| 6168 | return hlua_http_rep_hdr(L, msg, 0); |
| 6169 | } |
| 6170 | |
| 6171 | /* Add an header in the HTTP message. It relies on hlua_http_add_hdr() */ |
| 6172 | __LJMP static int hlua_http_msg_add_hdr(lua_State *L) |
| 6173 | { |
| 6174 | struct http_msg *msg; |
| 6175 | |
| 6176 | MAY_LJMP(check_args(L, 3, "add_header")); |
| 6177 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6178 | |
| 6179 | if (msg->msg_state > HTTP_MSG_BODY) |
| 6180 | WILL_LJMP(lua_error(L)); |
| 6181 | |
| 6182 | return hlua_http_add_hdr(L, msg); |
| 6183 | } |
| 6184 | |
| 6185 | /* Add an header in the HTTP message removing existing headers with the same |
| 6186 | * name. It relies on hlua_http_del_hdr() and hlua_http_add_hdr(). |
| 6187 | */ |
| 6188 | __LJMP static int hlua_http_msg_set_hdr(lua_State *L) |
| 6189 | { |
| 6190 | struct http_msg *msg; |
| 6191 | |
| 6192 | MAY_LJMP(check_args(L, 3, "set_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 | hlua_http_del_hdr(L, msg); |
| 6199 | return hlua_http_add_hdr(L, msg); |
| 6200 | } |
| 6201 | |
| 6202 | /* Rewrites the request method. It relies on http_req_replace_stline(). */ |
| 6203 | __LJMP static int hlua_http_msg_set_meth(lua_State *L) |
| 6204 | { |
| 6205 | struct stream *s; |
| 6206 | struct http_msg *msg; |
| 6207 | const char *name; |
| 6208 | size_t name_len; |
| 6209 | |
| 6210 | MAY_LJMP(check_args(L, 2, "set_method")); |
| 6211 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6212 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6213 | |
| 6214 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6215 | WILL_LJMP(lua_error(L)); |
| 6216 | |
| 6217 | s = chn_strm(msg->chn); |
| 6218 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, s->be, s) != -1); |
| 6219 | return 1; |
| 6220 | } |
| 6221 | |
| 6222 | /* Rewrites the request path. It relies on http_req_replace_stline(). */ |
| 6223 | __LJMP static int hlua_http_msg_set_path(lua_State *L) |
| 6224 | { |
| 6225 | struct stream *s; |
| 6226 | struct http_msg *msg; |
| 6227 | const char *name; |
| 6228 | size_t name_len; |
| 6229 | |
| 6230 | MAY_LJMP(check_args(L, 2, "set_path")); |
| 6231 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6232 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6233 | |
| 6234 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6235 | WILL_LJMP(lua_error(L)); |
| 6236 | |
| 6237 | s = chn_strm(msg->chn); |
| 6238 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, s->be, s) != -1); |
| 6239 | return 1; |
| 6240 | } |
| 6241 | |
| 6242 | /* Rewrites the request query-string. It relies on http_req_replace_stline(). */ |
| 6243 | __LJMP static int hlua_http_msg_set_query(lua_State *L) |
| 6244 | { |
| 6245 | struct stream *s; |
| 6246 | struct http_msg *msg; |
| 6247 | const char *name; |
| 6248 | size_t name_len; |
| 6249 | |
| 6250 | MAY_LJMP(check_args(L, 2, "set_query")); |
| 6251 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6252 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6253 | |
| 6254 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6255 | WILL_LJMP(lua_error(L)); |
| 6256 | |
| 6257 | /* Check length. */ |
| 6258 | if (name_len > trash.size - 1) { |
| 6259 | lua_pushboolean(L, 0); |
| 6260 | return 1; |
| 6261 | } |
| 6262 | |
| 6263 | /* Add the mark question as prefix. */ |
| 6264 | chunk_reset(&trash); |
| 6265 | trash.area[trash.data++] = '?'; |
| 6266 | memcpy(trash.area + trash.data, name, name_len); |
| 6267 | trash.data += name_len; |
| 6268 | |
| 6269 | s = chn_strm(msg->chn); |
| 6270 | lua_pushboolean(L, http_req_replace_stline(2, trash.area, trash.data, s->be, s) != -1); |
| 6271 | return 1; |
| 6272 | } |
| 6273 | |
| 6274 | /* Rewrites the request URI. It relies on http_req_replace_stline(). */ |
| 6275 | __LJMP static int hlua_http_msg_set_uri(lua_State *L) |
| 6276 | { |
| 6277 | struct stream *s; |
| 6278 | struct http_msg *msg; |
| 6279 | const char *name; |
| 6280 | size_t name_len; |
| 6281 | |
| 6282 | MAY_LJMP(check_args(L, 2, "set_uri")); |
| 6283 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6284 | name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 6285 | |
| 6286 | if ((msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6287 | WILL_LJMP(lua_error(L)); |
| 6288 | |
| 6289 | s = chn_strm(msg->chn); |
| 6290 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, s->be, s) != -1); |
| 6291 | return 1; |
| 6292 | } |
| 6293 | |
| 6294 | /* Rewrites the response status code. It relies on http_res_set_status(). */ |
| 6295 | __LJMP static int hlua_http_msg_set_status(lua_State *L) |
| 6296 | { |
| 6297 | struct http_msg *msg; |
| 6298 | unsigned int code; |
| 6299 | const char *reason; |
| 6300 | size_t reason_len; |
| 6301 | |
| 6302 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6303 | code = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6304 | reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, &reason_len)); |
| 6305 | |
| 6306 | if (!(msg->chn->flags & CF_ISRESP) || msg->msg_state > HTTP_MSG_BODY) |
| 6307 | WILL_LJMP(lua_error(L)); |
| 6308 | |
| 6309 | lua_pushboolean(L, http_res_set_status(code, ist2(reason, reason_len), chn_strm(msg->chn)) != -1); |
| 6310 | return 1; |
| 6311 | } |
| 6312 | |
| 6313 | /* Returns true if the HTTP message is full. */ |
| 6314 | __LJMP static int hlua_http_msg_is_full(lua_State *L) |
| 6315 | { |
| 6316 | struct http_msg *msg; |
| 6317 | |
| 6318 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 6319 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6320 | lua_pushboolean(L, channel_full(msg->chn, 0)); |
| 6321 | return 1; |
| 6322 | } |
| 6323 | |
| 6324 | /* Returns true if the HTTP message may still receive data. */ |
| 6325 | __LJMP static int hlua_http_msg_may_recv(lua_State *L) |
| 6326 | { |
| 6327 | struct http_msg *msg; |
| 6328 | struct htx *htx; |
| 6329 | |
| 6330 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6331 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6332 | htx = htxbuf(&msg->chn->buf); |
| 6333 | lua_pushboolean(L, (htx_expect_more(htx) && !channel_input_closed(msg->chn) && channel_may_recv(msg->chn))); |
| 6334 | return 1; |
| 6335 | } |
| 6336 | |
| 6337 | /* Returns true if the HTTP message EOM was received */ |
| 6338 | __LJMP static int hlua_http_msg_is_eom(lua_State *L) |
| 6339 | { |
| 6340 | struct http_msg *msg; |
| 6341 | struct htx *htx; |
| 6342 | |
| 6343 | MAY_LJMP(check_args(L, 1, "may_recv")); |
| 6344 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6345 | htx = htxbuf(&msg->chn->buf); |
| 6346 | lua_pushboolean(L, !htx_expect_more(htx)); |
| 6347 | return 1; |
| 6348 | } |
| 6349 | |
| 6350 | /* Returns the number of bytes available in the input side of the HTTP |
| 6351 | * message. This function never fails. |
| 6352 | */ |
| 6353 | __LJMP static int hlua_http_msg_get_in_len(lua_State *L) |
| 6354 | { |
| 6355 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6356 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6357 | |
| 6358 | MAY_LJMP(check_args(L, 1, "input")); |
| 6359 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6360 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6361 | lua_pushinteger(L, input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6362 | return 1; |
| 6363 | } |
| 6364 | |
| 6365 | /* Returns the number of bytes available in the output side of the HTTP |
| 6366 | * message. This function never fails. |
| 6367 | */ |
| 6368 | __LJMP static int hlua_http_msg_get_out_len(lua_State *L) |
| 6369 | { |
| 6370 | struct http_msg *msg; |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6371 | size_t output, input; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6372 | |
| 6373 | MAY_LJMP(check_args(L, 1, "output")); |
| 6374 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 6375 | hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6376 | lua_pushinteger(L, output); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6377 | return 1; |
| 6378 | } |
| 6379 | |
| 6380 | /* Copies at most <len> bytes of DATA blocks from the HTTP message <msg> |
| 6381 | * 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] | 6382 | * 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] | 6383 | * block. This function is called during the payload filtering, so the headers |
| 6384 | * are already scheduled for output (from the filter point of view). |
| 6385 | */ |
| 6386 | static int _hlua_http_msg_dup(struct http_msg *msg, lua_State *L, size_t offset, size_t len) |
| 6387 | { |
| 6388 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 6389 | struct htx_blk *blk; |
| 6390 | struct htx_ret htxret; |
| 6391 | luaL_Buffer b; |
| 6392 | int ret = 0; |
| 6393 | |
| 6394 | luaL_buffinit(L, &b); |
| 6395 | htxret = htx_find_offset(htx, offset); |
| 6396 | for (blk = htxret.blk, offset = htxret.ret; blk && len; blk = htx_get_next_blk(htx, blk)) { |
| 6397 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6398 | struct ist v; |
| 6399 | |
| 6400 | switch (type) { |
| 6401 | case HTX_BLK_UNUSED: |
| 6402 | break; |
| 6403 | |
| 6404 | case HTX_BLK_DATA: |
| 6405 | v = htx_get_blk_value(htx, blk); |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6406 | v = istadv(v, offset); |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6407 | v = isttrim(v, len); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6408 | |
| 6409 | luaL_addlstring(&b, v.ptr, v.len); |
| 6410 | ret += v.len; |
| 6411 | break; |
| 6412 | |
| 6413 | default: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6414 | if (!ret) |
| 6415 | goto no_data; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6416 | goto end; |
| 6417 | } |
| 6418 | offset = 0; |
| 6419 | } |
| 6420 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6421 | end: |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6422 | if (!ret && (htx->flags & HTX_FL_EOM)) |
| 6423 | goto no_data; |
| 6424 | luaL_pushresult(&b); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6425 | return ret; |
vishnu | 0af4bd7 | 2021-10-24 06:46:24 +0530 | [diff] [blame] | 6426 | |
| 6427 | no_data: |
| 6428 | /* Remove the empty string and push nil on the stack */ |
| 6429 | lua_pop(L, 1); |
| 6430 | lua_pushnil(L); |
| 6431 | return 0; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6432 | } |
| 6433 | |
| 6434 | /* Copies the string <str> to the HTTP message <msg> at the offset |
| 6435 | * <offset>. This function returns -1 if data cannot be copied. Otherwise, it |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6436 | * returns the amount of data written. This function is responsible to update |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6437 | * the filter context. |
| 6438 | */ |
| 6439 | static int _hlua_http_msg_insert(struct http_msg *msg, struct filter *filter, struct ist str, size_t offset) |
| 6440 | { |
| 6441 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6442 | struct htx_ret htxret; |
| 6443 | int /*max, */ret = 0; |
| 6444 | |
| 6445 | /* Nothing to do, just return */ |
| 6446 | if (unlikely(istlen(str) == 0)) |
| 6447 | goto end; |
| 6448 | |
| 6449 | if (istlen(str) > htx_free_data_space(htx)) { |
| 6450 | ret = -1; |
| 6451 | goto end; |
| 6452 | } |
| 6453 | |
| 6454 | htxret = htx_find_offset(htx, offset); |
| 6455 | if (!htxret.blk || htx_get_blk_type(htxret.blk) != HTX_BLK_DATA) { |
| 6456 | if (!htx_add_last_data(htx, str)) |
| 6457 | goto end; |
| 6458 | } |
| 6459 | else { |
| 6460 | struct ist v = htx_get_blk_value(htx, htxret.blk); |
| 6461 | v.ptr += htxret.ret; |
| 6462 | v.len = 0; |
| 6463 | if (!htx_replace_blk_value(htx, htxret.blk, v, str)) |
| 6464 | goto end; |
| 6465 | } |
| 6466 | ret = str.len; |
| 6467 | if (ret) { |
| 6468 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6469 | flt_update_offsets(filter, msg->chn, ret); |
| 6470 | flt_ctx->cur_len[CHN_IDX(msg->chn)] += ret; |
| 6471 | } |
| 6472 | |
| 6473 | end: |
| 6474 | htx_to_buf(htx, &msg->chn->buf); |
| 6475 | return ret; |
| 6476 | } |
| 6477 | |
| 6478 | /* Helper function removing at most <len> bytes of DATA blocks at the absolute |
| 6479 | * position <offset>. It stops on the first non-DATA HTX block. This function is |
| 6480 | * called during the payload filtering, so the headers are already scheduled for |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6481 | * output (from the filter point of view). This function is responsible to |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6482 | * update the filter context. |
| 6483 | */ |
| 6484 | static void _hlua_http_msg_delete(struct http_msg *msg, struct filter *filter, size_t offset, size_t len) |
| 6485 | { |
| 6486 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6487 | struct htx *htx = htx_from_buf(&msg->chn->buf); |
| 6488 | struct htx_blk *blk; |
| 6489 | struct htx_ret htxret; |
| 6490 | size_t ret = 0; |
| 6491 | |
| 6492 | /* Be sure <len> is always the amount of DATA to remove */ |
| 6493 | 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] | 6494 | /* When htx tail type == HTX_BLK_DATA, no need to take care |
| 6495 | * of special blocks like HTX_BLK_EOT. |
| 6496 | * We simply truncate after offset |
| 6497 | * (truncate targeted blk and discard the following ones) |
| 6498 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6499 | htx_truncate(htx, offset); |
| 6500 | ret = len; |
| 6501 | goto end; |
| 6502 | } |
| 6503 | |
| 6504 | htxret = htx_find_offset(htx, offset); |
| 6505 | blk = htxret.blk; |
| 6506 | if (htxret.ret) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6507 | /* dealing with offset: we need to trim targeted blk */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6508 | struct ist v; |
| 6509 | |
| 6510 | if (htx_get_blk_type(blk) != HTX_BLK_DATA) |
| 6511 | goto end; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6512 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6513 | v = htx_get_blk_value(htx, blk); |
| 6514 | v.ptr += htxret.ret; |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6515 | v.len -= htxret.ret; |
| 6516 | |
Tim Duesterhus | 2471f5c | 2021-11-08 09:05:01 +0100 | [diff] [blame] | 6517 | v = isttrim(v, len); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6518 | /* trimming data in blk: discard everything after the offset |
| 6519 | * (replace 'v' with 'IST_NULL') |
| 6520 | */ |
Tim Duesterhus | b113b5c | 2021-09-15 13:58:44 +0200 | [diff] [blame] | 6521 | blk = htx_replace_blk_value(htx, blk, v, IST_NULL); |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6522 | if (blk && v.len < len) { |
| 6523 | /* In this case, caller wants to keep removing data, |
| 6524 | * but we need to spare current blk |
| 6525 | * because it was already trimmed |
| 6526 | */ |
| 6527 | blk = htx_get_next_blk(htx, blk); |
| 6528 | } |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6529 | len -= v.len; |
| 6530 | ret += v.len; |
| 6531 | } |
| 6532 | |
| 6533 | |
| 6534 | while (blk && len) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6535 | /* there is more data that needs to be discarded */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6536 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6537 | uint32_t sz = htx_get_blksz(blk); |
| 6538 | |
| 6539 | switch (type) { |
| 6540 | case HTX_BLK_UNUSED: |
| 6541 | break; |
| 6542 | |
| 6543 | case HTX_BLK_DATA: |
| 6544 | if (len < sz) { |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6545 | /* don't discard whole blk, only part of it |
| 6546 | * (from the beginning) |
| 6547 | */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6548 | htx_cut_data_blk(htx, blk, len); |
| 6549 | ret += len; |
| 6550 | goto end; |
| 6551 | } |
| 6552 | break; |
| 6553 | |
| 6554 | default: |
Aurelien DARRAGON | bcbcf98 | 2022-09-28 19:08:15 +0200 | [diff] [blame] | 6555 | /* HTX_BLK_EOT blk won't be removed */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6556 | goto end; |
| 6557 | } |
| 6558 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6559 | /* Remove all the data block */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6560 | len -= sz; |
| 6561 | ret += sz; |
| 6562 | blk = htx_remove_blk(htx, blk); |
| 6563 | } |
| 6564 | |
| 6565 | end: |
| 6566 | flt_update_offsets(filter, msg->chn, -ret); |
| 6567 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6568 | /* WARNING: we don't call htx_to_buf() on purpose, because we don't want |
| 6569 | * to loose the EOM flag if the message is empty. |
| 6570 | */ |
| 6571 | } |
| 6572 | |
| 6573 | /* Copies input data found in an HTTP message. Unlike the channel function used |
| 6574 | * to duplicate raw data, this one can only be called inside a filter, from |
| 6575 | * http_payload callback. So it cannot yield. An exception is returned if it is |
| 6576 | * called from another callback. If nothing was copied, a nil value is pushed on |
| 6577 | * the stack. |
| 6578 | */ |
| 6579 | __LJMP static int hlua_http_msg_get_body(lua_State *L) |
| 6580 | { |
| 6581 | struct http_msg *msg; |
| 6582 | struct filter *filter; |
| 6583 | size_t output, input; |
| 6584 | int offset, len; |
| 6585 | |
| 6586 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 6587 | WILL_LJMP(luaL_error(L, "'data' expects at most 2 arguments")); |
| 6588 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6589 | |
| 6590 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6591 | WILL_LJMP(lua_error(L)); |
| 6592 | |
| 6593 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6594 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6595 | WILL_LJMP(lua_error(L)); |
| 6596 | |
| 6597 | if (!ci_data(msg->chn) && channel_input_closed(msg->chn)) { |
| 6598 | lua_pushnil(L); |
| 6599 | return 1; |
| 6600 | } |
| 6601 | |
| 6602 | offset = output; |
| 6603 | if (lua_gettop(L) > 1) { |
| 6604 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6605 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6606 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6607 | offset += output; |
| 6608 | if (offset < output || offset > input + output) { |
| 6609 | lua_pushfstring(L, "offset out of range."); |
| 6610 | WILL_LJMP(lua_error(L)); |
| 6611 | } |
| 6612 | } |
| 6613 | len = output + input - offset; |
| 6614 | if (lua_gettop(L) == 3) { |
| 6615 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6616 | if (!len) |
| 6617 | goto dup; |
| 6618 | if (len == -1) |
| 6619 | len = global.tune.bufsize; |
| 6620 | if (len < 0) { |
| 6621 | lua_pushfstring(L, "length out of range."); |
| 6622 | WILL_LJMP(lua_error(L)); |
| 6623 | } |
| 6624 | } |
| 6625 | |
| 6626 | dup: |
| 6627 | _hlua_http_msg_dup(msg, L, offset, len); |
| 6628 | return 1; |
| 6629 | } |
| 6630 | |
| 6631 | /* Appends a string to the HTTP message, after all existing DATA blocks but |
| 6632 | * before the trailers, if any. It returns the amount of data written or -1 if |
| 6633 | * nothing was copied. Unlike the channel function used to append data, this one |
| 6634 | * can only be called inside a filter, from http_payload callback. So it cannot |
| 6635 | * yield. An exception is returned if it is called from another callback. |
| 6636 | */ |
| 6637 | __LJMP static int hlua_http_msg_append(lua_State *L) |
| 6638 | { |
| 6639 | struct http_msg *msg; |
| 6640 | struct filter *filter; |
| 6641 | const char *str; |
| 6642 | size_t offset, len, sz; |
| 6643 | int ret; |
| 6644 | |
| 6645 | MAY_LJMP(check_args(L, 2, "append")); |
| 6646 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6647 | |
| 6648 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6649 | WILL_LJMP(lua_error(L)); |
| 6650 | |
| 6651 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6652 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6653 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6654 | WILL_LJMP(lua_error(L)); |
| 6655 | |
| 6656 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset+len); |
| 6657 | lua_pushinteger(L, ret); |
| 6658 | return 1; |
| 6659 | } |
| 6660 | |
| 6661 | /* Prepends a string to the HTTP message, before all existing DATA blocks. It |
| 6662 | * returns the amount of data written or -1 if nothing was copied. Unlike the |
| 6663 | * channel function used to prepend data, this one can only be called inside a |
| 6664 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6665 | * returned if it is called from another callback. |
| 6666 | */ |
| 6667 | __LJMP static int hlua_http_msg_prepend(lua_State *L) |
| 6668 | { |
| 6669 | struct http_msg *msg; |
| 6670 | struct filter *filter; |
| 6671 | const char *str; |
| 6672 | size_t offset, len, sz; |
| 6673 | int ret; |
| 6674 | |
| 6675 | MAY_LJMP(check_args(L, 2, "prepend")); |
| 6676 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6677 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6678 | if (msg->msg_state < HTTP_MSG_DATA) |
| 6679 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6680 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6681 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6682 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6683 | if (!filter || !hlua_filter_from_payload(filter)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6684 | WILL_LJMP(lua_error(L)); |
| 6685 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6686 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6687 | lua_pushinteger(L, ret); |
| 6688 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6689 | } |
| 6690 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6691 | /* Inserts a string to the HTTP message at a given offset. By default the string |
| 6692 | * is appended at the end of DATA blocks. It returns the amount of data written |
| 6693 | * or -1 if nothing was copied. Unlike the channel function used to insert data, |
| 6694 | * this one can only be called inside a filter, from http_payload callback. So |
| 6695 | * it cannot yield. An exception is returned if it is called from another |
| 6696 | * callback. |
| 6697 | */ |
| 6698 | __LJMP static int hlua_http_msg_insert_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6699 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6700 | struct http_msg *msg; |
| 6701 | struct filter *filter; |
| 6702 | const char *str; |
| 6703 | size_t input, output, sz; |
| 6704 | int offset; |
| 6705 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6706 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6707 | if (lua_gettop(L) < 2 || lua_gettop(L) > 3) |
| 6708 | 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] | 6709 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6710 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6711 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6712 | WILL_LJMP(lua_error(L)); |
| 6713 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6714 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6715 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6716 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6717 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6718 | |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6719 | offset = output; |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6720 | if (lua_gettop(L) > 2) { |
| 6721 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6722 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6723 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6724 | offset += output; |
Aurelien DARRAGON | 7fdba0a | 2022-09-28 16:03:45 +0200 | [diff] [blame] | 6725 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6726 | lua_pushfstring(L, "offset out of range."); |
| 6727 | WILL_LJMP(lua_error(L)); |
| 6728 | } |
| 6729 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6730 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6731 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6732 | lua_pushinteger(L, ret); |
| 6733 | return 1; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6734 | } |
| 6735 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6736 | /* Removes a given amount of data from the HTTP message at a given offset. By |
| 6737 | * default all DATA blocks are removed. It returns the amount of data |
| 6738 | * removed. Unlike the channel function used to remove data, this one can only |
| 6739 | * be called inside a filter, from http_payload callback. So it cannot yield. An |
| 6740 | * exception is returned if it is called from another callback. |
| 6741 | */ |
| 6742 | __LJMP static int hlua_http_msg_del_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6743 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6744 | struct http_msg *msg; |
| 6745 | struct filter *filter; |
| 6746 | size_t input, output; |
| 6747 | int offset, len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6748 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6749 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6750 | WILL_LJMP(luaL_error(L, "'remove' expects at most 2 arguments")); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6751 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6752 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6753 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6754 | WILL_LJMP(lua_error(L)); |
| 6755 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6756 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6757 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6758 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6759 | |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6760 | offset = output; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6761 | if (lua_gettop(L) > 1) { |
| 6762 | offset = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6763 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6764 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6765 | offset += output; |
Aurelien DARRAGON | d7c71b0 | 2022-09-28 15:52:18 +0200 | [diff] [blame] | 6766 | if (offset > output + input) { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6767 | lua_pushfstring(L, "offset out of range."); |
| 6768 | WILL_LJMP(lua_error(L)); |
| 6769 | } |
| 6770 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6771 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6772 | len = output + input - offset; |
Boyang Li | e0c5435 | 2022-05-10 17:47:23 +0000 | [diff] [blame] | 6773 | if (lua_gettop(L) == 3) { |
| 6774 | len = MAY_LJMP(luaL_checkinteger(L, 3)); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6775 | if (!len) |
| 6776 | goto end; |
| 6777 | if (len == -1) |
| 6778 | len = output + input - offset; |
| 6779 | if (len < 0 || offset + len > output + input) { |
| 6780 | lua_pushfstring(L, "length out of range."); |
| 6781 | WILL_LJMP(lua_error(L)); |
| 6782 | } |
| 6783 | } |
| 6784 | |
| 6785 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6786 | |
| 6787 | end: |
| 6788 | lua_pushinteger(L, len); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6789 | return 1; |
| 6790 | } |
| 6791 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 6792 | /* 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] | 6793 | * all remaining data are removed, accordingly to the filter context. It returns |
| 6794 | * the amount of data written or -1 if nothing was copied. Unlike the channel |
| 6795 | * function used to replace data, this one can only be called inside a filter, |
| 6796 | * from http_payload callback. So it cannot yield. An exception is returned if |
| 6797 | * it is called from another callback. |
| 6798 | */ |
| 6799 | __LJMP static int hlua_http_msg_set_data(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6800 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6801 | struct http_msg *msg; |
| 6802 | struct filter *filter; |
| 6803 | struct htx *htx; |
| 6804 | const char *str; |
| 6805 | size_t input, output, sz; |
| 6806 | int offset, len; |
| 6807 | int ret; |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 6808 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6809 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 6810 | WILL_LJMP(luaL_error(L, "'set' expects at least 1 argument and at most 3 arguments")); |
| 6811 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6812 | |
| 6813 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6814 | WILL_LJMP(lua_error(L)); |
| 6815 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6816 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6817 | filter = hlua_http_msg_filter(L, 1, msg, &output, &input); |
| 6818 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6819 | WILL_LJMP(lua_error(L)); |
| 6820 | |
| 6821 | offset = output; |
| 6822 | if (lua_gettop(L) > 2) { |
| 6823 | offset = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 6824 | if (offset < 0) |
Christopher Faulet | 70c4345 | 2021-08-13 08:11:00 +0200 | [diff] [blame] | 6825 | offset = MAX(0, (int)input + offset); |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6826 | offset += output; |
| 6827 | if (offset < output || offset > input + output) { |
| 6828 | lua_pushfstring(L, "offset out of range."); |
| 6829 | WILL_LJMP(lua_error(L)); |
| 6830 | } |
| 6831 | } |
| 6832 | |
| 6833 | len = output + input - offset; |
| 6834 | if (lua_gettop(L) == 4) { |
| 6835 | len = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6836 | if (!len) |
| 6837 | goto set; |
| 6838 | if (len == -1) |
| 6839 | len = output + input - offset; |
| 6840 | if (len < 0 || offset + len > output + input) { |
| 6841 | lua_pushfstring(L, "length out of range."); |
| 6842 | WILL_LJMP(lua_error(L)); |
| 6843 | } |
| 6844 | } |
| 6845 | |
| 6846 | set: |
| 6847 | /* Be sure we can copied the string once input data will be removed. */ |
| 6848 | htx = htx_from_buf(&msg->chn->buf); |
| 6849 | if (sz > htx_free_data_space(htx) + len) |
| 6850 | lua_pushinteger(L, -1); |
| 6851 | else { |
| 6852 | _hlua_http_msg_delete(msg, filter, offset, len); |
| 6853 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6854 | lua_pushinteger(L, ret); |
| 6855 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6856 | return 1; |
| 6857 | } |
| 6858 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6859 | /* Prepends data into an HTTP message and forward it, from the filter point of |
| 6860 | * view. It returns the amount of data written or -1 if nothing was sent. Unlike |
| 6861 | * the channel function used to send data, this one can only be called inside a |
| 6862 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6863 | * returned if it is called from another callback. |
| 6864 | */ |
| 6865 | __LJMP static int hlua_http_msg_send(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6866 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6867 | struct http_msg *msg; |
| 6868 | struct filter *filter; |
| 6869 | struct htx *htx; |
| 6870 | const char *str; |
| 6871 | size_t offset, len, sz; |
| 6872 | int ret; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6873 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6874 | MAY_LJMP(check_args(L, 2, "send")); |
| 6875 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6876 | |
| 6877 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6878 | WILL_LJMP(lua_error(L)); |
| 6879 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6880 | str = MAY_LJMP(luaL_checklstring(L, 2, &sz)); |
| 6881 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6882 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6883 | WILL_LJMP(lua_error(L)); |
| 6884 | |
| 6885 | /* Return an error if the channel's output is closed */ |
| 6886 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6887 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6888 | return 1; |
| 6889 | } |
| 6890 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6891 | htx = htx_from_buf(&msg->chn->buf); |
| 6892 | if (sz > htx_free_data_space(htx)) { |
| 6893 | lua_pushinteger(L, -1); |
| 6894 | return 1; |
| 6895 | } |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6896 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6897 | ret = _hlua_http_msg_insert(msg, filter, ist2(str, sz), offset); |
| 6898 | if (ret > 0) { |
| 6899 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6900 | |
| 6901 | FLT_OFF(filter, msg->chn) += ret; |
| 6902 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6903 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6904 | } |
| 6905 | |
| 6906 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6907 | return 1; |
| 6908 | } |
| 6909 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6910 | /* Forwards a given amount of bytes. It return -1 if the channel's output is |
| 6911 | * closed. Otherwise, it returns the number of bytes forwarded. Unlike the |
| 6912 | * channel function used to forward data, this one can only be called inside a |
| 6913 | * filter, from http_payload callback. So it cannot yield. An exception is |
| 6914 | * returned if it is called from another callback. All other functions deal with |
| 6915 | * DATA block, this one not. |
| 6916 | */ |
| 6917 | __LJMP static int hlua_http_msg_forward(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6918 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6919 | struct http_msg *msg; |
| 6920 | struct filter *filter; |
| 6921 | size_t offset, len; |
| 6922 | int fwd, ret = 0; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6923 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6924 | MAY_LJMP(check_args(L, 2, "forward")); |
| 6925 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6926 | |
| 6927 | if (msg->msg_state < HTTP_MSG_DATA) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6928 | WILL_LJMP(lua_error(L)); |
| 6929 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6930 | fwd = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 6931 | filter = hlua_http_msg_filter(L, 1, msg, &offset, &len); |
| 6932 | if (!filter || !hlua_filter_from_payload(filter)) |
| 6933 | WILL_LJMP(lua_error(L)); |
| 6934 | |
| 6935 | /* Nothing to do, just return */ |
| 6936 | if (!fwd) |
| 6937 | goto end; |
| 6938 | |
| 6939 | /* Return an error if the channel's output is closed */ |
| 6940 | if (unlikely(channel_output_closed(msg->chn))) { |
| 6941 | ret = -1; |
| 6942 | goto end; |
| 6943 | } |
| 6944 | |
| 6945 | ret = fwd; |
| 6946 | if (ret > len) |
| 6947 | ret = len; |
| 6948 | |
| 6949 | if (ret) { |
| 6950 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 6951 | |
| 6952 | FLT_OFF(filter, msg->chn) += ret; |
| 6953 | flt_ctx->cur_off[CHN_IDX(msg->chn)] += ret; |
| 6954 | flt_ctx->cur_len[CHN_IDX(msg->chn)] -= ret; |
| 6955 | } |
| 6956 | |
| 6957 | end: |
| 6958 | lua_pushinteger(L, ret); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6959 | return 1; |
| 6960 | } |
| 6961 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6962 | /* Set EOM flag on the HTX message. |
| 6963 | * |
| 6964 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6965 | * really know how to do without this feature. |
| 6966 | */ |
| 6967 | __LJMP static int hlua_http_msg_set_eom(lua_State *L) |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6968 | { |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6969 | struct http_msg *msg; |
| 6970 | struct htx *htx; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6971 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6972 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6973 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6974 | htx = htxbuf(&msg->chn->buf); |
| 6975 | htx->flags |= HTX_FL_EOM; |
| 6976 | return 0; |
| 6977 | } |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 6978 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 6979 | /* Unset EOM flag on the HTX message. |
| 6980 | * |
| 6981 | * NOTE: Not sure it is a good idea to manipulate this flag but for now I don't |
| 6982 | * really know how to do without this feature. |
| 6983 | */ |
| 6984 | __LJMP static int hlua_http_msg_unset_eom(lua_State *L) |
| 6985 | { |
| 6986 | struct http_msg *msg; |
| 6987 | struct htx *htx; |
| 6988 | |
| 6989 | MAY_LJMP(check_args(L, 1, "set_eom")); |
| 6990 | msg = MAY_LJMP(hlua_checkhttpmsg(L, 1)); |
| 6991 | htx = htxbuf(&msg->chn->buf); |
| 6992 | htx->flags &= ~HTX_FL_EOM; |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 6993 | return 0; |
| 6994 | } |
| 6995 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 6996 | /* |
| 6997 | * |
| 6998 | * |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 6999 | * Class HTTPClient |
| 7000 | * |
| 7001 | * |
| 7002 | */ |
| 7003 | __LJMP static struct hlua_httpclient *hlua_checkhttpclient(lua_State *L, int ud) |
| 7004 | { |
| 7005 | return MAY_LJMP(hlua_checkudata(L, ud, class_httpclient_ref)); |
| 7006 | } |
| 7007 | |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 7008 | |
| 7009 | /* stops the httpclient and ask it to kill itself */ |
| 7010 | __LJMP static int hlua_httpclient_gc(lua_State *L) |
| 7011 | { |
| 7012 | struct hlua_httpclient *hlua_hc; |
| 7013 | |
| 7014 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 7015 | |
| 7016 | hlua_hc = MAY_LJMP(hlua_checkhttpclient(L, 1)); |
| 7017 | |
| 7018 | httpclient_stop_and_destroy(hlua_hc->hc); |
| 7019 | |
| 7020 | hlua_hc->hc = NULL; |
| 7021 | |
| 7022 | |
| 7023 | return 0; |
| 7024 | } |
| 7025 | |
| 7026 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7027 | __LJMP static int hlua_httpclient_new(lua_State *L) |
| 7028 | { |
| 7029 | struct hlua_httpclient *hlua_hc; |
| 7030 | struct hlua *hlua; |
| 7031 | |
| 7032 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 7033 | hlua = hlua_gethlua(L); |
| 7034 | if (!hlua) |
| 7035 | return 0; |
| 7036 | |
| 7037 | /* Check stack size. */ |
| 7038 | if (!lua_checkstack(L, 3)) { |
| 7039 | hlua_pusherror(L, "httpclient: full stack"); |
| 7040 | goto err; |
| 7041 | } |
| 7042 | /* Create the object: obj[0] = userdata. */ |
| 7043 | lua_newtable(L); |
| 7044 | hlua_hc = MAY_LJMP(lua_newuserdata(L, sizeof(*hlua_hc))); |
| 7045 | lua_rawseti(L, -2, 0); |
| 7046 | memset(hlua_hc, 0, sizeof(*hlua_hc)); |
| 7047 | |
| 7048 | hlua_hc->hc = httpclient_new(hlua, 0, IST_NULL); |
| 7049 | if (!hlua_hc->hc) |
| 7050 | goto err; |
| 7051 | |
| 7052 | /* Pop a class stream metatable and affect it to the userdata. */ |
| 7053 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_httpclient_ref); |
| 7054 | lua_setmetatable(L, -2); |
| 7055 | |
| 7056 | return 1; |
| 7057 | |
| 7058 | err: |
| 7059 | WILL_LJMP(lua_error(L)); |
| 7060 | return 0; |
| 7061 | } |
| 7062 | |
| 7063 | |
| 7064 | /* |
| 7065 | * Callback of the httpclient, this callback wakes the lua task up, once the |
| 7066 | * httpclient receives some data |
| 7067 | * |
| 7068 | */ |
| 7069 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7070 | static void hlua_httpclient_cb(struct httpclient *hc) |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7071 | { |
| 7072 | struct hlua *hlua = hc->caller; |
| 7073 | |
| 7074 | if (!hlua || !hlua->task) |
| 7075 | return; |
| 7076 | |
| 7077 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7078 | } |
| 7079 | |
| 7080 | /* |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7081 | * Fill the lua stack with headers from the httpclient response |
| 7082 | * This works the same way as the hlua_http_get_headers() function |
| 7083 | */ |
| 7084 | __LJMP static int hlua_httpclient_get_headers(lua_State *L, struct hlua_httpclient *hlua_hc) |
| 7085 | { |
| 7086 | struct http_hdr *hdr; |
| 7087 | |
| 7088 | lua_newtable(L); |
| 7089 | |
William Lallemand | ef574b2 | 2021-10-05 16:19:31 +0200 | [diff] [blame] | 7090 | for (hdr = hlua_hc->hc->res.hdrs; hdr && isttest(hdr->n); hdr++) { |
William Lallemand | d7df73a | 2021-09-23 17:54:00 +0200 | [diff] [blame] | 7091 | struct ist n, v; |
| 7092 | int len; |
| 7093 | |
| 7094 | n = hdr->n; |
| 7095 | v = hdr->v; |
| 7096 | |
| 7097 | /* Check for existing entry: |
| 7098 | * assume that the table is on the top of the stack, and |
| 7099 | * push the key in the stack, the function lua_gettable() |
| 7100 | * perform the lookup. |
| 7101 | */ |
| 7102 | |
| 7103 | lua_pushlstring(L, n.ptr, n.len); |
| 7104 | lua_gettable(L, -2); |
| 7105 | |
| 7106 | switch (lua_type(L, -1)) { |
| 7107 | case LUA_TNIL: |
| 7108 | /* Table not found, create it. */ |
| 7109 | lua_pop(L, 1); /* remove the nil value. */ |
| 7110 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 7111 | lua_newtable(L); /* create and push empty table. */ |
| 7112 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7113 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 7114 | lua_rawset(L, -3); /* index new table with header name (pop the values). */ |
| 7115 | break; |
| 7116 | |
| 7117 | case LUA_TTABLE: |
| 7118 | /* Entry found: push the value in the table. */ |
| 7119 | len = lua_rawlen(L, -1); |
| 7120 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 7121 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 7122 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 7123 | break; |
| 7124 | |
| 7125 | default: |
| 7126 | /* Other cases are errors. */ |
| 7127 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 7128 | WILL_LJMP(lua_error(L)); |
| 7129 | } |
| 7130 | } |
| 7131 | return 1; |
| 7132 | } |
| 7133 | |
| 7134 | /* |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7135 | * Allocate and return an array of http_hdr ist extracted from the <headers> lua table |
| 7136 | * |
| 7137 | * Caller must free the result |
| 7138 | */ |
| 7139 | struct http_hdr *hlua_httpclient_table_to_hdrs(lua_State *L) |
| 7140 | { |
| 7141 | struct http_hdr hdrs[global.tune.max_http_hdr]; |
| 7142 | struct http_hdr *result = NULL; |
| 7143 | uint32_t hdr_num = 0; |
| 7144 | |
| 7145 | lua_pushnil(L); |
| 7146 | while (lua_next(L, -2) != 0) { |
| 7147 | struct ist name, value; |
| 7148 | const char *n, *v; |
| 7149 | size_t nlen, vlen; |
| 7150 | |
| 7151 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7152 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7153 | goto next_hdr; |
| 7154 | } |
| 7155 | |
| 7156 | n = lua_tolstring(L, -2, &nlen); |
| 7157 | name = ist2(n, nlen); |
| 7158 | |
| 7159 | /* Loop on header's values */ |
| 7160 | lua_pushnil(L); |
| 7161 | while (lua_next(L, -2)) { |
| 7162 | if (!lua_isstring(L, -1)) { |
| 7163 | /* Skip the value if it is not a string */ |
| 7164 | goto next_value; |
| 7165 | } |
| 7166 | |
| 7167 | v = lua_tolstring(L, -1, &vlen); |
| 7168 | value = ist2(v, vlen); |
| 7169 | name = ist2(n, nlen); |
| 7170 | |
| 7171 | hdrs[hdr_num].n = istdup(name); |
| 7172 | hdrs[hdr_num].v = istdup(value); |
| 7173 | |
| 7174 | hdr_num++; |
| 7175 | |
| 7176 | next_value: |
| 7177 | lua_pop(L, 1); |
| 7178 | } |
| 7179 | |
| 7180 | next_hdr: |
| 7181 | lua_pop(L, 1); |
| 7182 | |
| 7183 | } |
| 7184 | |
| 7185 | if (hdr_num) { |
| 7186 | /* alloc and copy the headers in the httpclient struct */ |
Tim Duesterhus | 16cc16d | 2021-11-06 15:14:45 +0100 | [diff] [blame] | 7187 | result = calloc((hdr_num + 1), sizeof(*result)); |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7188 | if (!result) |
| 7189 | goto skip_headers; |
| 7190 | memcpy(result, hdrs, sizeof(struct http_hdr) * (hdr_num + 1)); |
| 7191 | |
| 7192 | result[hdr_num].n = IST_NULL; |
| 7193 | result[hdr_num].v = IST_NULL; |
| 7194 | } |
| 7195 | |
| 7196 | skip_headers: |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7197 | |
| 7198 | return result; |
| 7199 | } |
| 7200 | |
| 7201 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7202 | /* |
| 7203 | * For each yield, checks if there is some data in the httpclient and push them |
| 7204 | * in the lua buffer, once the httpclient finished its job, push the result on |
| 7205 | * the stack |
| 7206 | */ |
| 7207 | __LJMP static int hlua_httpclient_rcv_yield(lua_State *L, int status, lua_KContext ctx) |
| 7208 | { |
| 7209 | struct buffer *tr; |
| 7210 | int res; |
| 7211 | struct hlua *hlua = hlua_gethlua(L); |
| 7212 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7213 | |
| 7214 | |
| 7215 | tr = get_trash_chunk(); |
| 7216 | |
| 7217 | res = httpclient_res_xfer(hlua_hc->hc, tr); |
| 7218 | luaL_addlstring(&hlua_hc->b, b_orig(tr), res); |
| 7219 | |
| 7220 | if (!httpclient_data(hlua_hc->hc) && httpclient_ended(hlua_hc->hc)) { |
| 7221 | |
| 7222 | luaL_pushresult(&hlua_hc->b); |
| 7223 | lua_settable(L, -3); |
| 7224 | |
| 7225 | lua_pushstring(L, "status"); |
| 7226 | lua_pushinteger(L, hlua_hc->hc->res.status); |
| 7227 | lua_settable(L, -3); |
| 7228 | |
| 7229 | |
| 7230 | lua_pushstring(L, "reason"); |
| 7231 | lua_pushlstring(L, hlua_hc->hc->res.reason.ptr, hlua_hc->hc->res.reason.len); |
| 7232 | lua_settable(L, -3); |
| 7233 | |
| 7234 | lua_pushstring(L, "headers"); |
| 7235 | hlua_httpclient_get_headers(L, hlua_hc); |
| 7236 | lua_settable(L, -3); |
| 7237 | |
| 7238 | return 1; |
| 7239 | } |
| 7240 | |
| 7241 | if (httpclient_data(hlua_hc->hc)) |
| 7242 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 7243 | |
| 7244 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7245 | |
| 7246 | return 0; |
| 7247 | } |
| 7248 | |
| 7249 | /* |
| 7250 | * Call this when trying to stream a body during a request |
| 7251 | */ |
| 7252 | __LJMP static int hlua_httpclient_snd_yield(lua_State *L, int status, lua_KContext ctx) |
| 7253 | { |
| 7254 | struct hlua *hlua; |
| 7255 | struct hlua_httpclient *hlua_hc = hlua_checkhttpclient(L, 1); |
| 7256 | const char *body_str = NULL; |
| 7257 | int ret; |
| 7258 | int end = 0; |
| 7259 | size_t buf_len; |
| 7260 | size_t to_send = 0; |
| 7261 | |
| 7262 | hlua = hlua_gethlua(L); |
| 7263 | |
| 7264 | if (!hlua || !hlua->task) |
| 7265 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7266 | "'frontend', 'backend' or 'task'")); |
| 7267 | |
| 7268 | ret = lua_getfield(L, -1, "body"); |
| 7269 | if (ret != LUA_TSTRING) |
| 7270 | goto rcv; |
| 7271 | |
| 7272 | body_str = lua_tolstring(L, -1, &buf_len); |
| 7273 | lua_pop(L, 1); |
| 7274 | |
Christopher Faulet | fc59129 | 2022-01-12 14:46:03 +0100 | [diff] [blame] | 7275 | to_send = buf_len - hlua_hc->sent; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7276 | |
| 7277 | if ((hlua_hc->sent + to_send) >= buf_len) |
| 7278 | end = 1; |
| 7279 | |
| 7280 | /* the end flag is always set since we are using the whole remaining size */ |
| 7281 | hlua_hc->sent += httpclient_req_xfer(hlua_hc->hc, ist2(body_str + hlua_hc->sent, to_send), end); |
| 7282 | |
| 7283 | if (buf_len > hlua_hc->sent) { |
| 7284 | /* still need to process the buffer */ |
| 7285 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_snd_yield, TICK_ETERNITY, 0)); |
| 7286 | } else { |
| 7287 | goto rcv; |
| 7288 | /* we sent the whole request buffer we can recv */ |
| 7289 | } |
| 7290 | return 0; |
| 7291 | |
| 7292 | rcv: |
| 7293 | |
| 7294 | /* we return a "res" object */ |
| 7295 | lua_newtable(L); |
| 7296 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7297 | lua_pushstring(L, "body"); |
William Lallemand | d1187eb | 2021-11-02 10:40:06 +0100 | [diff] [blame] | 7298 | luaL_buffinit(L, &hlua_hc->b); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7299 | |
William Lallemand | 933fe39 | 2021-11-04 09:45:58 +0100 | [diff] [blame] | 7300 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7301 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_httpclient_rcv_yield, TICK_ETERNITY, 0)); |
| 7302 | |
| 7303 | return 1; |
| 7304 | } |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7305 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7306 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7307 | * Send an HTTP request and wait for a response |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7308 | */ |
| 7309 | |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7310 | __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] | 7311 | { |
| 7312 | struct hlua_httpclient *hlua_hc; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7313 | struct http_hdr *hdrs = NULL; |
| 7314 | struct http_hdr *hdrs_i = NULL; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7315 | struct hlua *hlua; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7316 | const char *url_str = NULL; |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7317 | const char *body_str = NULL; |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7318 | size_t buf_len; |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7319 | int ret; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7320 | |
| 7321 | hlua = hlua_gethlua(L); |
| 7322 | |
| 7323 | if (!hlua || !hlua->task) |
| 7324 | WILL_LJMP(luaL_error(L, "The 'get' function is only allowed in " |
| 7325 | "'frontend', 'backend' or 'task'")); |
| 7326 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7327 | if (lua_gettop(L) != 2 || lua_type(L, -1) != LUA_TTABLE) |
| 7328 | WILL_LJMP(luaL_error(L, "'get' needs a table as argument")); |
| 7329 | |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7330 | hlua_hc = hlua_checkhttpclient(L, 1); |
| 7331 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7332 | lua_pushnil(L); /* first key */ |
| 7333 | while (lua_next(L, 2)) { |
| 7334 | if (strcmp(lua_tostring(L, -2), "dst") == 0) { |
| 7335 | if (httpclient_set_dst(hlua_hc->hc, lua_tostring(L, -1)) < 0) |
| 7336 | WILL_LJMP(luaL_error(L, "Can't use the 'dst' argument")); |
William Lallemand | 4f4f2b7 | 2022-02-17 20:00:23 +0100 | [diff] [blame] | 7337 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7338 | } else if (strcmp(lua_tostring(L, -2), "url") == 0) { |
| 7339 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7340 | WILL_LJMP(luaL_error(L, "invalid parameter in 'url', must be a string")); |
| 7341 | url_str = lua_tostring(L, -1); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7342 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7343 | } else if (strcmp(lua_tostring(L, -2), "timeout") == 0) { |
| 7344 | if (lua_type(L, -1) != LUA_TNUMBER) |
| 7345 | WILL_LJMP(luaL_error(L, "invalid parameter in 'timeout', must be a number")); |
| 7346 | httpclient_set_timeout(hlua_hc->hc, lua_tointeger(L, -1)); |
William Lallemand | b4a4ef6 | 2022-02-23 14:18:16 +0100 | [diff] [blame] | 7347 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7348 | } else if (strcmp(lua_tostring(L, -2), "headers") == 0) { |
| 7349 | if (lua_type(L, -1) != LUA_TTABLE) |
| 7350 | WILL_LJMP(luaL_error(L, "invalid parameter in 'headers', must be a table")); |
| 7351 | hdrs = hlua_httpclient_table_to_hdrs(L); |
William Lallemand | 79416cb | 2021-09-24 14:51:44 +0200 | [diff] [blame] | 7352 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7353 | } else if (strcmp(lua_tostring(L, -2), "body") == 0) { |
| 7354 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7355 | WILL_LJMP(luaL_error(L, "invalid parameter in 'body', must be a string")); |
| 7356 | body_str = lua_tolstring(L, -1, &buf_len); |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7357 | |
William Lallemand | 7177a95 | 2022-03-03 15:33:12 +0100 | [diff] [blame] | 7358 | } else { |
| 7359 | WILL_LJMP(luaL_error(L, "'%s' invalid parameter name", lua_tostring(L, -2))); |
| 7360 | } |
| 7361 | /* removes 'value'; keeps 'key' for next iteration */ |
| 7362 | lua_pop(L, 1); |
| 7363 | } |
William Lallemand | dec25c3 | 2021-10-25 19:48:37 +0200 | [diff] [blame] | 7364 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7365 | if (!url_str) { |
| 7366 | WILL_LJMP(luaL_error(L, "'get' need a 'url' argument")); |
| 7367 | return 0; |
| 7368 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7369 | |
William Lallemand | 10a3736 | 2022-03-02 16:18:26 +0100 | [diff] [blame] | 7370 | hlua_hc->sent = 0; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7371 | |
| 7372 | hlua_hc->hc->req.url = istdup(ist(url_str)); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7373 | hlua_hc->hc->req.meth = meth; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7374 | |
| 7375 | /* update the httpclient callbacks */ |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7376 | hlua_hc->hc->ops.res_stline = hlua_httpclient_cb; |
| 7377 | hlua_hc->hc->ops.res_headers = hlua_httpclient_cb; |
| 7378 | hlua_hc->hc->ops.res_payload = hlua_httpclient_cb; |
William Lallemand | 8f170c7 | 2022-03-15 10:52:07 +0100 | [diff] [blame] | 7379 | hlua_hc->hc->ops.res_end = hlua_httpclient_cb; |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7380 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7381 | /* a body is available, it will use the request callback */ |
| 7382 | if (body_str) { |
| 7383 | hlua_hc->hc->ops.req_payload = hlua_httpclient_cb; |
| 7384 | } |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7385 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7386 | 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] | 7387 | |
William Lallemand | 746e6f3 | 2021-10-06 10:57:44 +0200 | [diff] [blame] | 7388 | /* free the temporary headers array */ |
| 7389 | hdrs_i = hdrs; |
| 7390 | while (hdrs_i && isttest(hdrs_i->n)) { |
| 7391 | istfree(&hdrs_i->n); |
| 7392 | istfree(&hdrs_i->v); |
| 7393 | hdrs_i++; |
| 7394 | } |
| 7395 | ha_free(&hdrs); |
| 7396 | |
| 7397 | |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7398 | if (ret != ERR_NONE) { |
| 7399 | WILL_LJMP(luaL_error(L, "Can't generate the HTTP request")); |
| 7400 | return 0; |
| 7401 | } |
| 7402 | |
William Lallemand | c2d3db4 | 2022-04-26 11:46:13 +0200 | [diff] [blame] | 7403 | if (!httpclient_start(hlua_hc->hc)) |
| 7404 | WILL_LJMP(luaL_error(L, "couldn't start the httpclient")); |
William Lallemand | 6137a9e | 2021-10-26 15:01:53 +0200 | [diff] [blame] | 7405 | |
William Lallemand | bd5739e | 2021-10-28 15:41:38 +0200 | [diff] [blame] | 7406 | 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] | 7407 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7408 | return 0; |
| 7409 | } |
| 7410 | |
| 7411 | /* |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 7412 | * Sends an HTTP HEAD request and wait for a response |
| 7413 | * |
| 7414 | * httpclient:head(url, headers, payload) |
| 7415 | */ |
| 7416 | __LJMP static int hlua_httpclient_head(lua_State *L) |
| 7417 | { |
| 7418 | return hlua_httpclient_send(L, HTTP_METH_HEAD); |
| 7419 | } |
| 7420 | |
| 7421 | /* |
| 7422 | * Send an HTTP GET request and wait for a response |
| 7423 | * |
| 7424 | * httpclient:get(url, headers, payload) |
| 7425 | */ |
| 7426 | __LJMP static int hlua_httpclient_get(lua_State *L) |
| 7427 | { |
| 7428 | return hlua_httpclient_send(L, HTTP_METH_GET); |
| 7429 | |
| 7430 | } |
| 7431 | |
| 7432 | /* |
| 7433 | * Sends an HTTP PUT request and wait for a response |
| 7434 | * |
| 7435 | * httpclient:put(url, headers, payload) |
| 7436 | */ |
| 7437 | __LJMP static int hlua_httpclient_put(lua_State *L) |
| 7438 | { |
| 7439 | return hlua_httpclient_send(L, HTTP_METH_PUT); |
| 7440 | } |
| 7441 | |
| 7442 | /* |
| 7443 | * Send an HTTP POST request and wait for a response |
| 7444 | * |
| 7445 | * httpclient:post(url, headers, payload) |
| 7446 | */ |
| 7447 | __LJMP static int hlua_httpclient_post(lua_State *L) |
| 7448 | { |
| 7449 | return hlua_httpclient_send(L, HTTP_METH_POST); |
| 7450 | } |
| 7451 | |
| 7452 | |
| 7453 | /* |
| 7454 | * Sends an HTTP DELETE request and wait for a response |
| 7455 | * |
| 7456 | * httpclient:delete(url, headers, payload) |
| 7457 | */ |
| 7458 | __LJMP static int hlua_httpclient_delete(lua_State *L) |
| 7459 | { |
| 7460 | return hlua_httpclient_send(L, HTTP_METH_DELETE); |
| 7461 | } |
| 7462 | |
| 7463 | /* |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 7464 | * |
| 7465 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7466 | * Class TXN |
| 7467 | * |
| 7468 | * |
| 7469 | */ |
| 7470 | |
| 7471 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7472 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7473 | */ |
| 7474 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 7475 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7476 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7477 | } |
| 7478 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7479 | __LJMP static int hlua_set_var(lua_State *L) |
| 7480 | { |
| 7481 | struct hlua_txn *htxn; |
| 7482 | const char *name; |
| 7483 | size_t len; |
| 7484 | struct sample smp; |
| 7485 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 7486 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7487 | 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] | 7488 | |
| 7489 | /* It is useles to retrieve the stream, but this function |
| 7490 | * runs only in a stream context. |
| 7491 | */ |
| 7492 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7493 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7494 | |
| 7495 | /* Converts the third argument in a sample. */ |
Amaury Denoyelle | bc0af6a | 2020-10-29 17:21:20 +0100 | [diff] [blame] | 7496 | memset(&smp, 0, sizeof(smp)); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7497 | hlua_lua2smp(L, 3, &smp); |
| 7498 | |
| 7499 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7500 | 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] | 7501 | |
| 7502 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 7503 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 7504 | else |
| 7505 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 7506 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 7507 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7508 | } |
| 7509 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7510 | __LJMP static int hlua_unset_var(lua_State *L) |
| 7511 | { |
| 7512 | struct hlua_txn *htxn; |
| 7513 | const char *name; |
| 7514 | size_t len; |
| 7515 | struct sample smp; |
| 7516 | |
| 7517 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 7518 | |
| 7519 | /* It is useles to retrieve the stream, but this function |
| 7520 | * runs only in a stream context. |
| 7521 | */ |
| 7522 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7523 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7524 | |
| 7525 | /* Unset the variable. */ |
| 7526 | 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] | 7527 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 7528 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 7529 | } |
| 7530 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7531 | __LJMP static int hlua_get_var(lua_State *L) |
| 7532 | { |
| 7533 | struct hlua_txn *htxn; |
| 7534 | const char *name; |
| 7535 | size_t len; |
| 7536 | struct sample smp; |
| 7537 | |
| 7538 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 7539 | |
| 7540 | /* It is useles to retrieve the stream, but this function |
| 7541 | * runs only in a stream context. |
| 7542 | */ |
| 7543 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7544 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 7545 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 7546 | 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] | 7547 | if (!vars_get_by_name(name, len, &smp, NULL)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 7548 | lua_pushnil(L); |
| 7549 | return 1; |
| 7550 | } |
| 7551 | |
| 7552 | return hlua_smp2lua(L, &smp); |
| 7553 | } |
| 7554 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7555 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7556 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7557 | struct hlua *hlua; |
| 7558 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7559 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 7560 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7561 | /* It is useles to retrieve the stream, but this function |
| 7562 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7563 | */ |
| 7564 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7565 | |
| 7566 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7567 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7568 | if (!hlua) |
| 7569 | return 0; |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7570 | |
| 7571 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 7572 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7573 | |
| 7574 | /* Get and store new value. */ |
| 7575 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 7576 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 7577 | |
| 7578 | return 0; |
| 7579 | } |
| 7580 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7581 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7582 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7583 | struct hlua *hlua; |
| 7584 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7585 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 7586 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 7587 | /* It is useles to retrieve the stream, but this function |
| 7588 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7589 | */ |
| 7590 | MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7591 | |
| 7592 | /* Get hlua struct, or NULL if we execute from main lua state */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7593 | hlua = hlua_gethlua(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 7594 | if (!hlua) { |
| 7595 | lua_pushnil(L); |
| 7596 | return 1; |
| 7597 | } |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7598 | |
| 7599 | /* Push configuration index in the stack. */ |
| 7600 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 7601 | |
| 7602 | return 1; |
| 7603 | } |
| 7604 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7605 | /* Create stack entry containing a class TXN. This function |
| 7606 | * return 0 if the stack does not contains free slots, |
| 7607 | * otherwise it returns 1. |
| 7608 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7609 | 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] | 7610 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7611 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7612 | |
| 7613 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7614 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7615 | return 0; |
| 7616 | |
| 7617 | /* NOTE: The allocation never fails. The failure |
| 7618 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7619 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7620 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7621 | /* Create the object: obj[0] = userdata. */ |
| 7622 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7623 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 7624 | lua_rawseti(L, -2, 0); |
| 7625 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7626 | htxn->s = s; |
| 7627 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 7628 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 7629 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7630 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7631 | /* Create the "f" field that contains a list of fetches. */ |
| 7632 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7633 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7634 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7635 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7636 | |
| 7637 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 7638 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 7639 | 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] | 7640 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7641 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7642 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7643 | /* Create the "c" field that contains a list of converters. */ |
| 7644 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7645 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7646 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7647 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 7648 | |
| 7649 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 7650 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 7651 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7652 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7653 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7654 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7655 | /* Create the "req" field that contains the request channel object. */ |
| 7656 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7657 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7658 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7659 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7660 | |
| 7661 | /* Create the "res" field that contains the response channel object. */ |
| 7662 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 7663 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7664 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7665 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 7666 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7667 | /* Creates the HTTP object is the current proxy allows http. */ |
| 7668 | lua_pushstring(L, "http"); |
Christopher Faulet | 1bb6afa | 2021-03-08 17:57:53 +0100 | [diff] [blame] | 7669 | if (IS_HTX_STRM(s)) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 7670 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7671 | return 0; |
| 7672 | } |
| 7673 | else |
| 7674 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7675 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7676 | |
Christopher Faulet | 78c3547 | 2020-02-26 17:14:08 +0100 | [diff] [blame] | 7677 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) { |
| 7678 | /* HTTPMessage object are created when a lua TXN is created from |
| 7679 | * a filter context only |
| 7680 | */ |
| 7681 | |
| 7682 | /* Creates the HTTP-Request object is the current proxy allows http. */ |
| 7683 | lua_pushstring(L, "http_req"); |
| 7684 | if (p->mode == PR_MODE_HTTP) { |
| 7685 | if (!hlua_http_msg_new(L, &s->txn->req)) |
| 7686 | return 0; |
| 7687 | } |
| 7688 | else |
| 7689 | lua_pushnil(L); |
| 7690 | lua_rawset(L, -3); |
| 7691 | |
| 7692 | /* Creates the HTTP-Response object is the current proxy allows http. */ |
| 7693 | lua_pushstring(L, "http_res"); |
| 7694 | if (p->mode == PR_MODE_HTTP) { |
| 7695 | if (!hlua_http_msg_new(L, &s->txn->rsp)) |
| 7696 | return 0; |
| 7697 | } |
| 7698 | else |
| 7699 | lua_pushnil(L); |
| 7700 | lua_rawset(L, -3); |
| 7701 | } |
| 7702 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7703 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 7704 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 7705 | lua_setmetatable(L, -2); |
| 7706 | |
| 7707 | return 1; |
| 7708 | } |
| 7709 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7710 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 7711 | { |
| 7712 | const char *msg; |
| 7713 | struct hlua_txn *htxn; |
| 7714 | |
| 7715 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 7716 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7717 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7718 | |
| 7719 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 7720 | return 0; |
| 7721 | } |
| 7722 | |
| 7723 | __LJMP static int hlua_txn_log(lua_State *L) |
| 7724 | { |
| 7725 | int level; |
| 7726 | const char *msg; |
| 7727 | struct hlua_txn *htxn; |
| 7728 | |
| 7729 | MAY_LJMP(check_args(L, 3, "log")); |
| 7730 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7731 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7732 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 7733 | |
| 7734 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 7735 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 7736 | |
| 7737 | hlua_sendlog(htxn->s->be, level, msg); |
| 7738 | return 0; |
| 7739 | } |
| 7740 | |
| 7741 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 7742 | { |
| 7743 | const char *msg; |
| 7744 | struct hlua_txn *htxn; |
| 7745 | |
| 7746 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 7747 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7748 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7749 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 7750 | return 0; |
| 7751 | } |
| 7752 | |
| 7753 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 7754 | { |
| 7755 | const char *msg; |
| 7756 | struct hlua_txn *htxn; |
| 7757 | |
| 7758 | MAY_LJMP(check_args(L, 2, "Info")); |
| 7759 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7760 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7761 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 7762 | return 0; |
| 7763 | } |
| 7764 | |
| 7765 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 7766 | { |
| 7767 | const char *msg; |
| 7768 | struct hlua_txn *htxn; |
| 7769 | |
| 7770 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 7771 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7772 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7773 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 7774 | return 0; |
| 7775 | } |
| 7776 | |
| 7777 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 7778 | { |
| 7779 | const char *msg; |
| 7780 | struct hlua_txn *htxn; |
| 7781 | |
| 7782 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 7783 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7784 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7785 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 7786 | return 0; |
| 7787 | } |
| 7788 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7789 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 7790 | { |
| 7791 | struct hlua_txn *htxn; |
| 7792 | int ll; |
| 7793 | |
| 7794 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 7795 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7796 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7797 | |
| 7798 | if (ll < 0 || ll > 7) |
| 7799 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 7800 | |
| 7801 | htxn->s->logs.level = ll; |
| 7802 | return 0; |
| 7803 | } |
| 7804 | |
| 7805 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 7806 | { |
| 7807 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7808 | int tos; |
| 7809 | |
| 7810 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 7811 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7812 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7813 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 7814 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7815 | return 0; |
| 7816 | } |
| 7817 | |
| 7818 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 7819 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7820 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7821 | int mark; |
| 7822 | |
| 7823 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 7824 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7825 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 7826 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 7827 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 7828 | return 0; |
| 7829 | } |
| 7830 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7831 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 7832 | { |
| 7833 | struct hlua_txn *htxn; |
| 7834 | |
| 7835 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 7836 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7837 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7838 | return 0; |
| 7839 | } |
| 7840 | |
| 7841 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 7842 | { |
| 7843 | struct hlua_txn *htxn; |
| 7844 | |
| 7845 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 7846 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 7847 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 7848 | return 0; |
| 7849 | } |
| 7850 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7851 | /* 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] | 7852 | * 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] | 7853 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 7854 | * error. The Reply must be on top of the stack. |
| 7855 | */ |
| 7856 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 7857 | { |
| 7858 | struct htx *htx; |
| 7859 | struct htx_sl *sl; |
| 7860 | struct h1m h1m; |
| 7861 | const char *status, *reason, *body; |
| 7862 | size_t status_len, reason_len, body_len; |
| 7863 | int ret, code, flags; |
| 7864 | |
| 7865 | code = 200; |
| 7866 | status = "200"; |
| 7867 | status_len = 3; |
| 7868 | ret = lua_getfield(L, -1, "status"); |
| 7869 | if (ret == LUA_TNUMBER) { |
| 7870 | code = lua_tointeger(L, -1); |
| 7871 | status = lua_tolstring(L, -1, &status_len); |
| 7872 | } |
| 7873 | lua_pop(L, 1); |
| 7874 | |
| 7875 | reason = http_get_reason(code); |
| 7876 | reason_len = strlen(reason); |
| 7877 | ret = lua_getfield(L, -1, "reason"); |
| 7878 | if (ret == LUA_TSTRING) |
| 7879 | reason = lua_tolstring(L, -1, &reason_len); |
| 7880 | lua_pop(L, 1); |
| 7881 | |
| 7882 | body = NULL; |
| 7883 | body_len = 0; |
| 7884 | ret = lua_getfield(L, -1, "body"); |
| 7885 | if (ret == LUA_TSTRING) |
| 7886 | body = lua_tolstring(L, -1, &body_len); |
| 7887 | lua_pop(L, 1); |
| 7888 | |
| 7889 | /* Prepare the response before inserting the headers */ |
| 7890 | h1m_init_res(&h1m); |
| 7891 | htx = htx_from_buf(&s->res.buf); |
| 7892 | channel_htx_truncate(&s->res, htx); |
| 7893 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 7894 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 7895 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 7896 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7897 | } |
| 7898 | else { |
| 7899 | flags = HTX_SL_F_IS_RESP; |
| 7900 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 7901 | ist2(status, status_len), ist2(reason, reason_len)); |
| 7902 | } |
| 7903 | if (!sl) |
| 7904 | goto fail; |
| 7905 | sl->info.res.status = code; |
| 7906 | |
| 7907 | /* Push in the stack the "headers" entry. */ |
| 7908 | ret = lua_getfield(L, -1, "headers"); |
| 7909 | if (ret != LUA_TTABLE) |
| 7910 | goto skip_headers; |
| 7911 | |
| 7912 | lua_pushnil(L); |
| 7913 | while (lua_next(L, -2) != 0) { |
| 7914 | struct ist name, value; |
| 7915 | const char *n, *v; |
| 7916 | size_t nlen, vlen; |
| 7917 | |
| 7918 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 7919 | /* Skip element if the key is not a string or if the value is not a table */ |
| 7920 | goto next_hdr; |
| 7921 | } |
| 7922 | |
| 7923 | n = lua_tolstring(L, -2, &nlen); |
| 7924 | name = ist2(n, nlen); |
| 7925 | if (isteqi(name, ist("content-length"))) { |
| 7926 | /* Always skip content-length header. It will be added |
| 7927 | * later with the correct len |
| 7928 | */ |
| 7929 | goto next_hdr; |
| 7930 | } |
| 7931 | |
| 7932 | /* Loop on header's values */ |
| 7933 | lua_pushnil(L); |
| 7934 | while (lua_next(L, -2)) { |
| 7935 | if (!lua_isstring(L, -1)) { |
| 7936 | /* Skip the value if it is not a string */ |
| 7937 | goto next_value; |
| 7938 | } |
| 7939 | |
| 7940 | v = lua_tolstring(L, -1, &vlen); |
| 7941 | value = ist2(v, vlen); |
| 7942 | |
| 7943 | if (isteqi(name, ist("transfer-encoding"))) |
| 7944 | h1_parse_xfer_enc_header(&h1m, value); |
| 7945 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 7946 | goto fail; |
| 7947 | |
| 7948 | next_value: |
| 7949 | lua_pop(L, 1); |
| 7950 | } |
| 7951 | |
| 7952 | next_hdr: |
| 7953 | lua_pop(L, 1); |
| 7954 | } |
| 7955 | skip_headers: |
| 7956 | lua_pop(L, 1); |
| 7957 | |
| 7958 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 7959 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 7960 | const char *clen = ultoa(body_len); |
| 7961 | |
| 7962 | h1m.flags |= H1_MF_CLEN; |
| 7963 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 7964 | goto fail; |
| 7965 | } |
| 7966 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 7967 | h1m.flags |= H1_MF_XFER_LEN; |
| 7968 | |
| 7969 | /* Update HTX start-line flags */ |
| 7970 | if (h1m.flags & H1_MF_XFER_ENC) |
| 7971 | flags |= HTX_SL_F_XFER_ENC; |
| 7972 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 7973 | flags |= HTX_SL_F_XFER_LEN; |
| 7974 | if (h1m.flags & H1_MF_CHNK) |
| 7975 | flags |= HTX_SL_F_CHNK; |
| 7976 | else if (h1m.flags & H1_MF_CLEN) |
| 7977 | flags |= HTX_SL_F_CLEN; |
| 7978 | if (h1m.body_len == 0) |
| 7979 | flags |= HTX_SL_F_BODYLESS; |
| 7980 | } |
| 7981 | sl->flags |= flags; |
| 7982 | |
| 7983 | |
| 7984 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7985 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len)))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7986 | goto fail; |
| 7987 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 7988 | htx->flags |= HTX_FL_EOM; |
| 7989 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 7990 | /* Now, forward the response and terminate the transaction */ |
| 7991 | s->txn->status = code; |
| 7992 | htx_to_buf(htx, &s->res.buf); |
| 7993 | if (!http_forward_proxy_resp(s, 1)) |
| 7994 | goto fail; |
| 7995 | |
| 7996 | return 1; |
| 7997 | |
| 7998 | fail: |
| 7999 | channel_htx_truncate(&s->res, htx); |
| 8000 | return 0; |
| 8001 | } |
| 8002 | |
| 8003 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 8004 | * processing is just aborted. Nothing is returned to the client and all |
| 8005 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 8006 | * is forwarded to the client before terminating the transaction. On success, |
| 8007 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 8008 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 8009 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8010 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8011 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8012 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8013 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8014 | struct stream *s; |
| 8015 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8016 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 8017 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8018 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8019 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 8020 | * just end the execution of the current lua code. */ |
| 8021 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8022 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 8023 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8024 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8025 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8026 | struct channel *req = &s->req; |
| 8027 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 8028 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8029 | channel_auto_read(req); |
| 8030 | channel_abort(req); |
| 8031 | channel_auto_close(req); |
| 8032 | channel_erase(req); |
| 8033 | |
| 8034 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 8035 | channel_auto_read(res); |
| 8036 | channel_auto_close(res); |
| 8037 | channel_shutr_now(res); |
| 8038 | |
| 8039 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 8040 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8041 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 8042 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8043 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 8044 | /* No reply or invalid reply */ |
| 8045 | s->txn->status = 0; |
| 8046 | http_reply_and_close(s, 0, NULL); |
| 8047 | } |
| 8048 | else { |
| 8049 | /* Remove extra args to have the reply on top of the stack */ |
| 8050 | if (lua_gettop(L) > 2) |
| 8051 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8052 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8053 | if (!hlua_txn_forward_reply(L, s)) { |
| 8054 | if (!(s->flags & SF_ERR_MASK)) |
| 8055 | s->flags |= SF_ERR_PRXCOND; |
| 8056 | lua_pushinteger(L, ACT_RET_ERR); |
| 8057 | WILL_LJMP(hlua_done(L)); |
| 8058 | return 0; /* Never reached */ |
| 8059 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 8060 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8061 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8062 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 8063 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 8064 | /* let's log the request time */ |
| 8065 | s->logs.tv_request = now; |
| 8066 | 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] | 8067 | _HA_ATOMIC_INC(&s->sess->fe->fe_counters.intercepted_req); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8068 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8069 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8070 | done: |
| 8071 | if (!(s->flags & SF_ERR_MASK)) |
| 8072 | s->flags |= SF_ERR_LOCAL; |
| 8073 | if (!(s->flags & SF_FINST_MASK)) |
| 8074 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 8075 | |
Christopher Faulet | e48d1dc | 2021-08-13 14:11:17 +0200 | [diff] [blame] | 8076 | if ((htxn->flags & HLUA_TXN_CTX_MASK) == HLUA_TXN_FLT_CTX) |
| 8077 | lua_pushinteger(L, -1); |
| 8078 | else |
| 8079 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 8080 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8081 | return 0; |
| 8082 | } |
| 8083 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8084 | /* |
| 8085 | * |
| 8086 | * |
| 8087 | * Class REPLY |
| 8088 | * |
| 8089 | * |
| 8090 | */ |
| 8091 | |
| 8092 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 8093 | * free slots, the function fails and returns 0; |
| 8094 | */ |
| 8095 | static int hlua_txn_reply_new(lua_State *L) |
| 8096 | { |
| 8097 | struct hlua_txn *htxn; |
| 8098 | const char *reason, *body = NULL; |
| 8099 | int ret, status; |
| 8100 | |
| 8101 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 8102 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8103 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 8104 | WILL_LJMP(lua_error(L)); |
| 8105 | } |
| 8106 | |
| 8107 | /* Default value */ |
| 8108 | status = 200; |
| 8109 | reason = http_get_reason(status); |
| 8110 | |
| 8111 | if (lua_istable(L, 2)) { |
| 8112 | /* load status and reason from the table argument at index 2 */ |
| 8113 | ret = lua_getfield(L, 2, "status"); |
| 8114 | if (ret == LUA_TNIL) |
| 8115 | goto reason; |
| 8116 | else if (ret != LUA_TNUMBER) { |
| 8117 | /* invalid status: ignore the reason */ |
| 8118 | goto body; |
| 8119 | } |
| 8120 | status = lua_tointeger(L, -1); |
| 8121 | |
| 8122 | reason: |
| 8123 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 8124 | ret = lua_getfield(L, 2, "reason"); |
| 8125 | if (ret == LUA_TSTRING) |
| 8126 | reason = lua_tostring(L, -1); |
| 8127 | |
| 8128 | body: |
| 8129 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 8130 | ret = lua_getfield(L, 2, "body"); |
| 8131 | if (ret == LUA_TSTRING) |
| 8132 | body = lua_tostring(L, -1); |
| 8133 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 8134 | } |
| 8135 | |
| 8136 | /* Create the Reply table */ |
| 8137 | lua_newtable(L); |
| 8138 | |
| 8139 | /* Add status element */ |
| 8140 | lua_pushstring(L, "status"); |
| 8141 | lua_pushinteger(L, status); |
| 8142 | lua_settable(L, -3); |
| 8143 | |
| 8144 | /* Add reason element */ |
| 8145 | reason = http_get_reason(status); |
| 8146 | lua_pushstring(L, "reason"); |
| 8147 | lua_pushstring(L, reason); |
| 8148 | lua_settable(L, -3); |
| 8149 | |
| 8150 | /* Add body element, nil if undefined */ |
| 8151 | lua_pushstring(L, "body"); |
| 8152 | if (body) |
| 8153 | lua_pushstring(L, body); |
| 8154 | else |
| 8155 | lua_pushnil(L); |
| 8156 | lua_settable(L, -3); |
| 8157 | |
| 8158 | /* Add headers element */ |
| 8159 | lua_pushstring(L, "headers"); |
| 8160 | lua_newtable(L); |
| 8161 | |
| 8162 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8163 | if (lua_istable(L, 2)) { |
| 8164 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 8165 | ret = lua_getfield(L, 2, "headers"); |
| 8166 | if (ret == LUA_TTABLE) { |
| 8167 | /* stack: [ ... <headers:table>, <table> ] */ |
| 8168 | lua_pushnil(L); |
| 8169 | while (lua_next(L, -2) != 0) { |
| 8170 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 8171 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 8172 | /* invalid value type, skip it */ |
| 8173 | lua_pop(L, 1); |
| 8174 | continue; |
| 8175 | } |
| 8176 | |
| 8177 | |
| 8178 | /* Duplicate the key and swap it with the value. */ |
| 8179 | lua_pushvalue(L, -2); |
| 8180 | lua_insert(L, -2); |
| 8181 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 8182 | |
| 8183 | lua_newtable(L); |
| 8184 | lua_insert(L, -2); |
| 8185 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 8186 | |
| 8187 | if (lua_isstring(L, -1)) { |
| 8188 | /* push the value in the inner table */ |
| 8189 | lua_rawseti(L, -2, 1); |
| 8190 | } |
| 8191 | else { /* table */ |
| 8192 | lua_pushnil(L); |
| 8193 | while (lua_next(L, -2) != 0) { |
| 8194 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 8195 | if (!lua_isstring(L, -1)) { |
| 8196 | /* invalid value type, skip it*/ |
| 8197 | lua_pop(L, 1); |
| 8198 | continue; |
| 8199 | } |
| 8200 | /* push the value in the inner table */ |
| 8201 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 8202 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 8203 | } |
| 8204 | lua_pop(L, 1); |
| 8205 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 8206 | } |
| 8207 | |
| 8208 | /* push (k,v) on the stack in the headers table: |
| 8209 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 8210 | */ |
| 8211 | lua_settable(L, -5); |
| 8212 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 8213 | } |
| 8214 | } |
| 8215 | lua_pop(L, 1); |
| 8216 | } |
| 8217 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 8218 | lua_settable(L, -3); |
| 8219 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 8220 | |
| 8221 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 8222 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 8223 | lua_setmetatable(L, -2); |
| 8224 | return 1; |
| 8225 | } |
| 8226 | |
| 8227 | /* Set the reply status code, and optionally the reason. If no reason is |
| 8228 | * provided, the default one corresponding to the status code is used. |
| 8229 | */ |
| 8230 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 8231 | { |
| 8232 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 8233 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 8234 | |
| 8235 | /* First argument (self) must be a table */ |
| 8236 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8237 | |
| 8238 | if (status < 100 || status > 599) { |
| 8239 | lua_pushboolean(L, 0); |
| 8240 | return 1; |
| 8241 | } |
| 8242 | if (!reason) |
| 8243 | reason = http_get_reason(status); |
| 8244 | |
| 8245 | lua_pushinteger(L, status); |
| 8246 | lua_setfield(L, 1, "status"); |
| 8247 | |
| 8248 | lua_pushstring(L, reason); |
| 8249 | lua_setfield(L, 1, "reason"); |
| 8250 | |
| 8251 | lua_pushboolean(L, 1); |
| 8252 | return 1; |
| 8253 | } |
| 8254 | |
| 8255 | /* Add a header into the reply object. Each header name is associated to an |
| 8256 | * array of values in the "headers" table. If the header name is not found, a |
| 8257 | * new entry is created. |
| 8258 | */ |
| 8259 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 8260 | { |
| 8261 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8262 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 8263 | int ret; |
| 8264 | |
| 8265 | /* First argument (self) must be a table */ |
| 8266 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8267 | |
| 8268 | /* Push in the stack the "headers" entry. */ |
| 8269 | ret = lua_getfield(L, 1, "headers"); |
| 8270 | if (ret != LUA_TTABLE) { |
| 8271 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 8272 | WILL_LJMP(lua_error(L)); |
| 8273 | } |
| 8274 | |
| 8275 | /* check if the header is already registered. If not, register it. */ |
| 8276 | ret = lua_getfield(L, -1, name); |
| 8277 | if (ret == LUA_TNIL) { |
| 8278 | /* Entry not found. */ |
| 8279 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 8280 | |
| 8281 | /* Insert the new header name in the array in the top of the stack. |
| 8282 | * It left the new array in the top of the stack. |
| 8283 | */ |
| 8284 | lua_newtable(L); |
| 8285 | lua_pushstring(L, name); |
| 8286 | lua_pushvalue(L, -2); |
| 8287 | lua_settable(L, -4); |
| 8288 | } |
| 8289 | else if (ret != LUA_TTABLE) { |
| 8290 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 8291 | WILL_LJMP(lua_error(L)); |
| 8292 | } |
| 8293 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 8294 | /* Now the top of thestack is an array of values. We push |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8295 | * the header value as new entry. |
| 8296 | */ |
| 8297 | lua_pushstring(L, value); |
| 8298 | ret = lua_rawlen(L, -2); |
| 8299 | lua_rawseti(L, -2, ret + 1); |
| 8300 | |
| 8301 | lua_pushboolean(L, 1); |
| 8302 | return 1; |
| 8303 | } |
| 8304 | |
| 8305 | /* Remove all occurrences of a given header name. */ |
| 8306 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 8307 | { |
| 8308 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8309 | int ret; |
| 8310 | |
| 8311 | /* First argument (self) must be a table */ |
| 8312 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8313 | |
| 8314 | /* Push in the stack the "headers" entry. */ |
| 8315 | ret = lua_getfield(L, 1, "headers"); |
| 8316 | if (ret != LUA_TTABLE) { |
| 8317 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 8318 | WILL_LJMP(lua_error(L)); |
| 8319 | } |
| 8320 | |
| 8321 | lua_pushstring(L, name); |
| 8322 | lua_pushnil(L); |
| 8323 | lua_settable(L, -3); |
| 8324 | |
| 8325 | lua_pushboolean(L, 1); |
| 8326 | return 1; |
| 8327 | } |
| 8328 | |
| 8329 | /* Set the reply's body. Overwrite any existing entry. */ |
| 8330 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 8331 | { |
| 8332 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8333 | |
| 8334 | /* First argument (self) must be a table */ |
| 8335 | luaL_checktype(L, 1, LUA_TTABLE); |
| 8336 | |
| 8337 | lua_pushstring(L, payload); |
| 8338 | lua_setfield(L, 1, "body"); |
| 8339 | |
| 8340 | lua_pushboolean(L, 1); |
| 8341 | return 1; |
| 8342 | } |
| 8343 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8344 | __LJMP static int hlua_log(lua_State *L) |
| 8345 | { |
| 8346 | int level; |
| 8347 | const char *msg; |
| 8348 | |
| 8349 | MAY_LJMP(check_args(L, 2, "log")); |
| 8350 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 8351 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 8352 | |
| 8353 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 8354 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 8355 | |
| 8356 | hlua_sendlog(NULL, level, msg); |
| 8357 | return 0; |
| 8358 | } |
| 8359 | |
| 8360 | __LJMP static int hlua_log_debug(lua_State *L) |
| 8361 | { |
| 8362 | const char *msg; |
| 8363 | |
| 8364 | MAY_LJMP(check_args(L, 1, "debug")); |
| 8365 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8366 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 8367 | return 0; |
| 8368 | } |
| 8369 | |
| 8370 | __LJMP static int hlua_log_info(lua_State *L) |
| 8371 | { |
| 8372 | const char *msg; |
| 8373 | |
| 8374 | MAY_LJMP(check_args(L, 1, "info")); |
| 8375 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8376 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 8377 | return 0; |
| 8378 | } |
| 8379 | |
| 8380 | __LJMP static int hlua_log_warning(lua_State *L) |
| 8381 | { |
| 8382 | const char *msg; |
| 8383 | |
| 8384 | MAY_LJMP(check_args(L, 1, "warning")); |
| 8385 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8386 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 8387 | return 0; |
| 8388 | } |
| 8389 | |
| 8390 | __LJMP static int hlua_log_alert(lua_State *L) |
| 8391 | { |
| 8392 | const char *msg; |
| 8393 | |
| 8394 | MAY_LJMP(check_args(L, 1, "alert")); |
| 8395 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8396 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 8397 | return 0; |
| 8398 | } |
| 8399 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8400 | __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] | 8401 | { |
| 8402 | int wakeup_ms = lua_tointeger(L, -1); |
Willy Tarreau | 12c0270 | 2021-09-30 16:12:31 +0200 | [diff] [blame] | 8403 | if (!tick_is_expired(wakeup_ms, now_ms)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8404 | 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] | 8405 | return 0; |
| 8406 | } |
| 8407 | |
| 8408 | __LJMP static int hlua_sleep(lua_State *L) |
| 8409 | { |
| 8410 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8411 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8412 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8413 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8414 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8415 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8416 | wakeup_ms = tick_add(now_ms, delay); |
| 8417 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8418 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8419 | 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] | 8420 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8421 | } |
| 8422 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8423 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8424 | { |
| 8425 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8426 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8427 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8428 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8429 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8430 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 8431 | wakeup_ms = tick_add(now_ms, delay); |
| 8432 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8433 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8434 | 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] | 8435 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8436 | } |
| 8437 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8438 | /* This functionis an LUA binding. it permits to give back |
| 8439 | * the hand at the HAProxy scheduler. It is used when the |
| 8440 | * LUA processing consumes a lot of time. |
| 8441 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 8442 | __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] | 8443 | { |
| 8444 | return 0; |
| 8445 | } |
| 8446 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8447 | __LJMP static int hlua_yield(lua_State *L) |
| 8448 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 8449 | 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] | 8450 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8451 | } |
| 8452 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8453 | /* This function change the nice of the currently executed |
| 8454 | * task. It is used set low or high priority at the current |
| 8455 | * task. |
| 8456 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8457 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8458 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8459 | struct hlua *hlua; |
| 8460 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8461 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8462 | MAY_LJMP(check_args(L, 1, "set_nice")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8463 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8464 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 8465 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 8466 | hlua = hlua_gethlua(L); |
| 8467 | |
| 8468 | /* If the task is not set, I'm in a start mode. */ |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8469 | if (!hlua || !hlua->task) |
| 8470 | return 0; |
| 8471 | |
| 8472 | if (nice < -1024) |
| 8473 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8474 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 8475 | nice = 1024; |
| 8476 | |
| 8477 | hlua->task->nice = nice; |
| 8478 | return 0; |
| 8479 | } |
| 8480 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8481 | /* 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] | 8482 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 8483 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 8484 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8485 | * |
| 8486 | * Task wrapper are longjmp safe because the only one Lua code |
| 8487 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8488 | */ |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 8489 | 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] | 8490 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 8491 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8492 | enum hlua_exec status; |
| 8493 | |
Willy Tarreau | 6ef52f4 | 2022-06-15 14:19:48 +0200 | [diff] [blame] | 8494 | if (task->tid < 0) |
| 8495 | task->tid = tid; |
| 8496 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8497 | /* If it is the first call to the task, we must initialize the |
| 8498 | * execution timeouts. |
| 8499 | */ |
| 8500 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 8501 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8502 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8503 | /* Execute the Lua code. */ |
| 8504 | status = hlua_ctx_resume(hlua, 1); |
| 8505 | |
| 8506 | switch (status) { |
| 8507 | /* finished or yield */ |
| 8508 | case HLUA_E_OK: |
| 8509 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8510 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 8511 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8512 | break; |
| 8513 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 8514 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 8515 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 8516 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8517 | break; |
| 8518 | |
| 8519 | /* finished with error. */ |
| 8520 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8521 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8522 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8523 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8524 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8525 | break; |
| 8526 | |
| 8527 | case HLUA_E_ERR: |
| 8528 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8529 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8530 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 8531 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8532 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8533 | break; |
| 8534 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 8535 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8536 | } |
| 8537 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8538 | /* This function is an LUA binding that register LUA function to be |
| 8539 | * executed after the HAProxy configuration parsing and before the |
| 8540 | * HAProxy scheduler starts. This function expect only one LUA |
| 8541 | * argument that is a function. This function returns nothing, but |
| 8542 | * throws if an error is encountered. |
| 8543 | */ |
| 8544 | __LJMP static int hlua_register_init(lua_State *L) |
| 8545 | { |
| 8546 | struct hlua_init_function *init; |
| 8547 | int ref; |
| 8548 | |
| 8549 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 8550 | |
| 8551 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8552 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8553 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8554 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 8555 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8556 | |
| 8557 | init->function_ref = ref; |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 8558 | LIST_APPEND(&hlua_init_functions[hlua_state_id], &init->l); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8559 | return 0; |
| 8560 | } |
| 8561 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8562 | /* This functio is an LUA binding. It permits to register a task |
| 8563 | * executed in parallel of the main HAroxy activity. The task is |
| 8564 | * created and it is set in the HAProxy scheduler. It can be called |
| 8565 | * from the "init" section, "post init" or during the runtime. |
| 8566 | * |
| 8567 | * Lua prototype: |
| 8568 | * |
| 8569 | * <none> core.register_task(<function>) |
| 8570 | */ |
| 8571 | static int hlua_register_task(lua_State *L) |
| 8572 | { |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8573 | struct hlua *hlua = NULL; |
| 8574 | struct task *task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8575 | int ref; |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8576 | int state_id; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8577 | |
| 8578 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 8579 | |
| 8580 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 8581 | |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8582 | /* Get the reference state. If the reference is NULL, L is the master |
| 8583 | * state, otherwise hlua->T is. |
| 8584 | */ |
| 8585 | hlua = hlua_gethlua(L); |
| 8586 | if (hlua) |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8587 | /* we are in runtime processing */ |
| 8588 | state_id = hlua->state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8589 | else |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8590 | /* we are in initialization mode */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8591 | state_id = hlua_state_id; |
Thierry Fournier | 75fc029 | 2020-11-28 13:18:56 +0100 | [diff] [blame] | 8592 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 8593 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8594 | if (!hlua) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8595 | goto alloc_error; |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8596 | HLUA_INIT(hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8597 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8598 | /* We are in the common lua state, execute the task anywhere, |
| 8599 | * otherwise, inherit the current thread identifier |
| 8600 | */ |
| 8601 | if (state_id == 0) |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8602 | task = task_new_anywhere(); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8603 | else |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 8604 | task = task_new_here(); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8605 | if (!task) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8606 | goto alloc_error; |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 8607 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8608 | task->context = hlua; |
| 8609 | task->process = hlua_process_task; |
| 8610 | |
Thierry Fournier | 021d986 | 2020-11-28 23:42:03 +0100 | [diff] [blame] | 8611 | if (!hlua_ctx_init(hlua, state_id, task, 1)) |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8612 | goto alloc_error; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8613 | |
| 8614 | /* Restore the function in the stack. */ |
| 8615 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 8616 | hlua->nargs = 0; |
| 8617 | |
| 8618 | /* Schedule task. */ |
Willy Tarreau | e3957f8 | 2021-09-30 16:17:37 +0200 | [diff] [blame] | 8619 | task_wakeup(task, TASK_WOKEN_INIT); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8620 | |
| 8621 | return 0; |
Christopher Faulet | 5294ec0 | 2021-04-12 12:24:47 +0200 | [diff] [blame] | 8622 | |
| 8623 | alloc_error: |
| 8624 | task_destroy(task); |
| 8625 | hlua_ctx_destroy(hlua); |
| 8626 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8627 | return 0; /* Never reached */ |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8628 | } |
| 8629 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8630 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 8631 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 8632 | * resume converters. |
| 8633 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8634 | 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] | 8635 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8636 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8637 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8638 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8639 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8640 | if (!stream) |
| 8641 | return 0; |
| 8642 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8643 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8644 | * Lua context can be not initialized. This behavior |
| 8645 | * permits to save performances because a systematic |
| 8646 | * Lua initialization cause 5% performances loss. |
| 8647 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8648 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8649 | struct hlua *hlua; |
| 8650 | |
| 8651 | hlua = pool_alloc(pool_head_hlua); |
| 8652 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8653 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8654 | return 0; |
| 8655 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8656 | HLUA_INIT(hlua); |
| 8657 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8658 | 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] | 8659 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 8660 | return 0; |
| 8661 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8662 | } |
| 8663 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8664 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8665 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8666 | |
| 8667 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8668 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8669 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8670 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8671 | else |
| 8672 | error = "critical error"; |
| 8673 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8674 | return 0; |
| 8675 | } |
| 8676 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8677 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8678 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8679 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8680 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8681 | return 0; |
| 8682 | } |
| 8683 | |
| 8684 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8685 | 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] | 8686 | |
| 8687 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8688 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8689 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8690 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8691 | return 0; |
| 8692 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8693 | hlua_smp2lua(stream->hlua->T, smp); |
| 8694 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8695 | |
| 8696 | /* push keywords in the stack. */ |
| 8697 | if (arg_p) { |
| 8698 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8699 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8700 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8701 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8702 | return 0; |
| 8703 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8704 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8705 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8706 | } |
| 8707 | } |
| 8708 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8709 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8710 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8711 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8712 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8713 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8714 | } |
| 8715 | |
| 8716 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8717 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8718 | /* finished. */ |
| 8719 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8720 | /* If the stack is empty, the function fails. */ |
| 8721 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8722 | return 0; |
| 8723 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8724 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8725 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8726 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8727 | return 1; |
| 8728 | |
| 8729 | /* yield. */ |
| 8730 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8731 | 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] | 8732 | return 0; |
| 8733 | |
| 8734 | /* finished with error. */ |
| 8735 | case HLUA_E_ERRMSG: |
| 8736 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8737 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8738 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8739 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8740 | return 0; |
| 8741 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8742 | case HLUA_E_ETMOUT: |
| 8743 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 8744 | return 0; |
| 8745 | |
| 8746 | case HLUA_E_NOMEM: |
| 8747 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 8748 | return 0; |
| 8749 | |
| 8750 | case HLUA_E_YIELD: |
| 8751 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 8752 | return 0; |
| 8753 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8754 | case HLUA_E_ERR: |
| 8755 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8756 | 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] | 8757 | /* fall through */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8758 | |
| 8759 | default: |
| 8760 | return 0; |
| 8761 | } |
| 8762 | } |
| 8763 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8764 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 8765 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8766 | * resume sample-fetches. This function will be called by the sample |
| 8767 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8768 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 8769 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 8770 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8771 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 8772 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 8773 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8774 | const char *error; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 8775 | unsigned int hflags = HLUA_TXN_NOTERM | HLUA_TXN_SMP_CTX; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8776 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 8777 | if (!stream) |
| 8778 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 8779 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 8780 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8781 | * Lua context can be not initialized. This behavior |
| 8782 | * permits to save performances because a systematic |
| 8783 | * Lua initialization cause 5% performances loss. |
| 8784 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8785 | if (!stream->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8786 | struct hlua *hlua; |
| 8787 | |
| 8788 | hlua = pool_alloc(pool_head_hlua); |
| 8789 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8790 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8791 | return 0; |
| 8792 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 8793 | hlua->T = NULL; |
| 8794 | stream->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8795 | 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] | 8796 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 8797 | return 0; |
| 8798 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 8799 | } |
| 8800 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8801 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8802 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8803 | |
| 8804 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8805 | if (!SET_SAFE_LJMP(stream->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8806 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 8807 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8808 | else |
| 8809 | error = "critical error"; |
| 8810 | 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] | 8811 | return 0; |
| 8812 | } |
| 8813 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8814 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8815 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8816 | 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] | 8817 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8818 | return 0; |
| 8819 | } |
| 8820 | |
| 8821 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8822 | 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] | 8823 | |
| 8824 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 8825 | 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] | 8826 | 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] | 8827 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8828 | return 0; |
| 8829 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8830 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8831 | |
| 8832 | /* push keywords in the stack. */ |
| 8833 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 8834 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8835 | if (!lua_checkstack(stream->hlua->T, 1)) { |
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 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8840 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 8841 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8842 | } |
| 8843 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8844 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8845 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 8846 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8847 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 8848 | RESET_SAFE_LJMP(stream->hlua); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8849 | } |
| 8850 | |
| 8851 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8852 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8853 | /* finished. */ |
| 8854 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 8855 | /* If the stack is empty, the function fails. */ |
| 8856 | if (lua_gettop(stream->hlua->T) <= 0) |
| 8857 | return 0; |
| 8858 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8859 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8860 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 8861 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8862 | |
| 8863 | /* Set the end of execution flag. */ |
| 8864 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 8865 | return 1; |
| 8866 | |
| 8867 | /* yield. */ |
| 8868 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8869 | 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] | 8870 | return 0; |
| 8871 | |
| 8872 | /* finished with error. */ |
| 8873 | case HLUA_E_ERRMSG: |
| 8874 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8875 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 8876 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 8877 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8878 | return 0; |
| 8879 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8880 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8881 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 8882 | return 0; |
| 8883 | |
| 8884 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8885 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 8886 | return 0; |
| 8887 | |
| 8888 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 8889 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 8890 | return 0; |
| 8891 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8892 | case HLUA_E_ERR: |
| 8893 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 8894 | 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] | 8895 | /* fall through */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8896 | |
| 8897 | default: |
| 8898 | return 0; |
| 8899 | } |
| 8900 | } |
| 8901 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8902 | /* This function is an LUA binding used for registering |
| 8903 | * "sample-conv" functions. It expects a converter name used |
| 8904 | * in the haproxy configuration file, and an LUA function. |
| 8905 | */ |
| 8906 | __LJMP static int hlua_register_converters(lua_State *L) |
| 8907 | { |
| 8908 | struct sample_conv_kw_list *sck; |
| 8909 | const char *name; |
| 8910 | int ref; |
| 8911 | int len; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8912 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8913 | struct sample_conv *sc; |
| 8914 | struct buffer *trash; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8915 | |
| 8916 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 8917 | |
| 8918 | /* First argument : converter name. */ |
| 8919 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8920 | |
| 8921 | /* Second argument : lua function. */ |
| 8922 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 8923 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8924 | /* Check if the converter is already registered */ |
| 8925 | trash = get_trash_chunk(); |
| 8926 | chunk_printf(trash, "lua.%s", name); |
| 8927 | sc = find_sample_conv(trash->area, trash->data); |
| 8928 | if (sc != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 8929 | fcn = sc->private; |
| 8930 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 8931 | ha_warning("Trying to register converter 'lua.%s' more than once. " |
| 8932 | "This will become a hard error in version 2.5.\n", name); |
| 8933 | } |
| 8934 | fcn->function_ref[hlua_state_id] = ref; |
| 8935 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8936 | } |
| 8937 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8938 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8939 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8940 | if (!sck) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8941 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 8942 | fcn = new_hlua_function(); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8943 | if (!fcn) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8944 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8945 | |
| 8946 | /* Fill fcn. */ |
| 8947 | fcn->name = strdup(name); |
| 8948 | if (!fcn->name) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8949 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 8950 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8951 | |
| 8952 | /* List head */ |
| 8953 | sck->list.n = sck->list.p = NULL; |
| 8954 | |
| 8955 | /* converter keyword. */ |
| 8956 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 8957 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8958 | if (!sck->kw[0].kw) |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8959 | goto alloc_error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8960 | |
| 8961 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 8962 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 8963 | 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] | 8964 | sck->kw[0].val_args = NULL; |
| 8965 | sck->kw[0].in_type = SMP_T_STR; |
| 8966 | sck->kw[0].out_type = SMP_T_STR; |
| 8967 | sck->kw[0].private = fcn; |
| 8968 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8969 | /* Register this new converter */ |
| 8970 | sample_register_convs(sck); |
| 8971 | |
| 8972 | return 0; |
Christopher Faulet | aa22430 | 2021-04-12 14:08:21 +0200 | [diff] [blame] | 8973 | |
| 8974 | alloc_error: |
| 8975 | release_hlua_function(fcn); |
| 8976 | ha_free(&sck); |
| 8977 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 8978 | return 0; /* Never reached */ |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8979 | } |
| 8980 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8981 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8982 | * "sample-fetch" functions. It expects a converter name used |
| 8983 | * in the haproxy configuration file, and an LUA function. |
| 8984 | */ |
| 8985 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 8986 | { |
| 8987 | const char *name; |
| 8988 | int ref; |
| 8989 | int len; |
| 8990 | struct sample_fetch_kw_list *sfk; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 8991 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 8992 | struct sample_fetch *sf; |
| 8993 | struct buffer *trash; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8994 | |
| 8995 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 8996 | |
| 8997 | /* First argument : sample-fetch name. */ |
| 8998 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 8999 | |
| 9000 | /* Second argument : lua function. */ |
| 9001 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 9002 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9003 | /* Check if the sample-fetch is already registered */ |
| 9004 | trash = get_trash_chunk(); |
| 9005 | chunk_printf(trash, "lua.%s", name); |
| 9006 | sf = find_sample_fetch(trash->area, trash->data); |
| 9007 | if (sf != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9008 | fcn = sf->private; |
| 9009 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9010 | ha_warning("Trying to register sample-fetch 'lua.%s' more than once. " |
| 9011 | "This will become a hard error in version 2.5.\n", name); |
| 9012 | } |
| 9013 | fcn->function_ref[hlua_state_id] = ref; |
| 9014 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9015 | } |
| 9016 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9017 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9018 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9019 | if (!sfk) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9020 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9021 | fcn = new_hlua_function(); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9022 | if (!fcn) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9023 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9024 | |
| 9025 | /* Fill fcn. */ |
| 9026 | fcn->name = strdup(name); |
| 9027 | if (!fcn->name) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9028 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9029 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9030 | |
| 9031 | /* List head */ |
| 9032 | sfk->list.n = sfk->list.p = NULL; |
| 9033 | |
| 9034 | /* sample-fetch keyword. */ |
| 9035 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9036 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9037 | if (!sfk->kw[0].kw) |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9038 | goto alloc_error; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9039 | |
| 9040 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 9041 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 9042 | 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] | 9043 | sfk->kw[0].val_args = NULL; |
| 9044 | sfk->kw[0].out_type = SMP_T_STR; |
| 9045 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 9046 | sfk->kw[0].val = 0; |
| 9047 | sfk->kw[0].private = fcn; |
| 9048 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9049 | /* Register this new fetch. */ |
| 9050 | sample_register_fetches(sfk); |
| 9051 | |
| 9052 | return 0; |
Christopher Faulet | 2567f18 | 2021-04-12 14:11:50 +0200 | [diff] [blame] | 9053 | |
| 9054 | alloc_error: |
| 9055 | release_hlua_function(fcn); |
| 9056 | ha_free(&sfk); |
| 9057 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9058 | return 0; /* Never reached */ |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 9059 | } |
| 9060 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9061 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9062 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 9063 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9064 | { |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9065 | struct hlua *hlua; |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9066 | unsigned int delay; |
| 9067 | unsigned int wakeup_ms; |
| 9068 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 9069 | /* Get hlua struct, or NULL if we execute from main lua state */ |
| 9070 | hlua = hlua_gethlua(L); |
| 9071 | if (!hlua) { |
| 9072 | return 0; |
| 9073 | } |
| 9074 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9075 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 9076 | |
| 9077 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 9078 | wakeup_ms = tick_add(now_ms, delay); |
| 9079 | hlua->wake_time = wakeup_ms; |
| 9080 | return 0; |
| 9081 | } |
| 9082 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9083 | /* This function is a wrapper to execute each LUA function declared as an action |
| 9084 | * wrapper during the initialisation period. This function may return any |
| 9085 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 9086 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 9087 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9088 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9089 | 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] | 9090 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9091 | { |
| 9092 | char **arg; |
Christopher Faulet | 8c9e6bb | 2021-08-06 16:29:41 +0200 | [diff] [blame] | 9093 | unsigned int hflags = HLUA_TXN_ACT_CTX; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9094 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9095 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9096 | |
| 9097 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 9098 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 9099 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 9100 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 9101 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9102 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9103 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9104 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9105 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9106 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9107 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9108 | * Lua context can be not initialized. This behavior |
| 9109 | * permits to save performances because a systematic |
| 9110 | * Lua initialization cause 5% performances loss. |
| 9111 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9112 | if (!s->hlua) { |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9113 | struct hlua *hlua; |
| 9114 | |
| 9115 | hlua = pool_alloc(pool_head_hlua); |
| 9116 | if (!hlua) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9117 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9118 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9119 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9120 | } |
Christopher Faulet | 1e8433f | 2021-03-24 15:03:01 +0100 | [diff] [blame] | 9121 | HLUA_INIT(hlua); |
| 9122 | s->hlua = hlua; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9123 | 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] | 9124 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9125 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9126 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9127 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 9128 | } |
| 9129 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9130 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9131 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9132 | |
| 9133 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9134 | if (!SET_SAFE_LJMP(s->hlua)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9135 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 9136 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9137 | else |
| 9138 | error = "critical error"; |
| 9139 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9140 | rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9141 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9142 | } |
| 9143 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9144 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9145 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9146 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9147 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9148 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9149 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9150 | } |
| 9151 | |
| 9152 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9153 | 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] | 9154 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 9155 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 9156 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9157 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9158 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9159 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9160 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9161 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9162 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9163 | |
| 9164 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9165 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9166 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9167 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9168 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9169 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9170 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9171 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9172 | lua_pushstring(s->hlua->T, *arg); |
| 9173 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9174 | } |
| 9175 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9176 | /* Now the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9177 | RESET_SAFE_LJMP(s->hlua); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9178 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 9179 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9180 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9181 | } |
| 9182 | |
| 9183 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 9184 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9185 | /* finished. */ |
| 9186 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9187 | /* Catch the return value */ |
| 9188 | if (lua_gettop(s->hlua->T) > 0) |
| 9189 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9190 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9191 | /* Set timeout in the required channel. */ |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9192 | if (act_ret == ACT_RET_YIELD) { |
| 9193 | if (flags & ACT_OPT_FINAL) |
| 9194 | goto err_yield; |
| 9195 | |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9196 | if (dir == SMP_OPT_DIR_REQ) |
| 9197 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9198 | s->hlua->wake_time); |
| 9199 | else |
| 9200 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9201 | s->hlua->wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 9202 | } |
| 9203 | goto end; |
| 9204 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9205 | /* yield. */ |
| 9206 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 9207 | /* Set timeout in the required channel. */ |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9208 | if (dir == SMP_OPT_DIR_REQ) |
| 9209 | s->req.analyse_exp = tick_first((tick_is_expired(s->req.analyse_exp, now_ms) ? 0 : s->req.analyse_exp), |
| 9210 | s->hlua->wake_time); |
| 9211 | else |
| 9212 | s->res.analyse_exp = tick_first((tick_is_expired(s->res.analyse_exp, now_ms) ? 0 : s->res.analyse_exp), |
| 9213 | s->hlua->wake_time); |
| 9214 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9215 | /* Some actions can be wake up when a "write" event |
| 9216 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9217 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9218 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 9219 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9220 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9221 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 9222 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9223 | act_ret = ACT_RET_YIELD; |
| 9224 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9225 | |
| 9226 | /* finished with error. */ |
| 9227 | case HLUA_E_ERRMSG: |
| 9228 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9229 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9230 | rule->arg.hlua_rule->fcn->name, lua_tostring(s->hlua->T, -1)); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 9231 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9232 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9233 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9234 | case HLUA_E_ETMOUT: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9235 | 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] | 9236 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9237 | |
| 9238 | case HLUA_E_NOMEM: |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9239 | 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] | 9240 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9241 | |
| 9242 | case HLUA_E_YIELD: |
Christopher Faulet | 498c483 | 2020-07-28 11:59:58 +0200 | [diff] [blame] | 9243 | err_yield: |
| 9244 | act_ret = ACT_RET_CONT; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9245 | 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] | 9246 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9247 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9248 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9249 | case HLUA_E_ERR: |
| 9250 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 9251 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9252 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9253 | |
| 9254 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9255 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9256 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9257 | |
| 9258 | end: |
Christopher Faulet | 2361fd9 | 2020-07-30 10:40:58 +0200 | [diff] [blame] | 9259 | if (act_ret != ACT_RET_YIELD && s->hlua) |
Christopher Faulet | 8f587ea | 2020-07-28 12:01:55 +0200 | [diff] [blame] | 9260 | s->hlua->wake_time = TICK_ETERNITY; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 9261 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9262 | } |
| 9263 | |
Willy Tarreau | 144f84a | 2021-03-02 16:09:26 +0100 | [diff] [blame] | 9264 | 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] | 9265 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 9266 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9267 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9268 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 9269 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 9270 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9271 | } |
| 9272 | |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9273 | static int hlua_applet_tcp_init(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9274 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9275 | 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] | 9276 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9277 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9278 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9279 | struct task *task; |
| 9280 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9281 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9282 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9283 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9284 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9285 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9286 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9287 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9288 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9289 | HLUA_INIT(hlua); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9290 | tcp_ctx->hlua = hlua; |
| 9291 | tcp_ctx->flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9292 | |
| 9293 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9294 | task = task_new_here(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9295 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9296 | SEND_ERR(strm->be, "Lua applet tcp '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9297 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9298 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9299 | } |
| 9300 | task->nice = 0; |
| 9301 | task->context = ctx; |
| 9302 | task->process = hlua_applet_wakeup; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9303 | tcp_ctx->task = task; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9304 | |
| 9305 | /* In the execution wrappers linked with a stream, the |
| 9306 | * Lua context can be not initialized. This behavior |
| 9307 | * permits to save performances because a systematic |
| 9308 | * Lua initialization cause 5% performances loss. |
| 9309 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9310 | 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] | 9311 | 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] | 9312 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9313 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9314 | } |
| 9315 | |
| 9316 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9317 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9318 | |
| 9319 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9320 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9321 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9322 | error = lua_tostring(hlua->T, -1); |
| 9323 | else |
| 9324 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9325 | SEND_ERR(strm->be, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9326 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9327 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9328 | } |
| 9329 | |
| 9330 | /* Check stack available size. */ |
| 9331 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9332 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9333 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9334 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9335 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9336 | } |
| 9337 | |
| 9338 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9339 | 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] | 9340 | |
| 9341 | /* Create and and push object stream in the stack. */ |
| 9342 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9343 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9344 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9345 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9346 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9347 | } |
| 9348 | hlua->nargs = 1; |
| 9349 | |
| 9350 | /* push keywords in the stack. */ |
| 9351 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9352 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9353 | SEND_ERR(strm->be, "Lua applet tcp '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9354 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9355 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9356 | return -1; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9357 | } |
| 9358 | lua_pushstring(hlua->T, *arg); |
| 9359 | hlua->nargs++; |
| 9360 | } |
| 9361 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9362 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9363 | |
| 9364 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9365 | applet_need_more_data(ctx); |
Willy Tarreau | 4164eb9 | 2022-05-25 15:42:03 +0200 | [diff] [blame] | 9366 | applet_have_more_data(ctx); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9367 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9368 | return 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9369 | } |
| 9370 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9371 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9372 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9373 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9374 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9375 | struct stream *strm = __sc_strm(sc); |
| 9376 | struct channel *res = sc_ic(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9377 | struct act_rule *rule = ctx->rule; |
| 9378 | struct proxy *px = strm->be; |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9379 | struct hlua *hlua = tcp_ctx->hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9380 | |
| 9381 | /* The applet execution is already done. */ |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9382 | if (tcp_ctx->flags & APPLET_DONE) { |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9383 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9384 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9385 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 9386 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9387 | |
| 9388 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9389 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9390 | return; |
| 9391 | |
| 9392 | /* Execute the function. */ |
| 9393 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9394 | /* finished. */ |
| 9395 | case HLUA_E_OK: |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9396 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9397 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9398 | /* eat the whole request */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9399 | co_skip(sc_oc(sc), co_data(sc_oc(sc))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9400 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9401 | sc_shutr(sc); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9402 | return; |
| 9403 | |
| 9404 | /* yield. */ |
| 9405 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 9406 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9407 | task_schedule(tcp_ctx->task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9408 | return; |
| 9409 | |
| 9410 | /* finished with error. */ |
| 9411 | case HLUA_E_ERRMSG: |
| 9412 | /* Display log. */ |
| 9413 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9414 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9415 | lua_pop(hlua->T, 1); |
| 9416 | goto error; |
| 9417 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9418 | case HLUA_E_ETMOUT: |
| 9419 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9420 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9421 | goto error; |
| 9422 | |
| 9423 | case HLUA_E_NOMEM: |
| 9424 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9425 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9426 | goto error; |
| 9427 | |
| 9428 | case HLUA_E_YIELD: /* unexpected */ |
| 9429 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9430 | rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 9431 | goto error; |
| 9432 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9433 | case HLUA_E_ERR: |
| 9434 | /* Display log. */ |
| 9435 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9436 | rule->arg.hlua_rule->fcn->name); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9437 | goto error; |
| 9438 | |
| 9439 | default: |
| 9440 | goto error; |
| 9441 | } |
| 9442 | |
| 9443 | error: |
| 9444 | |
| 9445 | /* For all other cases, just close the stream. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9446 | sc_shutw(sc); |
| 9447 | sc_shutr(sc); |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9448 | tcp_ctx->flags |= APPLET_DONE; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9449 | } |
| 9450 | |
| 9451 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 9452 | { |
Willy Tarreau | e23f33b | 2022-05-06 14:07:13 +0200 | [diff] [blame] | 9453 | struct hlua_tcp_ctx *tcp_ctx = ctx->svcctx; |
| 9454 | |
| 9455 | task_destroy(tcp_ctx->task); |
| 9456 | tcp_ctx->task = NULL; |
| 9457 | hlua_ctx_destroy(tcp_ctx->hlua); |
| 9458 | tcp_ctx->hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9459 | } |
| 9460 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9461 | /* The function returns 0 if the initialisation is complete or -1 if |
| 9462 | * 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] | 9463 | */ |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9464 | static int hlua_applet_http_init(struct appctx *ctx) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9465 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9466 | 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] | 9467 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9468 | struct stream *strm = __sc_strm(sc); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9469 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9470 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9471 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9472 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9473 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9474 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9475 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 9476 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9477 | if (!hlua) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9478 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9479 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9480 | return -1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 9481 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9482 | HLUA_INIT(hlua); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9483 | http_ctx->hlua = hlua; |
| 9484 | http_ctx->left_bytes = -1; |
| 9485 | http_ctx->flags = 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9486 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9487 | if (txn->req.flags & HTTP_MSGF_VER_11) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9488 | http_ctx->flags |= APPLET_HTTP11; |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 9489 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9490 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | beeabf5 | 2021-10-01 18:23:30 +0200 | [diff] [blame] | 9491 | task = task_new_here(); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9492 | if (!task) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9493 | SEND_ERR(strm->be, "Lua applet http '%s': out of memory.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9494 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9495 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9496 | } |
| 9497 | task->nice = 0; |
| 9498 | task->context = ctx; |
| 9499 | task->process = hlua_applet_wakeup; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9500 | http_ctx->task = task; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9501 | |
| 9502 | /* In the execution wrappers linked with a stream, the |
| 9503 | * Lua context can be not initialized. This behavior |
| 9504 | * permits to save performances because a systematic |
| 9505 | * Lua initialization cause 5% performances loss. |
| 9506 | */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9507 | 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] | 9508 | 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] | 9509 | ctx->rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9510 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9511 | } |
| 9512 | |
| 9513 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 9514 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9515 | |
| 9516 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9517 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 9518 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 9519 | error = lua_tostring(hlua->T, -1); |
| 9520 | else |
| 9521 | error = "critical error"; |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9522 | SEND_ERR(strm->be, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9523 | ctx->rule->arg.hlua_rule->fcn->name, error); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9524 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9525 | } |
| 9526 | |
| 9527 | /* Check stack available size. */ |
| 9528 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9529 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9530 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9531 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9532 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9533 | } |
| 9534 | |
| 9535 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9536 | 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] | 9537 | |
| 9538 | /* Create and and push object stream in the stack. */ |
| 9539 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9540 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9541 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9542 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9543 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9544 | } |
| 9545 | hlua->nargs = 1; |
| 9546 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9547 | /* push keywords in the stack. */ |
| 9548 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 9549 | if (!lua_checkstack(hlua->T, 1)) { |
Christopher Faulet | 4aa1d28 | 2022-01-13 16:01:35 +0100 | [diff] [blame] | 9550 | SEND_ERR(strm->be, "Lua applet http '%s': full stack.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9551 | ctx->rule->arg.hlua_rule->fcn->name); |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9552 | RESET_SAFE_LJMP(hlua); |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9553 | return -1; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9554 | } |
| 9555 | lua_pushstring(hlua->T, *arg); |
| 9556 | hlua->nargs++; |
| 9557 | } |
| 9558 | |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 9559 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9560 | |
| 9561 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9562 | applet_need_more_data(ctx); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9563 | |
Christopher Faulet | c992938 | 2022-05-12 11:52:27 +0200 | [diff] [blame] | 9564 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9565 | } |
| 9566 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 9567 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9568 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9569 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 9570 | struct stconn *sc = appctx_sc(ctx); |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9571 | struct stream *strm = __sc_strm(sc); |
| 9572 | struct channel *req = sc_oc(sc); |
| 9573 | struct channel *res = sc_ic(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9574 | struct act_rule *rule = ctx->rule; |
| 9575 | struct proxy *px = strm->be; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9576 | struct hlua *hlua = http_ctx->hlua; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9577 | struct htx *req_htx, *res_htx; |
| 9578 | |
| 9579 | res_htx = htx_from_buf(&res->buf); |
| 9580 | |
| 9581 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9582 | if (unlikely(sc->state == SC_ST_DIS || sc->state == SC_ST_CLO)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9583 | goto out; |
| 9584 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9585 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9586 | if (!b_size(&res->buf)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9587 | sc_need_room(sc); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9588 | goto out; |
| 9589 | } |
| 9590 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9591 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9592 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9593 | |
| 9594 | /* Set the currently running flag. */ |
| 9595 | if (!HLUA_IS_RUNNING(hlua) && |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9596 | !(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | bd878d2 | 2021-04-28 10:50:21 +0200 | [diff] [blame] | 9597 | if (!co_data(req)) { |
Willy Tarreau | 90e8b45 | 2022-05-25 18:21:43 +0200 | [diff] [blame] | 9598 | applet_need_more_data(ctx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9599 | goto out; |
| 9600 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9601 | } |
| 9602 | |
| 9603 | /* Executes The applet if it is not done. */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9604 | if (!(http_ctx->flags & APPLET_DONE)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9605 | |
| 9606 | /* Execute the function. */ |
| 9607 | switch (hlua_ctx_resume(hlua, 1)) { |
| 9608 | /* finished. */ |
| 9609 | case HLUA_E_OK: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9610 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9611 | break; |
| 9612 | |
| 9613 | /* yield. */ |
| 9614 | case HLUA_E_AGAIN: |
| 9615 | if (hlua->wake_time != TICK_ETERNITY) |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9616 | task_schedule(http_ctx->task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9617 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9618 | |
| 9619 | /* finished with error. */ |
| 9620 | case HLUA_E_ERRMSG: |
| 9621 | /* Display log. */ |
| 9622 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9623 | rule->arg.hlua_rule->fcn->name, lua_tostring(hlua->T, -1)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9624 | lua_pop(hlua->T, 1); |
| 9625 | goto error; |
| 9626 | |
| 9627 | case HLUA_E_ETMOUT: |
| 9628 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9629 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9630 | goto error; |
| 9631 | |
| 9632 | case HLUA_E_NOMEM: |
| 9633 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9634 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9635 | goto error; |
| 9636 | |
| 9637 | case HLUA_E_YIELD: /* unexpected */ |
| 9638 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9639 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9640 | goto error; |
| 9641 | |
| 9642 | case HLUA_E_ERR: |
| 9643 | /* Display log. */ |
| 9644 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9645 | rule->arg.hlua_rule->fcn->name); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9646 | goto error; |
| 9647 | |
| 9648 | default: |
| 9649 | goto error; |
| 9650 | } |
| 9651 | } |
| 9652 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9653 | if (http_ctx->flags & APPLET_DONE) { |
| 9654 | if (http_ctx->flags & APPLET_RSP_SENT) |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9655 | goto done; |
| 9656 | |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9657 | if (!(http_ctx->flags & APPLET_HDR_SENT)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9658 | goto error; |
| 9659 | |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9660 | /* no more data are expected. If the response buffer is empty |
| 9661 | * for a chunked message, be sure to add something (EOT block in |
| 9662 | * this case) to have something to send. It is important to be |
| 9663 | * sure the EOM flags will be handled by the endpoint. |
| 9664 | */ |
| 9665 | if (htx_is_empty(res_htx) && (strm->txn->rsp.flags & (HTTP_MSGF_XFER_LEN|HTTP_MSGF_CNT_LEN)) == HTTP_MSGF_XFER_LEN) { |
| 9666 | if (!htx_add_endof(res_htx, HTX_BLK_EOT)) { |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9667 | sc_need_room(sc); |
Christopher Faulet | f89af9c | 2022-04-07 10:07:18 +0200 | [diff] [blame] | 9668 | goto out; |
| 9669 | } |
| 9670 | channel_add_input(res, 1); |
| 9671 | } |
| 9672 | |
Christopher Faulet | d1ac2b9 | 2020-12-02 19:12:22 +0100 | [diff] [blame] | 9673 | res_htx->flags |= HTX_FL_EOM; |
Christopher Faulet | d8d2708 | 2022-03-07 15:50:54 +0100 | [diff] [blame] | 9674 | res->flags |= CF_EOI; |
Willy Tarreau | d869e13 | 2022-05-17 18:05:31 +0200 | [diff] [blame] | 9675 | se_fl_set(ctx->sedesc, SE_FL_EOI); |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9676 | strm->txn->status = http_ctx->status; |
| 9677 | http_ctx->flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9678 | } |
| 9679 | |
| 9680 | done: |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9681 | if (http_ctx->flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9682 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9683 | res->flags |= CF_READ_NULL; |
Willy Tarreau | 3e7be36 | 2022-05-27 10:35:27 +0200 | [diff] [blame] | 9684 | sc_shutr(sc); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 9685 | } |
| 9686 | |
| 9687 | /* eat the whole request */ |
| 9688 | if (co_data(req)) { |
| 9689 | req_htx = htx_from_buf(&req->buf); |
| 9690 | co_htx_skip(req, req_htx, co_data(req)); |
| 9691 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9692 | } |
| 9693 | } |
| 9694 | |
| 9695 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9696 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9697 | return; |
| 9698 | |
| 9699 | error: |
| 9700 | |
| 9701 | /* If we are in HTTP mode, and we are not send any |
| 9702 | * data, return a 500 server error in best effort: |
| 9703 | * if there is no room available in the buffer, |
| 9704 | * just close the connection. |
| 9705 | */ |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9706 | if (!(http_ctx->flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 9707 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9708 | |
| 9709 | channel_erase(res); |
| 9710 | res->buf.data = b_data(err); |
| 9711 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 9712 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 9713 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9714 | } |
| 9715 | if (!(strm->flags & SF_ERR_MASK)) |
| 9716 | strm->flags |= SF_ERR_RESOURCE; |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9717 | http_ctx->flags |= APPLET_DONE; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 9718 | goto done; |
| 9719 | } |
| 9720 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9721 | static void hlua_applet_http_release(struct appctx *ctx) |
| 9722 | { |
Willy Tarreau | aa229cc | 2022-05-06 14:26:10 +0200 | [diff] [blame] | 9723 | struct hlua_http_ctx *http_ctx = ctx->svcctx; |
| 9724 | |
| 9725 | task_destroy(http_ctx->task); |
| 9726 | http_ctx->task = NULL; |
| 9727 | hlua_ctx_destroy(http_ctx->hlua); |
| 9728 | http_ctx->hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9729 | } |
| 9730 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9731 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9732 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 9733 | * |
| 9734 | * This function can fail with an abort() due to an Lua critical error. |
| 9735 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 9736 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9737 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9738 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 9739 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9740 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9741 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9742 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9743 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9744 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9745 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9746 | if (!rule->arg.hlua_rule) { |
| 9747 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9748 | goto error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9749 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9750 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9751 | /* Memory for arguments. */ |
Tim Duesterhus | e52b6e5 | 2020-09-12 20:26:43 +0200 | [diff] [blame] | 9752 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, |
| 9753 | sizeof(*rule->arg.hlua_rule->args)); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9754 | if (!rule->arg.hlua_rule->args) { |
| 9755 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9756 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9757 | } |
| 9758 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9759 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9760 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9761 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9762 | /* Expect some arguments */ |
| 9763 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9764 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9765 | memprintf(err, "expect %d arguments", fcn->nargs); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9766 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9767 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 9768 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9769 | if (!rule->arg.hlua_rule->args[i]) { |
| 9770 | memprintf(err, "out of memory error"); |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9771 | goto error; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9772 | } |
| 9773 | (*cur_arg)++; |
| 9774 | } |
| 9775 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9776 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 9777 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 9778 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 9779 | return ACT_RET_PRS_OK; |
Christopher Faulet | 528526f | 2021-04-12 14:37:32 +0200 | [diff] [blame] | 9780 | |
| 9781 | error: |
| 9782 | if (rule->arg.hlua_rule) { |
| 9783 | if (rule->arg.hlua_rule->args) { |
| 9784 | for (i = 0; i < fcn->nargs; i++) |
| 9785 | ha_free(&rule->arg.hlua_rule->args[i]); |
| 9786 | ha_free(&rule->arg.hlua_rule->args); |
| 9787 | } |
| 9788 | ha_free(&rule->arg.hlua_rule); |
| 9789 | } |
| 9790 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 9791 | } |
| 9792 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9793 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 9794 | struct act_rule *rule, char **err) |
| 9795 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9796 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9797 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9798 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9799 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9800 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 9801 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 9802 | * the call of this analyzer. |
| 9803 | */ |
| 9804 | if (rule->from != ACT_F_HTTP_REQ) { |
| 9805 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 9806 | return ACT_RET_PRS_ERR; |
| 9807 | } |
| 9808 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9809 | /* Memory for the rule. */ |
| 9810 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9811 | if (!rule->arg.hlua_rule) { |
| 9812 | memprintf(err, "out of memory error"); |
| 9813 | return ACT_RET_PRS_ERR; |
| 9814 | } |
| 9815 | |
| 9816 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9817 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 9818 | |
| 9819 | /* TODO: later accept arguments. */ |
| 9820 | rule->arg.hlua_rule->args = NULL; |
| 9821 | |
| 9822 | /* Add applet pointer in the rule. */ |
| 9823 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9824 | rule->applet.name = fcn->name; |
| 9825 | rule->applet.init = hlua_applet_http_init; |
| 9826 | rule->applet.fct = hlua_applet_http_fct; |
| 9827 | rule->applet.release = hlua_applet_http_release; |
| 9828 | rule->applet.timeout = hlua_timeout_applet; |
| 9829 | |
| 9830 | return ACT_RET_PRS_OK; |
| 9831 | } |
| 9832 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9833 | /* This function is an LUA binding used for registering |
| 9834 | * "sample-conv" functions. It expects a converter name used |
| 9835 | * in the haproxy configuration file, and an LUA function. |
| 9836 | */ |
| 9837 | __LJMP static int hlua_register_action(lua_State *L) |
| 9838 | { |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9839 | struct action_kw_list *akl = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9840 | const char *name; |
| 9841 | int ref; |
| 9842 | int len; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9843 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9844 | int nargs; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9845 | struct buffer *trash; |
| 9846 | struct action_kw *akw; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9847 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9848 | /* Initialise the number of expected arguments at 0. */ |
| 9849 | nargs = 0; |
| 9850 | |
| 9851 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 9852 | 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] | 9853 | |
| 9854 | /* First argument : converter name. */ |
| 9855 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 9856 | |
| 9857 | /* Second argument : environment. */ |
| 9858 | if (lua_type(L, 2) != LUA_TTABLE) |
| 9859 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9860 | |
| 9861 | /* Third argument : lua function. */ |
| 9862 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 9863 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9864 | /* 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] | 9865 | if (lua_gettop(L) >= 4) |
| 9866 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 9867 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 9868 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9869 | lua_pushnil(L); |
| 9870 | while (lua_next(L, 2) != 0) { |
| 9871 | if (lua_type(L, -1) != LUA_TSTRING) |
| 9872 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 9873 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9874 | /* Check if action exists */ |
| 9875 | trash = get_trash_chunk(); |
| 9876 | chunk_printf(trash, "lua.%s", name); |
| 9877 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) { |
| 9878 | akw = tcp_req_cont_action(trash->area); |
| 9879 | } else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) { |
| 9880 | akw = tcp_res_cont_action(trash->area); |
| 9881 | } else if (strcmp(lua_tostring(L, -1), "http-req") == 0) { |
| 9882 | akw = action_http_req_custom(trash->area); |
| 9883 | } else if (strcmp(lua_tostring(L, -1), "http-res") == 0) { |
| 9884 | akw = action_http_res_custom(trash->area); |
| 9885 | } else { |
| 9886 | akw = NULL; |
| 9887 | } |
| 9888 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 9889 | fcn = akw->private; |
| 9890 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 9891 | ha_warning("Trying to register action 'lua.%s' more than once. " |
| 9892 | "This will become a hard error in version 2.5.\n", name); |
| 9893 | } |
| 9894 | fcn->function_ref[hlua_state_id] = ref; |
| 9895 | |
| 9896 | /* pop the environment string. */ |
| 9897 | lua_pop(L, 1); |
| 9898 | continue; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 9899 | } |
| 9900 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9901 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 9902 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9903 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9904 | if (!akl) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9905 | goto alloc_error;; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 9906 | fcn = new_hlua_function(); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9907 | if (!fcn) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9908 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9909 | |
| 9910 | /* Fill fcn. */ |
| 9911 | fcn->name = strdup(name); |
| 9912 | if (!fcn->name) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9913 | goto alloc_error; |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 9914 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9915 | |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 9916 | /* Set the expected number of arguments. */ |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 9917 | fcn->nargs = nargs; |
| 9918 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9919 | /* List head */ |
| 9920 | akl->list.n = akl->list.p = NULL; |
| 9921 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9922 | /* action keyword. */ |
| 9923 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 9924 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9925 | if (!akl->kw[0].kw) |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9926 | goto alloc_error; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9927 | |
| 9928 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 9929 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 9930 | akl->kw[0].flags = 0; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9931 | akl->kw[0].private = fcn; |
| 9932 | akl->kw[0].parse = action_register_lua; |
| 9933 | |
| 9934 | /* select the action registering point. */ |
| 9935 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 9936 | tcp_req_cont_keywords_register(akl); |
| 9937 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 9938 | tcp_res_cont_keywords_register(akl); |
| 9939 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 9940 | http_req_keywords_register(akl); |
| 9941 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 9942 | http_res_keywords_register(akl); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9943 | else { |
| 9944 | release_hlua_function(fcn); |
| 9945 | if (akl) |
| 9946 | ha_free((char **)&(akl->kw[0].kw)); |
| 9947 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 9948 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9949 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 9950 | "are expected.", lua_tostring(L, -1))); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9951 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9952 | |
| 9953 | /* pop the environment string. */ |
| 9954 | lua_pop(L, 1); |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9955 | |
| 9956 | /* reset for next loop */ |
| 9957 | akl = NULL; |
| 9958 | fcn = NULL; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9959 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9960 | return ACT_RET_PRS_OK; |
Christopher Faulet | 4fc9da0 | 2021-04-12 15:08:12 +0200 | [diff] [blame] | 9961 | |
| 9962 | alloc_error: |
| 9963 | release_hlua_function(fcn); |
| 9964 | ha_free(&akl); |
| 9965 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 9966 | return 0; /* Never reached */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9967 | } |
| 9968 | |
| 9969 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 9970 | struct act_rule *rule, char **err) |
| 9971 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 9972 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9973 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 9974 | if (px->mode == PR_MODE_HTTP) { |
| 9975 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 9976 | return ACT_RET_PRS_ERR; |
| 9977 | } |
| 9978 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9979 | /* Memory for the rule. */ |
| 9980 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 9981 | if (!rule->arg.hlua_rule) { |
| 9982 | memprintf(err, "out of memory error"); |
| 9983 | return ACT_RET_PRS_ERR; |
| 9984 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 9985 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9986 | /* Reference the Lua function and store the reference. */ |
Thierry Fournier | ad5345f | 2020-11-29 02:05:57 +0100 | [diff] [blame] | 9987 | rule->arg.hlua_rule->fcn = fcn; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 9988 | |
| 9989 | /* TODO: later accept arguments. */ |
| 9990 | rule->arg.hlua_rule->args = NULL; |
| 9991 | |
| 9992 | /* Add applet pointer in the rule. */ |
| 9993 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 9994 | rule->applet.name = fcn->name; |
| 9995 | rule->applet.init = hlua_applet_tcp_init; |
| 9996 | rule->applet.fct = hlua_applet_tcp_fct; |
| 9997 | rule->applet.release = hlua_applet_tcp_release; |
| 9998 | rule->applet.timeout = hlua_timeout_applet; |
| 9999 | |
| 10000 | return 0; |
| 10001 | } |
| 10002 | |
| 10003 | /* This function is an LUA binding used for registering |
| 10004 | * "sample-conv" functions. It expects a converter name used |
| 10005 | * in the haproxy configuration file, and an LUA function. |
| 10006 | */ |
| 10007 | __LJMP static int hlua_register_service(lua_State *L) |
| 10008 | { |
| 10009 | struct action_kw_list *akl; |
| 10010 | const char *name; |
| 10011 | const char *env; |
| 10012 | int ref; |
| 10013 | int len; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10014 | struct hlua_function *fcn = NULL; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10015 | struct buffer *trash; |
| 10016 | struct action_kw *akw; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10017 | |
| 10018 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 10019 | |
| 10020 | /* First argument : converter name. */ |
| 10021 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 10022 | |
| 10023 | /* Second argument : environment. */ |
| 10024 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10025 | |
| 10026 | /* Third argument : lua function. */ |
| 10027 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10028 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10029 | /* Check for service already registered */ |
| 10030 | trash = get_trash_chunk(); |
| 10031 | chunk_printf(trash, "lua.%s", name); |
| 10032 | akw = service_find(trash->area); |
| 10033 | if (akw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10034 | fcn = akw->private; |
| 10035 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10036 | ha_warning("Trying to register service 'lua.%s' more than once. " |
| 10037 | "This will become a hard error in version 2.5.\n", name); |
| 10038 | } |
| 10039 | fcn->function_ref[hlua_state_id] = ref; |
| 10040 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10041 | } |
| 10042 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10043 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10044 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 10045 | if (!akl) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10046 | goto alloc_error; |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10047 | fcn = new_hlua_function(); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10048 | if (!fcn) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10049 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10050 | |
| 10051 | /* Fill fcn. */ |
| 10052 | len = strlen("<lua.>") + strlen(name) + 1; |
| 10053 | fcn->name = calloc(1, len); |
| 10054 | if (!fcn->name) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10055 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10056 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10057 | fcn->function_ref[hlua_state_id] = ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10058 | |
| 10059 | /* List head */ |
| 10060 | akl->list.n = akl->list.p = NULL; |
| 10061 | |
| 10062 | /* converter keyword. */ |
| 10063 | len = strlen("lua.") + strlen(name) + 1; |
| 10064 | akl->kw[0].kw = calloc(1, len); |
| 10065 | if (!akl->kw[0].kw) |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10066 | goto alloc_error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10067 | |
| 10068 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 10069 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 10070 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10071 | if (strcmp(env, "tcp") == 0) |
| 10072 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 10073 | else if (strcmp(env, "http") == 0) |
| 10074 | akl->kw[0].parse = action_register_service_http; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10075 | else { |
| 10076 | release_hlua_function(fcn); |
| 10077 | if (akl) |
| 10078 | ha_free((char **)&(akl->kw[0].kw)); |
| 10079 | ha_free(&akl); |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 10080 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 10081 | "'tcp' or 'http' are expected.", env)); |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10082 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10083 | |
Amaury Denoyelle | e4a617c | 2021-05-06 15:33:09 +0200 | [diff] [blame] | 10084 | akl->kw[0].flags = 0; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 10085 | akl->kw[0].private = fcn; |
| 10086 | |
| 10087 | /* End of array. */ |
| 10088 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 10089 | |
| 10090 | /* Register this new converter */ |
| 10091 | service_keywords_register(akl); |
| 10092 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10093 | return 0; |
Christopher Faulet | 5c028d7 | 2021-04-12 15:11:44 +0200 | [diff] [blame] | 10094 | |
| 10095 | alloc_error: |
| 10096 | release_hlua_function(fcn); |
| 10097 | ha_free(&akl); |
| 10098 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 10099 | return 0; /* Never reached */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 10100 | } |
| 10101 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10102 | /* This function initialises Lua cli handler. It copies the |
| 10103 | * arguments in the Lua stack and create channel IO objects. |
| 10104 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 10105 | 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] | 10106 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10107 | 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] | 10108 | struct hlua *hlua; |
| 10109 | struct hlua_function *fcn; |
| 10110 | int i; |
| 10111 | const char *error; |
| 10112 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10113 | fcn = private; |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10114 | ctx->fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10115 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 10116 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10117 | if (!hlua) { |
| 10118 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10119 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 10120 | } |
| 10121 | HLUA_INIT(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10122 | ctx->hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10123 | |
| 10124 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 10125 | * 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] | 10126 | * applet_http. It is absolutely compatible. |
| 10127 | */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10128 | ctx->task = task_new_here(); |
| 10129 | if (!ctx->task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 10130 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 10131 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10132 | } |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10133 | ctx->task->nice = 0; |
| 10134 | ctx->task->context = appctx; |
| 10135 | ctx->task->process = hlua_applet_wakeup; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10136 | |
| 10137 | /* Initialises the Lua context */ |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10138 | 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] | 10139 | 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] | 10140 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10141 | } |
| 10142 | |
| 10143 | /* The following Lua calls can fail. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10144 | if (!SET_SAFE_LJMP(hlua)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10145 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 10146 | error = lua_tostring(hlua->T, -1); |
| 10147 | else |
| 10148 | error = "critical error"; |
| 10149 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 10150 | goto error; |
| 10151 | } |
| 10152 | |
| 10153 | /* Check stack available size. */ |
| 10154 | if (!lua_checkstack(hlua->T, 2)) { |
| 10155 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10156 | goto error; |
| 10157 | } |
| 10158 | |
| 10159 | /* Restore the function in the stack. */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10160 | 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] | 10161 | |
| 10162 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 10163 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10164 | */ |
| 10165 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 10166 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10167 | goto error; |
| 10168 | } |
| 10169 | hlua->nargs = 1; |
| 10170 | |
| 10171 | /* push keywords in the stack. */ |
| 10172 | for (i = 0; *args[i]; i++) { |
| 10173 | /* Check stack available size. */ |
| 10174 | if (!lua_checkstack(hlua->T, 1)) { |
| 10175 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 10176 | goto error; |
| 10177 | } |
| 10178 | lua_pushstring(hlua->T, args[i]); |
| 10179 | hlua->nargs++; |
| 10180 | } |
| 10181 | |
| 10182 | /* We must initialize the execution timeouts. */ |
| 10183 | hlua->max_time = hlua_timeout_session; |
| 10184 | |
| 10185 | /* At this point the execution is safe. */ |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10186 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10187 | |
| 10188 | /* It's ok */ |
| 10189 | return 0; |
| 10190 | |
| 10191 | /* It's not ok. */ |
| 10192 | error: |
Thierry Fournier | 7cbe504 | 2020-11-28 17:02:21 +0100 | [diff] [blame] | 10193 | RESET_SAFE_LJMP(hlua); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10194 | hlua_ctx_destroy(hlua); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10195 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10196 | return 1; |
| 10197 | } |
| 10198 | |
| 10199 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 10200 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10201 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10202 | struct hlua *hlua; |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10203 | struct stconn *sc; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10204 | struct hlua_function *fcn; |
| 10205 | |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10206 | hlua = ctx->hlua; |
Willy Tarreau | c12b321 | 2022-05-27 11:08:15 +0200 | [diff] [blame] | 10207 | sc = appctx_sc(appctx); |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10208 | fcn = ctx->fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10209 | |
| 10210 | /* If the stream is disconnect or closed, ldo nothing. */ |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10211 | 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] | 10212 | return 1; |
| 10213 | |
| 10214 | /* Execute the function. */ |
| 10215 | switch (hlua_ctx_resume(hlua, 1)) { |
| 10216 | |
| 10217 | /* finished. */ |
| 10218 | case HLUA_E_OK: |
| 10219 | return 1; |
| 10220 | |
| 10221 | /* yield. */ |
| 10222 | case HLUA_E_AGAIN: |
| 10223 | /* We want write. */ |
| 10224 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | 475e463 | 2022-05-27 10:26:46 +0200 | [diff] [blame] | 10225 | sc_need_room(sc); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10226 | /* Set the timeout. */ |
| 10227 | if (hlua->wake_time != TICK_ETERNITY) |
| 10228 | task_schedule(hlua->task, hlua->wake_time); |
| 10229 | return 0; |
| 10230 | |
| 10231 | /* finished with error. */ |
| 10232 | case HLUA_E_ERRMSG: |
| 10233 | /* Display log. */ |
| 10234 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 10235 | fcn->name, lua_tostring(hlua->T, -1)); |
| 10236 | lua_pop(hlua->T, 1); |
| 10237 | return 1; |
| 10238 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 10239 | case HLUA_E_ETMOUT: |
| 10240 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 10241 | fcn->name); |
| 10242 | return 1; |
| 10243 | |
| 10244 | case HLUA_E_NOMEM: |
| 10245 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 10246 | fcn->name); |
| 10247 | return 1; |
| 10248 | |
| 10249 | case HLUA_E_YIELD: /* unexpected */ |
| 10250 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 10251 | fcn->name); |
| 10252 | return 1; |
| 10253 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10254 | case HLUA_E_ERR: |
| 10255 | /* Display log. */ |
| 10256 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 10257 | fcn->name); |
| 10258 | return 1; |
| 10259 | |
| 10260 | default: |
| 10261 | return 1; |
| 10262 | } |
| 10263 | |
| 10264 | return 1; |
| 10265 | } |
| 10266 | |
| 10267 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 10268 | { |
Willy Tarreau | bcda5f6 | 2022-05-03 18:13:39 +0200 | [diff] [blame] | 10269 | struct hlua_cli_ctx *ctx = appctx->svcctx; |
| 10270 | |
| 10271 | hlua_ctx_destroy(ctx->hlua); |
| 10272 | ctx->hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10273 | } |
| 10274 | |
| 10275 | /* This function is an LUA binding used for registering |
| 10276 | * new keywords in the cli. It expects a list of keywords |
| 10277 | * which are the "path". It is limited to 5 keywords. A |
| 10278 | * description of the command, a function to be executed |
| 10279 | * for the parsing and a function for io handlers. |
| 10280 | */ |
| 10281 | __LJMP static int hlua_register_cli(lua_State *L) |
| 10282 | { |
| 10283 | struct cli_kw_list *cli_kws; |
| 10284 | const char *message; |
| 10285 | int ref_io; |
| 10286 | int len; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10287 | struct hlua_function *fcn = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10288 | int index; |
| 10289 | int i; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10290 | struct buffer *trash; |
| 10291 | const char *kw[5]; |
| 10292 | struct cli_kw *cli_kw; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10293 | const char *errmsg; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10294 | |
| 10295 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 10296 | |
| 10297 | /* First argument : an array of maximum 5 keywords. */ |
| 10298 | if (!lua_istable(L, 1)) |
| 10299 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 10300 | |
| 10301 | /* Second argument : string with contextual message. */ |
| 10302 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 10303 | |
| 10304 | /* Third and fourth argument : lua function. */ |
| 10305 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 10306 | |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10307 | /* Check for CLI service already registered */ |
| 10308 | trash = get_trash_chunk(); |
| 10309 | index = 0; |
| 10310 | lua_pushnil(L); |
| 10311 | memset(kw, 0, sizeof(kw)); |
| 10312 | while (lua_next(L, 1) != 0) { |
| 10313 | if (index >= CLI_PREFIX_KW_NB) |
| 10314 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 10315 | if (lua_type(L, -1) != LUA_TSTRING) |
| 10316 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 10317 | kw[index] = lua_tostring(L, -1); |
| 10318 | if (index == 0) |
| 10319 | chunk_printf(trash, "%s", kw[index]); |
| 10320 | else |
| 10321 | chunk_appendf(trash, " %s", kw[index]); |
| 10322 | index++; |
| 10323 | lua_pop(L, 1); |
| 10324 | } |
| 10325 | cli_kw = cli_find_kw_exact((char **)kw); |
| 10326 | if (cli_kw != NULL) { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 10327 | fcn = cli_kw->private; |
| 10328 | if (fcn->function_ref[hlua_state_id] != -1) { |
| 10329 | ha_warning("Trying to register CLI keyword 'lua.%s' more than once. " |
| 10330 | "This will become a hard error in version 2.5.\n", trash->area); |
| 10331 | } |
| 10332 | fcn->function_ref[hlua_state_id] = ref_io; |
| 10333 | return 0; |
Thierry Fournier | f67442e | 2020-11-28 20:41:07 +0100 | [diff] [blame] | 10334 | } |
| 10335 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10336 | /* Allocate and fill the sample fetch keyword struct. */ |
| 10337 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10338 | if (!cli_kws) { |
| 10339 | errmsg = "Lua out of memory error."; |
| 10340 | goto error; |
| 10341 | } |
Thierry Fournier | 62a22aa | 2020-11-28 21:06:35 +0100 | [diff] [blame] | 10342 | fcn = new_hlua_function(); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10343 | if (!fcn) { |
| 10344 | errmsg = "Lua out of memory error."; |
| 10345 | goto error; |
| 10346 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10347 | |
| 10348 | /* Fill path. */ |
| 10349 | index = 0; |
| 10350 | lua_pushnil(L); |
| 10351 | while(lua_next(L, 1) != 0) { |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10352 | if (index >= 5) { |
| 10353 | errmsg = "1st argument must be a table with a maximum of 5 entries"; |
| 10354 | goto error; |
| 10355 | } |
| 10356 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 10357 | errmsg = "1st argument must be a table filled with strings"; |
| 10358 | goto error; |
| 10359 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10360 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10361 | if (!cli_kws->kw[0].str_kw[index]) { |
| 10362 | errmsg = "Lua out of memory error."; |
| 10363 | goto error; |
| 10364 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10365 | index++; |
| 10366 | lua_pop(L, 1); |
| 10367 | } |
| 10368 | |
| 10369 | /* Copy help message. */ |
| 10370 | cli_kws->kw[0].usage = strdup(message); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10371 | if (!cli_kws->kw[0].usage) { |
| 10372 | errmsg = "Lua out of memory error."; |
| 10373 | goto error; |
| 10374 | } |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10375 | |
| 10376 | /* Fill fcn io handler. */ |
| 10377 | len = strlen("<lua.cli>") + 1; |
| 10378 | for (i = 0; i < index; i++) |
| 10379 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 10380 | fcn->name = calloc(1, len); |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10381 | if (!fcn->name) { |
| 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 | strncat((char *)fcn->name, "<lua.cli", len); |
| 10386 | for (i = 0; i < index; i++) { |
| 10387 | strncat((char *)fcn->name, ".", len); |
| 10388 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 10389 | } |
| 10390 | strncat((char *)fcn->name, ">", len); |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 10391 | fcn->function_ref[hlua_state_id] = ref_io; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10392 | |
| 10393 | /* Fill last entries. */ |
| 10394 | cli_kws->kw[0].private = fcn; |
| 10395 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 10396 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 10397 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 10398 | |
| 10399 | /* Register this new converter */ |
| 10400 | cli_register_kw(cli_kws); |
| 10401 | |
| 10402 | return 0; |
Christopher Faulet | 3a9a12b | 2021-04-12 15:31:29 +0200 | [diff] [blame] | 10403 | |
| 10404 | error: |
| 10405 | release_hlua_function(fcn); |
| 10406 | if (cli_kws) { |
| 10407 | for (i = 0; i < index; i++) |
| 10408 | ha_free((char **)&(cli_kws->kw[0].str_kw[i])); |
| 10409 | ha_free((char **)&(cli_kws->kw[0].usage)); |
| 10410 | } |
| 10411 | ha_free(&cli_kws); |
| 10412 | WILL_LJMP(luaL_error(L, errmsg)); |
| 10413 | return 0; /* Never reached */ |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 10414 | } |
| 10415 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10416 | static int hlua_filter_init_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10417 | { |
| 10418 | struct hlua_flt_config *conf = fconf->conf; |
| 10419 | lua_State *L; |
| 10420 | int error, pos, state_id, flt_ref; |
| 10421 | |
| 10422 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10423 | L = hlua_states[state_id]; |
| 10424 | pos = lua_gettop(L); |
| 10425 | |
| 10426 | /* The filter parsing function */ |
| 10427 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->fun_ref[state_id]); |
| 10428 | |
| 10429 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10430 | lua_rawgeti(L, LUA_REGISTRYINDEX, conf->reg->flt_ref[state_id]); |
| 10431 | |
| 10432 | /* Duplicate the filter class so each filter will have its own copy */ |
| 10433 | lua_newtable(L); |
| 10434 | lua_pushnil(L); |
| 10435 | |
| 10436 | while (lua_next(L, pos+2)) { |
| 10437 | lua_pushvalue(L, -2); |
| 10438 | lua_insert(L, -2); |
| 10439 | lua_settable(L, -4); |
| 10440 | } |
| 10441 | flt_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
| 10442 | |
| 10443 | /* Remove the original lua filter class from the stack */ |
| 10444 | lua_pop(L, 1); |
| 10445 | |
| 10446 | /* Push the copy on the stack */ |
| 10447 | lua_rawgeti(L, LUA_REGISTRYINDEX, flt_ref); |
| 10448 | |
| 10449 | /* extra args are pushed in a table */ |
| 10450 | lua_newtable(L); |
| 10451 | for (pos = 0; conf->args[pos]; pos++) { |
| 10452 | /* Check stack available size. */ |
| 10453 | if (!lua_checkstack(L, 1)) { |
| 10454 | ha_alert("Lua filter '%s' : Lua error : full stack.", conf->reg->name); |
| 10455 | goto error; |
| 10456 | } |
| 10457 | lua_pushstring(L, conf->args[pos]); |
| 10458 | lua_rawseti(L, -2, lua_rawlen(L, -2) + 1); |
| 10459 | } |
| 10460 | |
| 10461 | error = lua_pcall(L, 2, LUA_MULTRET, 0); |
| 10462 | switch (error) { |
| 10463 | case LUA_OK: |
| 10464 | /* replace the filter ref */ |
| 10465 | conf->ref[state_id] = flt_ref; |
| 10466 | break; |
| 10467 | case LUA_ERRRUN: |
| 10468 | ha_alert("Lua filter '%s' : runtime error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10469 | goto error; |
| 10470 | case LUA_ERRMEM: |
| 10471 | ha_alert("Lua filter '%s' : out of memory error", conf->reg->name); |
| 10472 | goto error; |
| 10473 | case LUA_ERRERR: |
| 10474 | ha_alert("Lua filter '%s' : message handler error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10475 | goto error; |
| 10476 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
| 10477 | case LUA_ERRGCMM: |
| 10478 | ha_alert("Lua filter '%s' : garbage collector error : %s", conf->reg->name, lua_tostring(L, -1)); |
| 10479 | goto error; |
| 10480 | #endif |
| 10481 | default: |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 10482 | 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] | 10483 | goto error; |
| 10484 | } |
| 10485 | |
| 10486 | lua_settop(L, 0); |
| 10487 | return 0; |
| 10488 | |
| 10489 | error: |
| 10490 | lua_settop(L, 0); |
| 10491 | return -1; |
| 10492 | } |
| 10493 | |
| 10494 | static void hlua_filter_deinit_per_thread(struct proxy *px, struct flt_conf *fconf) |
| 10495 | { |
| 10496 | struct hlua_flt_config *conf = fconf->conf; |
| 10497 | lua_State *L; |
| 10498 | int state_id; |
| 10499 | |
| 10500 | if (!conf) |
| 10501 | return; |
| 10502 | |
| 10503 | state_id = reg_flt_to_stack_id(conf->reg); |
| 10504 | L = hlua_states[state_id]; |
| 10505 | luaL_unref(L, LUA_REGISTRYINDEX, conf->ref[state_id]); |
| 10506 | } |
| 10507 | |
| 10508 | static int hlua_filter_init(struct proxy *px, struct flt_conf *fconf) |
| 10509 | { |
| 10510 | struct hlua_flt_config *conf = fconf->conf; |
| 10511 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10512 | |
| 10513 | /* Rely on per-thread init for global scripts */ |
| 10514 | if (!state_id) |
| 10515 | return hlua_filter_init_per_thread(px, fconf); |
| 10516 | return 0; |
| 10517 | } |
| 10518 | |
| 10519 | static void hlua_filter_deinit(struct proxy *px, struct flt_conf *fconf) |
| 10520 | { |
| 10521 | |
| 10522 | if (fconf->conf) { |
| 10523 | struct hlua_flt_config *conf = fconf->conf; |
| 10524 | int state_id = reg_flt_to_stack_id(conf->reg); |
| 10525 | int pos; |
| 10526 | |
| 10527 | /* Rely on per-thread deinit for global scripts */ |
| 10528 | if (!state_id) |
| 10529 | hlua_filter_deinit_per_thread(px, fconf); |
| 10530 | |
| 10531 | for (pos = 0; conf->args[pos]; pos++) |
| 10532 | free(conf->args[pos]); |
| 10533 | free(conf->args); |
| 10534 | } |
| 10535 | ha_free(&fconf->conf); |
| 10536 | ha_free((char **)&fconf->id); |
| 10537 | ha_free(&fconf->ops); |
| 10538 | } |
| 10539 | |
| 10540 | static int hlua_filter_new(struct stream *s, struct filter *filter) |
| 10541 | { |
| 10542 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10543 | struct hlua_flt_ctx *flt_ctx = NULL; |
| 10544 | int ret = 1; |
| 10545 | |
| 10546 | /* In the execution wrappers linked with a stream, the |
| 10547 | * Lua context can be not initialized. This behavior |
| 10548 | * permits to save performances because a systematic |
| 10549 | * Lua initialization cause 5% performances loss. |
| 10550 | */ |
| 10551 | if (!s->hlua) { |
| 10552 | struct hlua *hlua; |
| 10553 | |
| 10554 | hlua = pool_alloc(pool_head_hlua); |
| 10555 | if (!hlua) { |
| 10556 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10557 | conf->reg->name); |
| 10558 | ret = 0; |
| 10559 | goto end; |
| 10560 | } |
| 10561 | HLUA_INIT(hlua); |
| 10562 | s->hlua = hlua; |
| 10563 | if (!hlua_ctx_init(s->hlua, reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10564 | SEND_ERR(s->be, "Lua filter '%s': can't initialize Lua context.\n", |
| 10565 | conf->reg->name); |
| 10566 | ret = 0; |
| 10567 | goto end; |
| 10568 | } |
| 10569 | } |
| 10570 | |
| 10571 | flt_ctx = pool_zalloc(pool_head_hlua_flt_ctx); |
| 10572 | if (!flt_ctx) { |
| 10573 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10574 | conf->reg->name); |
| 10575 | ret = 0; |
| 10576 | goto end; |
| 10577 | } |
| 10578 | flt_ctx->hlua[0] = pool_alloc(pool_head_hlua); |
| 10579 | flt_ctx->hlua[1] = pool_alloc(pool_head_hlua); |
| 10580 | if (!flt_ctx->hlua[0] || !flt_ctx->hlua[1]) { |
| 10581 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10582 | conf->reg->name); |
| 10583 | ret = 0; |
| 10584 | goto end; |
| 10585 | } |
| 10586 | HLUA_INIT(flt_ctx->hlua[0]); |
| 10587 | HLUA_INIT(flt_ctx->hlua[1]); |
| 10588 | if (!hlua_ctx_init(flt_ctx->hlua[0], reg_flt_to_stack_id(conf->reg), s->task, 0) || |
| 10589 | !hlua_ctx_init(flt_ctx->hlua[1], reg_flt_to_stack_id(conf->reg), s->task, 0)) { |
| 10590 | SEND_ERR(s->be, "Lua filter '%s': can't initialize filter Lua context.\n", |
| 10591 | conf->reg->name); |
| 10592 | ret = 0; |
| 10593 | goto end; |
| 10594 | } |
| 10595 | |
| 10596 | if (!HLUA_IS_RUNNING(s->hlua)) { |
| 10597 | /* The following Lua calls can fail. */ |
| 10598 | if (!SET_SAFE_LJMP(s->hlua)) { |
| 10599 | const char *error; |
| 10600 | |
| 10601 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 10602 | error = lua_tostring(s->hlua->T, -1); |
| 10603 | else |
| 10604 | error = "critical error"; |
| 10605 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10606 | ret = 0; |
| 10607 | goto end; |
| 10608 | } |
| 10609 | |
| 10610 | /* Check stack size. */ |
| 10611 | if (!lua_checkstack(s->hlua->T, 1)) { |
| 10612 | 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] | 10613 | RESET_SAFE_LJMP(s->hlua); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10614 | ret = 0; |
| 10615 | goto end; |
| 10616 | } |
| 10617 | |
| 10618 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, conf->ref[s->hlua->state_id]); |
| 10619 | if (lua_getfield(s->hlua->T, -1, "new") != LUA_TFUNCTION) { |
| 10620 | SEND_ERR(s->be, "Lua filter '%s': 'new' field is not a function.\n", |
| 10621 | conf->reg->name); |
| 10622 | RESET_SAFE_LJMP(s->hlua); |
| 10623 | ret = 0; |
| 10624 | goto end; |
| 10625 | } |
| 10626 | lua_insert(s->hlua->T, -2); |
| 10627 | |
| 10628 | /* Push the copy on the stack */ |
| 10629 | s->hlua->nargs = 1; |
| 10630 | |
| 10631 | /* We must initialize the execution timeouts. */ |
| 10632 | s->hlua->max_time = hlua_timeout_session; |
| 10633 | |
| 10634 | /* At this point the execution is safe. */ |
| 10635 | RESET_SAFE_LJMP(s->hlua); |
| 10636 | } |
| 10637 | |
| 10638 | switch (hlua_ctx_resume(s->hlua, 0)) { |
| 10639 | case HLUA_E_OK: |
| 10640 | /* Nothing returned or not a table, ignore the filter for current stream */ |
| 10641 | if (!lua_gettop(s->hlua->T) || !lua_istable(s->hlua->T, 1)) { |
| 10642 | ret = 0; |
| 10643 | goto end; |
| 10644 | } |
| 10645 | |
| 10646 | /* Attached the filter pointer to the ctx */ |
| 10647 | lua_pushstring(s->hlua->T, "__filter"); |
| 10648 | lua_pushlightuserdata(s->hlua->T, filter); |
| 10649 | lua_settable(s->hlua->T, -3); |
| 10650 | |
| 10651 | /* Save a ref on the filter ctx */ |
| 10652 | lua_pushvalue(s->hlua->T, 1); |
| 10653 | flt_ctx->ref = luaL_ref(s->hlua->T, LUA_REGISTRYINDEX); |
| 10654 | filter->ctx = flt_ctx; |
| 10655 | break; |
| 10656 | case HLUA_E_ERRMSG: |
| 10657 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(s->hlua->T, -1)); |
| 10658 | ret = -1; |
| 10659 | goto end; |
| 10660 | case HLUA_E_ETMOUT: |
| 10661 | SEND_ERR(s->be, "Lua filter '%s' : 'new' execution timeout.\n", conf->reg->name); |
| 10662 | ret = 0; |
| 10663 | goto end; |
| 10664 | case HLUA_E_NOMEM: |
| 10665 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10666 | ret = 0; |
| 10667 | goto end; |
| 10668 | case HLUA_E_AGAIN: |
| 10669 | case HLUA_E_YIELD: |
| 10670 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10671 | " are not allowed from 'new' function.\n", conf->reg->name); |
| 10672 | ret = 0; |
| 10673 | goto end; |
| 10674 | case HLUA_E_ERR: |
| 10675 | SEND_ERR(s->be, "Lua filter '%s': 'new' returns an unknown error.\n", conf->reg->name); |
| 10676 | ret = 0; |
| 10677 | goto end; |
| 10678 | default: |
| 10679 | ret = 0; |
| 10680 | goto end; |
| 10681 | } |
| 10682 | |
| 10683 | end: |
| 10684 | if (s->hlua) |
| 10685 | lua_settop(s->hlua->T, 0); |
| 10686 | if (ret <= 0) { |
| 10687 | if (flt_ctx) { |
| 10688 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10689 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10690 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10691 | } |
| 10692 | } |
| 10693 | return ret; |
| 10694 | } |
| 10695 | |
| 10696 | static void hlua_filter_delete(struct stream *s, struct filter *filter) |
| 10697 | { |
| 10698 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10699 | |
| 10700 | luaL_unref(s->hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10701 | hlua_ctx_destroy(flt_ctx->hlua[0]); |
| 10702 | hlua_ctx_destroy(flt_ctx->hlua[1]); |
| 10703 | pool_free(pool_head_hlua_flt_ctx, flt_ctx); |
| 10704 | filter->ctx = NULL; |
| 10705 | } |
| 10706 | |
Christopher Faulet | 9f55a50 | 2020-02-25 15:21:02 +0100 | [diff] [blame] | 10707 | static int hlua_filter_from_payload(struct filter *filter) |
| 10708 | { |
| 10709 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10710 | |
| 10711 | return (flt_ctx && !!(flt_ctx->flags & HLUA_FLT_CTX_FL_PAYLOAD)); |
| 10712 | } |
| 10713 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10714 | static int hlua_filter_callback(struct stream *s, struct filter *filter, const char *fun, |
| 10715 | int dir, unsigned int flags) |
| 10716 | { |
| 10717 | struct hlua *flt_hlua; |
| 10718 | struct hlua_flt_config *conf = FLT_CONF(filter); |
| 10719 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10720 | unsigned int hflags = HLUA_TXN_FLT_CTX; |
| 10721 | int ret = 1; |
| 10722 | |
| 10723 | flt_hlua = flt_ctx->hlua[(dir == SMP_OPT_DIR_REQ ? 0 : 1)]; |
| 10724 | if (!flt_hlua) |
| 10725 | goto end; |
| 10726 | |
| 10727 | if (!HLUA_IS_RUNNING(flt_hlua)) { |
| 10728 | int extra_idx = lua_gettop(flt_hlua->T); |
| 10729 | |
| 10730 | /* The following Lua calls can fail. */ |
| 10731 | if (!SET_SAFE_LJMP(flt_hlua)) { |
| 10732 | const char *error; |
| 10733 | |
| 10734 | if (lua_type(flt_hlua->T, -1) == LUA_TSTRING) |
| 10735 | error = lua_tostring(flt_hlua->T, -1); |
| 10736 | else |
| 10737 | error = "critical error"; |
| 10738 | SEND_ERR(s->be, "Lua filter '%s': %s.\n", conf->reg->name, error); |
| 10739 | goto end; |
| 10740 | } |
| 10741 | |
| 10742 | /* Check stack size. */ |
| 10743 | if (!lua_checkstack(flt_hlua->T, 3)) { |
| 10744 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10745 | RESET_SAFE_LJMP(flt_hlua); |
| 10746 | goto end; |
| 10747 | } |
| 10748 | |
| 10749 | lua_rawgeti(flt_hlua->T, LUA_REGISTRYINDEX, flt_ctx->ref); |
| 10750 | if (lua_getfield(flt_hlua->T, -1, fun) != LUA_TFUNCTION) { |
| 10751 | RESET_SAFE_LJMP(flt_hlua); |
| 10752 | goto end; |
| 10753 | } |
| 10754 | lua_insert(flt_hlua->T, -2); |
| 10755 | |
| 10756 | if (!hlua_txn_new(flt_hlua->T, s, s->be, dir, hflags)) { |
| 10757 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10758 | RESET_SAFE_LJMP(flt_hlua); |
| 10759 | goto end; |
| 10760 | } |
| 10761 | flt_hlua->nargs = 2; |
| 10762 | |
| 10763 | if (flags & HLUA_FLT_CB_ARG_CHN) { |
| 10764 | if (dir == SMP_OPT_DIR_REQ) |
| 10765 | lua_getfield(flt_hlua->T, -1, "req"); |
| 10766 | else |
| 10767 | lua_getfield(flt_hlua->T, -1, "res"); |
| 10768 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10769 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10770 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10771 | lua_settable(flt_hlua->T, -3); |
| 10772 | } |
| 10773 | flt_hlua->nargs++; |
| 10774 | } |
| 10775 | else if (flags & HLUA_FLT_CB_ARG_HTTP_MSG) { |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10776 | if (dir == SMP_OPT_DIR_REQ) |
| 10777 | lua_getfield(flt_hlua->T, -1, "http_req"); |
| 10778 | else |
| 10779 | lua_getfield(flt_hlua->T, -1, "http_res"); |
| 10780 | if (lua_type(flt_hlua->T, -1) == LUA_TTABLE) { |
| 10781 | lua_pushstring(flt_hlua->T, "__filter"); |
| 10782 | lua_pushlightuserdata(flt_hlua->T, filter); |
| 10783 | lua_settable(flt_hlua->T, -3); |
| 10784 | } |
| 10785 | flt_hlua->nargs++; |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10786 | } |
| 10787 | |
| 10788 | /* Check stack size. */ |
| 10789 | if (!lua_checkstack(flt_hlua->T, 1)) { |
| 10790 | SEND_ERR(s->be, "Lua filter '%s': full stack.\n", conf->reg->name); |
| 10791 | RESET_SAFE_LJMP(flt_hlua); |
| 10792 | goto end; |
| 10793 | } |
| 10794 | |
| 10795 | while (extra_idx--) { |
| 10796 | lua_pushvalue(flt_hlua->T, 1); |
| 10797 | lua_remove(flt_hlua->T, 1); |
| 10798 | flt_hlua->nargs++; |
| 10799 | } |
| 10800 | |
| 10801 | /* We must initialize the execution timeouts. */ |
| 10802 | flt_hlua->max_time = hlua_timeout_session; |
| 10803 | |
| 10804 | /* At this point the execution is safe. */ |
| 10805 | RESET_SAFE_LJMP(flt_hlua); |
| 10806 | } |
| 10807 | |
| 10808 | switch (hlua_ctx_resume(flt_hlua, !(flags & HLUA_FLT_CB_FINAL))) { |
| 10809 | case HLUA_E_OK: |
| 10810 | /* Catch the return value if it required */ |
| 10811 | if ((flags & HLUA_FLT_CB_RETVAL) && lua_gettop(flt_hlua->T) > 0) { |
| 10812 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10813 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10814 | } |
| 10815 | |
| 10816 | /* Set timeout in the required channel. */ |
| 10817 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10818 | if (dir == SMP_OPT_DIR_REQ) |
| 10819 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10820 | else |
| 10821 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10822 | } |
| 10823 | break; |
| 10824 | case HLUA_E_AGAIN: |
| 10825 | /* Set timeout in the required channel. */ |
| 10826 | if (flt_hlua->wake_time != TICK_ETERNITY) { |
| 10827 | if (dir == SMP_OPT_DIR_REQ) |
| 10828 | s->req.analyse_exp = flt_hlua->wake_time; |
| 10829 | else |
| 10830 | s->res.analyse_exp = flt_hlua->wake_time; |
| 10831 | } |
| 10832 | /* Some actions can be wake up when a "write" event |
| 10833 | * is detected on a response channel. This is useful |
| 10834 | * only for actions targeted on the requests. |
| 10835 | */ |
| 10836 | if (HLUA_IS_WAKERESWR(flt_hlua)) |
| 10837 | s->res.flags |= CF_WAKE_WRITE; |
| 10838 | if (HLUA_IS_WAKEREQWR(flt_hlua)) |
| 10839 | s->req.flags |= CF_WAKE_WRITE; |
| 10840 | ret = 0; |
| 10841 | goto end; |
| 10842 | case HLUA_E_ERRMSG: |
| 10843 | SEND_ERR(s->be, "Lua filter '%s' : %s.\n", conf->reg->name, lua_tostring(flt_hlua->T, -1)); |
| 10844 | ret = -1; |
| 10845 | goto end; |
| 10846 | case HLUA_E_ETMOUT: |
| 10847 | SEND_ERR(s->be, "Lua filter '%s' : '%s' callback execution timeout.\n", conf->reg->name, fun); |
| 10848 | goto end; |
| 10849 | case HLUA_E_NOMEM: |
| 10850 | SEND_ERR(s->be, "Lua filter '%s' : out of memory error.\n", conf->reg->name); |
| 10851 | goto end; |
| 10852 | case HLUA_E_YIELD: |
| 10853 | SEND_ERR(s->be, "Lua filter '%s': yield functions like core.tcp() or core.sleep()" |
| 10854 | " are not allowed from '%s' callback.\n", conf->reg->name, fun); |
| 10855 | goto end; |
| 10856 | case HLUA_E_ERR: |
| 10857 | SEND_ERR(s->be, "Lua filter '%s': '%s' returns an unknown error.\n", conf->reg->name, fun); |
| 10858 | goto end; |
| 10859 | default: |
| 10860 | goto end; |
| 10861 | } |
| 10862 | |
| 10863 | |
| 10864 | end: |
| 10865 | return ret; |
| 10866 | } |
| 10867 | |
| 10868 | static int hlua_filter_start_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10869 | { |
| 10870 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10871 | |
| 10872 | flt_ctx->flags = 0; |
| 10873 | return hlua_filter_callback(s, filter, "start_analyze", |
| 10874 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10875 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10876 | } |
| 10877 | |
| 10878 | static int hlua_filter_end_analyze(struct stream *s, struct filter *filter, struct channel *chn) |
| 10879 | { |
| 10880 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10881 | |
| 10882 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10883 | return hlua_filter_callback(s, filter, "end_analyze", |
| 10884 | (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10885 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_CHN)); |
| 10886 | } |
| 10887 | |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10888 | static int hlua_filter_http_headers(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10889 | { |
| 10890 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10891 | |
| 10892 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10893 | return hlua_filter_callback(s, filter, "http_headers", |
| 10894 | (!(msg->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_HTTP_MSG)); |
| 10896 | } |
| 10897 | |
| 10898 | static int hlua_filter_http_payload(struct stream *s, struct filter *filter, struct http_msg *msg, |
| 10899 | unsigned int offset, unsigned int len) |
| 10900 | { |
| 10901 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10902 | struct hlua *flt_hlua; |
| 10903 | int dir = (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10904 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10905 | int ret; |
| 10906 | |
| 10907 | flt_hlua = flt_ctx->hlua[idx]; |
| 10908 | flt_ctx->cur_off[idx] = offset; |
| 10909 | flt_ctx->cur_len[idx] = len; |
| 10910 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10911 | ret = hlua_filter_callback(s, filter, "http_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10912 | if (ret != -1) { |
| 10913 | ret = flt_ctx->cur_len[idx]; |
| 10914 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10915 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10916 | if (ret > flt_ctx->cur_len[idx]) |
| 10917 | ret = flt_ctx->cur_len[idx]; |
| 10918 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10919 | } |
| 10920 | } |
| 10921 | return ret; |
| 10922 | } |
| 10923 | |
| 10924 | static int hlua_filter_http_end(struct stream *s, struct filter *filter, struct http_msg *msg) |
| 10925 | { |
| 10926 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10927 | |
| 10928 | flt_ctx->flags &= ~HLUA_FLT_CTX_FL_PAYLOAD; |
| 10929 | return hlua_filter_callback(s, filter, "http_end", |
| 10930 | (!(msg->chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES), |
| 10931 | (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_RETVAL | HLUA_FLT_CB_ARG_HTTP_MSG)); |
| 10932 | } |
| 10933 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10934 | static int hlua_filter_tcp_payload(struct stream *s, struct filter *filter, struct channel *chn, |
| 10935 | unsigned int offset, unsigned int len) |
| 10936 | { |
| 10937 | struct hlua_flt_ctx *flt_ctx = filter->ctx; |
| 10938 | struct hlua *flt_hlua; |
| 10939 | int dir = (!(chn->flags & CF_ISRESP) ? SMP_OPT_DIR_REQ : SMP_OPT_DIR_RES); |
| 10940 | int idx = (dir == SMP_OPT_DIR_REQ ? 0 : 1); |
| 10941 | int ret; |
| 10942 | |
| 10943 | flt_hlua = flt_ctx->hlua[idx]; |
| 10944 | flt_ctx->cur_off[idx] = offset; |
| 10945 | flt_ctx->cur_len[idx] = len; |
| 10946 | flt_ctx->flags |= HLUA_FLT_CTX_FL_PAYLOAD; |
| 10947 | ret = hlua_filter_callback(s, filter, "tcp_payload", dir, (HLUA_FLT_CB_FINAL | HLUA_FLT_CB_ARG_CHN)); |
| 10948 | if (ret != -1) { |
| 10949 | ret = flt_ctx->cur_len[idx]; |
| 10950 | if (lua_gettop(flt_hlua->T) > 0) { |
| 10951 | ret = lua_tointeger(flt_hlua->T, -1); |
| 10952 | if (ret > flt_ctx->cur_len[idx]) |
| 10953 | ret = flt_ctx->cur_len[idx]; |
| 10954 | lua_settop(flt_hlua->T, 0); /* Empty the stack. */ |
| 10955 | } |
| 10956 | } |
| 10957 | return ret; |
| 10958 | } |
| 10959 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10960 | static int hlua_filter_parse_fct(char **args, int *cur_arg, struct proxy *px, |
| 10961 | struct flt_conf *fconf, char **err, void *private) |
| 10962 | { |
| 10963 | struct hlua_reg_filter *reg_flt = private; |
| 10964 | lua_State *L; |
| 10965 | struct hlua_flt_config *conf = NULL; |
| 10966 | const char *flt_id = NULL; |
| 10967 | int state_id, pos, flt_flags = 0; |
| 10968 | struct flt_ops *hlua_flt_ops = NULL; |
| 10969 | |
| 10970 | state_id = reg_flt_to_stack_id(reg_flt); |
| 10971 | L = hlua_states[state_id]; |
| 10972 | |
| 10973 | /* Initialize the filter ops with default callbacks */ |
| 10974 | hlua_flt_ops = calloc(1, sizeof(*hlua_flt_ops)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 10975 | if (!hlua_flt_ops) |
| 10976 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 10977 | hlua_flt_ops->init = hlua_filter_init; |
| 10978 | hlua_flt_ops->deinit = hlua_filter_deinit; |
| 10979 | if (state_id) { |
| 10980 | /* Set per-thread callback if script is loaded per-thread */ |
| 10981 | hlua_flt_ops->init_per_thread = hlua_filter_init_per_thread; |
| 10982 | hlua_flt_ops->deinit_per_thread = hlua_filter_deinit_per_thread; |
| 10983 | } |
| 10984 | hlua_flt_ops->attach = hlua_filter_new; |
| 10985 | hlua_flt_ops->detach = hlua_filter_delete; |
| 10986 | |
| 10987 | /* Push the filter class on the stack and resolve all callbacks */ |
| 10988 | lua_rawgeti(L, LUA_REGISTRYINDEX, reg_flt->flt_ref[state_id]); |
| 10989 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 10990 | if (lua_getfield(L, -1, "start_analyze") == LUA_TFUNCTION) |
| 10991 | hlua_flt_ops->channel_start_analyze = hlua_filter_start_analyze; |
| 10992 | lua_pop(L, 1); |
| 10993 | if (lua_getfield(L, -1, "end_analyze") == LUA_TFUNCTION) |
| 10994 | hlua_flt_ops->channel_end_analyze = hlua_filter_end_analyze; |
| 10995 | lua_pop(L, 1); |
Christopher Faulet | eae8afa | 2020-02-26 17:15:48 +0100 | [diff] [blame] | 10996 | if (lua_getfield(L, -1, "http_headers") == LUA_TFUNCTION) |
| 10997 | hlua_flt_ops->http_headers = hlua_filter_http_headers; |
| 10998 | lua_pop(L, 1); |
| 10999 | if (lua_getfield(L, -1, "http_payload") == LUA_TFUNCTION) |
| 11000 | hlua_flt_ops->http_payload = hlua_filter_http_payload; |
| 11001 | lua_pop(L, 1); |
| 11002 | if (lua_getfield(L, -1, "http_end") == LUA_TFUNCTION) |
| 11003 | hlua_flt_ops->http_end = hlua_filter_http_end; |
| 11004 | lua_pop(L, 1); |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11005 | if (lua_getfield(L, -1, "tcp_payload") == LUA_TFUNCTION) |
| 11006 | hlua_flt_ops->tcp_payload = hlua_filter_tcp_payload; |
| 11007 | lua_pop(L, 1); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11008 | |
| 11009 | /* Get id and flags of the filter class */ |
| 11010 | if (lua_getfield(L, -1, "id") == LUA_TSTRING) |
| 11011 | flt_id = lua_tostring(L, -1); |
| 11012 | lua_pop(L, 1); |
| 11013 | if (lua_getfield(L, -1, "flags") == LUA_TNUMBER) |
| 11014 | flt_flags = lua_tointeger(L, -1); |
| 11015 | lua_pop(L, 1); |
| 11016 | |
| 11017 | /* Create the filter config */ |
| 11018 | conf = calloc(1, sizeof(*conf)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11019 | if (!conf) |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11020 | goto error; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11021 | conf->reg = reg_flt; |
| 11022 | |
| 11023 | /* duplicate args */ |
| 11024 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++); |
| 11025 | conf->args = calloc(pos + 1, sizeof(*conf->args)); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11026 | if (!conf->args) |
| 11027 | goto error; |
| 11028 | for (pos = 0; *args[*cur_arg + 1 + pos]; pos++) { |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11029 | conf->args[pos] = strdup(args[*cur_arg + 1 + pos]); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11030 | if (!conf->args[pos]) |
| 11031 | goto error; |
| 11032 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11033 | conf->args[pos] = NULL; |
| 11034 | *cur_arg += pos + 1; |
| 11035 | |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11036 | if (flt_id) { |
| 11037 | fconf->id = strdup(flt_id); |
| 11038 | if (!fconf->id) |
| 11039 | goto error; |
| 11040 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11041 | fconf->flags = flt_flags; |
| 11042 | fconf->conf = conf; |
| 11043 | fconf->ops = hlua_flt_ops; |
| 11044 | |
| 11045 | lua_settop(L, 0); |
| 11046 | return 0; |
| 11047 | |
| 11048 | error: |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11049 | 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] | 11050 | free(hlua_flt_ops); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11051 | if (conf && conf->args) { |
| 11052 | for (pos = 0; conf->args[pos]; pos++) |
| 11053 | free(conf->args[pos]); |
| 11054 | free(conf->args); |
| 11055 | } |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11056 | free(conf); |
Christopher Faulet | c86bb87 | 2021-08-13 08:33:57 +0200 | [diff] [blame] | 11057 | free((char *)fconf->id); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11058 | lua_settop(L, 0); |
| 11059 | return -1; |
| 11060 | } |
| 11061 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 11062 | __LJMP static int hlua_register_data_filter(lua_State *L) |
| 11063 | { |
| 11064 | struct filter *filter; |
| 11065 | struct channel *chn; |
| 11066 | |
| 11067 | MAY_LJMP(check_args(L, 2, "register_data_filter")); |
| 11068 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11069 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11070 | |
| 11071 | lua_getfield(L, 1, "__filter"); |
| 11072 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11073 | filter = lua_touserdata (L, -1); |
| 11074 | lua_pop(L, 1); |
| 11075 | |
| 11076 | register_data_filter(chn_strm(chn), chn, filter); |
| 11077 | return 1; |
| 11078 | } |
| 11079 | |
| 11080 | __LJMP static int hlua_unregister_data_filter(lua_State *L) |
| 11081 | { |
| 11082 | struct filter *filter; |
| 11083 | struct channel *chn; |
| 11084 | |
| 11085 | MAY_LJMP(check_args(L, 2, "unregister_data_filter")); |
| 11086 | MAY_LJMP(luaL_checktype(L, 1, LUA_TTABLE)); |
| 11087 | chn = MAY_LJMP(hlua_checkchannel(L, 2)); |
| 11088 | |
| 11089 | lua_getfield(L, 1, "__filter"); |
| 11090 | MAY_LJMP(luaL_checktype(L, -1, LUA_TLIGHTUSERDATA)); |
| 11091 | filter = lua_touserdata (L, -1); |
| 11092 | lua_pop(L, 1); |
| 11093 | |
| 11094 | unregister_data_filter(chn_strm(chn), chn, filter); |
| 11095 | return 1; |
| 11096 | } |
| 11097 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11098 | /* 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] | 11099 | * 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] | 11100 | * parse configuration arguments. |
| 11101 | */ |
| 11102 | __LJMP static int hlua_register_filter(lua_State *L) |
| 11103 | { |
| 11104 | struct buffer *trash; |
| 11105 | struct flt_kw_list *fkl; |
| 11106 | struct flt_kw *fkw; |
| 11107 | const char *name; |
| 11108 | struct hlua_reg_filter *reg_flt= NULL; |
| 11109 | int flt_ref, fun_ref; |
| 11110 | int len; |
| 11111 | |
| 11112 | MAY_LJMP(check_args(L, 3, "register_filter")); |
| 11113 | |
| 11114 | /* First argument : filter name. */ |
| 11115 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 11116 | |
| 11117 | /* Second argument : The filter class */ |
| 11118 | flt_ref = MAY_LJMP(hlua_checktable(L, 2)); |
| 11119 | |
| 11120 | /* Third argument : lua function. */ |
| 11121 | fun_ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 11122 | |
| 11123 | trash = get_trash_chunk(); |
| 11124 | chunk_printf(trash, "lua.%s", name); |
| 11125 | fkw = flt_find_kw(trash->area); |
| 11126 | if (fkw != NULL) { |
| 11127 | reg_flt = fkw->private; |
| 11128 | if (reg_flt->flt_ref[hlua_state_id] != -1 || reg_flt->fun_ref[hlua_state_id] != -1) { |
| 11129 | ha_warning("Trying to register filter 'lua.%s' more than once. " |
| 11130 | "This will become a hard error in version 2.5.\n", name); |
| 11131 | } |
| 11132 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11133 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11134 | return 0; |
| 11135 | } |
| 11136 | |
| 11137 | fkl = calloc(1, sizeof(*fkl) + sizeof(struct flt_kw) * 2); |
| 11138 | if (!fkl) |
| 11139 | goto alloc_error; |
| 11140 | fkl->scope = "HLUA"; |
| 11141 | |
| 11142 | reg_flt = new_hlua_reg_filter(name); |
| 11143 | if (!reg_flt) |
| 11144 | goto alloc_error; |
| 11145 | |
| 11146 | reg_flt->flt_ref[hlua_state_id] = flt_ref; |
| 11147 | reg_flt->fun_ref[hlua_state_id] = fun_ref; |
| 11148 | |
| 11149 | /* The filter keyword */ |
| 11150 | len = strlen("lua.") + strlen(name) + 1; |
| 11151 | fkl->kw[0].kw = calloc(1, len); |
| 11152 | if (!fkl->kw[0].kw) |
| 11153 | goto alloc_error; |
| 11154 | |
| 11155 | snprintf((char *)fkl->kw[0].kw, len, "lua.%s", name); |
| 11156 | |
| 11157 | fkl->kw[0].parse = hlua_filter_parse_fct; |
| 11158 | fkl->kw[0].private = reg_flt; |
| 11159 | memset(&fkl->kw[1], 0, sizeof(*fkl->kw)); |
| 11160 | |
| 11161 | /* Register this new filter */ |
| 11162 | flt_register_keywords(fkl); |
| 11163 | |
| 11164 | return 0; |
| 11165 | |
| 11166 | alloc_error: |
| 11167 | release_hlua_reg_filter(reg_flt); |
| 11168 | ha_free(&fkl); |
| 11169 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 11170 | return 0; /* Never reached */ |
| 11171 | } |
| 11172 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11173 | 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] | 11174 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11175 | char **err, unsigned int *timeout) |
| 11176 | { |
| 11177 | const char *error; |
| 11178 | |
| 11179 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 11180 | if (error == PARSE_TIME_OVER) { |
| 11181 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 11182 | args[1], args[0]); |
| 11183 | return -1; |
| 11184 | } |
| 11185 | else if (error == PARSE_TIME_UNDER) { |
| 11186 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 11187 | args[1], args[0]); |
| 11188 | return -1; |
| 11189 | } |
| 11190 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11191 | memprintf(err, "%s: invalid timeout", args[0]); |
| 11192 | return -1; |
| 11193 | } |
| 11194 | return 0; |
| 11195 | } |
| 11196 | |
| 11197 | 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] | 11198 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11199 | char **err) |
| 11200 | { |
| 11201 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11202 | file, line, err, &hlua_timeout_session); |
| 11203 | } |
| 11204 | |
| 11205 | 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] | 11206 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11207 | char **err) |
| 11208 | { |
| 11209 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11210 | file, line, err, &hlua_timeout_task); |
| 11211 | } |
| 11212 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11213 | 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] | 11214 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 11215 | char **err) |
| 11216 | { |
| 11217 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 11218 | file, line, err, &hlua_timeout_applet); |
| 11219 | } |
| 11220 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11221 | 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] | 11222 | const struct proxy *defpx, const char *file, int line, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11223 | char **err) |
| 11224 | { |
| 11225 | char *error; |
| 11226 | |
| 11227 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 11228 | if (*error != '\0') { |
| 11229 | memprintf(err, "%s: invalid number", args[0]); |
| 11230 | return -1; |
| 11231 | } |
| 11232 | return 0; |
| 11233 | } |
| 11234 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11235 | 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] | 11236 | const struct proxy *defpx, const char *file, int line, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11237 | char **err) |
| 11238 | { |
| 11239 | char *error; |
| 11240 | |
| 11241 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11242 | 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] | 11243 | return -1; |
| 11244 | } |
| 11245 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 11246 | if (*error != '\0') { |
| 11247 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 11248 | return -1; |
| 11249 | } |
| 11250 | return 0; |
| 11251 | } |
| 11252 | |
| 11253 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11254 | /* This function is called by the main configuration key "lua-load". It loads and |
| 11255 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 11256 | * the main lua entry point. |
| 11257 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 11258 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 11259 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11260 | * |
| 11261 | * In some error case, LUA set an error message in top of the stack. This function |
| 11262 | * 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] | 11263 | * |
| 11264 | * This function can fail with an abort() due to an Lua critical error. |
| 11265 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 11266 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11267 | */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11268 | static int hlua_load_state(char **args, lua_State *L, char **err) |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11269 | { |
| 11270 | int error; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11271 | int nargs; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11272 | |
| 11273 | /* Just load and compile the file. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11274 | error = luaL_loadfile(L, args[0]); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11275 | if (error) { |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11276 | 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] | 11277 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11278 | return -1; |
| 11279 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11280 | |
| 11281 | /* Push args in the Lua stack, except the first one which is the filename */ |
| 11282 | for (nargs = 1; *(args[nargs]) != 0; nargs++) { |
Aurelien DARRAGON | 4d7aefe | 2022-09-23 10:22:14 +0200 | [diff] [blame] | 11283 | /* Check stack size. */ |
| 11284 | if (!lua_checkstack(L, 1)) { |
| 11285 | memprintf(err, "Lua runtime error while loading arguments: stack is full."); |
| 11286 | return -1; |
| 11287 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11288 | lua_pushstring(L, args[nargs]); |
| 11289 | } |
| 11290 | nargs--; |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11291 | |
| 11292 | /* If no syntax error where detected, execute the code. */ |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11293 | error = lua_pcall(L, nargs, LUA_MULTRET, 0); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11294 | switch (error) { |
| 11295 | case LUA_OK: |
| 11296 | break; |
| 11297 | case LUA_ERRRUN: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11298 | memprintf(err, "Lua runtime error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11299 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11300 | return -1; |
| 11301 | case LUA_ERRMEM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11302 | memprintf(err, "Lua out of memory error"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11303 | return -1; |
| 11304 | case LUA_ERRERR: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11305 | memprintf(err, "Lua message handler error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11306 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11307 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11308 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM <= 503 |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11309 | case LUA_ERRGCMM: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11310 | memprintf(err, "Lua garbage collector error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11311 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11312 | return -1; |
Christopher Faulet | 08ed98f | 2020-07-28 10:33:25 +0200 | [diff] [blame] | 11313 | #endif |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11314 | default: |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11315 | memprintf(err, "Lua unknown error: %s", lua_tostring(L, -1)); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11316 | lua_pop(L, 1); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11317 | return -1; |
| 11318 | } |
| 11319 | |
| 11320 | return 0; |
| 11321 | } |
| 11322 | |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11323 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
Willy Tarreau | 0182516 | 2021-03-09 09:53:46 +0100 | [diff] [blame] | 11324 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11325 | char **err) |
| 11326 | { |
| 11327 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11328 | memprintf(err, "'%s' expects a file name as parameter.", args[0]); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11329 | return -1; |
| 11330 | } |
| 11331 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11332 | /* loading for global state */ |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11333 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11334 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11335 | return hlua_load_state(&args[1], hlua_states[0], err); |
Thierry Fournier | c93c15c | 2020-11-28 15:02:13 +0100 | [diff] [blame] | 11336 | } |
| 11337 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11338 | 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] | 11339 | const struct proxy *defpx, const char *file, int line, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11340 | char **err) |
| 11341 | { |
| 11342 | int len; |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11343 | int i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11344 | |
| 11345 | if (*(args[1]) == 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11346 | memprintf(err, "'%s' expects a file as parameter.", args[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11347 | return -1; |
| 11348 | } |
| 11349 | |
| 11350 | if (per_thread_load == NULL) { |
| 11351 | /* allocate the first entry large enough to store the final NULL */ |
| 11352 | per_thread_load = calloc(1, sizeof(*per_thread_load)); |
| 11353 | if (per_thread_load == NULL) { |
| 11354 | memprintf(err, "out of memory error"); |
| 11355 | return -1; |
| 11356 | } |
| 11357 | } |
| 11358 | |
| 11359 | /* count used entries */ |
| 11360 | for (len = 0; per_thread_load[len] != NULL; len++) |
| 11361 | ; |
| 11362 | |
| 11363 | per_thread_load = realloc(per_thread_load, (len + 2) * sizeof(*per_thread_load)); |
| 11364 | if (per_thread_load == NULL) { |
| 11365 | memprintf(err, "out of memory error"); |
| 11366 | return -1; |
| 11367 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11368 | per_thread_load[len + 1] = NULL; |
| 11369 | |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11370 | /* count args excepting the first, allocate array and copy args */ |
| 11371 | for (i = 0; *(args[i + 1]) != 0; i++); |
Aurelien DARRAGON | b12d169 | 2022-09-23 08:48:34 +0200 | [diff] [blame] | 11372 | per_thread_load[len] = calloc(i + 1, sizeof(*per_thread_load[len])); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11373 | if (per_thread_load[len] == NULL) { |
| 11374 | memprintf(err, "out of memory error"); |
| 11375 | return -1; |
| 11376 | } |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11377 | for (i = 1; *(args[i]) != 0; i++) { |
| 11378 | per_thread_load[len][i - 1] = strdup(args[i]); |
| 11379 | if (per_thread_load[len][i - 1] == NULL) { |
| 11380 | memprintf(err, "out of memory error"); |
| 11381 | return -1; |
| 11382 | } |
| 11383 | } |
| 11384 | per_thread_load[len][i - 1] = strdup(""); |
| 11385 | if (per_thread_load[len][i - 1] == NULL) { |
| 11386 | memprintf(err, "out of memory error"); |
| 11387 | return -1; |
| 11388 | } |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11389 | |
| 11390 | /* loading for thread 1 only */ |
| 11391 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11392 | ha_set_thread(NULL); |
Thierry Fournier | ae6b568 | 2022-09-19 09:04:16 +0200 | [diff] [blame] | 11393 | return hlua_load_state(per_thread_load[len], hlua_states[1], err); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11394 | } |
| 11395 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11396 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 11397 | * in the given <ctx>. |
| 11398 | */ |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11399 | static int hlua_prepend_path(lua_State *L, char *type, char *path) |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11400 | { |
Thierry Fournier | 3fb9e51 | 2020-11-28 10:13:12 +0100 | [diff] [blame] | 11401 | lua_getglobal(L, "package"); /* push package variable */ |
| 11402 | lua_pushstring(L, path); /* push given path */ |
| 11403 | lua_pushstring(L, ";"); /* push semicolon */ |
| 11404 | lua_getfield(L, -3, type); /* push old path */ |
| 11405 | lua_concat(L, 3); /* concatenate to new path */ |
| 11406 | lua_setfield(L, -2, type); /* store new path */ |
| 11407 | lua_pop(L, 1); /* pop package variable */ |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 11408 | |
| 11409 | return 0; |
| 11410 | } |
| 11411 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11412 | 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] | 11413 | const struct proxy *defpx, const char *file, int line, |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11414 | char **err) |
| 11415 | { |
| 11416 | char *path; |
| 11417 | char *type = "path"; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11418 | struct prepend_path *p = NULL; |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11419 | size_t i; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11420 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11421 | if (too_many_args(2, args, err, NULL)) { |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11422 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11423 | } |
| 11424 | |
| 11425 | if (!(*args[1])) { |
| 11426 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11427 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11428 | } |
| 11429 | path = args[1]; |
| 11430 | |
| 11431 | if (*args[2]) { |
| 11432 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 11433 | 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] | 11434 | goto err; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11435 | } |
| 11436 | type = args[2]; |
| 11437 | } |
| 11438 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11439 | p = calloc(1, sizeof(*p)); |
| 11440 | if (p == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11441 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11442 | goto err; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11443 | } |
| 11444 | p->path = strdup(path); |
| 11445 | if (p->path == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11446 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11447 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11448 | } |
| 11449 | p->type = strdup(type); |
| 11450 | if (p->type == NULL) { |
Tim Duesterhus | f89d43a | 2021-01-03 20:04:37 +0100 | [diff] [blame] | 11451 | memprintf(err, "memory allocation failed"); |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11452 | goto err2; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11453 | } |
Willy Tarreau | 2b71810 | 2021-04-21 07:32:39 +0200 | [diff] [blame] | 11454 | LIST_APPEND(&prepend_path_list, &p->l); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11455 | |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11456 | /* Handle the global state and the per-thread state for the first |
| 11457 | * thread. The remaining threads will be initialized based on |
| 11458 | * prepend_path_list. |
| 11459 | */ |
Bertrand Jacquin | 7fbc770 | 2021-11-24 21:16:06 +0000 | [diff] [blame] | 11460 | for (i = 0; i < 2; i++) { |
Tim Duesterhus | 9e5e586 | 2021-10-11 18:51:08 +0200 | [diff] [blame] | 11461 | lua_State *L = hlua_states[i]; |
| 11462 | const char *error; |
| 11463 | |
| 11464 | if (setjmp(safe_ljmp_env) != 0) { |
| 11465 | lua_atpanic(L, hlua_panic_safe); |
| 11466 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11467 | error = lua_tostring(L, -1); |
| 11468 | else |
| 11469 | error = "critical error"; |
| 11470 | fprintf(stderr, "lua-prepend-path: %s.\n", error); |
| 11471 | exit(1); |
| 11472 | } else { |
| 11473 | lua_atpanic(L, hlua_panic_ljmp); |
| 11474 | } |
| 11475 | |
| 11476 | hlua_prepend_path(L, type, path); |
| 11477 | |
| 11478 | lua_atpanic(L, hlua_panic_safe); |
| 11479 | } |
| 11480 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11481 | return 0; |
Tim Duesterhus | 621e74a | 2021-01-03 20:04:36 +0100 | [diff] [blame] | 11482 | |
| 11483 | err2: |
| 11484 | free(p->type); |
| 11485 | free(p->path); |
| 11486 | err: |
| 11487 | free(p); |
| 11488 | return -1; |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11489 | } |
| 11490 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11491 | /* configuration keywords declaration */ |
| 11492 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 11493 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11494 | { CFG_GLOBAL, "lua-load", hlua_load }, |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11495 | { CFG_GLOBAL, "lua-load-per-thread", hlua_load_per_thread }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 11496 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 11497 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 11498 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 11499 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11500 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 11501 | { 0, NULL, NULL }, |
| 11502 | }}; |
| 11503 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 11504 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 11505 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11506 | #ifdef USE_OPENSSL |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 11507 | |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11508 | /* |
| 11509 | * This function replace a ckch_store by another one, and rebuild the ckch_inst and all its dependencies. |
| 11510 | * It does the sam as "cli_io_handler_commit_cert" but for lua, the major |
| 11511 | * difference is that the yield in lua and for the CLI is not handled the same |
| 11512 | * way. |
| 11513 | */ |
| 11514 | __LJMP static int hlua_ckch_commit_yield(lua_State *L, int status, lua_KContext ctx) |
| 11515 | { |
| 11516 | struct ckch_inst **lua_ckchi = lua_touserdata(L, -1); |
| 11517 | struct ckch_store **lua_ckchs = lua_touserdata(L, -2); |
| 11518 | struct ckch_inst *ckchi = *lua_ckchi; |
| 11519 | struct ckch_store *old_ckchs = lua_ckchs[0]; |
| 11520 | struct ckch_store *new_ckchs = lua_ckchs[1]; |
| 11521 | struct hlua *hlua; |
| 11522 | char *err = NULL; |
| 11523 | int y = 1; |
| 11524 | |
| 11525 | hlua = hlua_gethlua(L); |
| 11526 | |
| 11527 | /* get the first ckchi to copy */ |
| 11528 | if (ckchi == NULL) |
| 11529 | ckchi = LIST_ELEM(old_ckchs->ckch_inst.n, typeof(ckchi), by_ckchs); |
| 11530 | |
| 11531 | /* walk through the old ckch_inst and creates new ckch_inst using the updated ckchs */ |
| 11532 | list_for_each_entry_from(ckchi, &old_ckchs->ckch_inst, by_ckchs) { |
| 11533 | struct ckch_inst *new_inst; |
| 11534 | |
| 11535 | /* it takes a lot of CPU to creates SSL_CTXs, so we yield every 10 CKCH instances */ |
| 11536 | if (y % 10 == 0) { |
| 11537 | |
| 11538 | *lua_ckchi = ckchi; |
| 11539 | |
| 11540 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11541 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11542 | } |
| 11543 | |
| 11544 | if (ckch_inst_rebuild(new_ckchs, ckchi, &new_inst, &err)) |
| 11545 | goto error; |
| 11546 | |
| 11547 | /* link the new ckch_inst to the duplicate */ |
| 11548 | LIST_APPEND(&new_ckchs->ckch_inst, &new_inst->by_ckchs); |
| 11549 | y++; |
| 11550 | } |
| 11551 | |
| 11552 | /* The generation is finished, we can insert everything */ |
| 11553 | ckch_store_replace(old_ckchs, new_ckchs); |
| 11554 | |
| 11555 | lua_pop(L, 2); /* pop the lua_ckchs and ckchi */ |
| 11556 | |
| 11557 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11558 | |
| 11559 | return 0; |
| 11560 | |
| 11561 | error: |
| 11562 | ckch_store_free(new_ckchs); |
| 11563 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11564 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11565 | free(err); |
| 11566 | |
| 11567 | return 0; |
| 11568 | } |
| 11569 | |
| 11570 | /* |
| 11571 | * Replace a ckch_store <filename> in the ckchs_tree with a ckch_store created |
| 11572 | * from the table in parameter. |
| 11573 | * |
| 11574 | * This is equivalent to "set ssl cert" + "commit ssl cert" over the CLI, which |
| 11575 | * means it does not need to have a transaction since everything is done in the |
| 11576 | * same function. |
| 11577 | * |
| 11578 | * CertCache.set{filename="", crt="", key="", sctl="", ocsp="", issuer=""} |
| 11579 | * |
| 11580 | */ |
| 11581 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11582 | { |
| 11583 | struct hlua *hlua; |
| 11584 | struct ckch_inst **lua_ckchi; |
| 11585 | struct ckch_store **lua_ckchs; |
| 11586 | struct ckch_store *old_ckchs = NULL; |
| 11587 | struct ckch_store *new_ckchs = NULL; |
| 11588 | int errcode = 0; |
| 11589 | char *err = NULL; |
| 11590 | struct cert_exts *cert_ext = NULL; |
| 11591 | char *filename; |
| 11592 | struct cert_key_and_chain *ckch; |
| 11593 | int ret; |
| 11594 | |
| 11595 | if (lua_type(L, -1) != LUA_TTABLE) |
| 11596 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs a table as argument")); |
| 11597 | |
| 11598 | hlua = hlua_gethlua(L); |
| 11599 | |
| 11600 | /* FIXME: this should not return an error but should come back later */ |
| 11601 | if (HA_SPIN_TRYLOCK(CKCH_LOCK, &ckch_lock)) |
| 11602 | WILL_LJMP(luaL_error(L, "CertCache already under lock")); |
| 11603 | |
| 11604 | ret = lua_getfield(L, -1, "filename"); |
| 11605 | if (ret != LUA_TSTRING) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11606 | memprintf(&err, "%sNo filename specified!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11607 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11608 | goto end; |
| 11609 | } |
| 11610 | filename = (char *)lua_tostring(L, -1); |
| 11611 | |
| 11612 | |
| 11613 | /* look for the filename in the tree */ |
| 11614 | old_ckchs = ckchs_lookup(filename); |
| 11615 | if (!old_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11616 | 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] | 11617 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11618 | goto end; |
| 11619 | } |
| 11620 | /* TODO: handle extra_files_noext */ |
| 11621 | |
| 11622 | new_ckchs = ckchs_dup(old_ckchs); |
| 11623 | if (!new_ckchs) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11624 | memprintf(&err, "%sCannot allocate memory!", err ? err : ""); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11625 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11626 | goto end; |
| 11627 | } |
| 11628 | |
| 11629 | ckch = new_ckchs->ckch; |
| 11630 | |
| 11631 | /* loop on the field in the table, which have the same name as the |
| 11632 | * possible extensions of files */ |
| 11633 | lua_pushnil(L); |
| 11634 | while (lua_next(L, 1)) { |
| 11635 | int i; |
| 11636 | const char *field = lua_tostring(L, -2); |
| 11637 | char *payload = (char *)lua_tostring(L, -1); |
| 11638 | |
| 11639 | if (!field || strcmp(field, "filename") == 0) { |
| 11640 | lua_pop(L, 1); |
| 11641 | continue; |
| 11642 | } |
| 11643 | |
| 11644 | for (i = 0; field && cert_exts[i].ext != NULL; i++) { |
| 11645 | if (strcmp(field, cert_exts[i].ext) == 0) { |
| 11646 | cert_ext = &cert_exts[i]; |
| 11647 | break; |
| 11648 | } |
| 11649 | } |
| 11650 | |
| 11651 | /* this is the default type, the field is not supported */ |
| 11652 | if (cert_ext == NULL) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11653 | memprintf(&err, "%sUnsupported field '%s'", err ? err : "", field); |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 11654 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11655 | goto end; |
| 11656 | } |
| 11657 | |
| 11658 | /* appply the change on the duplicate */ |
William Lallemand | d8c195a | 2022-05-26 11:20:13 +0200 | [diff] [blame] | 11659 | if (cert_ext->load(filename, payload, ckch, &err) != 0) { |
Thierry Fournier | 70e38e9 | 2022-09-19 09:01:53 +0200 | [diff] [blame] | 11660 | 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] | 11661 | errcode |= ERR_ALERT | ERR_FATAL; |
| 11662 | goto end; |
| 11663 | } |
| 11664 | lua_pop(L, 1); |
| 11665 | } |
| 11666 | |
| 11667 | /* store the pointers on the lua stack */ |
| 11668 | lua_ckchs = lua_newuserdata(L, sizeof(struct ckch_store *) * 2); |
| 11669 | lua_ckchs[0] = old_ckchs; |
| 11670 | lua_ckchs[1] = new_ckchs; |
| 11671 | lua_ckchi = lua_newuserdata(L, sizeof(struct ckch_inst *)); |
| 11672 | *lua_ckchi = NULL; |
| 11673 | |
| 11674 | task_wakeup(hlua->task, TASK_WOKEN_MSG); |
| 11675 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_ckch_commit_yield, TICK_ETERNITY, 0)); |
| 11676 | |
| 11677 | end: |
| 11678 | HA_SPIN_UNLOCK(CKCH_LOCK, &ckch_lock); |
| 11679 | |
| 11680 | if (errcode & ERR_CODE) { |
| 11681 | ckch_store_free(new_ckchs); |
| 11682 | WILL_LJMP(luaL_error(L, "%s", err)); |
| 11683 | } |
| 11684 | free(err); |
| 11685 | |
| 11686 | return 0; |
| 11687 | } |
| 11688 | |
William Lallemand | 5213918 | 2022-03-30 15:05:42 +0200 | [diff] [blame] | 11689 | #else |
| 11690 | |
| 11691 | __LJMP static int hlua_ckch_set(lua_State *L) |
| 11692 | { |
| 11693 | WILL_LJMP(luaL_error(L, "'CertCache.set' needs an HAProxy built with OpenSSL")); |
| 11694 | |
| 11695 | return 0; |
| 11696 | } |
| 11697 | #endif /* ! USE_OPENSSL */ |
| 11698 | |
| 11699 | |
| 11700 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 11701 | /* This function can fail with an abort() due to an Lua critical error. |
| 11702 | * We are in the initialisation process of HAProxy, this abort() is |
| 11703 | * tolerated. |
| 11704 | */ |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11705 | int hlua_post_init_state(lua_State *L) |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11706 | { |
| 11707 | struct hlua_init_function *init; |
| 11708 | const char *msg; |
| 11709 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11710 | const char *error; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11711 | const char *kind; |
| 11712 | const char *trace; |
| 11713 | int return_status = 1; |
| 11714 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
| 11715 | int nres; |
| 11716 | #endif |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11717 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11718 | /* disable memory limit checks if limit is not set */ |
| 11719 | if (!hlua_global_allocator.limit) |
| 11720 | hlua_global_allocator.limit = ~hlua_global_allocator.limit; |
| 11721 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 11722 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11723 | if (setjmp(safe_ljmp_env) != 0) { |
| 11724 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11725 | if (lua_type(L, -1) == LUA_TSTRING) |
| 11726 | error = lua_tostring(L, -1); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11727 | else |
| 11728 | error = "critical error"; |
| 11729 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 11730 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11731 | } else { |
| 11732 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11733 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 11734 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11735 | hlua_fcn_post_init(L); |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 11736 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11737 | list_for_each_entry(init, &hlua_init_functions[hlua_state_id], l) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11738 | lua_rawgeti(L, LUA_REGISTRYINDEX, init->function_ref); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11739 | |
| 11740 | #if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM >= 504 |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11741 | ret = lua_resume(L, L, 0, &nres); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11742 | #else |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11743 | ret = lua_resume(L, L, 0); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11744 | #endif |
| 11745 | kind = NULL; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11746 | switch (ret) { |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11747 | |
| 11748 | case LUA_OK: |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11749 | lua_pop(L, -1); |
Thierry Fournier | 13d08b7 | 2020-11-28 11:02:58 +0100 | [diff] [blame] | 11750 | break; |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11751 | |
| 11752 | case LUA_ERRERR: |
| 11753 | kind = "message handler error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11754 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11755 | case LUA_ERRRUN: |
| 11756 | if (!kind) |
| 11757 | kind = "runtime error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11758 | msg = lua_tostring(L, -1); |
| 11759 | lua_settop(L, 0); /* Empty the stack. */ |
| 11760 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11761 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11762 | if (msg) |
| 11763 | ha_alert("Lua init: %s: '%s' from %s\n", kind, msg, trace); |
| 11764 | else |
| 11765 | ha_alert("Lua init: unknown %s from %s\n", kind, trace); |
| 11766 | return_status = 0; |
| 11767 | break; |
| 11768 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11769 | default: |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11770 | /* Unknown error */ |
| 11771 | kind = "Unknown error"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11772 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11773 | case LUA_YIELD: |
| 11774 | /* yield is not configured at this step, this state doesn't happen */ |
| 11775 | if (!kind) |
| 11776 | kind = "yield not allowed"; |
Thayne McCombs | 8f0cc5c | 2021-01-07 21:35:52 -0700 | [diff] [blame] | 11777 | /* Fall through */ |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11778 | case LUA_ERRMEM: |
| 11779 | if (!kind) |
| 11780 | kind = "out of memory error"; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11781 | lua_settop(L, 0); |
| 11782 | lua_pop(L, 1); |
Christopher Faulet | d09cc51 | 2021-03-24 14:48:45 +0100 | [diff] [blame] | 11783 | trace = hlua_traceback(L, ", "); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11784 | ha_alert("Lua init: %s: %s\n", kind, trace); |
| 11785 | return_status = 0; |
| 11786 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11787 | } |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11788 | if (!return_status) |
| 11789 | break; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11790 | } |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 11791 | |
| 11792 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 670db24 | 2020-11-28 10:49:59 +0100 | [diff] [blame] | 11793 | return return_status; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 11794 | } |
| 11795 | |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11796 | int hlua_post_init() |
| 11797 | { |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11798 | int ret; |
| 11799 | int i; |
| 11800 | int errors; |
| 11801 | char *err = NULL; |
| 11802 | struct hlua_function *fcn; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11803 | struct hlua_reg_filter *reg_flt; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11804 | |
Willy Tarreau | b131049 | 2021-08-30 09:35:18 +0200 | [diff] [blame] | 11805 | #if defined(USE_OPENSSL) |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11806 | /* Initialize SSL server. */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11807 | if (socket_ssl->xprt->prepare_srv) { |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11808 | int saved_used_backed = global.ssl_used_backend; |
| 11809 | // don't affect maxconn automatic computation |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 11810 | socket_ssl->xprt->prepare_srv(socket_ssl); |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11811 | global.ssl_used_backend = saved_used_backed; |
| 11812 | } |
| 11813 | #endif |
| 11814 | |
Thierry Fournier | c749259 | 2020-11-28 23:57:24 +0100 | [diff] [blame] | 11815 | /* Perform post init of common thread */ |
| 11816 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11817 | ha_set_thread(&ha_thread_info[0]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11818 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11819 | if (ret == 0) |
| 11820 | return 0; |
| 11821 | |
| 11822 | /* init remaining lua states and load files */ |
| 11823 | for (hlua_state_id = 2; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11824 | |
| 11825 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11826 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11827 | |
| 11828 | /* Init lua state */ |
| 11829 | hlua_states[hlua_state_id] = hlua_init_state(hlua_state_id); |
| 11830 | |
| 11831 | /* Load lua files */ |
| 11832 | for (i = 0; per_thread_load && per_thread_load[i]; i++) { |
| 11833 | ret = hlua_load_state(per_thread_load[i], hlua_states[hlua_state_id], &err); |
| 11834 | if (ret != 0) { |
| 11835 | ha_alert("Lua init: %s\n", err); |
| 11836 | return 0; |
| 11837 | } |
| 11838 | } |
| 11839 | } |
| 11840 | |
| 11841 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11842 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11843 | |
| 11844 | /* Execute post init for all states */ |
| 11845 | for (hlua_state_id = 1; hlua_state_id < global.nbthread + 1; hlua_state_id++) { |
| 11846 | |
| 11847 | /* set thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11848 | ha_set_thread(&ha_thread_info[hlua_state_id - 1]); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11849 | |
| 11850 | /* run post init */ |
| 11851 | ret = hlua_post_init_state(hlua_states[hlua_state_id]); |
| 11852 | if (ret == 0) |
| 11853 | return 0; |
| 11854 | } |
| 11855 | |
| 11856 | /* Reset thread context */ |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 11857 | ha_set_thread(NULL); |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11858 | |
| 11859 | /* control functions registering. Each function must have: |
| 11860 | * - only the function_ref[0] set positive and all other to -1 |
| 11861 | * - only the function_ref[0] set to -1 and all other positive |
| 11862 | * This ensure a same reference is not used both in shared |
| 11863 | * lua state and thread dedicated lua state. Note: is the case |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11864 | * reach, the shared state is priority, but the bug will be |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11865 | * complicated to found for the end user. |
| 11866 | */ |
| 11867 | errors = 0; |
| 11868 | list_for_each_entry(fcn, &referenced_functions, l) { |
| 11869 | ret = 0; |
| 11870 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11871 | if (fcn->function_ref[i] == -1) |
| 11872 | ret--; |
| 11873 | else |
| 11874 | ret++; |
| 11875 | } |
| 11876 | if (abs(ret) != global.nbthread) { |
| 11877 | ha_alert("Lua function '%s' is not referenced in all thread. " |
| 11878 | "Expect function in all thread or in none thread.\n", fcn->name); |
| 11879 | errors++; |
| 11880 | continue; |
| 11881 | } |
| 11882 | |
| 11883 | if ((fcn->function_ref[0] == -1) == (ret < 0)) { |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11884 | ha_alert("Lua function '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11885 | "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] | 11886 | "exclusive.\n", fcn->name); |
| 11887 | errors++; |
| 11888 | } |
| 11889 | } |
| 11890 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 11891 | /* Do the same with registered filters */ |
| 11892 | list_for_each_entry(reg_flt, &referenced_filters, l) { |
| 11893 | ret = 0; |
| 11894 | for (i = 1; i < global.nbthread + 1; i++) { |
| 11895 | if (reg_flt->flt_ref[i] == -1) |
| 11896 | ret--; |
| 11897 | else |
| 11898 | ret++; |
| 11899 | } |
| 11900 | if (abs(ret) != global.nbthread) { |
| 11901 | ha_alert("Lua filter '%s' is not referenced in all thread. " |
| 11902 | "Expect function in all thread or in none thread.\n", reg_flt->name); |
| 11903 | errors++; |
| 11904 | continue; |
| 11905 | } |
| 11906 | |
| 11907 | if ((reg_flt->flt_ref[0] == -1) == (ret < 0)) { |
| 11908 | ha_alert("Lua filter '%s' is referenced both ins shared Lua context (through lua-load) " |
| 11909 | "and per-thread Lua context (through lua-load-per-thread). these two context " |
| 11910 | "exclusive.\n", fcn->name); |
| 11911 | errors++; |
| 11912 | } |
| 11913 | } |
| 11914 | |
| 11915 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11916 | if (errors > 0) |
| 11917 | return 0; |
| 11918 | |
Ilya Shipitsin | b8888ab | 2021-01-06 21:20:16 +0500 | [diff] [blame] | 11919 | /* 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] | 11920 | * -1 in order to have probably a segfault if someone use it |
| 11921 | */ |
| 11922 | hlua_state_id = -1; |
| 11923 | |
| 11924 | return 1; |
Thierry Fournier | b8cef17 | 2020-11-28 15:37:17 +0100 | [diff] [blame] | 11925 | } |
| 11926 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11927 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 11928 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 11929 | * 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] | 11930 | * allocation. <nsize> is the requested new size. A new allocation is |
| 11931 | * indicated by <ptr> being NULL. A free is indicated by <nsize> being |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11932 | * zero. This one verifies that the limits are respected but is optimized |
| 11933 | * 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] | 11934 | * |
| 11935 | * Warning: while this API ressembles glibc's realloc() a lot, glibc surpasses |
| 11936 | * POSIX by making realloc(ptr,0) an effective free(), but others do not do |
| 11937 | * that and will simply allocate zero as if it were the result of malloc(0), |
| 11938 | * so mapping this onto realloc() will lead to memory leaks on non-glibc |
| 11939 | * systems. |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11940 | */ |
| 11941 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 11942 | { |
| 11943 | struct hlua_mem_allocator *zone = ud; |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11944 | size_t limit, old, new; |
| 11945 | |
| 11946 | /* a limit of ~0 means unlimited and boot complete, so there's no need |
| 11947 | * for accounting anymore. |
| 11948 | */ |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11949 | if (likely(~zone->limit == 0)) { |
| 11950 | if (!nsize) |
| 11951 | ha_free(&ptr); |
| 11952 | else |
| 11953 | ptr = realloc(ptr, nsize); |
| 11954 | return ptr; |
| 11955 | } |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11956 | |
Willy Tarreau | d36c7fa | 2020-12-02 12:26:29 +0100 | [diff] [blame] | 11957 | if (!ptr) |
| 11958 | osize = 0; |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11959 | |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11960 | /* enforce strict limits across all threads */ |
| 11961 | limit = zone->limit; |
| 11962 | old = _HA_ATOMIC_LOAD(&zone->allocated); |
| 11963 | do { |
| 11964 | new = old + nsize - osize; |
| 11965 | if (unlikely(nsize && limit && new > limit)) |
| 11966 | return NULL; |
| 11967 | } while (!_HA_ATOMIC_CAS(&zone->allocated, &old, new)); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11968 | |
Willy Tarreau | a5efdff | 2021-10-22 16:00:02 +0200 | [diff] [blame] | 11969 | if (!nsize) |
| 11970 | ha_free(&ptr); |
| 11971 | else |
| 11972 | ptr = realloc(ptr, nsize); |
Willy Tarreau | cdb5346 | 2020-12-02 12:12:00 +0100 | [diff] [blame] | 11973 | |
| 11974 | if (unlikely(!ptr && nsize)) // failed |
| 11975 | _HA_ATOMIC_SUB(&zone->allocated, nsize - osize); |
| 11976 | |
| 11977 | __ha_barrier_atomic_store(); |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 11978 | return ptr; |
| 11979 | } |
| 11980 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11981 | /* 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] | 11982 | * We are in the initialisation process of HAProxy, this abort() is |
| 11983 | * tolerated. |
| 11984 | */ |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 11985 | lua_State *hlua_init_state(int thread_num) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 11986 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11987 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11988 | int idx; |
| 11989 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 11990 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 11991 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 11992 | const char *error_msg; |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 11993 | void **context; |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11994 | lua_State *L; |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 11995 | struct prepend_path *pp; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 11996 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11997 | /* Init main lua stack. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 11998 | L = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 11999 | |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12000 | /* Initialise Lua context to NULL */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12001 | context = lua_getextraspace(L); |
Thierry Fournier | 4234dbd | 2020-11-28 13:18:23 +0100 | [diff] [blame] | 12002 | *context = NULL; |
| 12003 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12004 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 12005 | * the Lua function can fail with an abort. We are in the initialisation |
| 12006 | * process of HAProxy, this abort() is tolerated. |
| 12007 | */ |
| 12008 | |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12009 | /* Call post initialisation function in safe environment. */ |
| 12010 | if (setjmp(safe_ljmp_env) != 0) { |
| 12011 | lua_atpanic(L, hlua_panic_safe); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12012 | if (lua_type(L, -1) == LUA_TSTRING) |
| 12013 | error_msg = lua_tostring(L, -1); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12014 | else |
| 12015 | error_msg = "critical error"; |
| 12016 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
| 12017 | exit(1); |
Thierry Fournier | 3c53932 | 2020-11-28 16:05:05 +0100 | [diff] [blame] | 12018 | } else { |
| 12019 | lua_atpanic(L, hlua_panic_ljmp); |
Thierry Fournier | 2f05cc6 | 2020-11-28 16:08:02 +0100 | [diff] [blame] | 12020 | } |
| 12021 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 12022 | /* Initialise lua. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12023 | luaL_openlibs(L); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12024 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 12025 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 12026 | #ifdef HLUA_PREPEND_PATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12027 | hlua_prepend_path(L, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12028 | #endif |
| 12029 | #ifdef HLUA_PREPEND_CPATH |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12030 | hlua_prepend_path(L, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 12031 | #endif |
| 12032 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 12033 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12034 | |
Thierry Fournier | 59f11be | 2020-11-29 00:37:41 +0100 | [diff] [blame] | 12035 | /* Apply configured prepend path */ |
| 12036 | list_for_each_entry(pp, &prepend_path_list, l) |
| 12037 | hlua_prepend_path(L, pp->type, pp->path); |
| 12038 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12039 | /* |
| 12040 | * |
| 12041 | * Create "core" object. |
| 12042 | * |
| 12043 | */ |
| 12044 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 12045 | /* This table entry is the object "core" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12046 | lua_newtable(L); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12047 | |
Thierry Fournier | ecb83c2 | 2020-11-28 15:49:44 +0100 | [diff] [blame] | 12048 | /* set the thread id */ |
| 12049 | hlua_class_const_int(L, "thread", thread_num); |
| 12050 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12051 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 12052 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12053 | hlua_class_const_int(L, log_levels[i], i); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 12054 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12055 | /* Register special functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12056 | hlua_class_function(L, "register_init", hlua_register_init); |
| 12057 | hlua_class_function(L, "register_task", hlua_register_task); |
| 12058 | hlua_class_function(L, "register_fetches", hlua_register_fetches); |
| 12059 | hlua_class_function(L, "register_converters", hlua_register_converters); |
| 12060 | hlua_class_function(L, "register_action", hlua_register_action); |
| 12061 | hlua_class_function(L, "register_service", hlua_register_service); |
| 12062 | hlua_class_function(L, "register_cli", hlua_register_cli); |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12063 | hlua_class_function(L, "register_filter", hlua_register_filter); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12064 | hlua_class_function(L, "yield", hlua_yield); |
| 12065 | hlua_class_function(L, "set_nice", hlua_set_nice); |
| 12066 | hlua_class_function(L, "sleep", hlua_sleep); |
| 12067 | hlua_class_function(L, "msleep", hlua_msleep); |
| 12068 | hlua_class_function(L, "add_acl", hlua_add_acl); |
| 12069 | hlua_class_function(L, "del_acl", hlua_del_acl); |
| 12070 | hlua_class_function(L, "set_map", hlua_set_map); |
| 12071 | hlua_class_function(L, "del_map", hlua_del_map); |
| 12072 | hlua_class_function(L, "tcp", hlua_socket_new); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12073 | hlua_class_function(L, "httpclient", hlua_httpclient_new); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12074 | hlua_class_function(L, "log", hlua_log); |
| 12075 | hlua_class_function(L, "Debug", hlua_log_debug); |
| 12076 | hlua_class_function(L, "Info", hlua_log_info); |
| 12077 | hlua_class_function(L, "Warning", hlua_log_warning); |
| 12078 | hlua_class_function(L, "Alert", hlua_log_alert); |
| 12079 | hlua_class_function(L, "done", hlua_done); |
| 12080 | hlua_fcn_reg_core_fcn(L); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 12081 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12082 | lua_setglobal(L, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12083 | |
| 12084 | /* |
| 12085 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12086 | * Create "act" object. |
| 12087 | * |
| 12088 | */ |
| 12089 | |
| 12090 | /* This table entry is the object "act" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12091 | lua_newtable(L); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12092 | |
| 12093 | /* push action return constants */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12094 | hlua_class_const_int(L, "CONTINUE", ACT_RET_CONT); |
| 12095 | hlua_class_const_int(L, "STOP", ACT_RET_STOP); |
| 12096 | hlua_class_const_int(L, "YIELD", ACT_RET_YIELD); |
| 12097 | hlua_class_const_int(L, "ERROR", ACT_RET_ERR); |
| 12098 | hlua_class_const_int(L, "DONE", ACT_RET_DONE); |
| 12099 | hlua_class_const_int(L, "DENY", ACT_RET_DENY); |
| 12100 | hlua_class_const_int(L, "ABORT", ACT_RET_ABRT); |
| 12101 | hlua_class_const_int(L, "INVALID", ACT_RET_INV); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12102 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12103 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 12104 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12105 | lua_setglobal(L, "act"); |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 12106 | |
| 12107 | /* |
| 12108 | * |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12109 | * Create "Filter" object. |
| 12110 | * |
| 12111 | */ |
| 12112 | |
| 12113 | /* This table entry is the object "filter" base. */ |
| 12114 | lua_newtable(L); |
| 12115 | |
| 12116 | /* push flags and constants */ |
| 12117 | hlua_class_const_int(L, "CONTINUE", 1); |
| 12118 | hlua_class_const_int(L, "WAIT", 0); |
| 12119 | hlua_class_const_int(L, "ERROR", -1); |
| 12120 | |
| 12121 | hlua_class_const_int(L, "FLT_CFG_FL_HTX", FLT_CFG_FL_HTX); |
| 12122 | |
Christopher Faulet | c404f11 | 2020-02-26 15:03:09 +0100 | [diff] [blame] | 12123 | hlua_class_function(L, "wake_time", hlua_set_wake_time); |
| 12124 | hlua_class_function(L, "register_data_filter", hlua_register_data_filter); |
| 12125 | hlua_class_function(L, "unregister_data_filter", hlua_unregister_data_filter); |
| 12126 | |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12127 | lua_setglobal(L, "filter"); |
| 12128 | |
| 12129 | /* |
| 12130 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12131 | * Register class Map |
| 12132 | * |
| 12133 | */ |
| 12134 | |
| 12135 | /* This table entry is the object "Map" base. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12136 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12137 | |
| 12138 | /* register pattern types. */ |
| 12139 | for (i=0; i<PAT_MATCH_NUM; i++) |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12140 | hlua_class_const_int(L, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12141 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12142 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12143 | hlua_class_const_int(L, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 12144 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12145 | |
| 12146 | /* register constructor. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12147 | hlua_class_function(L, "new", hlua_map_new); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12148 | |
| 12149 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12150 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12151 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12152 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12153 | lua_pushstring(L, "__index"); |
| 12154 | lua_newtable(L); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12155 | |
| 12156 | /* Register . */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12157 | hlua_class_function(L, "lookup", hlua_map_lookup); |
| 12158 | hlua_class_function(L, "slookup", hlua_map_slookup); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12159 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12160 | lua_rawset(L, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12161 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 12162 | /* Register previous table in the registry with reference and named entry. |
| 12163 | * The function hlua_register_metatable() pops the stack, so we |
| 12164 | * previously create a copy of the table. |
| 12165 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12166 | lua_pushvalue(L, -1); /* Copy the -1 entry and push it on the stack. */ |
| 12167 | class_map_ref = hlua_register_metatable(L, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12168 | |
| 12169 | /* Assign the metatable to the mai Map object. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12170 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12171 | |
| 12172 | /* Set a name to the table. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12173 | lua_setglobal(L, "Map"); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 12174 | |
| 12175 | /* |
| 12176 | * |
William Lallemand | 30fcca1 | 2022-03-30 12:03:12 +0200 | [diff] [blame] | 12177 | * Register "CertCache" class |
| 12178 | * |
| 12179 | */ |
| 12180 | |
| 12181 | /* Create and fill the metatable. */ |
| 12182 | lua_newtable(L); |
| 12183 | /* Register */ |
| 12184 | hlua_class_function(L, "set", hlua_ckch_set); |
| 12185 | lua_setglobal(L, CLASS_CERTCACHE); /* Create global object called Regex */ |
| 12186 | |
| 12187 | /* |
| 12188 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12189 | * Register class Channel |
| 12190 | * |
| 12191 | */ |
| 12192 | |
| 12193 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12194 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12195 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12196 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12197 | lua_pushstring(L, "__index"); |
| 12198 | lua_newtable(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12199 | |
| 12200 | /* Register . */ |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12201 | hlua_class_function(L, "data", hlua_channel_get_data); |
| 12202 | hlua_class_function(L, "line", hlua_channel_get_line); |
| 12203 | hlua_class_function(L, "set", hlua_channel_set_data); |
| 12204 | hlua_class_function(L, "remove", hlua_channel_del_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12205 | hlua_class_function(L, "append", hlua_channel_append); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12206 | hlua_class_function(L, "prepend", hlua_channel_prepend); |
| 12207 | hlua_class_function(L, "insert", hlua_channel_insert_data); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12208 | hlua_class_function(L, "send", hlua_channel_send); |
| 12209 | hlua_class_function(L, "forward", hlua_channel_forward); |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12210 | hlua_class_function(L, "input", hlua_channel_get_in_len); |
| 12211 | hlua_class_function(L, "output", hlua_channel_get_out_len); |
| 12212 | hlua_class_function(L, "may_recv", hlua_channel_may_recv); |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12213 | hlua_class_function(L, "is_full", hlua_channel_is_full); |
| 12214 | hlua_class_function(L, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12215 | |
Christopher Faulet | 6a79fc1 | 2021-08-06 16:02:36 +0200 | [diff] [blame] | 12216 | /* Deprecated API */ |
| 12217 | hlua_class_function(L, "get", hlua_channel_get); |
| 12218 | hlua_class_function(L, "dup", hlua_channel_dup); |
| 12219 | hlua_class_function(L, "getline", hlua_channel_getline); |
| 12220 | hlua_class_function(L, "get_in_len", hlua_channel_get_in_len); |
| 12221 | hlua_class_function(L, "get_out_len", hlua_channel_get_out_len); |
| 12222 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12223 | lua_rawset(L, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12224 | |
| 12225 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12226 | class_channel_ref = hlua_register_metatable(L, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 12227 | |
| 12228 | /* |
| 12229 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12230 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12231 | * |
| 12232 | */ |
| 12233 | |
| 12234 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12235 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12236 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12237 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12238 | lua_pushstring(L, "__index"); |
| 12239 | lua_newtable(L); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12240 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12241 | /* Browse existing fetches and create the associated |
| 12242 | * object method. |
| 12243 | */ |
| 12244 | sf = NULL; |
| 12245 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12246 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12247 | * by an underscore. |
| 12248 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12249 | strlcpy2(trash.area, sf->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12250 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12251 | if (*p == '.' || *p == '-' || *p == '+') |
| 12252 | *p = '_'; |
| 12253 | |
| 12254 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12255 | lua_pushstring(L, trash.area); |
| 12256 | lua_pushlightuserdata(L, sf); |
| 12257 | lua_pushcclosure(L, hlua_run_sample_fetch, 1); |
| 12258 | lua_rawset(L, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 12259 | } |
| 12260 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12261 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12262 | |
| 12263 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12264 | class_fetches_ref = hlua_register_metatable(L, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12265 | |
| 12266 | /* |
| 12267 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12268 | * Register class Converters |
| 12269 | * |
| 12270 | */ |
| 12271 | |
| 12272 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12273 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12274 | |
| 12275 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12276 | lua_pushstring(L, "__index"); |
| 12277 | lua_newtable(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12278 | |
| 12279 | /* Browse existing converters and create the associated |
| 12280 | * object method. |
| 12281 | */ |
| 12282 | sc = NULL; |
| 12283 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12284 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 12285 | * by an underscore. |
| 12286 | */ |
Willy Tarreau | 5ef9656 | 2021-08-26 16:48:53 +0200 | [diff] [blame] | 12287 | strlcpy2(trash.area, sc->kw, trash.size); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 12288 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12289 | if (*p == '.' || *p == '-' || *p == '+') |
| 12290 | *p = '_'; |
| 12291 | |
| 12292 | /* Register the function. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12293 | lua_pushstring(L, trash.area); |
| 12294 | lua_pushlightuserdata(L, sc); |
| 12295 | lua_pushcclosure(L, hlua_run_sample_conv, 1); |
| 12296 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12297 | } |
| 12298 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12299 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12300 | |
| 12301 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12302 | class_converters_ref = hlua_register_metatable(L, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 12303 | |
| 12304 | /* |
| 12305 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12306 | * Register class HTTP |
| 12307 | * |
| 12308 | */ |
| 12309 | |
| 12310 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12311 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12312 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12313 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12314 | lua_pushstring(L, "__index"); |
| 12315 | lua_newtable(L); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12316 | |
| 12317 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12318 | hlua_class_function(L, "req_get_headers",hlua_http_req_get_headers); |
| 12319 | hlua_class_function(L, "req_del_header", hlua_http_req_del_hdr); |
| 12320 | hlua_class_function(L, "req_rep_header", hlua_http_req_rep_hdr); |
| 12321 | hlua_class_function(L, "req_rep_value", hlua_http_req_rep_val); |
| 12322 | hlua_class_function(L, "req_add_header", hlua_http_req_add_hdr); |
| 12323 | hlua_class_function(L, "req_set_header", hlua_http_req_set_hdr); |
| 12324 | hlua_class_function(L, "req_set_method", hlua_http_req_set_meth); |
| 12325 | hlua_class_function(L, "req_set_path", hlua_http_req_set_path); |
| 12326 | hlua_class_function(L, "req_set_query", hlua_http_req_set_query); |
| 12327 | hlua_class_function(L, "req_set_uri", hlua_http_req_set_uri); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12328 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12329 | hlua_class_function(L, "res_get_headers",hlua_http_res_get_headers); |
| 12330 | hlua_class_function(L, "res_del_header", hlua_http_res_del_hdr); |
| 12331 | hlua_class_function(L, "res_rep_header", hlua_http_res_rep_hdr); |
| 12332 | hlua_class_function(L, "res_rep_value", hlua_http_res_rep_val); |
| 12333 | hlua_class_function(L, "res_add_header", hlua_http_res_add_hdr); |
| 12334 | hlua_class_function(L, "res_set_header", hlua_http_res_set_hdr); |
| 12335 | hlua_class_function(L, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12336 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12337 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12338 | |
| 12339 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12340 | class_http_ref = hlua_register_metatable(L, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12341 | |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12342 | /* |
| 12343 | * |
| 12344 | * Register class HTTPMessage |
| 12345 | * |
| 12346 | */ |
| 12347 | |
| 12348 | /* Create and fill the metatable. */ |
| 12349 | lua_newtable(L); |
| 12350 | |
Ilya Shipitsin | ff0f278 | 2021-08-22 22:18:07 +0500 | [diff] [blame] | 12351 | /* Create and fill the __index entry. */ |
Christopher Faulet | df97ac4 | 2020-02-26 16:57:19 +0100 | [diff] [blame] | 12352 | lua_pushstring(L, "__index"); |
| 12353 | lua_newtable(L); |
| 12354 | |
| 12355 | /* Register Lua functions. */ |
| 12356 | hlua_class_function(L, "is_resp", hlua_http_msg_is_resp); |
| 12357 | hlua_class_function(L, "get_stline", hlua_http_msg_get_stline); |
| 12358 | hlua_class_function(L, "get_headers", hlua_http_msg_get_headers); |
| 12359 | hlua_class_function(L, "del_header", hlua_http_msg_del_hdr); |
| 12360 | hlua_class_function(L, "rep_header", hlua_http_msg_rep_hdr); |
| 12361 | hlua_class_function(L, "rep_value", hlua_http_msg_rep_val); |
| 12362 | hlua_class_function(L, "add_header", hlua_http_msg_add_hdr); |
| 12363 | hlua_class_function(L, "set_header", hlua_http_msg_set_hdr); |
| 12364 | hlua_class_function(L, "set_method", hlua_http_msg_set_meth); |
| 12365 | hlua_class_function(L, "set_path", hlua_http_msg_set_path); |
| 12366 | hlua_class_function(L, "set_query", hlua_http_msg_set_query); |
| 12367 | hlua_class_function(L, "set_uri", hlua_http_msg_set_uri); |
| 12368 | hlua_class_function(L, "set_status", hlua_http_msg_set_status); |
| 12369 | hlua_class_function(L, "is_full", hlua_http_msg_is_full); |
| 12370 | hlua_class_function(L, "may_recv", hlua_http_msg_may_recv); |
| 12371 | hlua_class_function(L, "eom", hlua_http_msg_is_eom); |
| 12372 | hlua_class_function(L, "input", hlua_http_msg_get_in_len); |
| 12373 | hlua_class_function(L, "output", hlua_http_msg_get_out_len); |
| 12374 | |
| 12375 | hlua_class_function(L, "body", hlua_http_msg_get_body); |
| 12376 | hlua_class_function(L, "set", hlua_http_msg_set_data); |
| 12377 | hlua_class_function(L, "remove", hlua_http_msg_del_data); |
| 12378 | hlua_class_function(L, "append", hlua_http_msg_append); |
| 12379 | hlua_class_function(L, "prepend", hlua_http_msg_prepend); |
| 12380 | hlua_class_function(L, "insert", hlua_http_msg_insert_data); |
| 12381 | hlua_class_function(L, "set_eom", hlua_http_msg_set_eom); |
| 12382 | hlua_class_function(L, "unset_eom", hlua_http_msg_unset_eom); |
| 12383 | |
| 12384 | hlua_class_function(L, "send", hlua_http_msg_send); |
| 12385 | hlua_class_function(L, "forward", hlua_http_msg_forward); |
| 12386 | |
| 12387 | lua_rawset(L, -3); |
| 12388 | |
| 12389 | /* Register previous table in the registry with reference and named entry. */ |
| 12390 | class_http_msg_ref = hlua_register_metatable(L, CLASS_HTTP_MSG); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12391 | |
| 12392 | /* |
| 12393 | * |
| 12394 | * Register class HTTPClient |
| 12395 | * |
| 12396 | */ |
| 12397 | |
| 12398 | /* Create and fill the metatable. */ |
| 12399 | lua_newtable(L); |
| 12400 | lua_pushstring(L, "__index"); |
| 12401 | lua_newtable(L); |
| 12402 | hlua_class_function(L, "get", hlua_httpclient_get); |
William Lallemand | dc2cc90 | 2021-10-26 11:43:26 +0200 | [diff] [blame] | 12403 | hlua_class_function(L, "head", hlua_httpclient_head); |
| 12404 | hlua_class_function(L, "put", hlua_httpclient_put); |
| 12405 | hlua_class_function(L, "post", hlua_httpclient_post); |
| 12406 | hlua_class_function(L, "delete", hlua_httpclient_delete); |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12407 | lua_settable(L, -3); /* Sets the __index entry. */ |
William Lallemand | f77f1de | 2021-09-28 19:10:38 +0200 | [diff] [blame] | 12408 | /* Register the garbage collector entry. */ |
| 12409 | lua_pushstring(L, "__gc"); |
| 12410 | lua_pushcclosure(L, hlua_httpclient_gc, 0); |
| 12411 | lua_settable(L, -3); /* Push the last 2 entries in the table at index -3 */ |
| 12412 | |
William Lallemand | 3956c4e | 2021-09-21 16:25:15 +0200 | [diff] [blame] | 12413 | |
| 12414 | |
| 12415 | class_httpclient_ref = hlua_register_metatable(L, CLASS_HTTPCLIENT); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 12416 | /* |
| 12417 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12418 | * Register class AppletTCP |
| 12419 | * |
| 12420 | */ |
| 12421 | |
| 12422 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12423 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12424 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12425 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12426 | lua_pushstring(L, "__index"); |
| 12427 | lua_newtable(L); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12428 | |
| 12429 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12430 | hlua_class_function(L, "getline", hlua_applet_tcp_getline); |
| 12431 | hlua_class_function(L, "receive", hlua_applet_tcp_recv); |
| 12432 | hlua_class_function(L, "send", hlua_applet_tcp_send); |
| 12433 | hlua_class_function(L, "set_priv", hlua_applet_tcp_set_priv); |
| 12434 | hlua_class_function(L, "get_priv", hlua_applet_tcp_get_priv); |
| 12435 | hlua_class_function(L, "set_var", hlua_applet_tcp_set_var); |
| 12436 | hlua_class_function(L, "unset_var", hlua_applet_tcp_unset_var); |
| 12437 | hlua_class_function(L, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12438 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12439 | lua_settable(L, -3); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12440 | |
| 12441 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12442 | class_applet_tcp_ref = hlua_register_metatable(L, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 12443 | |
| 12444 | /* |
| 12445 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12446 | * Register class AppletHTTP |
| 12447 | * |
| 12448 | */ |
| 12449 | |
| 12450 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12451 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12452 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12453 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12454 | lua_pushstring(L, "__index"); |
| 12455 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12456 | |
| 12457 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12458 | hlua_class_function(L, "set_priv", hlua_applet_http_set_priv); |
| 12459 | hlua_class_function(L, "get_priv", hlua_applet_http_get_priv); |
| 12460 | hlua_class_function(L, "set_var", hlua_applet_http_set_var); |
| 12461 | hlua_class_function(L, "unset_var", hlua_applet_http_unset_var); |
| 12462 | hlua_class_function(L, "get_var", hlua_applet_http_get_var); |
| 12463 | hlua_class_function(L, "getline", hlua_applet_http_getline); |
| 12464 | hlua_class_function(L, "receive", hlua_applet_http_recv); |
| 12465 | hlua_class_function(L, "send", hlua_applet_http_send); |
| 12466 | hlua_class_function(L, "add_header", hlua_applet_http_addheader); |
| 12467 | hlua_class_function(L, "set_status", hlua_applet_http_status); |
| 12468 | hlua_class_function(L, "start_response", hlua_applet_http_start_response); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12469 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12470 | lua_settable(L, -3); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12471 | |
| 12472 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12473 | class_applet_http_ref = hlua_register_metatable(L, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 12474 | |
| 12475 | /* |
| 12476 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12477 | * Register class TXN |
| 12478 | * |
| 12479 | */ |
| 12480 | |
| 12481 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12482 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12483 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12484 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12485 | lua_pushstring(L, "__index"); |
| 12486 | lua_newtable(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 12487 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12488 | /* Register Lua functions. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12489 | hlua_class_function(L, "set_priv", hlua_set_priv); |
| 12490 | hlua_class_function(L, "get_priv", hlua_get_priv); |
| 12491 | hlua_class_function(L, "set_var", hlua_set_var); |
| 12492 | hlua_class_function(L, "unset_var", hlua_unset_var); |
| 12493 | hlua_class_function(L, "get_var", hlua_get_var); |
| 12494 | hlua_class_function(L, "done", hlua_txn_done); |
| 12495 | hlua_class_function(L, "reply", hlua_txn_reply_new); |
| 12496 | hlua_class_function(L, "set_loglevel", hlua_txn_set_loglevel); |
| 12497 | hlua_class_function(L, "set_tos", hlua_txn_set_tos); |
| 12498 | hlua_class_function(L, "set_mark", hlua_txn_set_mark); |
| 12499 | hlua_class_function(L, "set_priority_class", hlua_txn_set_priority_class); |
| 12500 | hlua_class_function(L, "set_priority_offset", hlua_txn_set_priority_offset); |
| 12501 | hlua_class_function(L, "deflog", hlua_txn_deflog); |
| 12502 | hlua_class_function(L, "log", hlua_txn_log); |
| 12503 | hlua_class_function(L, "Debug", hlua_txn_log_debug); |
| 12504 | hlua_class_function(L, "Info", hlua_txn_log_info); |
| 12505 | hlua_class_function(L, "Warning", hlua_txn_log_warning); |
| 12506 | hlua_class_function(L, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 12507 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12508 | lua_rawset(L, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 12509 | |
| 12510 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12511 | class_txn_ref = hlua_register_metatable(L, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12512 | |
| 12513 | /* |
| 12514 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12515 | * Register class reply |
| 12516 | * |
| 12517 | */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12518 | lua_newtable(L); |
| 12519 | lua_pushstring(L, "__index"); |
| 12520 | lua_newtable(L); |
| 12521 | hlua_class_function(L, "set_status", hlua_txn_reply_set_status); |
| 12522 | hlua_class_function(L, "add_header", hlua_txn_reply_add_header); |
| 12523 | hlua_class_function(L, "del_header", hlua_txn_reply_del_header); |
| 12524 | hlua_class_function(L, "set_body", hlua_txn_reply_set_body); |
| 12525 | lua_settable(L, -3); /* Sets the __index entry. */ |
| 12526 | class_txn_reply_ref = luaL_ref(L, LUA_REGISTRYINDEX); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 12527 | |
| 12528 | |
| 12529 | /* |
| 12530 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12531 | * Register class Socket |
| 12532 | * |
| 12533 | */ |
| 12534 | |
| 12535 | /* Create and fill the metatable. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12536 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12537 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 12538 | /* Create and fill the __index entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12539 | lua_pushstring(L, "__index"); |
| 12540 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12541 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12542 | #ifdef USE_OPENSSL |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12543 | hlua_class_function(L, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 12544 | #endif |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12545 | hlua_class_function(L, "connect", hlua_socket_connect); |
| 12546 | hlua_class_function(L, "send", hlua_socket_send); |
| 12547 | hlua_class_function(L, "receive", hlua_socket_receive); |
| 12548 | hlua_class_function(L, "close", hlua_socket_close); |
| 12549 | hlua_class_function(L, "getpeername", hlua_socket_getpeername); |
| 12550 | hlua_class_function(L, "getsockname", hlua_socket_getsockname); |
| 12551 | hlua_class_function(L, "setoption", hlua_socket_setoption); |
| 12552 | hlua_class_function(L, "settimeout", hlua_socket_settimeout); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12553 | |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12554 | lua_rawset(L, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12555 | |
| 12556 | /* Register the garbage collector entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12557 | lua_pushstring(L, "__gc"); |
| 12558 | lua_pushcclosure(L, hlua_socket_gc, 0); |
| 12559 | 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] | 12560 | |
| 12561 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 1eac28f | 2020-11-28 12:26:24 +0100 | [diff] [blame] | 12562 | class_socket_ref = hlua_register_metatable(L, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12563 | |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12564 | lua_atpanic(L, hlua_panic_safe); |
| 12565 | |
| 12566 | return L; |
| 12567 | } |
| 12568 | |
| 12569 | void hlua_init(void) { |
| 12570 | int i; |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12571 | char *errmsg; |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12572 | #ifdef USE_OPENSSL |
| 12573 | struct srv_kw *kw; |
| 12574 | int tmp_error; |
| 12575 | char *error; |
| 12576 | char *args[] = { /* SSL client configuration. */ |
| 12577 | "ssl", |
| 12578 | "verify", |
| 12579 | "none", |
| 12580 | NULL |
| 12581 | }; |
| 12582 | #endif |
| 12583 | |
| 12584 | /* Init post init function list head */ |
| 12585 | for (i = 0; i < MAX_THREADS + 1; i++) |
| 12586 | LIST_INIT(&hlua_init_functions[i]); |
| 12587 | |
| 12588 | /* Init state for common/shared lua parts */ |
| 12589 | hlua_state_id = 0; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12590 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12591 | hlua_states[0] = hlua_init_state(0); |
| 12592 | |
| 12593 | /* Init state 1 for thread 0. We have at least one thread. */ |
| 12594 | hlua_state_id = 1; |
Willy Tarreau | 43ab05b | 2021-09-28 09:43:11 +0200 | [diff] [blame] | 12595 | ha_set_thread(NULL); |
Thierry Fournier | aafc777 | 2020-12-04 11:47:47 +0100 | [diff] [blame] | 12596 | hlua_states[1] = hlua_init_state(1); |
| 12597 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12598 | /* Proxy and server configuration initialisation. */ |
William Lallemand | 6bb77b9 | 2021-07-28 15:48:16 +0200 | [diff] [blame] | 12599 | 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] | 12600 | if (!socket_proxy) { |
| 12601 | fprintf(stderr, "Lua init: %s\n", errmsg); |
| 12602 | exit(1); |
| 12603 | } |
| 12604 | proxy_preset_defaults(socket_proxy); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12605 | |
| 12606 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12607 | socket_tcp = new_server(socket_proxy); |
| 12608 | if (!socket_tcp) { |
| 12609 | fprintf(stderr, "Lua init: failed to allocate tcp server socket\n"); |
| 12610 | exit(1); |
| 12611 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12612 | |
| 12613 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12614 | /* Init TCP server: unchanged parameters */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12615 | socket_ssl = new_server(socket_proxy); |
| 12616 | if (!socket_ssl) { |
| 12617 | fprintf(stderr, "Lua init: failed to allocate ssl server socket\n"); |
| 12618 | exit(1); |
| 12619 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12620 | |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12621 | socket_ssl->use_ssl = 1; |
| 12622 | socket_ssl->xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12623 | |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12624 | for (i = 0; args[i] != NULL; i++) { |
| 12625 | 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] | 12626 | /* |
| 12627 | * |
| 12628 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 12629 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12630 | * features like client certificates and ssl_verify. |
| 12631 | * |
| 12632 | */ |
Amaury Denoyelle | 704ba1d | 2021-03-24 17:57:47 +0100 | [diff] [blame] | 12633 | tmp_error = kw->parse(args, &i, socket_proxy, socket_ssl, &error); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12634 | if (tmp_error != 0) { |
| 12635 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 12636 | abort(); /* This must be never arrives because the command line |
| 12637 | not editable by the user. */ |
| 12638 | } |
Bertrand Jacquin | 80839ff | 2021-01-21 19:14:46 +0000 | [diff] [blame] | 12639 | i += kw->skip; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12640 | } |
| 12641 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 12642 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 12643 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 12644 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12645 | |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12646 | static void hlua_deinit() |
| 12647 | { |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12648 | int thr; |
Christopher Faulet | 69c581a | 2021-05-31 08:54:04 +0200 | [diff] [blame] | 12649 | struct hlua_reg_filter *reg_flt, *reg_flt_bck; |
| 12650 | |
| 12651 | list_for_each_entry_safe(reg_flt, reg_flt_bck, &referenced_filters, l) |
| 12652 | release_hlua_reg_filter(reg_flt); |
Willy Tarreau | 186f376 | 2020-12-04 11:48:12 +0100 | [diff] [blame] | 12653 | |
| 12654 | for (thr = 0; thr < MAX_THREADS+1; thr++) { |
| 12655 | if (hlua_states[thr]) |
| 12656 | lua_close(hlua_states[thr]); |
| 12657 | } |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12658 | |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12659 | srv_drop(socket_tcp); |
Willy Tarreau | 430bf4a | 2021-03-04 09:45:32 +0100 | [diff] [blame] | 12660 | |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12661 | #ifdef USE_OPENSSL |
Amaury Denoyelle | bc2ebfa | 2021-08-25 15:34:53 +0200 | [diff] [blame] | 12662 | srv_drop(socket_ssl); |
Willy Tarreau | 0f143af | 2021-03-05 10:41:48 +0100 | [diff] [blame] | 12663 | #endif |
Amaury Denoyelle | 239fdbf | 2021-03-24 10:22:03 +0100 | [diff] [blame] | 12664 | |
| 12665 | free_proxy(socket_proxy); |
Tim Duesterhus | d0c0ca2 | 2020-07-04 11:53:26 +0200 | [diff] [blame] | 12666 | } |
| 12667 | |
| 12668 | REGISTER_POST_DEINIT(hlua_deinit); |
| 12669 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12670 | static void hlua_register_build_options(void) |
| 12671 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12672 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12673 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 12674 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 12675 | hap_register_build_opts(ptr, 1); |
| 12676 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 12677 | |
| 12678 | INITCALL0(STG_REGISTER, hlua_register_build_options); |