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 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 45 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 46 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 47 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 48 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 49 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 50 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 51 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 52 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 53 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 54 | #include <proto/payload.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 55 | #include <proto/http_ana.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 56 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 59 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 60 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 61 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 62 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 63 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 64 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 65 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 66 | * macro is used only for identifying the function that can |
| 67 | * not return because a longjmp is executed. |
| 68 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 69 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 70 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 71 | */ |
| 72 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 73 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 74 | #define MAY_LJMP(func) func |
| 75 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 76 | /* This couple of function executes securely some Lua calls outside of |
| 77 | * the lua runtime environment. Each Lua call can return a longjmp |
| 78 | * if it encounter a memory error. |
| 79 | * |
| 80 | * Lua documentation extract: |
| 81 | * |
| 82 | * If an error happens outside any protected environment, Lua calls |
| 83 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 84 | * exiting the host application. Your panic function can avoid this |
| 85 | * exit by never returning (e.g., doing a long jump to your own |
| 86 | * recovery point outside Lua). |
| 87 | * |
| 88 | * The panic function runs as if it were a message handler (see |
| 89 | * §2.3); in particular, the error message is at the top of the |
| 90 | * stack. However, there is no guarantee about stack space. To push |
| 91 | * anything on the stack, the panic function must first check the |
| 92 | * available space (see §4.2). |
| 93 | * |
| 94 | * We must check all the Lua entry point. This includes: |
| 95 | * - The include/proto/hlua.h exported functions |
| 96 | * - the task wrapper function |
| 97 | * - The action wrapper function |
| 98 | * - The converters wrapper function |
| 99 | * - The sample-fetch wrapper functions |
| 100 | * |
| 101 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 102 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 103 | * |
| 104 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 105 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 106 | * because they must be exists in the program stack when the longjmp |
| 107 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 108 | * |
| 109 | * Note that the Lua processing is not really thread safe. It provides |
| 110 | * heavy system which consists to add our own lock function in the Lua |
| 111 | * code and recompile the library. This system will probably not accepted |
| 112 | * by maintainers of various distribs. |
| 113 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 114 | * 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] | 115 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 116 | * of function and a lua_unlock() at the end of the function. So I |
| 117 | * conclude that the Lua thread safe mode just perform a mutex around |
| 118 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 119 | * easier for distro maintainers. |
| 120 | * |
| 121 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 122 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 123 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 125 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 126 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 128 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | |
| 130 | #define SET_SAFE_LJMP(__L) \ |
| 131 | ({ \ |
| 132 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 135 | lua_atpanic(__L, hlua_panic_safe); \ |
| 136 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 137 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 138 | } else { \ |
| 139 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 140 | ret = 1; \ |
| 141 | } \ |
| 142 | ret; \ |
| 143 | }) |
| 144 | |
| 145 | /* If we are the last function catching Lua errors, we |
| 146 | * must reset the panic function. |
| 147 | */ |
| 148 | #define RESET_SAFE_LJMP(__L) \ |
| 149 | do { \ |
| 150 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 151 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 152 | } while(0) |
| 153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 154 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 155 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 156 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 158 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 159 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 160 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 161 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 162 | /* The main Lua execution context. */ |
| 163 | struct hlua gL; |
| 164 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | /* This is the memory pool containing struct lua for applets |
| 166 | * (including cli). |
| 167 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 168 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 169 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 170 | /* Used for Socket connection. */ |
| 171 | static struct proxy socket_proxy; |
| 172 | static struct server socket_tcp; |
| 173 | #ifdef USE_OPENSSL |
| 174 | static struct server socket_ssl; |
| 175 | #endif |
| 176 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 177 | /* List head of the function called at the initialisation time. */ |
| 178 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 179 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 180 | /* The following variables contains the reference of the different |
| 181 | * Lua classes. These references are useful for identify metadata |
| 182 | * associated with an object. |
| 183 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 184 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 185 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 186 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 187 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 188 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 189 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 190 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 191 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 193 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 194 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 195 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 196 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 198 | * because a task may remain alive during all the haproxy execution. |
| 199 | */ |
| 200 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 201 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 202 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 204 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 205 | * it is used for preventing infinite loops. |
| 206 | * |
| 207 | * I test the scheer with an infinite loop containing one incrementation |
| 208 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 209 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 210 | * to one interrupt each 10 000 instructions. |
| 211 | * |
| 212 | * configured | Number of |
| 213 | * instructions | loops executed |
| 214 | * between two | in milions |
| 215 | * forced yields | |
| 216 | * ---------------+--------------- |
| 217 | * 10 | 160 |
| 218 | * 500 | 670 |
| 219 | * 1000 | 680 |
| 220 | * 5000 | 700 |
| 221 | * 7000 | 700 |
| 222 | * 8000 | 700 |
| 223 | * 9000 | 710 <- ceil |
| 224 | * 10000 | 710 |
| 225 | * 100000 | 710 |
| 226 | * 1000000 | 710 |
| 227 | * |
| 228 | */ |
| 229 | static unsigned int hlua_nb_instruction = 10000; |
| 230 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 231 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 232 | * be enforced on any memory allocation. |
| 233 | */ |
| 234 | struct hlua_mem_allocator { |
| 235 | size_t allocated; |
| 236 | size_t limit; |
| 237 | }; |
| 238 | |
| 239 | static struct hlua_mem_allocator hlua_global_allocator; |
| 240 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 241 | /* These functions converts types between HAProxy internal args or |
| 242 | * sample and LUA types. Another function permits to check if the |
| 243 | * LUA stack contains arguments according with an required ARG_T |
| 244 | * format. |
| 245 | */ |
| 246 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 247 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 248 | __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] | 249 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 250 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 251 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 252 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 253 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 254 | __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] | 255 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 256 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 257 | do { \ |
| 258 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 259 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 260 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 261 | } while (0) |
| 262 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 263 | /* Used to check an Lua function type in the stack. It creates and |
| 264 | * returns a reference of the function. This function throws an |
| 265 | * error if the rgument is not a "function". |
| 266 | */ |
| 267 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 268 | { |
| 269 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 270 | 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] | 271 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 272 | } |
| 273 | lua_pushvalue(L, argno); |
| 274 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 275 | } |
| 276 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 277 | /* Return the string that is of the top of the stack. */ |
| 278 | const char *hlua_get_top_error_string(lua_State *L) |
| 279 | { |
| 280 | if (lua_gettop(L) < 1) |
| 281 | return "unknown error"; |
| 282 | if (lua_type(L, -1) != LUA_TSTRING) |
| 283 | return "unknown error"; |
| 284 | return lua_tostring(L, -1); |
| 285 | } |
| 286 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 287 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 288 | { |
| 289 | lua_Debug ar; |
| 290 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 291 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 292 | int filled = 0; |
| 293 | |
| 294 | while (lua_getstack(L, level++, &ar)) { |
| 295 | |
| 296 | /* Add separator */ |
| 297 | if (filled) |
| 298 | chunk_appendf(msg, ", "); |
| 299 | filled = 1; |
| 300 | |
| 301 | /* Fill fields: |
| 302 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 303 | * 'l': fills in the field currentline; |
| 304 | * 'n': fills in the field name and namewhat; |
| 305 | * 't': fills in the field istailcall; |
| 306 | */ |
| 307 | lua_getinfo(L, "Slnt", &ar); |
| 308 | |
| 309 | /* Append code localisation */ |
| 310 | if (ar.currentline > 0) |
| 311 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 312 | else |
| 313 | chunk_appendf(msg, "%s ", ar.short_src); |
| 314 | |
| 315 | /* |
| 316 | * Get function name |
| 317 | * |
| 318 | * if namewhat is no empty, name is defined. |
| 319 | * what contains "Lua" for Lua function, "C" for C function, |
| 320 | * or "main" for main code. |
| 321 | */ |
| 322 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 323 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 324 | |
| 325 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 326 | chunk_appendf(msg, "main chunk"); |
| 327 | |
| 328 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 329 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 330 | |
| 331 | else /* nothing left... */ |
| 332 | chunk_appendf(msg, "?"); |
| 333 | |
| 334 | |
| 335 | /* Display tailed call */ |
| 336 | if (ar.istailcall) |
| 337 | chunk_appendf(msg, " ..."); |
| 338 | } |
| 339 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 340 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 344 | /* This function check the number of arguments available in the |
| 345 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 346 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 347 | */ |
| 348 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 349 | { |
| 350 | if (lua_gettop(L) == nb) |
| 351 | return; |
| 352 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 353 | } |
| 354 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 355 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 356 | * and the line number where the error is encountered. |
| 357 | */ |
| 358 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 359 | { |
| 360 | va_list argp; |
| 361 | va_start(argp, fmt); |
| 362 | luaL_where(L, 1); |
| 363 | lua_pushvfstring(L, fmt, argp); |
| 364 | va_end(argp); |
| 365 | lua_concat(L, 2); |
| 366 | return 1; |
| 367 | } |
| 368 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 369 | /* This functions is used with sample fetch and converters. It |
| 370 | * converts the HAProxy configuration argument in a lua stack |
| 371 | * values. |
| 372 | * |
| 373 | * It takes an array of "arg", and each entry of the array is |
| 374 | * converted and pushed in the LUA stack. |
| 375 | */ |
| 376 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 377 | { |
| 378 | switch (arg->type) { |
| 379 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | case ARGT_TIME: |
| 381 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 382 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 386 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | case ARGT_IPV4: |
| 390 | case ARGT_IPV6: |
| 391 | case ARGT_MSK4: |
| 392 | case ARGT_MSK6: |
| 393 | case ARGT_FE: |
| 394 | case ARGT_BE: |
| 395 | case ARGT_TAB: |
| 396 | case ARGT_SRV: |
| 397 | case ARGT_USR: |
| 398 | case ARGT_MAP: |
| 399 | default: |
| 400 | lua_pushnil(L); |
| 401 | break; |
| 402 | } |
| 403 | return 1; |
| 404 | } |
| 405 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 406 | /* 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] | 407 | * 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] | 408 | * with sample fetch wrappers. The input arguments are given to the |
| 409 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 410 | */ |
| 411 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 412 | { |
| 413 | switch (lua_type(L, ud)) { |
| 414 | |
| 415 | case LUA_TNUMBER: |
| 416 | case LUA_TBOOLEAN: |
| 417 | arg->type = ARGT_SINT; |
| 418 | arg->data.sint = lua_tointeger(L, ud); |
| 419 | break; |
| 420 | |
| 421 | case LUA_TSTRING: |
| 422 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 423 | 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] | 424 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 425 | arg->data.str.size = arg->data.str.data; |
| 426 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | case LUA_TUSERDATA: |
| 430 | case LUA_TNIL: |
| 431 | case LUA_TTABLE: |
| 432 | case LUA_TFUNCTION: |
| 433 | case LUA_TTHREAD: |
| 434 | case LUA_TLIGHTUSERDATA: |
| 435 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 436 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | /* the following functions are used to convert a struct sample |
| 443 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 444 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 446 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 448 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 450 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 451 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case SMP_T_BIN: |
| 455 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 456 | 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] | 457 | break; |
| 458 | |
| 459 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 462 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 463 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 464 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 465 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 466 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 467 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 468 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 469 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 470 | 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] | 471 | break; |
| 472 | default: |
| 473 | lua_pushnil(L); |
| 474 | break; |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | case SMP_T_IPV4: |
| 479 | case SMP_T_IPV6: |
| 480 | 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] | 481 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 482 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 483 | 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] | 484 | else |
| 485 | lua_pushnil(L); |
| 486 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 487 | default: |
| 488 | lua_pushnil(L); |
| 489 | break; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
| 493 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 494 | /* the following functions are used to convert a struct sample |
| 495 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 496 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | */ |
| 498 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 499 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 500 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | |
| 502 | case SMP_T_BIN: |
| 503 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 504 | 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] | 505 | break; |
| 506 | |
| 507 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 508 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 509 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 510 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 511 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 512 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 513 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 514 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 515 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 516 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 517 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 518 | 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] | 519 | break; |
| 520 | default: |
| 521 | lua_pushstring(L, ""); |
| 522 | break; |
| 523 | } |
| 524 | break; |
| 525 | |
| 526 | case SMP_T_SINT: |
| 527 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | case SMP_T_IPV4: |
| 529 | case SMP_T_IPV6: |
| 530 | 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] | 531 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 532 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 533 | 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] | 534 | else |
| 535 | lua_pushstring(L, ""); |
| 536 | break; |
| 537 | default: |
| 538 | lua_pushstring(L, ""); |
| 539 | break; |
| 540 | } |
| 541 | return 1; |
| 542 | } |
| 543 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 544 | /* the following functions are used to convert an Lua type in a |
| 545 | * struct sample. This is useful to provide data from a converter |
| 546 | * to the LUA code. |
| 547 | */ |
| 548 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 549 | { |
| 550 | switch (lua_type(L, ud)) { |
| 551 | |
| 552 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 553 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 554 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | |
| 558 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 560 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 565 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 566 | 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] | 567 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 568 | smp->data.u.str.size = smp->data.u.str.data; |
| 569 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TUSERDATA: |
| 573 | case LUA_TNIL: |
| 574 | case LUA_TTABLE: |
| 575 | case LUA_TFUNCTION: |
| 576 | case LUA_TTHREAD: |
| 577 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 578 | case LUA_TNONE: |
| 579 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 580 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 581 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 587 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 588 | * 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] | 589 | * 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] | 590 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 591 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 592 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 594 | __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] | 595 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | { |
| 597 | int min_arg; |
| 598 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 599 | struct proxy *px; |
| 600 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | |
| 602 | idx = 0; |
| 603 | min_arg = ARGM(mask); |
| 604 | mask >>= ARGM_BITS; |
| 605 | |
| 606 | while (1) { |
| 607 | |
| 608 | /* Check oversize. */ |
| 609 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 610 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* Check for mandatory arguments. */ |
| 614 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 615 | if (idx < min_arg) { |
| 616 | |
| 617 | /* If miss other argument than the first one, we return an error. */ |
| 618 | if (idx > 0) |
| 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 620 | |
| 621 | /* If first argument have a certain type, some default values |
| 622 | * may be used. See the function smp_resolve_args(). |
| 623 | */ |
| 624 | switch (mask & ARGT_MASK) { |
| 625 | |
| 626 | case ARGT_FE: |
| 627 | if (!(p->cap & PR_CAP_FE)) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | argp[idx].data.prx = p; |
| 630 | argp[idx].type = ARGT_FE; |
| 631 | argp[idx+1].type = ARGT_STOP; |
| 632 | break; |
| 633 | |
| 634 | case ARGT_BE: |
| 635 | if (!(p->cap & PR_CAP_BE)) |
| 636 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 637 | argp[idx].data.prx = p; |
| 638 | argp[idx].type = ARGT_BE; |
| 639 | argp[idx+1].type = ARGT_STOP; |
| 640 | break; |
| 641 | |
| 642 | case ARGT_TAB: |
| 643 | argp[idx].data.prx = p; |
| 644 | argp[idx].type = ARGT_TAB; |
| 645 | argp[idx+1].type = ARGT_STOP; |
| 646 | break; |
| 647 | |
| 648 | default: |
| 649 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 650 | break; |
| 651 | } |
| 652 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 656 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 658 | argp[idx].type != ARGT_STOP) { |
| 659 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 660 | } |
| 661 | |
| 662 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 663 | argp[idx].type == ARGT_STOP) { |
| 664 | return 0; |
| 665 | } |
| 666 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 667 | /* Convert some argument types. */ |
| 668 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | if (argp[idx].type != ARGT_SINT) |
| 671 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 672 | argp[idx].type = ARGT_SINT; |
| 673 | break; |
| 674 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | case ARGT_TIME: |
| 676 | if (argp[idx].type != ARGT_SINT) |
| 677 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 678 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | break; |
| 680 | |
| 681 | case ARGT_SIZE: |
| 682 | if (argp[idx].type != ARGT_SINT) |
| 683 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 684 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 685 | break; |
| 686 | |
| 687 | case ARGT_FE: |
| 688 | if (argp[idx].type != ARGT_STR) |
| 689 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 690 | memcpy(trash.area, argp[idx].data.str.area, |
| 691 | argp[idx].data.str.data); |
| 692 | trash.area[argp[idx].data.str.data] = 0; |
| 693 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | if (!argp[idx].data.prx) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 696 | argp[idx].type = ARGT_FE; |
| 697 | break; |
| 698 | |
| 699 | case ARGT_BE: |
| 700 | if (argp[idx].type != ARGT_STR) |
| 701 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 702 | memcpy(trash.area, argp[idx].data.str.area, |
| 703 | argp[idx].data.str.data); |
| 704 | trash.area[argp[idx].data.str.data] = 0; |
| 705 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 706 | if (!argp[idx].data.prx) |
| 707 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 708 | argp[idx].type = ARGT_BE; |
| 709 | break; |
| 710 | |
| 711 | case ARGT_TAB: |
| 712 | if (argp[idx].type != ARGT_STR) |
| 713 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 714 | memcpy(trash.area, argp[idx].data.str.area, |
| 715 | argp[idx].data.str.data); |
| 716 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 717 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 718 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 719 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 720 | argp[idx].type = ARGT_TAB; |
| 721 | break; |
| 722 | |
| 723 | case ARGT_SRV: |
| 724 | if (argp[idx].type != ARGT_STR) |
| 725 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 726 | memcpy(trash.area, argp[idx].data.str.area, |
| 727 | argp[idx].data.str.data); |
| 728 | trash.area[argp[idx].data.str.data] = 0; |
| 729 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (sname) { |
| 731 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 732 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 733 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 734 | if (!px) |
| 735 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 736 | } |
| 737 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 738 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | argp[idx].data.srv = findserver(px, sname); |
| 742 | if (!argp[idx].data.srv) |
| 743 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 744 | argp[idx].type = ARGT_SRV; |
| 745 | break; |
| 746 | |
| 747 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | memcpy(trash.area, argp[idx].data.str.area, |
| 749 | argp[idx].data.str.data); |
| 750 | trash.area[argp[idx].data.str.data] = 0; |
| 751 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 753 | argp[idx].type = ARGT_IPV4; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | memcpy(trash.area, argp[idx].data.str.area, |
| 758 | argp[idx].data.str.data); |
| 759 | trash.area[argp[idx].data.str.data] = 0; |
| 760 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 762 | argp[idx].type = ARGT_MSK4; |
| 763 | break; |
| 764 | |
| 765 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 771 | argp[idx].type = ARGT_IPV6; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 780 | argp[idx].type = ARGT_MSK6; |
| 781 | break; |
| 782 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | case ARGT_MAP: |
| 784 | case ARGT_REG: |
| 785 | case ARGT_USR: |
| 786 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
| 790 | /* Check for type of argument. */ |
| 791 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 792 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 793 | arg_type_names[(mask & ARGT_MASK)], |
| 794 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 796 | } |
| 797 | |
| 798 | /* Next argument. */ |
| 799 | mask >>= ARGT_BITS; |
| 800 | idx++; |
| 801 | } |
| 802 | } |
| 803 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 805 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | * |
| 808 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 810 | */ |
| 811 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 812 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 813 | struct hlua **hlua = lua_getextraspace(L); |
| 814 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | } |
| 816 | static inline void hlua_sethlua(struct hlua *hlua) |
| 817 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 818 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 819 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 822 | /* This function is used to send logs. It try to send on screen (stderr) |
| 823 | * and on the default syslog server. |
| 824 | */ |
| 825 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 826 | { |
| 827 | struct tm tm; |
| 828 | char *p; |
| 829 | |
| 830 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 831 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 832 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 834 | /* Break the message if exceed the buffer size. */ |
| 835 | *(p-4) = ' '; |
| 836 | *(p-3) = '.'; |
| 837 | *(p-2) = '.'; |
| 838 | *(p-1) = '.'; |
| 839 | break; |
| 840 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 841 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 842 | *p = *msg; |
| 843 | else |
| 844 | *p = '.'; |
| 845 | } |
| 846 | *p = '\0'; |
| 847 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 848 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 850 | get_localtime(date.tv_sec, &tm); |
| 851 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | 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] | 853 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | fflush(stderr); |
| 855 | } |
| 856 | } |
| 857 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | /* This function just ensure that the yield will be always |
| 859 | * returned with a timeout and permit to set some flags |
| 860 | */ |
| 861 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 862 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | { |
| 864 | struct hlua *hlua = hlua_gethlua(L); |
| 865 | |
| 866 | /* Set the wake timeout. If timeout is required, we set |
| 867 | * the expiration time. |
| 868 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 869 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 871 | hlua->flags |= flags; |
| 872 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 873 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 874 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 877 | /* This function initialises the Lua environment stored in the stream. |
| 878 | * 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] | 879 | * 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] | 880 | * |
| 881 | * This function is particular. it initialises a new Lua thread. If the |
| 882 | * initialisation fails (example: out of memory error), the lua function |
| 883 | * throws an error (longjmp). |
| 884 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 885 | * 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] | 886 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 887 | * threads appear, the safe environment set a lock to ensure only one |
| 888 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 889 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 890 | * |
| 891 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 892 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 893 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 894 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 895 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 896 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 897 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 899 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 901 | if (!already_safe) { |
| 902 | if (!SET_SAFE_LJMP(gL.T)) { |
| 903 | lua->Tref = LUA_REFNIL; |
| 904 | return 0; |
| 905 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 906 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 908 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 909 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 910 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 911 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | lua->T = lua_newthread(gL.T); |
| 913 | if (!lua->T) { |
| 914 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 915 | if (!already_safe) |
| 916 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | hlua_sethlua(lua); |
| 920 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 921 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | } |
| 926 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 927 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 929 | * is not freed. |
| 930 | */ |
| 931 | void hlua_ctx_destroy(struct hlua *lua) |
| 932 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 933 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 934 | return; |
| 935 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 936 | if (!lua->T) |
| 937 | goto end; |
| 938 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 940 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 941 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | if (!SET_SAFE_LJMP(lua->T)) |
| 943 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 944 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 946 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 947 | if (!SET_SAFE_LJMP(gL.T)) |
| 948 | return; |
| 949 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 950 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 951 | /* Forces a garbage collecting process. If the Lua program is finished |
| 952 | * without error, we run the GC on the thread pointer. Its freed all |
| 953 | * the unused memory. |
| 954 | * If the thread is finnish with an error or is currently yielded, |
| 955 | * it seems that the GC applied on the thread doesn't clean anything, |
| 956 | * so e run the GC on the main thread. |
| 957 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 958 | * the garbage collection. |
| 959 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 960 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 961 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 962 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 963 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 964 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 965 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 966 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 967 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 968 | |
| 969 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 970 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* This function is used to restore the Lua context when a coroutine |
| 974 | * fails. This function copy the common memory between old coroutine |
| 975 | * and the new coroutine. The old coroutine is destroyed, and its |
| 976 | * replaced by the new coroutine. |
| 977 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 978 | * as string error message and it is copied in the new stack. |
| 979 | */ |
| 980 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 981 | { |
| 982 | lua_State *T; |
| 983 | int new_ref; |
| 984 | |
| 985 | /* Renew the main LUA stack doesn't have sense. */ |
| 986 | if (lua == &gL) |
| 987 | return 0; |
| 988 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 989 | /* New Lua coroutine. */ |
| 990 | T = lua_newthread(gL.T); |
| 991 | if (!T) |
| 992 | return 0; |
| 993 | |
| 994 | /* Copy last error message. */ |
| 995 | if (keep_msg) |
| 996 | lua_xmove(lua->T, T, 1); |
| 997 | |
| 998 | /* Copy data between the coroutines. */ |
| 999 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1000 | lua_xmove(lua->T, T, 1); |
| 1001 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1002 | |
| 1003 | /* Destroy old data. */ |
| 1004 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1005 | |
| 1006 | /* The thread is garbage collected by Lua. */ |
| 1007 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1008 | |
| 1009 | /* Fill the struct with the new coroutine values. */ |
| 1010 | lua->Mref = new_ref; |
| 1011 | lua->T = T; |
| 1012 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1013 | |
| 1014 | /* Set context. */ |
| 1015 | hlua_sethlua(lua); |
| 1016 | |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1020 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1021 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1022 | struct hlua *hlua = hlua_gethlua(L); |
| 1023 | |
| 1024 | /* Lua cannot yield when its returning from a function, |
| 1025 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1026 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1027 | */ |
| 1028 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1029 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* restore the interrupt condition. */ |
| 1034 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1035 | |
| 1036 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1037 | * If the state is not yieldable, trying yield causes an error. |
| 1038 | */ |
| 1039 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1040 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1042 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1043 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1044 | hlua->run_time += now_ms - hlua->start_time; |
| 1045 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1046 | lua_pushfstring(L, "execution timeout"); |
| 1047 | WILL_LJMP(lua_error(L)); |
| 1048 | } |
| 1049 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1050 | /* Update the start time. */ |
| 1051 | hlua->start_time = now_ms; |
| 1052 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | /* Try to interrupt the process at the end of the current |
| 1054 | * unyieldable function. |
| 1055 | */ |
| 1056 | 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] | 1057 | } |
| 1058 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | /* This function start or resumes the Lua stack execution. If the flag |
| 1060 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1061 | * The function return an error. |
| 1062 | * |
| 1063 | * The function can returns 4 values: |
| 1064 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1065 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1066 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * - 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] | 1068 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1071 | * 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] | 1072 | * LUA code. |
| 1073 | */ |
| 1074 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1075 | { |
| 1076 | int ret; |
| 1077 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1078 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1080 | /* Initialise run time counter. */ |
| 1081 | if (!HLUA_IS_RUNNING(lua)) |
| 1082 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1083 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1084 | /* Lock the whole Lua execution. This lock must be before the |
| 1085 | * label "resume_execution". |
| 1086 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1087 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1088 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1089 | resume_execution: |
| 1090 | |
| 1091 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1092 | * instructions. it is used for preventing infinite loops. |
| 1093 | */ |
| 1094 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1095 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1096 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1097 | HLUA_SET_RUN(lua); |
| 1098 | HLUA_CLR_CTRLYIELD(lua); |
| 1099 | HLUA_CLR_WAKERESWR(lua); |
| 1100 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1101 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1102 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1103 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1104 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1105 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1106 | /* Call the function. */ |
| 1107 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1108 | switch (ret) { |
| 1109 | |
| 1110 | case LUA_OK: |
| 1111 | ret = HLUA_E_OK; |
| 1112 | break; |
| 1113 | |
| 1114 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1115 | /* Check if the execution timeout is expired. It it is the case, we |
| 1116 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1117 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1118 | tv_update_date(0, 1); |
| 1119 | lua->run_time += now_ms - lua->start_time; |
| 1120 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1121 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1122 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | /* Process the forced yield. if the general yield is not allowed or |
| 1126 | * if no task were associated this the current Lua execution |
| 1127 | * coroutine, we resume the execution. Else we want to return in the |
| 1128 | * scheduler and we want to be waked up again, to continue the |
| 1129 | * current Lua execution. So we schedule our own task. |
| 1130 | */ |
| 1131 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1132 | if (!yield_allowed || !lua->task) |
| 1133 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1134 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1135 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | if (!yield_allowed) { |
| 1137 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1138 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | ret = HLUA_E_AGAIN; |
| 1142 | break; |
| 1143 | |
| 1144 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1145 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1146 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1147 | * because the errors ares the only one mean to return immediately |
| 1148 | * from and lua execution. |
| 1149 | */ |
| 1150 | if (lua->flags & HLUA_EXIT) { |
| 1151 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1152 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1156 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | if (!lua_checkstack(lua->T, 1)) { |
| 1158 | ret = HLUA_E_ERR; |
| 1159 | break; |
| 1160 | } |
| 1161 | msg = lua_tostring(lua->T, -1); |
| 1162 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1163 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1164 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1166 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1168 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | ret = HLUA_E_ERRMSG; |
| 1170 | break; |
| 1171 | |
| 1172 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1173 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1174 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1175 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | break; |
| 1177 | |
| 1178 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | if (!lua_checkstack(lua->T, 1)) { |
| 1181 | ret = HLUA_E_ERR; |
| 1182 | break; |
| 1183 | } |
| 1184 | msg = lua_tostring(lua->T, -1); |
| 1185 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1186 | lua_pop(lua->T, 1); |
| 1187 | if (msg) |
| 1188 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1189 | else |
| 1190 | lua_pushfstring(lua->T, "message handler error"); |
| 1191 | ret = HLUA_E_ERRMSG; |
| 1192 | break; |
| 1193 | |
| 1194 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1195 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1197 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | switch (ret) { |
| 1202 | case HLUA_E_AGAIN: |
| 1203 | break; |
| 1204 | |
| 1205 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1206 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1208 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | break; |
| 1210 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1211 | case HLUA_E_ETMOUT: |
| 1212 | case HLUA_E_NOMEM: |
| 1213 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1215 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1216 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | hlua_ctx_renew(lua, 0); |
| 1218 | break; |
| 1219 | |
| 1220 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1221 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1222 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1226 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1227 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1228 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1229 | return ret; |
| 1230 | } |
| 1231 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1232 | /* This function exit the current code. */ |
| 1233 | __LJMP static int hlua_done(lua_State *L) |
| 1234 | { |
| 1235 | struct hlua *hlua = hlua_gethlua(L); |
| 1236 | |
| 1237 | hlua->flags |= HLUA_EXIT; |
| 1238 | WILL_LJMP(lua_error(L)); |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1243 | /* This function is an LUA binding. It provides a function |
| 1244 | * for deleting ACL from a referenced ACL file. |
| 1245 | */ |
| 1246 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1247 | { |
| 1248 | const char *name; |
| 1249 | const char *key; |
| 1250 | struct pat_ref *ref; |
| 1251 | |
| 1252 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1253 | |
| 1254 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1255 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1256 | |
| 1257 | ref = pat_ref_lookup(name); |
| 1258 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1259 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1260 | |
| 1261 | pat_ref_delete(ref, key); |
| 1262 | return 0; |
| 1263 | } |
| 1264 | |
| 1265 | /* This function is an LUA binding. It provides a function |
| 1266 | * for deleting map entry from a referenced map file. |
| 1267 | */ |
| 1268 | static int hlua_del_map(lua_State *L) |
| 1269 | { |
| 1270 | const char *name; |
| 1271 | const char *key; |
| 1272 | struct pat_ref *ref; |
| 1273 | |
| 1274 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1275 | |
| 1276 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1277 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1278 | |
| 1279 | ref = pat_ref_lookup(name); |
| 1280 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1281 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1282 | |
| 1283 | pat_ref_delete(ref, key); |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
| 1287 | /* This function is an LUA binding. It provides a function |
| 1288 | * for adding ACL pattern from a referenced ACL file. |
| 1289 | */ |
| 1290 | static int hlua_add_acl(lua_State *L) |
| 1291 | { |
| 1292 | const char *name; |
| 1293 | const char *key; |
| 1294 | struct pat_ref *ref; |
| 1295 | |
| 1296 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1297 | |
| 1298 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1299 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1300 | |
| 1301 | ref = pat_ref_lookup(name); |
| 1302 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1303 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1304 | |
| 1305 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1306 | pat_ref_add(ref, key, NULL, NULL); |
| 1307 | return 0; |
| 1308 | } |
| 1309 | |
| 1310 | /* This function is an LUA binding. It provides a function |
| 1311 | * for setting map pattern and sample from a referenced map |
| 1312 | * file. |
| 1313 | */ |
| 1314 | static int hlua_set_map(lua_State *L) |
| 1315 | { |
| 1316 | const char *name; |
| 1317 | const char *key; |
| 1318 | const char *value; |
| 1319 | struct pat_ref *ref; |
| 1320 | |
| 1321 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1322 | |
| 1323 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1324 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1325 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1326 | |
| 1327 | ref = pat_ref_lookup(name); |
| 1328 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1329 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1330 | |
| 1331 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1332 | pat_ref_set(ref, key, value, NULL); |
| 1333 | else |
| 1334 | pat_ref_add(ref, key, value, NULL); |
| 1335 | return 0; |
| 1336 | } |
| 1337 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1338 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1339 | * 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] | 1340 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1341 | * the name of the object (_G[<name>] = <metable> ). |
| 1342 | * |
| 1343 | * A metable is a table that modify the standard behavior of a standard |
| 1344 | * access to the associated data. The entries of this new metatable are |
| 1345 | * defined as is: |
| 1346 | * |
| 1347 | * http://lua-users.org/wiki/MetatableEvents |
| 1348 | * |
| 1349 | * __index |
| 1350 | * |
| 1351 | * we access an absent field in a table, the result is nil. This is |
| 1352 | * true, but it is not the whole truth. Actually, such access triggers |
| 1353 | * the interpreter to look for an __index metamethod: If there is no |
| 1354 | * such method, as usually happens, then the access results in nil; |
| 1355 | * otherwise, the metamethod will provide the result. |
| 1356 | * |
| 1357 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1358 | * the key does not appear in the table, but the metatable has an __index |
| 1359 | * property: |
| 1360 | * |
| 1361 | * - if the value is a function, the function is called, passing in the |
| 1362 | * table and the key; the return value of that function is returned as |
| 1363 | * the result. |
| 1364 | * |
| 1365 | * - if the value is another table, the value of the key in that table is |
| 1366 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1367 | * table's metatable has an __index property, then it continues on up) |
| 1368 | * |
| 1369 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1370 | * |
| 1371 | * http://www.lua.org/pil/13.4.1.html |
| 1372 | * |
| 1373 | * __newindex |
| 1374 | * |
| 1375 | * Like __index, but control property assignment. |
| 1376 | * |
| 1377 | * __mode - Control weak references. A string value with one or both |
| 1378 | * of the characters 'k' and 'v' which specifies that the the |
| 1379 | * keys and/or values in the table are weak references. |
| 1380 | * |
| 1381 | * __call - Treat a table like a function. When a table is followed by |
| 1382 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1383 | * a __call key pointing to a function, that function is invoked |
| 1384 | * (passing any specified arguments) and the return value is |
| 1385 | * returned. |
| 1386 | * |
| 1387 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1388 | * called, if the metatable for myTable has a __metatable |
| 1389 | * key, the value of that key is returned instead of the |
| 1390 | * actual metatable. |
| 1391 | * |
| 1392 | * __tostring - Control string representation. When the builtin |
| 1393 | * "tostring( myTable )" function is called, if the metatable |
| 1394 | * for myTable has a __tostring property set to a function, |
| 1395 | * that function is invoked (passing myTable to it) and the |
| 1396 | * return value is used as the string representation. |
| 1397 | * |
| 1398 | * __len - Control table length. When the table length is requested using |
| 1399 | * the length operator ( '#' ), if the metatable for myTable has |
| 1400 | * a __len key pointing to a function, that function is invoked |
| 1401 | * (passing myTable to it) and the return value used as the value |
| 1402 | * of "#myTable". |
| 1403 | * |
| 1404 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1405 | * collected, if the metatable has a __gc field pointing to a |
| 1406 | * function, that function is first invoked, passing the userdata |
| 1407 | * to it. The __gc metamethod is not called for tables. |
| 1408 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1409 | * |
| 1410 | * Special metamethods for redefining standard operators: |
| 1411 | * http://www.lua.org/pil/13.1.html |
| 1412 | * |
| 1413 | * __add "+" |
| 1414 | * __sub "-" |
| 1415 | * __mul "*" |
| 1416 | * __div "/" |
| 1417 | * __unm "!" |
| 1418 | * __pow "^" |
| 1419 | * __concat ".." |
| 1420 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1421 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1422 | * http://www.lua.org/pil/13.2.html |
| 1423 | * |
| 1424 | * __eq "==" |
| 1425 | * __lt "<" |
| 1426 | * __le "<=" |
| 1427 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1428 | |
| 1429 | /* |
| 1430 | * |
| 1431 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1432 | * Class Map |
| 1433 | * |
| 1434 | * |
| 1435 | */ |
| 1436 | |
| 1437 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1438 | * a class session, otherwise it throws an error. |
| 1439 | */ |
| 1440 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1441 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1442 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1443 | } |
| 1444 | |
| 1445 | /* This function is the map constructor. It don't need |
| 1446 | * the class Map object. It creates and return a new Map |
| 1447 | * object. It must be called only during "body" or "init" |
| 1448 | * context because it process some filesystem accesses. |
| 1449 | */ |
| 1450 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1451 | { |
| 1452 | const char *fn; |
| 1453 | int match = PAT_MATCH_STR; |
| 1454 | struct sample_conv conv; |
| 1455 | const char *file = ""; |
| 1456 | int line = 0; |
| 1457 | lua_Debug ar; |
| 1458 | char *err = NULL; |
| 1459 | struct arg args[2]; |
| 1460 | |
| 1461 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1462 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1463 | |
| 1464 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1465 | |
| 1466 | if (lua_gettop(L) >= 2) { |
| 1467 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1468 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1469 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1470 | } |
| 1471 | |
| 1472 | /* Get Lua filename and line number. */ |
| 1473 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1474 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1475 | if (ar.currentline > 0) { /* is there info? */ |
| 1476 | file = ar.short_src; |
| 1477 | line = ar.currentline; |
| 1478 | } |
| 1479 | } |
| 1480 | |
| 1481 | /* fill fake sample_conv struct. */ |
| 1482 | conv.kw = ""; /* unused. */ |
| 1483 | conv.process = NULL; /* unused. */ |
| 1484 | conv.arg_mask = 0; /* unused. */ |
| 1485 | conv.val_args = NULL; /* unused. */ |
| 1486 | conv.out_type = SMP_T_STR; |
| 1487 | conv.private = (void *)(long)match; |
| 1488 | switch (match) { |
| 1489 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1490 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1491 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1492 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1493 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1494 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1496 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1497 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1498 | default: |
| 1499 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1500 | } |
| 1501 | |
| 1502 | /* fill fake args. */ |
| 1503 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1504 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1505 | args[1].type = ARGT_STOP; |
| 1506 | |
| 1507 | /* load the map. */ |
| 1508 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1509 | /* error case: we cant use luaL_error because we must |
| 1510 | * free the err variable. |
| 1511 | */ |
| 1512 | luaL_where(L, 1); |
| 1513 | lua_pushfstring(L, "'new': %s.", err); |
| 1514 | lua_concat(L, 2); |
| 1515 | free(err); |
| 1516 | WILL_LJMP(lua_error(L)); |
| 1517 | } |
| 1518 | |
| 1519 | /* create the lua object. */ |
| 1520 | lua_newtable(L); |
| 1521 | lua_pushlightuserdata(L, args[0].data.map); |
| 1522 | lua_rawseti(L, -2, 0); |
| 1523 | |
| 1524 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1525 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1526 | lua_setmetatable(L, -2); |
| 1527 | |
| 1528 | |
| 1529 | return 1; |
| 1530 | } |
| 1531 | |
| 1532 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1533 | { |
| 1534 | struct map_descriptor *desc; |
| 1535 | struct pattern *pat; |
| 1536 | struct sample smp; |
| 1537 | |
| 1538 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1539 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1540 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1541 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1542 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1543 | } |
| 1544 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1545 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1546 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1547 | smp.data.u.str.area = (char *)MAY_LJMP(luaL_checklstring(L, 2, (size_t *)&smp.data.u.str.data)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1548 | } |
| 1549 | |
| 1550 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1551 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1552 | if (str) |
| 1553 | lua_pushstring(L, ""); |
| 1554 | else |
| 1555 | lua_pushnil(L); |
| 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | /* 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] | 1560 | 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] | 1561 | return 1; |
| 1562 | } |
| 1563 | |
| 1564 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1565 | { |
| 1566 | return _hlua_map_lookup(L, 0); |
| 1567 | } |
| 1568 | |
| 1569 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1570 | { |
| 1571 | return _hlua_map_lookup(L, 1); |
| 1572 | } |
| 1573 | |
| 1574 | /* |
| 1575 | * |
| 1576 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1577 | * Class Socket |
| 1578 | * |
| 1579 | * |
| 1580 | */ |
| 1581 | |
| 1582 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1583 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1584 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1585 | } |
| 1586 | |
| 1587 | /* 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] | 1588 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | * received. |
| 1590 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1591 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1592 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1593 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1594 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1595 | if (appctx->ctx.hlua_cosocket.die) { |
| 1596 | si_shutw(si); |
| 1597 | si_shutr(si); |
| 1598 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1599 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1600 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1601 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1602 | } |
| 1603 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1604 | /* If we cant write, wakeup the pending write signals. */ |
| 1605 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1606 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1607 | |
| 1608 | /* If we cant read, wakeup the pending read signals. */ |
| 1609 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1610 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1611 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1612 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1613 | * to be notified whenever the connection completes. |
| 1614 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1615 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1616 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1617 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1618 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1619 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1620 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
| 1622 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1623 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1624 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1625 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1626 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1627 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1628 | |
| 1629 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1630 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1631 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1632 | |
| 1633 | /* Some data were injected in the buffer, notify the stream |
| 1634 | * interface. |
| 1635 | */ |
| 1636 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1637 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1638 | |
| 1639 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1640 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1641 | */ |
| 1642 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1643 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1644 | } |
| 1645 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1646 | /* This function is called when the "struct stream" is destroyed. |
| 1647 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | * Wake all the pending signals. |
| 1649 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1650 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1652 | struct xref *peer; |
| 1653 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1654 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1655 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1656 | if (peer) |
| 1657 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1658 | |
| 1659 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1660 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | } |
| 1663 | |
| 1664 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1665 | * uses this object. If the stream does not exists, just quit. |
| 1666 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1667 | * pending signal can rest in the read and write lists. destroy |
| 1668 | * it. |
| 1669 | */ |
| 1670 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1671 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1672 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1673 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1674 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1676 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1677 | |
| 1678 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1679 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1680 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1682 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1684 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1685 | appctx->ctx.hlua_cosocket.die = 1; |
| 1686 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1688 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1689 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1690 | return 0; |
| 1691 | } |
| 1692 | |
| 1693 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1694 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1696 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1697 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1698 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1700 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1701 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1703 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Check if we run on the same thread than the xreator thread. |
| 1706 | * We cannot access to the socket if the thread is different. |
| 1707 | */ |
| 1708 | if (socket->tid != tid) |
| 1709 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1710 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1711 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1712 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1714 | |
| 1715 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1716 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1717 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1718 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1719 | appctx->ctx.hlua_cosocket.die = 1; |
| 1720 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1722 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1723 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1724 | return 0; |
| 1725 | } |
| 1726 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1727 | /* The close function calls close_helper. |
| 1728 | */ |
| 1729 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1730 | { |
| 1731 | MAY_LJMP(check_args(L, 1, "close")); |
| 1732 | return hlua_socket_close_helper(L); |
| 1733 | } |
| 1734 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1735 | /* This Lua function assumes that the stack contain three parameters. |
| 1736 | * 1 - USERDATA containing a struct socket |
| 1737 | * 2 - INTEGER with values of the macro defined below |
| 1738 | * If the integer is -1, we must read at most one line. |
| 1739 | * If the integer is -2, we ust read all the data until the |
| 1740 | * end of the stream. |
| 1741 | * If the integer is positive value, we must read a number of |
| 1742 | * bytes corresponding to this value. |
| 1743 | */ |
| 1744 | #define HLSR_READ_LINE (-1) |
| 1745 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1746 | __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] | 1747 | { |
| 1748 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1749 | int wanted = lua_tointeger(L, 2); |
| 1750 | struct hlua *hlua = hlua_gethlua(L); |
| 1751 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1752 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1753 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1754 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1755 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1756 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1757 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1758 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1759 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1760 | struct stream_interface *si; |
| 1761 | struct stream *s; |
| 1762 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1763 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1764 | |
| 1765 | /* Check if this lua stack is schedulable. */ |
| 1766 | if (!hlua || !hlua->task) |
| 1767 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1768 | "'frontend', 'backend' or 'task'")); |
| 1769 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1770 | /* Check if we run on the same thread than the xreator thread. |
| 1771 | * We cannot access to the socket if the thread is different. |
| 1772 | */ |
| 1773 | if (socket->tid != tid) |
| 1774 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1775 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1776 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1777 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1778 | if (!peer) |
| 1779 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1780 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1781 | si = appctx->owner; |
| 1782 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1784 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1787 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | if (nblk < 0) /* Connection close. */ |
| 1789 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1790 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1792 | |
| 1793 | /* remove final \r\n. */ |
| 1794 | if (nblk == 1) { |
| 1795 | if (blk1[len1-1] == '\n') { |
| 1796 | len1--; |
| 1797 | skip_at_end++; |
| 1798 | if (blk1[len1-1] == '\r') { |
| 1799 | len1--; |
| 1800 | skip_at_end++; |
| 1801 | } |
| 1802 | } |
| 1803 | } |
| 1804 | else { |
| 1805 | if (blk2[len2-1] == '\n') { |
| 1806 | len2--; |
| 1807 | skip_at_end++; |
| 1808 | if (blk2[len2-1] == '\r') { |
| 1809 | len2--; |
| 1810 | skip_at_end++; |
| 1811 | } |
| 1812 | } |
| 1813 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | } |
| 1815 | |
| 1816 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1818 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | if (nblk < 0) /* Connection close. */ |
| 1820 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1821 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | goto connection_empty; |
| 1823 | } |
| 1824 | |
| 1825 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1827 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | if (nblk < 0) /* Connection close. */ |
| 1829 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1830 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | goto connection_empty; |
| 1832 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1833 | missing_bytes = wanted - socket->b.n; |
| 1834 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1835 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1836 | len1 = missing_bytes; |
| 1837 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1838 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | } |
| 1840 | |
| 1841 | len = len1; |
| 1842 | |
| 1843 | luaL_addlstring(&socket->b, blk1, len1); |
| 1844 | if (nblk == 2) { |
| 1845 | len += len2; |
| 1846 | luaL_addlstring(&socket->b, blk2, len2); |
| 1847 | } |
| 1848 | |
| 1849 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1850 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1851 | |
| 1852 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1853 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1854 | |
| 1855 | /* If the pattern reclaim to read all the data |
| 1856 | * in the connection, got out. |
| 1857 | */ |
| 1858 | if (wanted == HLSR_READ_ALL) |
| 1859 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1860 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1861 | goto connection_empty; |
| 1862 | |
| 1863 | /* Return result. */ |
| 1864 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1865 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | return 1; |
| 1867 | |
| 1868 | connection_closed: |
| 1869 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1870 | xref_unlock(&socket->xref, peer); |
| 1871 | |
| 1872 | no_peer: |
| 1873 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | /* If the buffer containds data. */ |
| 1875 | if (socket->b.n > 0) { |
| 1876 | luaL_pushresult(&socket->b); |
| 1877 | return 1; |
| 1878 | } |
| 1879 | lua_pushnil(L); |
| 1880 | lua_pushstring(L, "connection closed."); |
| 1881 | return 2; |
| 1882 | |
| 1883 | connection_empty: |
| 1884 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1885 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1886 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1887 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1888 | } |
| 1889 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1890 | 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] | 1891 | return 0; |
| 1892 | } |
| 1893 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1894 | /* This Lua function gets two parameters. The first one can be string |
| 1895 | * or a number. If the string is "*l", the user requires one line. If |
| 1896 | * 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] | 1897 | * If the value is a number, the user require a number of bytes equal |
| 1898 | * to the value. The default value is "*l" (a line). |
| 1899 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1900 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1901 | * integer takes this values: |
| 1902 | * -1 : read a line |
| 1903 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1904 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1906 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1907 | * concatenated with the read data. |
| 1908 | */ |
| 1909 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1910 | { |
| 1911 | int wanted = HLSR_READ_LINE; |
| 1912 | const char *pattern; |
| 1913 | int type; |
| 1914 | char *error; |
| 1915 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1916 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1917 | |
| 1918 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1919 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1920 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1921 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1923 | /* Check if we run on the same thread than the xreator thread. |
| 1924 | * We cannot access to the socket if the thread is different. |
| 1925 | */ |
| 1926 | if (socket->tid != tid) |
| 1927 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1928 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1929 | /* check for pattern. */ |
| 1930 | if (lua_gettop(L) >= 2) { |
| 1931 | type = lua_type(L, 2); |
| 1932 | if (type == LUA_TSTRING) { |
| 1933 | pattern = lua_tostring(L, 2); |
| 1934 | if (strcmp(pattern, "*a") == 0) |
| 1935 | wanted = HLSR_READ_ALL; |
| 1936 | else if (strcmp(pattern, "*l") == 0) |
| 1937 | wanted = HLSR_READ_LINE; |
| 1938 | else { |
| 1939 | wanted = strtoll(pattern, &error, 10); |
| 1940 | if (*error != '\0') |
| 1941 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1942 | } |
| 1943 | } |
| 1944 | else if (type == LUA_TNUMBER) { |
| 1945 | wanted = lua_tointeger(L, 2); |
| 1946 | if (wanted < 0) |
| 1947 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | /* Set pattern. */ |
| 1952 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1953 | |
| 1954 | /* Check if we would replace the top by itself. */ |
| 1955 | if (lua_gettop(L) != 2) |
| 1956 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1958 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1959 | luaL_buffinit(L, &socket->b); |
| 1960 | |
| 1961 | /* Check prefix. */ |
| 1962 | if (lua_gettop(L) >= 3) { |
| 1963 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1964 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1965 | pattern = lua_tolstring(L, 3, &len); |
| 1966 | luaL_addlstring(&socket->b, pattern, len); |
| 1967 | } |
| 1968 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1969 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1970 | } |
| 1971 | |
| 1972 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1973 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1974 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1975 | 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] | 1976 | { |
| 1977 | struct hlua_socket *socket; |
| 1978 | struct hlua *hlua = hlua_gethlua(L); |
| 1979 | struct appctx *appctx; |
| 1980 | size_t buf_len; |
| 1981 | const char *buf; |
| 1982 | int len; |
| 1983 | int send_len; |
| 1984 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1985 | struct xref *peer; |
| 1986 | struct stream_interface *si; |
| 1987 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1988 | |
| 1989 | /* Check if this lua stack is schedulable. */ |
| 1990 | if (!hlua || !hlua->task) |
| 1991 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1992 | "'frontend', 'backend' or 'task'")); |
| 1993 | |
| 1994 | /* Get object */ |
| 1995 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1996 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1997 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1998 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1999 | /* Check if we run on the same thread than the xreator thread. |
| 2000 | * We cannot access to the socket if the thread is different. |
| 2001 | */ |
| 2002 | if (socket->tid != tid) |
| 2003 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2004 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2005 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2006 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2007 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2008 | lua_pushinteger(L, -1); |
| 2009 | return 1; |
| 2010 | } |
| 2011 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2012 | si = appctx->owner; |
| 2013 | s = si_strm(si); |
| 2014 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2016 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2017 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2018 | lua_pushinteger(L, -1); |
| 2019 | return 1; |
| 2020 | } |
| 2021 | |
| 2022 | /* Update the input buffer data. */ |
| 2023 | buf += sent; |
| 2024 | send_len = buf_len - sent; |
| 2025 | |
| 2026 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2027 | if (sent >= buf_len) { |
| 2028 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2030 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2031 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2032 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2033 | * the request buffer if its not required. |
| 2034 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2035 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2036 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2037 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2038 | } |
| 2039 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2040 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2041 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2042 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2044 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2045 | |
| 2046 | /* send data */ |
| 2047 | if (len < send_len) |
| 2048 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2049 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2052 | * the data" (-2) are not expected because the available length |
| 2053 | * is tested. |
| 2054 | * Other unknown error are also not expected. |
| 2055 | */ |
| 2056 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2057 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2058 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2059 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2060 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2062 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2063 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2064 | return 1; |
| 2065 | } |
| 2066 | |
| 2067 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2068 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2069 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2070 | s->req.rex = TICK_ETERNITY; |
| 2071 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | |
| 2073 | /* Update length sent. */ |
| 2074 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2075 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2076 | |
| 2077 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | if (sent + len >= buf_len) { |
| 2079 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2081 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2082 | |
| 2083 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2084 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2085 | xref_unlock(&socket->xref, peer); |
| 2086 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2087 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2088 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2089 | 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] | 2090 | return 0; |
| 2091 | } |
| 2092 | |
| 2093 | /* This function initiate the send of data. It just check the input |
| 2094 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2095 | * 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] | 2096 | * "hlua_socket_write_yield" that can yield. |
| 2097 | * |
| 2098 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2099 | * the associated object. The second is a string buffer. The third is |
| 2100 | * a facultative integer that represents where is the buffer position |
| 2101 | * of the start of the data that can send. The first byte is the |
| 2102 | * position "1". The default value is "1". The fourth argument is a |
| 2103 | * facultative integer that represents where is the buffer position |
| 2104 | * of the end of the data that can send. The default is the last byte. |
| 2105 | */ |
| 2106 | static int hlua_socket_send(struct lua_State *L) |
| 2107 | { |
| 2108 | int i; |
| 2109 | int j; |
| 2110 | const char *buf; |
| 2111 | size_t buf_len; |
| 2112 | |
| 2113 | /* Check number of arguments. */ |
| 2114 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2115 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2116 | |
| 2117 | /* Get the string. */ |
| 2118 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2119 | |
| 2120 | /* Get and check j. */ |
| 2121 | if (lua_gettop(L) == 4) { |
| 2122 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2123 | if (j < 0) |
| 2124 | j = buf_len + j + 1; |
| 2125 | if (j > buf_len) |
| 2126 | j = buf_len + 1; |
| 2127 | lua_pop(L, 1); |
| 2128 | } |
| 2129 | else |
| 2130 | j = buf_len; |
| 2131 | |
| 2132 | /* Get and check i. */ |
| 2133 | if (lua_gettop(L) == 3) { |
| 2134 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2135 | if (i < 0) |
| 2136 | i = buf_len + i + 1; |
| 2137 | if (i > buf_len) |
| 2138 | i = buf_len + 1; |
| 2139 | lua_pop(L, 1); |
| 2140 | } else |
| 2141 | i = 1; |
| 2142 | |
| 2143 | /* Check bth i and j. */ |
| 2144 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2145 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2146 | return 1; |
| 2147 | } |
| 2148 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2149 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2150 | return 1; |
| 2151 | } |
| 2152 | if (i == 0) |
| 2153 | i = 1; |
| 2154 | if (j == 0) |
| 2155 | j = 1; |
| 2156 | |
| 2157 | /* Pop the string. */ |
| 2158 | lua_pop(L, 1); |
| 2159 | |
| 2160 | /* Update the buffer length. */ |
| 2161 | buf += i - 1; |
| 2162 | buf_len = j - i + 1; |
| 2163 | lua_pushlstring(L, buf, buf_len); |
| 2164 | |
| 2165 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2166 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2168 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2169 | } |
| 2170 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2171 | #define SOCKET_INFO_MAX_LEN sizeof("[0000:0000:0000:0000:0000:0000:0000:0000]:12345") |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2173 | { |
| 2174 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2175 | int ret; |
| 2176 | int len; |
| 2177 | char *p; |
| 2178 | |
| 2179 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2180 | if (ret <= 0) { |
| 2181 | lua_pushnil(L); |
| 2182 | return 1; |
| 2183 | } |
| 2184 | |
| 2185 | if (ret == AF_UNIX) { |
| 2186 | lua_pushstring(L, buffer+1); |
| 2187 | return 1; |
| 2188 | } |
| 2189 | else if (ret == AF_INET6) { |
| 2190 | buffer[0] = '['; |
| 2191 | len = strlen(buffer); |
| 2192 | buffer[len] = ']'; |
| 2193 | len++; |
| 2194 | buffer[len] = ':'; |
| 2195 | len++; |
| 2196 | p = buffer; |
| 2197 | } |
| 2198 | else if (ret == AF_INET) { |
| 2199 | p = buffer + 1; |
| 2200 | len = strlen(p); |
| 2201 | p[len] = ':'; |
| 2202 | len++; |
| 2203 | } |
| 2204 | else { |
| 2205 | lua_pushnil(L); |
| 2206 | return 1; |
| 2207 | } |
| 2208 | |
| 2209 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2210 | lua_pushnil(L); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | lua_pushstring(L, p); |
| 2215 | return 1; |
| 2216 | } |
| 2217 | |
| 2218 | /* Returns information about the peer of the connection. */ |
| 2219 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2220 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2221 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2222 | struct xref *peer; |
| 2223 | struct appctx *appctx; |
| 2224 | struct stream_interface *si; |
| 2225 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2226 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2229 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2230 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2231 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2232 | /* Check if we run on the same thread than the xreator thread. |
| 2233 | * We cannot access to the socket if the thread is different. |
| 2234 | */ |
| 2235 | if (socket->tid != tid) |
| 2236 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2237 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2238 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2239 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2240 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2241 | lua_pushnil(L); |
| 2242 | return 1; |
| 2243 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2245 | si = appctx->owner; |
| 2246 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2248 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2249 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2250 | lua_pushnil(L); |
| 2251 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2252 | } |
| 2253 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2254 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2255 | xref_unlock(&socket->xref, peer); |
| 2256 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | } |
| 2258 | |
| 2259 | /* Returns information about my connection side. */ |
| 2260 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2261 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2262 | struct hlua_socket *socket; |
| 2263 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2264 | struct appctx *appctx; |
| 2265 | struct xref *peer; |
| 2266 | struct stream_interface *si; |
| 2267 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2268 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2269 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2271 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2272 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2273 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2274 | /* Check if we run on the same thread than the xreator thread. |
| 2275 | * We cannot access to the socket if the thread is different. |
| 2276 | */ |
| 2277 | if (socket->tid != tid) |
| 2278 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2279 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2280 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2281 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2282 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | lua_pushnil(L); |
| 2284 | return 1; |
| 2285 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2286 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2287 | si = appctx->owner; |
| 2288 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2289 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2290 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2291 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2292 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2293 | lua_pushnil(L); |
| 2294 | return 1; |
| 2295 | } |
| 2296 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2297 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2298 | xref_unlock(&socket->xref, peer); |
| 2299 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2300 | } |
| 2301 | |
| 2302 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2303 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | .obj_type = OBJ_TYPE_APPLET, |
| 2305 | .name = "<LUA_TCP>", |
| 2306 | .fct = hlua_socket_handler, |
| 2307 | .release = hlua_socket_release, |
| 2308 | }; |
| 2309 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2310 | __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] | 2311 | { |
| 2312 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2313 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2315 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2316 | struct stream_interface *si; |
| 2317 | struct stream *s; |
| 2318 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2319 | /* Check if we run on the same thread than the xreator thread. |
| 2320 | * We cannot access to the socket if the thread is different. |
| 2321 | */ |
| 2322 | if (socket->tid != tid) |
| 2323 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2324 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2326 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2327 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2328 | lua_pushnil(L); |
| 2329 | lua_pushstring(L, "Can't connect"); |
| 2330 | return 2; |
| 2331 | } |
| 2332 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2333 | si = appctx->owner; |
| 2334 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2335 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2336 | /* Check if we run on the same thread than the xreator thread. |
| 2337 | * We cannot access to the socket if the thread is different. |
| 2338 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | if (socket->tid != tid) { |
| 2340 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2341 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2342 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2343 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2345 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2346 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2347 | lua_pushnil(L); |
| 2348 | lua_pushstring(L, "Can't connect"); |
| 2349 | return 2; |
| 2350 | } |
| 2351 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2352 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2353 | |
| 2354 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2355 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2356 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2357 | lua_pushinteger(L, 1); |
| 2358 | return 1; |
| 2359 | } |
| 2360 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2362 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | } |
| 2365 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2366 | 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] | 2367 | return 0; |
| 2368 | } |
| 2369 | |
| 2370 | /* This function fail or initite the connection. */ |
| 2371 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2372 | { |
| 2373 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2374 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2375 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2376 | struct hlua *hlua; |
| 2377 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2378 | int low, high; |
| 2379 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2380 | struct xref *peer; |
| 2381 | struct stream_interface *si; |
| 2382 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2384 | if (lua_gettop(L) < 2) |
| 2385 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2386 | |
| 2387 | /* Get args. */ |
| 2388 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2389 | |
| 2390 | /* Check if we run on the same thread than the xreator thread. |
| 2391 | * We cannot access to the socket if the thread is different. |
| 2392 | */ |
| 2393 | if (socket->tid != tid) |
| 2394 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2395 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2396 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2397 | if (lua_gettop(L) >= 3) { |
| 2398 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2399 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2400 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2401 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2402 | * that are not enclosed within square brackets. |
| 2403 | */ |
| 2404 | if (port > 0) { |
| 2405 | luaL_buffinit(L, &b); |
| 2406 | luaL_addstring(&b, ip); |
| 2407 | luaL_addchar(&b, ':'); |
| 2408 | luaL_pushresult(&b); |
| 2409 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2410 | } |
| 2411 | } |
| 2412 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2414 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2415 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2416 | lua_pushnil(L); |
| 2417 | return 1; |
| 2418 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2419 | |
| 2420 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2421 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | if (!addr) { |
| 2423 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2424 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2425 | } |
| 2426 | if (low != high) { |
| 2427 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2428 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2429 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2430 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2431 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2432 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2433 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2434 | if (port == -1) { |
| 2435 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2436 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2438 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2439 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | if (port == -1) { |
| 2441 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2442 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2443 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2444 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2445 | } |
| 2446 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2447 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2448 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2449 | si = appctx->owner; |
| 2450 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2451 | |
| 2452 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2453 | xref_unlock(&socket->xref, peer); |
| 2454 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2455 | } |
| 2456 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2457 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2458 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2459 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2460 | |
| 2461 | /* inform the stream that we want to be notified whenever the |
| 2462 | * connection completes. |
| 2463 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2464 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2465 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2466 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2467 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2468 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2469 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2471 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2472 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2473 | } |
| 2474 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2475 | |
| 2476 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2477 | /* Return yield waiting for connection. */ |
| 2478 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2479 | 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] | 2480 | |
| 2481 | return 0; |
| 2482 | } |
| 2483 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2484 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2485 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2486 | { |
| 2487 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2488 | struct xref *peer; |
| 2489 | struct appctx *appctx; |
| 2490 | struct stream_interface *si; |
| 2491 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2492 | |
| 2493 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2494 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2495 | |
| 2496 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2497 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2498 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2499 | lua_pushnil(L); |
| 2500 | return 1; |
| 2501 | } |
| 2502 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2503 | si = appctx->owner; |
| 2504 | s = si_strm(si); |
| 2505 | |
| 2506 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2507 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2509 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2510 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2511 | |
| 2512 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2513 | { |
| 2514 | return 0; |
| 2515 | } |
| 2516 | |
| 2517 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2518 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2519 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2520 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2521 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2522 | struct xref *peer; |
| 2523 | struct appctx *appctx; |
| 2524 | struct stream_interface *si; |
| 2525 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2526 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2527 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2528 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2529 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2530 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2531 | /* convert the timeout to millis */ |
| 2532 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2533 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2534 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2535 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2536 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2537 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2538 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2539 | 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] | 2540 | |
| 2541 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2542 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2543 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2544 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2545 | /* Check if we run on the same thread than the xreator thread. |
| 2546 | * We cannot access to the socket if the thread is different. |
| 2547 | */ |
| 2548 | if (socket->tid != tid) |
| 2549 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2550 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2551 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2552 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2553 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2554 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2555 | WILL_LJMP(lua_error(L)); |
| 2556 | return 0; |
| 2557 | } |
| 2558 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2559 | si = appctx->owner; |
| 2560 | s = si_strm(si); |
| 2561 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2562 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2563 | s->req.rto = tmout; |
| 2564 | s->req.wto = tmout; |
| 2565 | s->res.rto = tmout; |
| 2566 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2567 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2568 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2569 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2570 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2571 | |
| 2572 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2573 | task_queue(s->task); |
| 2574 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2575 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2576 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2577 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2578 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2582 | { |
| 2583 | struct hlua_socket *socket; |
| 2584 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2585 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2586 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | |
| 2588 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2589 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2590 | hlua_pusherror(L, "socket: full stack"); |
| 2591 | goto out_fail_conf; |
| 2592 | } |
| 2593 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2594 | /* Create the object: obj[0] = userdata. */ |
| 2595 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2597 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2599 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2601 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2602 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2603 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2604 | goto out_fail_conf; |
| 2605 | } |
| 2606 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2607 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2609 | lua_setmetatable(L, -2); |
| 2610 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2611 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2612 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2613 | if (!appctx) { |
| 2614 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2615 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2616 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2617 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2618 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2619 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2620 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2621 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2622 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2623 | /* Now create a session, task and stream for this applet */ |
| 2624 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2625 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2627 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | } |
| 2629 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2630 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2631 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2633 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | } |
| 2635 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2636 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2637 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2639 | /* Configure "right" stream interface. this "si" is used to connect |
| 2640 | * and retrieve data from the server. The connection is initialized |
| 2641 | * with the "struct server". |
| 2642 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2643 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2644 | |
| 2645 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2646 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2647 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2648 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2649 | return 1; |
| 2650 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2651 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2652 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2653 | out_fail_sess: |
| 2654 | appctx_free(appctx); |
| 2655 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | WILL_LJMP(lua_error(L)); |
| 2657 | return 0; |
| 2658 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2659 | |
| 2660 | /* |
| 2661 | * |
| 2662 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2663 | * Class Channel |
| 2664 | * |
| 2665 | * |
| 2666 | */ |
| 2667 | |
| 2668 | /* Returns the struct hlua_channel join to the class channel in the |
| 2669 | * stack entry "ud" or throws an argument error. |
| 2670 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2671 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2672 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2673 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2674 | } |
| 2675 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2676 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2677 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2679 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2681 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2682 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2683 | return 0; |
| 2684 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2685 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2686 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2687 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2688 | |
| 2689 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2690 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2691 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2692 | return 1; |
| 2693 | } |
| 2694 | |
| 2695 | /* Duplicate all the data present in the input channel and put it |
| 2696 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2697 | * the stack if the channel is closed and all the data are consumed, |
| 2698 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2699 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2700 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2701 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2702 | { |
| 2703 | char *blk1; |
| 2704 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2705 | size_t len1; |
| 2706 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2707 | int ret; |
| 2708 | luaL_Buffer b; |
| 2709 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2710 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2711 | if (unlikely(ret == 0)) |
| 2712 | return 0; |
| 2713 | |
| 2714 | if (unlikely(ret < 0)) { |
| 2715 | lua_pushnil(L); |
| 2716 | return -1; |
| 2717 | } |
| 2718 | |
| 2719 | luaL_buffinit(L, &b); |
| 2720 | luaL_addlstring(&b, blk1, len1); |
| 2721 | if (unlikely(ret == 2)) |
| 2722 | luaL_addlstring(&b, blk2, len2); |
| 2723 | luaL_pushresult(&b); |
| 2724 | |
| 2725 | if (unlikely(ret == 2)) |
| 2726 | return len1 + len2; |
| 2727 | return len1; |
| 2728 | } |
| 2729 | |
| 2730 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2731 | * a yield. This function keep the data in the buffer. |
| 2732 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2733 | __LJMP static int hlua_channel_dup_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2734 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2735 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2736 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2737 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2738 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2739 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2740 | WILL_LJMP(lua_error(L)); |
| 2741 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2742 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2743 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_dup_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2744 | return 1; |
| 2745 | } |
| 2746 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2747 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2748 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2749 | { |
| 2750 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2751 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2752 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2753 | } |
| 2754 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2755 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2756 | * a yield. This function consumes the data in the buffer. It returns |
| 2757 | * a string containing the data or a nil pointer if no data are available |
| 2758 | * and the channel is closed. |
| 2759 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2760 | __LJMP static int hlua_channel_get_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2761 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2762 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2763 | int ret; |
| 2764 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2765 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2766 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2767 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2768 | WILL_LJMP(lua_error(L)); |
| 2769 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2770 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2772 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_get_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2773 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2774 | if (unlikely(ret == -1)) |
| 2775 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2776 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2777 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | return 1; |
| 2779 | } |
| 2780 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2781 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2782 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2783 | { |
| 2784 | MAY_LJMP(check_args(L, 1, "get")); |
| 2785 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2786 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2787 | } |
| 2788 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2789 | /* This functions consumes and returns one line. If the channel is closed, |
| 2790 | * 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] | 2791 | * 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] | 2792 | * value. |
| 2793 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2794 | __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] | 2795 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2796 | char *blk1; |
| 2797 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2798 | size_t len1; |
| 2799 | size_t len2; |
| 2800 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2801 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2802 | int ret; |
| 2803 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2804 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2805 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2806 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2807 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2808 | WILL_LJMP(lua_error(L)); |
| 2809 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2810 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2812 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_getline_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2813 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2814 | if (ret == -1) { |
| 2815 | lua_pushnil(L); |
| 2816 | return 1; |
| 2817 | } |
| 2818 | |
| 2819 | luaL_buffinit(L, &b); |
| 2820 | luaL_addlstring(&b, blk1, len1); |
| 2821 | len = len1; |
| 2822 | if (unlikely(ret == 2)) { |
| 2823 | luaL_addlstring(&b, blk2, len2); |
| 2824 | len += len2; |
| 2825 | } |
| 2826 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2827 | b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn) + len, NULL, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2828 | return 1; |
| 2829 | } |
| 2830 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2831 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2832 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2833 | { |
| 2834 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2835 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2836 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2837 | } |
| 2838 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2839 | /* This function takes a string as input, and append it at the |
| 2840 | * input side of channel. If the data is too big, but a space |
| 2841 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2842 | * yields. If the data is bigger than the buffer, or if the |
| 2843 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2844 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2846 | __LJMP static int hlua_channel_append_yield(lua_State *L, int status, lua_KContext ctx) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2848 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | size_t len; |
| 2850 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2851 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2852 | int ret; |
| 2853 | int max; |
| 2854 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2855 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2856 | WILL_LJMP(lua_error(L)); |
| 2857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2858 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2859 | * the request buffer if its not required. |
| 2860 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2861 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2862 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2863 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2864 | } |
| 2865 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2866 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | if (max > len - l) |
| 2868 | max = len - l; |
| 2869 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2870 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2871 | if (ret == -2 || ret == -3) { |
| 2872 | lua_pushinteger(L, -1); |
| 2873 | return 1; |
| 2874 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2875 | if (ret == -1) { |
| 2876 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2877 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2878 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | l += ret; |
| 2880 | lua_pop(L, 1); |
| 2881 | lua_pushinteger(L, l); |
| 2882 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2883 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2884 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2885 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2886 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2887 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2888 | */ |
| 2889 | return 1; |
| 2890 | } |
| 2891 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2892 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_append_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2893 | return 1; |
| 2894 | } |
| 2895 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2896 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2897 | * of the written string, or -1 if the channel is closed or if the |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2898 | * buffer size is too little for the data. |
| 2899 | */ |
| 2900 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2901 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2902 | size_t len; |
| 2903 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2904 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2905 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2906 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2907 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | lua_pushinteger(L, 0); |
| 2909 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2910 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | } |
| 2912 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2913 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2914 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2915 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | * or -1 if the channel is closed or if the buffer size is too |
| 2917 | * little for the data. |
| 2918 | */ |
| 2919 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2920 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2921 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2922 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2923 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2924 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2925 | lua_pushinteger(L, 0); |
| 2926 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2927 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2928 | WILL_LJMP(lua_error(L)); |
| 2929 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2930 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2932 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | } |
| 2934 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2935 | /* Append data in the output side of the buffer. This data is immediately |
| 2936 | * sent. The function returns the amount of data written. If the buffer |
| 2937 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | * if the channel is closed. |
| 2939 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2940 | __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] | 2941 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2942 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2943 | size_t len; |
| 2944 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2945 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2946 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2947 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2949 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2950 | WILL_LJMP(lua_error(L)); |
| 2951 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2952 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | lua_pushinteger(L, -1); |
| 2954 | return 1; |
| 2955 | } |
| 2956 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2957 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2958 | * the request buffer if its not required. |
| 2959 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2960 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2961 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2962 | MAY_LJMP(hlua_yieldk(L, 0, 0, hlua_channel_send_yield, TICK_ETERNITY, 0)); |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2963 | } |
| 2964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | /* The written data will be immediately sent, so we can check |
| 2966 | * the available space without taking in account the reserve. |
| 2967 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2968 | * data, because the buffer will be flushed. |
| 2969 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2970 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2971 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2972 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2973 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2974 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2975 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2976 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2977 | return 1; |
| 2978 | |
| 2979 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2980 | if (max > len - l) |
| 2981 | max = len - l; |
| 2982 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2983 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2984 | * detects a non contiguous buffer and realign it. |
| 2985 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2986 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2987 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2988 | |
| 2989 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2990 | max = b_rep_blk(&chn->buf, ci_head(chn), ci_head(chn), str + l, max); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2991 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2992 | /* buffer replace considers that the input part is filled. |
| 2993 | * so, I must forward these new data in the output part. |
| 2994 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 2995 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | |
| 2997 | l += max; |
| 2998 | lua_pop(L, 1); |
| 2999 | lua_pushinteger(L, l); |
| 3000 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3001 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3002 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3003 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3004 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3005 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3006 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3007 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3008 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3009 | if (l < len) { |
| 3010 | /* If we are waiting for space in the response buffer, we |
| 3011 | * must set the flag WAKERESWR. This flag required the task |
| 3012 | * wake up if any activity is detected on the response buffer. |
| 3013 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3014 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3015 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3016 | else |
| 3017 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3018 | 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] | 3019 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | |
| 3021 | return 1; |
| 3022 | } |
| 3023 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3024 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | * yield the LUA process, and resume it without checking the |
| 3026 | * input arguments. |
| 3027 | */ |
| 3028 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3029 | { |
| 3030 | MAY_LJMP(check_args(L, 2, "send")); |
| 3031 | lua_pushinteger(L, 0); |
| 3032 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3033 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3034 | } |
| 3035 | |
| 3036 | /* This function forward and amount of butes. The data pass from |
| 3037 | * the input side of the buffer to the output side, and can be |
| 3038 | * forwarded. This function never fails. |
| 3039 | * |
| 3040 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3041 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3043 | __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] | 3044 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3045 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3046 | int len; |
| 3047 | int l; |
| 3048 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3049 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | |
| 3051 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3052 | |
| 3053 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3054 | WILL_LJMP(lua_error(L)); |
| 3055 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3056 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3057 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3058 | |
| 3059 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3060 | if (max > ci_data(chn)) |
| 3061 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3062 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | l += max; |
| 3064 | |
| 3065 | lua_pop(L, 1); |
| 3066 | lua_pushinteger(L, l); |
| 3067 | |
| 3068 | /* Check if it miss bytes to forward. */ |
| 3069 | if (l < len) { |
| 3070 | /* The the input channel or the output channel are closed, we |
| 3071 | * must return the amount of data forwarded. |
| 3072 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3073 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3074 | return 1; |
| 3075 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3076 | /* If we are waiting for space data in the response buffer, we |
| 3077 | * must set the flag WAKERESWR. This flag required the task |
| 3078 | * wake up if any activity is detected on the response buffer. |
| 3079 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3080 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3081 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3082 | else |
| 3083 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3084 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3085 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3086 | 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] | 3087 | } |
| 3088 | |
| 3089 | return 1; |
| 3090 | } |
| 3091 | |
| 3092 | /* Just check the input and prepare the stack for the previous |
| 3093 | * function "hlua_channel_forward_yield" |
| 3094 | */ |
| 3095 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3096 | { |
| 3097 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3098 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3099 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3100 | |
| 3101 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3102 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3103 | } |
| 3104 | |
| 3105 | /* Just returns the number of bytes available in the input |
| 3106 | * side of the buffer. This function never fails. |
| 3107 | */ |
| 3108 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3109 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3110 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3111 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3112 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3113 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3114 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3115 | struct htx *htx = htxbuf(&chn->buf); |
| 3116 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3117 | } |
| 3118 | else |
| 3119 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3120 | return 1; |
| 3121 | } |
| 3122 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3123 | /* Returns true if the channel is full. */ |
| 3124 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3125 | { |
| 3126 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3127 | |
| 3128 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3129 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3130 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3131 | * applet). |
| 3132 | */ |
| 3133 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3134 | return 1; |
| 3135 | } |
| 3136 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3137 | /* Returns true if the channel is the response channel. */ |
| 3138 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3139 | { |
| 3140 | struct channel *chn; |
| 3141 | |
| 3142 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3143 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3144 | |
| 3145 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3146 | return 1; |
| 3147 | } |
| 3148 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3149 | /* Just returns the number of bytes available in the output |
| 3150 | * side of the buffer. This function never fails. |
| 3151 | */ |
| 3152 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3153 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3154 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3155 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3156 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3157 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3158 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3159 | return 1; |
| 3160 | } |
| 3161 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3162 | /* |
| 3163 | * |
| 3164 | * |
| 3165 | * Class Fetches |
| 3166 | * |
| 3167 | * |
| 3168 | */ |
| 3169 | |
| 3170 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3171 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3172 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3173 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3174 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3175 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3176 | } |
| 3177 | |
| 3178 | /* This function creates and push in the stack a fetch object according |
| 3179 | * with a current TXN. |
| 3180 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3181 | 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] | 3182 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3183 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3184 | |
| 3185 | /* Check stack size. */ |
| 3186 | if (!lua_checkstack(L, 3)) |
| 3187 | return 0; |
| 3188 | |
| 3189 | /* Create the object: obj[0] = userdata. |
| 3190 | * Note that the base of the Fetches object is the |
| 3191 | * transaction object. |
| 3192 | */ |
| 3193 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3194 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3195 | lua_rawseti(L, -2, 0); |
| 3196 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3197 | hsmp->s = txn->s; |
| 3198 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3199 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3200 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3201 | |
| 3202 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3203 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3204 | lua_setmetatable(L, -2); |
| 3205 | |
| 3206 | return 1; |
| 3207 | } |
| 3208 | |
| 3209 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3210 | * It uses closure argument to store the associated sample-fetch. It |
| 3211 | * returns only one argument or throws an error. An error is thrown |
| 3212 | * only if an error is encountered during the argument parsing. If |
| 3213 | * the "sample-fetch" function fails, nil is returned. |
| 3214 | */ |
| 3215 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3216 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3217 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3218 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3219 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3220 | int i; |
| 3221 | struct sample smp; |
| 3222 | |
| 3223 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3224 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3225 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3226 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3227 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3228 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3229 | /* Check execution authorization. */ |
| 3230 | if (f->use & SMP_USE_HTTP_ANY && |
| 3231 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3232 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3233 | "is not available in Lua services", f->kw); |
| 3234 | WILL_LJMP(lua_error(L)); |
| 3235 | } |
| 3236 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3237 | /* Get extra arguments. */ |
| 3238 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3239 | if (i >= ARGM_NBARGS) |
| 3240 | break; |
| 3241 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3242 | } |
| 3243 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3244 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3245 | |
| 3246 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3247 | 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] | 3248 | |
| 3249 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3250 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3251 | lua_pushfstring(L, "error in arguments"); |
| 3252 | WILL_LJMP(lua_error(L)); |
| 3253 | } |
| 3254 | |
| 3255 | /* Initialise the sample. */ |
| 3256 | memset(&smp, 0, sizeof(smp)); |
| 3257 | |
| 3258 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3259 | 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] | 3260 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3261 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3262 | lua_pushstring(L, ""); |
| 3263 | else |
| 3264 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3265 | return 1; |
| 3266 | } |
| 3267 | |
| 3268 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3269 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3270 | hlua_smp2lua_str(L, &smp); |
| 3271 | else |
| 3272 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3273 | return 1; |
| 3274 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3275 | |
| 3276 | /* |
| 3277 | * |
| 3278 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3279 | * Class Converters |
| 3280 | * |
| 3281 | * |
| 3282 | */ |
| 3283 | |
| 3284 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3285 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3286 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3287 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3288 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3289 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3290 | } |
| 3291 | |
| 3292 | /* This function creates and push in the stack a Converters object |
| 3293 | * according with a current TXN. |
| 3294 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3295 | 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] | 3296 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3297 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3298 | |
| 3299 | /* Check stack size. */ |
| 3300 | if (!lua_checkstack(L, 3)) |
| 3301 | return 0; |
| 3302 | |
| 3303 | /* Create the object: obj[0] = userdata. |
| 3304 | * Note that the base of the Converters object is the |
| 3305 | * same than the TXN object. |
| 3306 | */ |
| 3307 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3308 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3309 | lua_rawseti(L, -2, 0); |
| 3310 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3311 | hsmp->s = txn->s; |
| 3312 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3313 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3314 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3315 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3316 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3317 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3318 | lua_setmetatable(L, -2); |
| 3319 | |
| 3320 | return 1; |
| 3321 | } |
| 3322 | |
| 3323 | /* This function is an LUA binding. It is called with each converter. |
| 3324 | * It uses closure argument to store the associated converter. It |
| 3325 | * returns only one argument or throws an error. An error is thrown |
| 3326 | * only if an error is encountered during the argument parsing. If |
| 3327 | * the converter function function fails, nil is returned. |
| 3328 | */ |
| 3329 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3330 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3331 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3332 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3333 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3334 | int i; |
| 3335 | struct sample smp; |
| 3336 | |
| 3337 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3338 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3339 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3340 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3341 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3342 | |
| 3343 | /* Get extra arguments. */ |
| 3344 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3345 | if (i >= ARGM_NBARGS) |
| 3346 | break; |
| 3347 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3348 | } |
| 3349 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3350 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3351 | |
| 3352 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3353 | 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] | 3354 | |
| 3355 | /* Run the special args checker. */ |
| 3356 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3357 | hlua_pusherror(L, "error in arguments"); |
| 3358 | WILL_LJMP(lua_error(L)); |
| 3359 | } |
| 3360 | |
| 3361 | /* Initialise the sample. */ |
| 3362 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3363 | hlua_pusherror(L, "error in the input argument"); |
| 3364 | WILL_LJMP(lua_error(L)); |
| 3365 | } |
| 3366 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3367 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3368 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3369 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3370 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3371 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3372 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3373 | WILL_LJMP(lua_error(L)); |
| 3374 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3375 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3376 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | hlua_pusherror(L, "error during the input argument casting"); |
| 3378 | WILL_LJMP(lua_error(L)); |
| 3379 | } |
| 3380 | |
| 3381 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3382 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3383 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3384 | lua_pushstring(L, ""); |
| 3385 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3386 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3387 | return 1; |
| 3388 | } |
| 3389 | |
| 3390 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3391 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3392 | hlua_smp2lua_str(L, &smp); |
| 3393 | else |
| 3394 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3395 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3396 | } |
| 3397 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3398 | /* |
| 3399 | * |
| 3400 | * |
| 3401 | * Class AppletTCP |
| 3402 | * |
| 3403 | * |
| 3404 | */ |
| 3405 | |
| 3406 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3407 | * a class stream, otherwise it throws an error. |
| 3408 | */ |
| 3409 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3410 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3411 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3412 | } |
| 3413 | |
| 3414 | /* This function creates and push in the stack an Applet object |
| 3415 | * according with a current TXN. |
| 3416 | */ |
| 3417 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3418 | { |
| 3419 | struct hlua_appctx *appctx; |
| 3420 | struct stream_interface *si = ctx->owner; |
| 3421 | struct stream *s = si_strm(si); |
| 3422 | struct proxy *p = s->be; |
| 3423 | |
| 3424 | /* Check stack size. */ |
| 3425 | if (!lua_checkstack(L, 3)) |
| 3426 | return 0; |
| 3427 | |
| 3428 | /* Create the object: obj[0] = userdata. |
| 3429 | * Note that the base of the Converters object is the |
| 3430 | * same than the TXN object. |
| 3431 | */ |
| 3432 | lua_newtable(L); |
| 3433 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3434 | lua_rawseti(L, -2, 0); |
| 3435 | appctx->appctx = ctx; |
| 3436 | appctx->htxn.s = s; |
| 3437 | appctx->htxn.p = p; |
| 3438 | |
| 3439 | /* Create the "f" field that contains a list of fetches. */ |
| 3440 | lua_pushstring(L, "f"); |
| 3441 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3442 | return 0; |
| 3443 | lua_settable(L, -3); |
| 3444 | |
| 3445 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3446 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3447 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3448 | return 0; |
| 3449 | lua_settable(L, -3); |
| 3450 | |
| 3451 | /* Create the "c" field that contains a list of converters. */ |
| 3452 | lua_pushstring(L, "c"); |
| 3453 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3454 | return 0; |
| 3455 | lua_settable(L, -3); |
| 3456 | |
| 3457 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3458 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3459 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3460 | return 0; |
| 3461 | lua_settable(L, -3); |
| 3462 | |
| 3463 | /* Pop a class stream metatable and affect it to the table. */ |
| 3464 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3465 | lua_setmetatable(L, -2); |
| 3466 | |
| 3467 | return 1; |
| 3468 | } |
| 3469 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3470 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3471 | { |
| 3472 | struct hlua_appctx *appctx; |
| 3473 | struct stream *s; |
| 3474 | const char *name; |
| 3475 | size_t len; |
| 3476 | struct sample smp; |
| 3477 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3478 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3479 | 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] | 3480 | |
| 3481 | /* It is useles to retrieve the stream, but this function |
| 3482 | * runs only in a stream context. |
| 3483 | */ |
| 3484 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3485 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3486 | s = appctx->htxn.s; |
| 3487 | |
| 3488 | /* Converts the third argument in a sample. */ |
| 3489 | hlua_lua2smp(L, 3, &smp); |
| 3490 | |
| 3491 | /* Store the sample in a variable. */ |
| 3492 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3493 | |
| 3494 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3495 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3496 | else |
| 3497 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3498 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3499 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3500 | } |
| 3501 | |
| 3502 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3503 | { |
| 3504 | struct hlua_appctx *appctx; |
| 3505 | struct stream *s; |
| 3506 | const char *name; |
| 3507 | size_t len; |
| 3508 | struct sample smp; |
| 3509 | |
| 3510 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3511 | |
| 3512 | /* It is useles to retrieve the stream, but this function |
| 3513 | * runs only in a stream context. |
| 3514 | */ |
| 3515 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3516 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3517 | s = appctx->htxn.s; |
| 3518 | |
| 3519 | /* Unset the variable. */ |
| 3520 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3521 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3522 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3523 | } |
| 3524 | |
| 3525 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3526 | { |
| 3527 | struct hlua_appctx *appctx; |
| 3528 | struct stream *s; |
| 3529 | const char *name; |
| 3530 | size_t len; |
| 3531 | struct sample smp; |
| 3532 | |
| 3533 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3534 | |
| 3535 | /* It is useles to retrieve the stream, but this function |
| 3536 | * runs only in a stream context. |
| 3537 | */ |
| 3538 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3539 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3540 | s = appctx->htxn.s; |
| 3541 | |
| 3542 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3543 | if (!vars_get_by_name(name, len, &smp)) { |
| 3544 | lua_pushnil(L); |
| 3545 | return 1; |
| 3546 | } |
| 3547 | |
| 3548 | return hlua_smp2lua(L, &smp); |
| 3549 | } |
| 3550 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3551 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3552 | { |
| 3553 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3554 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3555 | struct hlua *hlua; |
| 3556 | |
| 3557 | /* 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] | 3558 | if (!s->hlua) |
| 3559 | return 0; |
| 3560 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3561 | |
| 3562 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3563 | |
| 3564 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3565 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3566 | |
| 3567 | /* Get and store new value. */ |
| 3568 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3569 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3570 | |
| 3571 | return 0; |
| 3572 | } |
| 3573 | |
| 3574 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3575 | { |
| 3576 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3577 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3578 | struct hlua *hlua; |
| 3579 | |
| 3580 | /* 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] | 3581 | if (!s->hlua) { |
| 3582 | lua_pushnil(L); |
| 3583 | return 1; |
| 3584 | } |
| 3585 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3586 | |
| 3587 | /* Push configuration index in the stack. */ |
| 3588 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3589 | |
| 3590 | return 1; |
| 3591 | } |
| 3592 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3593 | /* If expected data not yet available, it returns a yield. This function |
| 3594 | * consumes the data in the buffer. It returns a string containing the |
| 3595 | * data. This string can be empty. |
| 3596 | */ |
| 3597 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3598 | { |
| 3599 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3600 | struct stream_interface *si = appctx->appctx->owner; |
| 3601 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3602 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3603 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3604 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3605 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3606 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3607 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3608 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3609 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3610 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3611 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3612 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3613 | 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] | 3614 | } |
| 3615 | |
| 3616 | /* End of data: commit the total strings and return. */ |
| 3617 | if (ret < 0) { |
| 3618 | luaL_pushresult(&appctx->b); |
| 3619 | return 1; |
| 3620 | } |
| 3621 | |
| 3622 | /* Ensure that the block 2 length is usable. */ |
| 3623 | if (ret == 1) |
| 3624 | len2 = 0; |
| 3625 | |
| 3626 | /* dont check the max length read and dont check. */ |
| 3627 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3628 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3629 | |
| 3630 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3631 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3632 | luaL_pushresult(&appctx->b); |
| 3633 | return 1; |
| 3634 | } |
| 3635 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3636 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3637 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3638 | { |
| 3639 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3640 | |
| 3641 | /* Initialise the string catenation. */ |
| 3642 | luaL_buffinit(L, &appctx->b); |
| 3643 | |
| 3644 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3645 | } |
| 3646 | |
| 3647 | /* If expected data not yet available, it returns a yield. This function |
| 3648 | * consumes the data in the buffer. It returns a string containing the |
| 3649 | * data. This string can be empty. |
| 3650 | */ |
| 3651 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3652 | { |
| 3653 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3654 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3655 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3656 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3657 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3658 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3659 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3660 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3661 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3662 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3663 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3664 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3665 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3666 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3667 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3668 | 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] | 3669 | } |
| 3670 | |
| 3671 | /* End of data: commit the total strings and return. */ |
| 3672 | if (ret < 0) { |
| 3673 | luaL_pushresult(&appctx->b); |
| 3674 | return 1; |
| 3675 | } |
| 3676 | |
| 3677 | /* Ensure that the block 2 length is usable. */ |
| 3678 | if (ret == 1) |
| 3679 | len2 = 0; |
| 3680 | |
| 3681 | if (len == -1) { |
| 3682 | |
| 3683 | /* If len == -1, catenate all the data avalaile and |
| 3684 | * yield because we want to get all the data until |
| 3685 | * the end of data stream. |
| 3686 | */ |
| 3687 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3688 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3689 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3690 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3691 | 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] | 3692 | |
| 3693 | } else { |
| 3694 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3695 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3696 | if (len1 > len) |
| 3697 | len1 = len; |
| 3698 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3699 | len -= len1; |
| 3700 | |
| 3701 | /* Copy the second block. */ |
| 3702 | if (len2 > len) |
| 3703 | len2 = len; |
| 3704 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3705 | len -= len2; |
| 3706 | |
| 3707 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3708 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3709 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3710 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3711 | if (len > 0) { |
| 3712 | lua_pushinteger(L, len); |
| 3713 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3714 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3715 | 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] | 3716 | } |
| 3717 | |
| 3718 | /* return the result. */ |
| 3719 | luaL_pushresult(&appctx->b); |
| 3720 | return 1; |
| 3721 | } |
| 3722 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3723 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3724 | hlua_pusherror(L, "Lua: internal error"); |
| 3725 | WILL_LJMP(lua_error(L)); |
| 3726 | return 0; |
| 3727 | } |
| 3728 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3729 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3730 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3731 | { |
| 3732 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3733 | int len = -1; |
| 3734 | |
| 3735 | if (lua_gettop(L) > 2) |
| 3736 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3737 | if (lua_gettop(L) >= 2) { |
| 3738 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3739 | lua_pop(L, 1); |
| 3740 | } |
| 3741 | |
| 3742 | /* Confirm or set the required length */ |
| 3743 | lua_pushinteger(L, len); |
| 3744 | |
| 3745 | /* Initialise the string catenation. */ |
| 3746 | luaL_buffinit(L, &appctx->b); |
| 3747 | |
| 3748 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3749 | } |
| 3750 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3751 | /* Append data in the output side of the buffer. This data is immediately |
| 3752 | * sent. The function returns the amount of data written. If the buffer |
| 3753 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3754 | * if the channel is closed. |
| 3755 | */ |
| 3756 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3757 | { |
| 3758 | size_t len; |
| 3759 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3760 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3761 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3762 | struct stream_interface *si = appctx->appctx->owner; |
| 3763 | struct channel *chn = si_ic(si); |
| 3764 | int max; |
| 3765 | |
| 3766 | /* Get the max amount of data which can write as input in the channel. */ |
| 3767 | max = channel_recv_max(chn); |
| 3768 | if (max > (len - l)) |
| 3769 | max = len - l; |
| 3770 | |
| 3771 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3772 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3773 | |
| 3774 | /* update counters. */ |
| 3775 | l += max; |
| 3776 | lua_pop(L, 1); |
| 3777 | lua_pushinteger(L, l); |
| 3778 | |
| 3779 | /* If some data is not send, declares the situation to the |
| 3780 | * applet, and returns a yield. |
| 3781 | */ |
| 3782 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3783 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3784 | 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] | 3785 | } |
| 3786 | |
| 3787 | return 1; |
| 3788 | } |
| 3789 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3790 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3791 | * yield the LUA process, and resume it without checking the |
| 3792 | * input arguments. |
| 3793 | */ |
| 3794 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3795 | { |
| 3796 | MAY_LJMP(check_args(L, 2, "send")); |
| 3797 | lua_pushinteger(L, 0); |
| 3798 | |
| 3799 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3800 | } |
| 3801 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3802 | /* |
| 3803 | * |
| 3804 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3805 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3806 | * |
| 3807 | * |
| 3808 | */ |
| 3809 | |
| 3810 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3811 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3812 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3813 | __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] | 3814 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3815 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3816 | } |
| 3817 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3818 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3819 | * according with a current TXN. |
| 3820 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3821 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3822 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3823 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3824 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3825 | struct stream_interface *si = ctx->owner; |
| 3826 | struct stream *s = si_strm(si); |
| 3827 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3828 | struct htx *htx; |
| 3829 | struct htx_blk *blk; |
| 3830 | struct htx_sl *sl; |
| 3831 | struct ist path; |
| 3832 | unsigned long long len = 0; |
| 3833 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3834 | |
| 3835 | /* Check stack size. */ |
| 3836 | if (!lua_checkstack(L, 3)) |
| 3837 | return 0; |
| 3838 | |
| 3839 | /* Create the object: obj[0] = userdata. |
| 3840 | * Note that the base of the Converters object is the |
| 3841 | * same than the TXN object. |
| 3842 | */ |
| 3843 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3844 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3845 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3846 | appctx->appctx = ctx; |
| 3847 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3848 | appctx->appctx->ctx.hlua_apphttp.reason = NULL; /* Use default reason based on status */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3849 | appctx->htxn.s = s; |
| 3850 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3851 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3852 | /* Create the "f" field that contains a list of fetches. */ |
| 3853 | lua_pushstring(L, "f"); |
| 3854 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3855 | return 0; |
| 3856 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3857 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3858 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3859 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3860 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | return 0; |
| 3862 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3863 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3864 | /* Create the "c" field that contains a list of converters. */ |
| 3865 | lua_pushstring(L, "c"); |
| 3866 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3867 | return 0; |
| 3868 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3869 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3870 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3871 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3872 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | return 0; |
| 3874 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3875 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3876 | htx = htxbuf(&s->req.buf); |
| 3877 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3878 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3879 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3880 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3881 | /* Stores the request method. */ |
| 3882 | lua_pushstring(L, "method"); |
| 3883 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3884 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3885 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3886 | /* Stores the http version. */ |
| 3887 | lua_pushstring(L, "version"); |
| 3888 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3889 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3890 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3891 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3892 | * the array on the top of the stack. |
| 3893 | */ |
| 3894 | lua_pushstring(L, "headers"); |
| 3895 | htxn.s = s; |
| 3896 | htxn.p = px; |
| 3897 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3898 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3899 | return 0; |
| 3900 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3901 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3902 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3903 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3904 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3905 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3906 | p = path.ptr; |
| 3907 | end = path.ptr + path.len; |
| 3908 | q = p; |
| 3909 | while (q < end && *q != '?') |
| 3910 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3911 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3912 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3913 | lua_pushstring(L, "path"); |
| 3914 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3915 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | /* Stores the query string. */ |
| 3918 | lua_pushstring(L, "qs"); |
| 3919 | if (*q == '?') |
| 3920 | q++; |
| 3921 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3922 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3923 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3924 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3925 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3926 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3927 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3928 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3929 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3930 | break; |
| 3931 | if (type == HTX_BLK_DATA) |
| 3932 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3933 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3934 | if (htx->extra != ULLONG_MAX) |
| 3935 | len += htx->extra; |
| 3936 | |
| 3937 | /* Stores the request path. */ |
| 3938 | lua_pushstring(L, "length"); |
| 3939 | lua_pushinteger(L, len); |
| 3940 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3941 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3942 | /* Create an empty array of HTTP request headers. */ |
| 3943 | lua_pushstring(L, "response"); |
| 3944 | lua_newtable(L); |
| 3945 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3946 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3947 | /* Pop a class stream metatable and affect it to the table. */ |
| 3948 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3949 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3950 | |
| 3951 | return 1; |
| 3952 | } |
| 3953 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3954 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3955 | { |
| 3956 | struct hlua_appctx *appctx; |
| 3957 | struct stream *s; |
| 3958 | const char *name; |
| 3959 | size_t len; |
| 3960 | struct sample smp; |
| 3961 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3962 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3963 | 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] | 3964 | |
| 3965 | /* It is useles to retrieve the stream, but this function |
| 3966 | * runs only in a stream context. |
| 3967 | */ |
| 3968 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3969 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3970 | s = appctx->htxn.s; |
| 3971 | |
| 3972 | /* Converts the third argument in a sample. */ |
| 3973 | hlua_lua2smp(L, 3, &smp); |
| 3974 | |
| 3975 | /* Store the sample in a variable. */ |
| 3976 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3977 | |
| 3978 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3979 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3980 | else |
| 3981 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3982 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3983 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3984 | } |
| 3985 | |
| 3986 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3987 | { |
| 3988 | struct hlua_appctx *appctx; |
| 3989 | struct stream *s; |
| 3990 | const char *name; |
| 3991 | size_t len; |
| 3992 | struct sample smp; |
| 3993 | |
| 3994 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3995 | |
| 3996 | /* It is useles to retrieve the stream, but this function |
| 3997 | * runs only in a stream context. |
| 3998 | */ |
| 3999 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4000 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4001 | s = appctx->htxn.s; |
| 4002 | |
| 4003 | /* Unset the variable. */ |
| 4004 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4005 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4006 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4007 | } |
| 4008 | |
| 4009 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4010 | { |
| 4011 | struct hlua_appctx *appctx; |
| 4012 | struct stream *s; |
| 4013 | const char *name; |
| 4014 | size_t len; |
| 4015 | struct sample smp; |
| 4016 | |
| 4017 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4018 | |
| 4019 | /* It is useles to retrieve the stream, but this function |
| 4020 | * runs only in a stream context. |
| 4021 | */ |
| 4022 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4023 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4024 | s = appctx->htxn.s; |
| 4025 | |
| 4026 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4027 | if (!vars_get_by_name(name, len, &smp)) { |
| 4028 | lua_pushnil(L); |
| 4029 | return 1; |
| 4030 | } |
| 4031 | |
| 4032 | return hlua_smp2lua(L, &smp); |
| 4033 | } |
| 4034 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4035 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4036 | { |
| 4037 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4038 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4039 | struct hlua *hlua; |
| 4040 | |
| 4041 | /* 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] | 4042 | if (!s->hlua) |
| 4043 | return 0; |
| 4044 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4045 | |
| 4046 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4047 | |
| 4048 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4049 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4050 | |
| 4051 | /* Get and store new value. */ |
| 4052 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4053 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4054 | |
| 4055 | return 0; |
| 4056 | } |
| 4057 | |
| 4058 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4059 | { |
| 4060 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4061 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4062 | struct hlua *hlua; |
| 4063 | |
| 4064 | /* 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] | 4065 | if (!s->hlua) { |
| 4066 | lua_pushnil(L); |
| 4067 | return 1; |
| 4068 | } |
| 4069 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4070 | |
| 4071 | /* Push configuration index in the stack. */ |
| 4072 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4073 | |
| 4074 | return 1; |
| 4075 | } |
| 4076 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4077 | /* If expected data not yet available, it returns a yield. This function |
| 4078 | * consumes the data in the buffer. It returns a string containing the |
| 4079 | * data. This string can be empty. |
| 4080 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4081 | __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] | 4082 | { |
| 4083 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4084 | struct stream_interface *si = appctx->appctx->owner; |
| 4085 | struct channel *req = si_oc(si); |
| 4086 | struct htx *htx; |
| 4087 | struct htx_blk *blk; |
| 4088 | size_t count; |
| 4089 | int stop = 0; |
| 4090 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4091 | htx = htx_from_buf(&req->buf); |
| 4092 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4093 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4094 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4095 | while (count && !stop && blk) { |
| 4096 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4097 | uint32_t sz = htx_get_blksz(blk); |
| 4098 | struct ist v; |
| 4099 | uint32_t vlen; |
| 4100 | char *nl; |
| 4101 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4102 | if (type == HTX_BLK_EOM) { |
| 4103 | stop = 1; |
| 4104 | break; |
| 4105 | } |
| 4106 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4107 | vlen = sz; |
| 4108 | if (vlen > count) { |
| 4109 | if (type != HTX_BLK_DATA) |
| 4110 | break; |
| 4111 | vlen = count; |
| 4112 | } |
| 4113 | |
| 4114 | switch (type) { |
| 4115 | case HTX_BLK_UNUSED: |
| 4116 | break; |
| 4117 | |
| 4118 | case HTX_BLK_DATA: |
| 4119 | v = htx_get_blk_value(htx, blk); |
| 4120 | v.len = vlen; |
| 4121 | nl = istchr(v, '\n'); |
| 4122 | if (nl != NULL) { |
| 4123 | stop = 1; |
| 4124 | vlen = nl - v.ptr + 1; |
| 4125 | } |
| 4126 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4127 | break; |
| 4128 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4129 | case HTX_BLK_TLR: |
| 4130 | case HTX_BLK_EOM: |
| 4131 | stop = 1; |
| 4132 | break; |
| 4133 | |
| 4134 | default: |
| 4135 | break; |
| 4136 | } |
| 4137 | |
| 4138 | co_set_data(req, co_data(req) - vlen); |
| 4139 | count -= vlen; |
| 4140 | if (sz == vlen) |
| 4141 | blk = htx_remove_blk(htx, blk); |
| 4142 | else { |
| 4143 | htx_cut_data_blk(htx, blk, vlen); |
| 4144 | break; |
| 4145 | } |
| 4146 | } |
| 4147 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4148 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4149 | if (!stop) { |
| 4150 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4151 | 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] | 4152 | } |
| 4153 | |
| 4154 | /* return the result. */ |
| 4155 | luaL_pushresult(&appctx->b); |
| 4156 | return 1; |
| 4157 | } |
| 4158 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4159 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4160 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4161 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4162 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4163 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4164 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4165 | /* Initialise the string catenation. */ |
| 4166 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4167 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4168 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4169 | } |
| 4170 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | /* If expected data not yet available, it returns a yield. This function |
| 4172 | * consumes the data in the buffer. It returns a string containing the |
| 4173 | * data. This string can be empty. |
| 4174 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4175 | __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] | 4176 | { |
| 4177 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4178 | struct stream_interface *si = appctx->appctx->owner; |
| 4179 | struct channel *req = si_oc(si); |
| 4180 | struct htx *htx; |
| 4181 | struct htx_blk *blk; |
| 4182 | size_t count; |
| 4183 | int len; |
| 4184 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4185 | htx = htx_from_buf(&req->buf); |
| 4186 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4187 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4188 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4189 | while (count && len && blk) { |
| 4190 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4191 | uint32_t sz = htx_get_blksz(blk); |
| 4192 | struct ist v; |
| 4193 | uint32_t vlen; |
| 4194 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4195 | if (type == HTX_BLK_EOM) { |
| 4196 | len = 0; |
| 4197 | break; |
| 4198 | } |
| 4199 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4200 | vlen = sz; |
| 4201 | if (len > 0 && vlen > len) |
| 4202 | vlen = len; |
| 4203 | if (vlen > count) { |
| 4204 | if (type != HTX_BLK_DATA) |
| 4205 | break; |
| 4206 | vlen = count; |
| 4207 | } |
| 4208 | |
| 4209 | switch (type) { |
| 4210 | case HTX_BLK_UNUSED: |
| 4211 | break; |
| 4212 | |
| 4213 | case HTX_BLK_DATA: |
| 4214 | v = htx_get_blk_value(htx, blk); |
| 4215 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4216 | break; |
| 4217 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4218 | case HTX_BLK_TLR: |
| 4219 | case HTX_BLK_EOM: |
| 4220 | len = 0; |
| 4221 | break; |
| 4222 | |
| 4223 | default: |
| 4224 | break; |
| 4225 | } |
| 4226 | |
| 4227 | co_set_data(req, co_data(req) - vlen); |
| 4228 | count -= vlen; |
| 4229 | if (len > 0) |
| 4230 | len -= vlen; |
| 4231 | if (sz == vlen) |
| 4232 | blk = htx_remove_blk(htx, blk); |
| 4233 | else { |
| 4234 | htx_cut_data_blk(htx, blk, vlen); |
| 4235 | break; |
| 4236 | } |
| 4237 | } |
| 4238 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4239 | htx_to_buf(htx, &req->buf); |
| 4240 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4241 | /* If we are no other data available, yield waiting for new data. */ |
| 4242 | if (len) { |
| 4243 | if (len > 0) { |
| 4244 | lua_pushinteger(L, len); |
| 4245 | lua_replace(L, 2); |
| 4246 | } |
| 4247 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4248 | 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] | 4249 | } |
| 4250 | |
| 4251 | /* return the result. */ |
| 4252 | luaL_pushresult(&appctx->b); |
| 4253 | return 1; |
| 4254 | } |
| 4255 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4256 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4257 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4258 | { |
| 4259 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4260 | int len = -1; |
| 4261 | |
| 4262 | /* Check arguments. */ |
| 4263 | if (lua_gettop(L) > 2) |
| 4264 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4265 | if (lua_gettop(L) >= 2) { |
| 4266 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4267 | lua_pop(L, 1); |
| 4268 | } |
| 4269 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4270 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4271 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4272 | /* Initialise the string catenation. */ |
| 4273 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4274 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4275 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4276 | } |
| 4277 | |
| 4278 | /* Append data in the output side of the buffer. This data is immediately |
| 4279 | * sent. The function returns the amount of data written. If the buffer |
| 4280 | * cannot contain the data, the function yields. The function returns -1 |
| 4281 | * if the channel is closed. |
| 4282 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4283 | __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] | 4284 | { |
| 4285 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4286 | struct stream_interface *si = appctx->appctx->owner; |
| 4287 | struct channel *res = si_ic(si); |
| 4288 | struct htx *htx = htx_from_buf(&res->buf); |
| 4289 | const char *data; |
| 4290 | size_t len; |
| 4291 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4292 | int max; |
| 4293 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4294 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4295 | if (!max) |
| 4296 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4297 | |
| 4298 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4299 | |
| 4300 | /* Get the max amount of data which can write as input in the channel. */ |
| 4301 | if (max > (len - l)) |
| 4302 | max = len - l; |
| 4303 | |
| 4304 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4305 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4306 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4307 | |
| 4308 | /* update counters. */ |
| 4309 | l += max; |
| 4310 | lua_pop(L, 1); |
| 4311 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4312 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4313 | /* If some data is not send, declares the situation to the |
| 4314 | * applet, and returns a yield. |
| 4315 | */ |
| 4316 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4317 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4318 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4319 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4320 | 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] | 4321 | } |
| 4322 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4323 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4324 | return 1; |
| 4325 | } |
| 4326 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4327 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4328 | * yield the LUA process, and resume it without checking the |
| 4329 | * input arguments. |
| 4330 | */ |
| 4331 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4332 | { |
| 4333 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4334 | |
| 4335 | /* We want to send some data. Headers must be sent. */ |
| 4336 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4337 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4338 | WILL_LJMP(lua_error(L)); |
| 4339 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4340 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4341 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4342 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4343 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4344 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4345 | } |
| 4346 | |
| 4347 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4348 | { |
| 4349 | const char *name; |
| 4350 | int ret; |
| 4351 | |
| 4352 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4353 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4354 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4355 | |
| 4356 | /* Push in the stack the "response" entry. */ |
| 4357 | ret = lua_getfield(L, 1, "response"); |
| 4358 | if (ret != LUA_TTABLE) { |
| 4359 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4360 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4361 | WILL_LJMP(lua_error(L)); |
| 4362 | } |
| 4363 | |
| 4364 | /* check if the header is already registered if it is not |
| 4365 | * the case, register it. |
| 4366 | */ |
| 4367 | ret = lua_getfield(L, -1, name); |
| 4368 | if (ret == LUA_TNIL) { |
| 4369 | |
| 4370 | /* Entry not found. */ |
| 4371 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4372 | |
| 4373 | /* Insert the new header name in the array in the top of the stack. |
| 4374 | * It left the new array in the top of the stack. |
| 4375 | */ |
| 4376 | lua_newtable(L); |
| 4377 | lua_pushvalue(L, 2); |
| 4378 | lua_pushvalue(L, -2); |
| 4379 | lua_settable(L, -4); |
| 4380 | |
| 4381 | } else if (ret != LUA_TTABLE) { |
| 4382 | |
| 4383 | /* corruption error. */ |
| 4384 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4385 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4386 | WILL_LJMP(lua_error(L)); |
| 4387 | } |
| 4388 | |
| 4389 | /* Now the top od thestack is an array of values. We push |
| 4390 | * the header value as new entry. |
| 4391 | */ |
| 4392 | lua_pushvalue(L, 3); |
| 4393 | ret = lua_rawlen(L, -2); |
| 4394 | lua_rawseti(L, -2, ret + 1); |
| 4395 | lua_pushboolean(L, 1); |
| 4396 | return 1; |
| 4397 | } |
| 4398 | |
| 4399 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4400 | { |
| 4401 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4402 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4403 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4404 | |
| 4405 | if (status < 100 || status > 599) { |
| 4406 | lua_pushboolean(L, 0); |
| 4407 | return 1; |
| 4408 | } |
| 4409 | |
| 4410 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4411 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4412 | lua_pushboolean(L, 1); |
| 4413 | return 1; |
| 4414 | } |
| 4415 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4416 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4417 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4418 | { |
| 4419 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4420 | struct stream_interface *si = appctx->appctx->owner; |
| 4421 | struct channel *res = si_ic(si); |
| 4422 | struct htx *htx; |
| 4423 | struct htx_sl *sl; |
| 4424 | struct h1m h1m; |
| 4425 | const char *status, *reason; |
| 4426 | const char *name, *value; |
| 4427 | size_t nlen, vlen; |
| 4428 | unsigned int flags; |
| 4429 | |
| 4430 | /* Send the message at once. */ |
| 4431 | htx = htx_from_buf(&res->buf); |
| 4432 | h1m_init_res(&h1m); |
| 4433 | |
| 4434 | /* Use the same http version than the request. */ |
| 4435 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4436 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4437 | if (reason == NULL) |
| 4438 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4439 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4440 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4441 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4442 | } |
| 4443 | else { |
| 4444 | flags = HTX_SL_F_IS_RESP; |
| 4445 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4446 | } |
| 4447 | if (!sl) { |
| 4448 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4449 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4450 | WILL_LJMP(lua_error(L)); |
| 4451 | } |
| 4452 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4453 | |
| 4454 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4455 | lua_pushvalue(L, 0); |
| 4456 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4457 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4458 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4459 | WILL_LJMP(lua_error(L)); |
| 4460 | } |
| 4461 | |
| 4462 | /* Browse the list of headers. */ |
| 4463 | lua_pushnil(L); |
| 4464 | while(lua_next(L, -2) != 0) { |
| 4465 | /* We expect a string as -2. */ |
| 4466 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4467 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4468 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4469 | lua_typename(L, lua_type(L, -2))); |
| 4470 | WILL_LJMP(lua_error(L)); |
| 4471 | } |
| 4472 | name = lua_tolstring(L, -2, &nlen); |
| 4473 | |
| 4474 | /* We expect an array as -1. */ |
| 4475 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4476 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4477 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4478 | name, |
| 4479 | lua_typename(L, lua_type(L, -1))); |
| 4480 | WILL_LJMP(lua_error(L)); |
| 4481 | } |
| 4482 | |
| 4483 | /* Browse the table who is on the top of the stack. */ |
| 4484 | lua_pushnil(L); |
| 4485 | while(lua_next(L, -2) != 0) { |
| 4486 | int id; |
| 4487 | |
| 4488 | /* We expect a number as -2. */ |
| 4489 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4490 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4491 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4492 | name, |
| 4493 | lua_typename(L, lua_type(L, -2))); |
| 4494 | WILL_LJMP(lua_error(L)); |
| 4495 | } |
| 4496 | id = lua_tointeger(L, -2); |
| 4497 | |
| 4498 | /* We expect a string as -2. */ |
| 4499 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4500 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4501 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4502 | name, id, |
| 4503 | lua_typename(L, lua_type(L, -1))); |
| 4504 | WILL_LJMP(lua_error(L)); |
| 4505 | } |
| 4506 | value = lua_tolstring(L, -1, &vlen); |
| 4507 | |
| 4508 | /* Simple Protocol checks. */ |
| 4509 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4510 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4511 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4512 | struct ist v = ist2(value, vlen); |
| 4513 | int ret; |
| 4514 | |
| 4515 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4516 | if (ret < 0) { |
| 4517 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4518 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4519 | name); |
| 4520 | WILL_LJMP(lua_error(L)); |
| 4521 | } |
| 4522 | else if (ret == 0) |
| 4523 | goto next; /* Skip it */ |
| 4524 | } |
| 4525 | |
| 4526 | /* Add a new header */ |
| 4527 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4528 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4529 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4530 | name); |
| 4531 | WILL_LJMP(lua_error(L)); |
| 4532 | } |
| 4533 | next: |
| 4534 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4535 | lua_pop(L, 1); |
| 4536 | } |
| 4537 | |
| 4538 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4539 | lua_pop(L, 1); |
| 4540 | } |
| 4541 | |
| 4542 | if (h1m.flags & H1_MF_CHNK) |
| 4543 | h1m.flags &= ~H1_MF_CLEN; |
| 4544 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4545 | h1m.flags |= H1_MF_XFER_LEN; |
| 4546 | |
| 4547 | /* Uset HTX start-line flags */ |
| 4548 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4549 | flags |= HTX_SL_F_XFER_ENC; |
| 4550 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4551 | flags |= HTX_SL_F_XFER_LEN; |
| 4552 | if (h1m.flags & H1_MF_CHNK) |
| 4553 | flags |= HTX_SL_F_CHNK; |
| 4554 | else if (h1m.flags & H1_MF_CLEN) |
| 4555 | flags |= HTX_SL_F_CLEN; |
| 4556 | if (h1m.body_len == 0) |
| 4557 | flags |= HTX_SL_F_BODYLESS; |
| 4558 | } |
| 4559 | sl->flags |= flags; |
| 4560 | |
| 4561 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4562 | * and the status code implies the presence of a message body, we must |
| 4563 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4564 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4565 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4566 | */ |
| 4567 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4568 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4569 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4570 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4571 | /* Add a new header */ |
| 4572 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4573 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4574 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4575 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4576 | WILL_LJMP(lua_error(L)); |
| 4577 | } |
| 4578 | } |
| 4579 | |
| 4580 | /* Finalize headers. */ |
| 4581 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4582 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4583 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4584 | WILL_LJMP(lua_error(L)); |
| 4585 | } |
| 4586 | |
| 4587 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4588 | b_reset(&res->buf); |
| 4589 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4590 | WILL_LJMP(lua_error(L)); |
| 4591 | } |
| 4592 | |
| 4593 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4594 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4595 | |
| 4596 | /* Headers sent, set the flag. */ |
| 4597 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4598 | return 0; |
| 4599 | |
| 4600 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4601 | /* We will build the status line and the headers of the HTTP response. |
| 4602 | * We will try send at once if its not possible, we give back the hand |
| 4603 | * waiting for more room. |
| 4604 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4605 | __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] | 4606 | { |
| 4607 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4608 | struct stream_interface *si = appctx->appctx->owner; |
| 4609 | struct channel *res = si_ic(si); |
| 4610 | |
| 4611 | if (co_data(res)) { |
| 4612 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4613 | 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] | 4614 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4615 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4616 | } |
| 4617 | |
| 4618 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4620 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4621 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4622 | } |
| 4623 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4624 | /* |
| 4625 | * |
| 4626 | * |
| 4627 | * Class HTTP |
| 4628 | * |
| 4629 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4630 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4631 | |
| 4632 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4633 | * a class stream, otherwise it throws an error. |
| 4634 | */ |
| 4635 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4636 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4637 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4638 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4639 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4640 | /* This function creates and push in the stack a HTTP object |
| 4641 | * according with a current TXN. |
| 4642 | */ |
| 4643 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4644 | { |
| 4645 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4646 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4647 | /* Check stack size. */ |
| 4648 | if (!lua_checkstack(L, 3)) |
| 4649 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4650 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4651 | /* Create the object: obj[0] = userdata. |
| 4652 | * Note that the base of the Converters object is the |
| 4653 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4655 | lua_newtable(L); |
| 4656 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4657 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | |
| 4659 | htxn->s = txn->s; |
| 4660 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4661 | htxn->dir = txn->dir; |
| 4662 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4663 | |
| 4664 | /* Pop a class stream metatable and affect it to the table. */ |
| 4665 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4666 | lua_setmetatable(L, -2); |
| 4667 | |
| 4668 | return 1; |
| 4669 | } |
| 4670 | |
| 4671 | /* This function creates ans returns an array of HTTP headers. |
| 4672 | * This function does not fails. It is used as wrapper with the |
| 4673 | * 2 following functions. |
| 4674 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4675 | __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] | 4676 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | struct htx *htx; |
| 4678 | int32_t pos; |
| 4679 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | /* Create the table. */ |
| 4681 | lua_newtable(L); |
| 4682 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4683 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4684 | htx = htxbuf(&msg->chn->buf); |
| 4685 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4686 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4687 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4688 | struct ist n, v; |
| 4689 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4690 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4691 | if (type == HTX_BLK_HDR) { |
| 4692 | n = htx_get_blk_name(htx,blk); |
| 4693 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4694 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4695 | else if (type == HTX_BLK_EOH) |
| 4696 | break; |
| 4697 | else |
| 4698 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4699 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4700 | /* Check for existing entry: |
| 4701 | * assume that the table is on the top of the stack, and |
| 4702 | * push the key in the stack, the function lua_gettable() |
| 4703 | * perform the lookup. |
| 4704 | */ |
| 4705 | lua_pushlstring(L, n.ptr, n.len); |
| 4706 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4707 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4708 | switch (lua_type(L, -1)) { |
| 4709 | case LUA_TNIL: |
| 4710 | /* Table not found, create it. */ |
| 4711 | lua_pop(L, 1); /* remove the nil value. */ |
| 4712 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4713 | lua_newtable(L); /* create and push empty table. */ |
| 4714 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4715 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4716 | 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] | 4717 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4718 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4719 | case LUA_TTABLE: |
| 4720 | /* Entry found: push the value in the table. */ |
| 4721 | len = lua_rawlen(L, -1); |
| 4722 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4723 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4724 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4725 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4726 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4727 | default: |
| 4728 | /* Other cases are errors. */ |
| 4729 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4730 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4731 | } |
| 4732 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4733 | return 1; |
| 4734 | } |
| 4735 | |
| 4736 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4737 | { |
| 4738 | struct hlua_txn *htxn; |
| 4739 | |
| 4740 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4741 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4742 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4743 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4744 | WILL_LJMP(lua_error(L)); |
| 4745 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4746 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4747 | } |
| 4748 | |
| 4749 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4750 | { |
| 4751 | struct hlua_txn *htxn; |
| 4752 | |
| 4753 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4754 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4755 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4756 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4757 | WILL_LJMP(lua_error(L)); |
| 4758 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4759 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4760 | } |
| 4761 | |
| 4762 | /* This function replace full header, or just a value in |
| 4763 | * the request or in the response. It is a wrapper fir the |
| 4764 | * 4 following functions. |
| 4765 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4766 | __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] | 4767 | { |
| 4768 | size_t name_len; |
| 4769 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4770 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4771 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4772 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4773 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4774 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4775 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4776 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4777 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4778 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4779 | 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] | 4780 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4781 | return 0; |
| 4782 | } |
| 4783 | |
| 4784 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4785 | { |
| 4786 | struct hlua_txn *htxn; |
| 4787 | |
| 4788 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4789 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4790 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4791 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4792 | WILL_LJMP(lua_error(L)); |
| 4793 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4794 | 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] | 4795 | } |
| 4796 | |
| 4797 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4798 | { |
| 4799 | struct hlua_txn *htxn; |
| 4800 | |
| 4801 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4802 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4803 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4804 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4805 | WILL_LJMP(lua_error(L)); |
| 4806 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4807 | 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] | 4808 | } |
| 4809 | |
| 4810 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4811 | { |
| 4812 | struct hlua_txn *htxn; |
| 4813 | |
| 4814 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4815 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4816 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4817 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4818 | WILL_LJMP(lua_error(L)); |
| 4819 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4820 | 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] | 4821 | } |
| 4822 | |
| 4823 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4824 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4825 | struct hlua_txn *htxn; |
| 4826 | |
| 4827 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4828 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4829 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4830 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4831 | WILL_LJMP(lua_error(L)); |
| 4832 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4833 | 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] | 4834 | } |
| 4835 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4836 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4837 | * It is a wrapper for the 2 following functions. |
| 4838 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4839 | __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] | 4840 | { |
| 4841 | size_t len; |
| 4842 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4843 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4844 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4845 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4846 | ctx.blk = NULL; |
| 4847 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4848 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4849 | return 0; |
| 4850 | } |
| 4851 | |
| 4852 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4853 | { |
| 4854 | struct hlua_txn *htxn; |
| 4855 | |
| 4856 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4857 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4858 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4859 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4860 | WILL_LJMP(lua_error(L)); |
| 4861 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4862 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4863 | } |
| 4864 | |
| 4865 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4866 | { |
| 4867 | struct hlua_txn *htxn; |
| 4868 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4869 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4870 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4871 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4872 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4873 | WILL_LJMP(lua_error(L)); |
| 4874 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4875 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4876 | } |
| 4877 | |
| 4878 | /* This function adds an header. It is a wrapper used by |
| 4879 | * the 2 following functions. |
| 4880 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4881 | __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] | 4882 | { |
| 4883 | size_t name_len; |
| 4884 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4885 | size_t value_len; |
| 4886 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4888 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4889 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4890 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4891 | return 0; |
| 4892 | } |
| 4893 | |
| 4894 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4895 | { |
| 4896 | struct hlua_txn *htxn; |
| 4897 | |
| 4898 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4899 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4900 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4901 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4902 | WILL_LJMP(lua_error(L)); |
| 4903 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4904 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4905 | } |
| 4906 | |
| 4907 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4908 | { |
| 4909 | struct hlua_txn *htxn; |
| 4910 | |
| 4911 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4912 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4913 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4914 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4915 | WILL_LJMP(lua_error(L)); |
| 4916 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4917 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4918 | } |
| 4919 | |
| 4920 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4921 | { |
| 4922 | struct hlua_txn *htxn; |
| 4923 | |
| 4924 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4925 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4926 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4927 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4928 | WILL_LJMP(lua_error(L)); |
| 4929 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4930 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4931 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4932 | } |
| 4933 | |
| 4934 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4935 | { |
| 4936 | struct hlua_txn *htxn; |
| 4937 | |
| 4938 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4939 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4940 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4941 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4942 | WILL_LJMP(lua_error(L)); |
| 4943 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4944 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4945 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4946 | } |
| 4947 | |
| 4948 | /* This function set the method. */ |
| 4949 | static int hlua_http_req_set_meth(lua_State *L) |
| 4950 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4951 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4952 | size_t name_len; |
| 4953 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4954 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4955 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4956 | WILL_LJMP(lua_error(L)); |
| 4957 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4958 | lua_pushboolean(L, http_req_replace_stline(0, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4959 | return 1; |
| 4960 | } |
| 4961 | |
| 4962 | /* This function set the method. */ |
| 4963 | static int hlua_http_req_set_path(lua_State *L) |
| 4964 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4965 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4966 | size_t name_len; |
| 4967 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4968 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4969 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4970 | WILL_LJMP(lua_error(L)); |
| 4971 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4972 | lua_pushboolean(L, http_req_replace_stline(1, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4973 | return 1; |
| 4974 | } |
| 4975 | |
| 4976 | /* This function set the query-string. */ |
| 4977 | static int hlua_http_req_set_query(lua_State *L) |
| 4978 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4979 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4980 | size_t name_len; |
| 4981 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4982 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4983 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4984 | WILL_LJMP(lua_error(L)); |
| 4985 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | /* Check length. */ |
| 4987 | if (name_len > trash.size - 1) { |
| 4988 | lua_pushboolean(L, 0); |
| 4989 | return 1; |
| 4990 | } |
| 4991 | |
| 4992 | /* Add the mark question as prefix. */ |
| 4993 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4994 | trash.area[trash.data++] = '?'; |
| 4995 | memcpy(trash.area + trash.data, name, name_len); |
| 4996 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4998 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4999 | http_req_replace_stline(2, trash.area, trash.data, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5000 | return 1; |
| 5001 | } |
| 5002 | |
| 5003 | /* This function set the uri. */ |
| 5004 | static int hlua_http_req_set_uri(lua_State *L) |
| 5005 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5006 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5007 | size_t name_len; |
| 5008 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5009 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5010 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5011 | WILL_LJMP(lua_error(L)); |
| 5012 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5013 | lua_pushboolean(L, http_req_replace_stline(3, name, name_len, htxn->p, htxn->s) != -1); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5014 | return 1; |
| 5015 | } |
| 5016 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5017 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5018 | static int hlua_http_res_set_status(lua_State *L) |
| 5019 | { |
| 5020 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5021 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5022 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5023 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5024 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5025 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5026 | WILL_LJMP(lua_error(L)); |
| 5027 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5028 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5029 | return 0; |
| 5030 | } |
| 5031 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5032 | /* |
| 5033 | * |
| 5034 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5035 | * Class TXN |
| 5036 | * |
| 5037 | * |
| 5038 | */ |
| 5039 | |
| 5040 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5041 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5042 | */ |
| 5043 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5044 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5045 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5046 | } |
| 5047 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5048 | __LJMP static int hlua_set_var(lua_State *L) |
| 5049 | { |
| 5050 | struct hlua_txn *htxn; |
| 5051 | const char *name; |
| 5052 | size_t len; |
| 5053 | struct sample smp; |
| 5054 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5055 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5056 | 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] | 5057 | |
| 5058 | /* It is useles to retrieve the stream, but this function |
| 5059 | * runs only in a stream context. |
| 5060 | */ |
| 5061 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5062 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5063 | |
| 5064 | /* Converts the third argument in a sample. */ |
| 5065 | hlua_lua2smp(L, 3, &smp); |
| 5066 | |
| 5067 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5068 | 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] | 5069 | |
| 5070 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5071 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5072 | else |
| 5073 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5074 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5075 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5076 | } |
| 5077 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5078 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5079 | { |
| 5080 | struct hlua_txn *htxn; |
| 5081 | const char *name; |
| 5082 | size_t len; |
| 5083 | struct sample smp; |
| 5084 | |
| 5085 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5086 | |
| 5087 | /* It is useles to retrieve the stream, but this function |
| 5088 | * runs only in a stream context. |
| 5089 | */ |
| 5090 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5091 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5092 | |
| 5093 | /* Unset the variable. */ |
| 5094 | 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] | 5095 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5096 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5097 | } |
| 5098 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5099 | __LJMP static int hlua_get_var(lua_State *L) |
| 5100 | { |
| 5101 | struct hlua_txn *htxn; |
| 5102 | const char *name; |
| 5103 | size_t len; |
| 5104 | struct sample smp; |
| 5105 | |
| 5106 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5107 | |
| 5108 | /* It is useles to retrieve the stream, but this function |
| 5109 | * runs only in a stream context. |
| 5110 | */ |
| 5111 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5112 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5113 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5114 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Willy Tarreau | 6204cd9 | 2016-03-10 16:33:04 +0100 | [diff] [blame] | 5115 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5116 | lua_pushnil(L); |
| 5117 | return 1; |
| 5118 | } |
| 5119 | |
| 5120 | return hlua_smp2lua(L, &smp); |
| 5121 | } |
| 5122 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5123 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5124 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5125 | struct hlua *hlua; |
| 5126 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5127 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5128 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5129 | /* It is useles to retrieve the stream, but this function |
| 5130 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5131 | */ |
| 5132 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5133 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5134 | |
| 5135 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5136 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5137 | |
| 5138 | /* Get and store new value. */ |
| 5139 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5140 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5141 | |
| 5142 | return 0; |
| 5143 | } |
| 5144 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5145 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5146 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5147 | struct hlua *hlua; |
| 5148 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5149 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5150 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5151 | /* It is useles to retrieve the stream, but this function |
| 5152 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5153 | */ |
| 5154 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5155 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5156 | |
| 5157 | /* Push configuration index in the stack. */ |
| 5158 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5159 | |
| 5160 | return 1; |
| 5161 | } |
| 5162 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5163 | /* Create stack entry containing a class TXN. This function |
| 5164 | * return 0 if the stack does not contains free slots, |
| 5165 | * otherwise it returns 1. |
| 5166 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5167 | 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] | 5168 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5169 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5170 | |
| 5171 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5172 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5173 | return 0; |
| 5174 | |
| 5175 | /* NOTE: The allocation never fails. The failure |
| 5176 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5177 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5178 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5179 | /* Create the object: obj[0] = userdata. */ |
| 5180 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5181 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5182 | lua_rawseti(L, -2, 0); |
| 5183 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5184 | htxn->s = s; |
| 5185 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5186 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5187 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5188 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5189 | /* Create the "f" field that contains a list of fetches. */ |
| 5190 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5191 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5192 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5193 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5194 | |
| 5195 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5196 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5197 | 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] | 5198 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5199 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5200 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5201 | /* Create the "c" field that contains a list of converters. */ |
| 5202 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5203 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5204 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5205 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5206 | |
| 5207 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5208 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5209 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5210 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5211 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5212 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5213 | /* Create the "req" field that contains the request channel object. */ |
| 5214 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5215 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5216 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5217 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5218 | |
| 5219 | /* Create the "res" field that contains the response channel object. */ |
| 5220 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5221 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5222 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5223 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5224 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5225 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5226 | lua_pushstring(L, "http"); |
| 5227 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5228 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5229 | return 0; |
| 5230 | } |
| 5231 | else |
| 5232 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5233 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5234 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5235 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5236 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5237 | lua_setmetatable(L, -2); |
| 5238 | |
| 5239 | return 1; |
| 5240 | } |
| 5241 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5242 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5243 | { |
| 5244 | const char *msg; |
| 5245 | struct hlua_txn *htxn; |
| 5246 | |
| 5247 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5248 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5249 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5250 | |
| 5251 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5252 | return 0; |
| 5253 | } |
| 5254 | |
| 5255 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5256 | { |
| 5257 | int level; |
| 5258 | const char *msg; |
| 5259 | struct hlua_txn *htxn; |
| 5260 | |
| 5261 | MAY_LJMP(check_args(L, 3, "log")); |
| 5262 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5263 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5264 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5265 | |
| 5266 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5267 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5268 | |
| 5269 | hlua_sendlog(htxn->s->be, level, msg); |
| 5270 | return 0; |
| 5271 | } |
| 5272 | |
| 5273 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5274 | { |
| 5275 | const char *msg; |
| 5276 | struct hlua_txn *htxn; |
| 5277 | |
| 5278 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5279 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5280 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5281 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5282 | return 0; |
| 5283 | } |
| 5284 | |
| 5285 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5286 | { |
| 5287 | const char *msg; |
| 5288 | struct hlua_txn *htxn; |
| 5289 | |
| 5290 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5291 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5292 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5293 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5294 | return 0; |
| 5295 | } |
| 5296 | |
| 5297 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5298 | { |
| 5299 | const char *msg; |
| 5300 | struct hlua_txn *htxn; |
| 5301 | |
| 5302 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5303 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5304 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5305 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5306 | return 0; |
| 5307 | } |
| 5308 | |
| 5309 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5310 | { |
| 5311 | const char *msg; |
| 5312 | struct hlua_txn *htxn; |
| 5313 | |
| 5314 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5315 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5316 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5317 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5318 | return 0; |
| 5319 | } |
| 5320 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5321 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5322 | { |
| 5323 | struct hlua_txn *htxn; |
| 5324 | int ll; |
| 5325 | |
| 5326 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5327 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5328 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5329 | |
| 5330 | if (ll < 0 || ll > 7) |
| 5331 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5332 | |
| 5333 | htxn->s->logs.level = ll; |
| 5334 | return 0; |
| 5335 | } |
| 5336 | |
| 5337 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5338 | { |
| 5339 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5340 | int tos; |
| 5341 | |
| 5342 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5343 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5344 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5345 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5346 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5347 | return 0; |
| 5348 | } |
| 5349 | |
| 5350 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5351 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5352 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5353 | int mark; |
| 5354 | |
| 5355 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5356 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5357 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5358 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5359 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5360 | return 0; |
| 5361 | } |
| 5362 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5363 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5364 | { |
| 5365 | struct hlua_txn *htxn; |
| 5366 | |
| 5367 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5368 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5369 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5370 | return 0; |
| 5371 | } |
| 5372 | |
| 5373 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5374 | { |
| 5375 | struct hlua_txn *htxn; |
| 5376 | |
| 5377 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5378 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5379 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5380 | return 0; |
| 5381 | } |
| 5382 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5383 | /* 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] | 5384 | * 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] | 5385 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5386 | * error. The Reply must be on top of the stack. |
| 5387 | */ |
| 5388 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5389 | { |
| 5390 | struct htx *htx; |
| 5391 | struct htx_sl *sl; |
| 5392 | struct h1m h1m; |
| 5393 | const char *status, *reason, *body; |
| 5394 | size_t status_len, reason_len, body_len; |
| 5395 | int ret, code, flags; |
| 5396 | |
| 5397 | code = 200; |
| 5398 | status = "200"; |
| 5399 | status_len = 3; |
| 5400 | ret = lua_getfield(L, -1, "status"); |
| 5401 | if (ret == LUA_TNUMBER) { |
| 5402 | code = lua_tointeger(L, -1); |
| 5403 | status = lua_tolstring(L, -1, &status_len); |
| 5404 | } |
| 5405 | lua_pop(L, 1); |
| 5406 | |
| 5407 | reason = http_get_reason(code); |
| 5408 | reason_len = strlen(reason); |
| 5409 | ret = lua_getfield(L, -1, "reason"); |
| 5410 | if (ret == LUA_TSTRING) |
| 5411 | reason = lua_tolstring(L, -1, &reason_len); |
| 5412 | lua_pop(L, 1); |
| 5413 | |
| 5414 | body = NULL; |
| 5415 | body_len = 0; |
| 5416 | ret = lua_getfield(L, -1, "body"); |
| 5417 | if (ret == LUA_TSTRING) |
| 5418 | body = lua_tolstring(L, -1, &body_len); |
| 5419 | lua_pop(L, 1); |
| 5420 | |
| 5421 | /* Prepare the response before inserting the headers */ |
| 5422 | h1m_init_res(&h1m); |
| 5423 | htx = htx_from_buf(&s->res.buf); |
| 5424 | channel_htx_truncate(&s->res, htx); |
| 5425 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5426 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5427 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5428 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5429 | } |
| 5430 | else { |
| 5431 | flags = HTX_SL_F_IS_RESP; |
| 5432 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5433 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5434 | } |
| 5435 | if (!sl) |
| 5436 | goto fail; |
| 5437 | sl->info.res.status = code; |
| 5438 | |
| 5439 | /* Push in the stack the "headers" entry. */ |
| 5440 | ret = lua_getfield(L, -1, "headers"); |
| 5441 | if (ret != LUA_TTABLE) |
| 5442 | goto skip_headers; |
| 5443 | |
| 5444 | lua_pushnil(L); |
| 5445 | while (lua_next(L, -2) != 0) { |
| 5446 | struct ist name, value; |
| 5447 | const char *n, *v; |
| 5448 | size_t nlen, vlen; |
| 5449 | |
| 5450 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5451 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5452 | goto next_hdr; |
| 5453 | } |
| 5454 | |
| 5455 | n = lua_tolstring(L, -2, &nlen); |
| 5456 | name = ist2(n, nlen); |
| 5457 | if (isteqi(name, ist("content-length"))) { |
| 5458 | /* Always skip content-length header. It will be added |
| 5459 | * later with the correct len |
| 5460 | */ |
| 5461 | goto next_hdr; |
| 5462 | } |
| 5463 | |
| 5464 | /* Loop on header's values */ |
| 5465 | lua_pushnil(L); |
| 5466 | while (lua_next(L, -2)) { |
| 5467 | if (!lua_isstring(L, -1)) { |
| 5468 | /* Skip the value if it is not a string */ |
| 5469 | goto next_value; |
| 5470 | } |
| 5471 | |
| 5472 | v = lua_tolstring(L, -1, &vlen); |
| 5473 | value = ist2(v, vlen); |
| 5474 | |
| 5475 | if (isteqi(name, ist("transfer-encoding"))) |
| 5476 | h1_parse_xfer_enc_header(&h1m, value); |
| 5477 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5478 | goto fail; |
| 5479 | |
| 5480 | next_value: |
| 5481 | lua_pop(L, 1); |
| 5482 | } |
| 5483 | |
| 5484 | next_hdr: |
| 5485 | lua_pop(L, 1); |
| 5486 | } |
| 5487 | skip_headers: |
| 5488 | lua_pop(L, 1); |
| 5489 | |
| 5490 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5491 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5492 | const char *clen = ultoa(body_len); |
| 5493 | |
| 5494 | h1m.flags |= H1_MF_CLEN; |
| 5495 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5496 | goto fail; |
| 5497 | } |
| 5498 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5499 | h1m.flags |= H1_MF_XFER_LEN; |
| 5500 | |
| 5501 | /* Update HTX start-line flags */ |
| 5502 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5503 | flags |= HTX_SL_F_XFER_ENC; |
| 5504 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5505 | flags |= HTX_SL_F_XFER_LEN; |
| 5506 | if (h1m.flags & H1_MF_CHNK) |
| 5507 | flags |= HTX_SL_F_CHNK; |
| 5508 | else if (h1m.flags & H1_MF_CLEN) |
| 5509 | flags |= HTX_SL_F_CLEN; |
| 5510 | if (h1m.body_len == 0) |
| 5511 | flags |= HTX_SL_F_BODYLESS; |
| 5512 | } |
| 5513 | sl->flags |= flags; |
| 5514 | |
| 5515 | |
| 5516 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5517 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5518 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5519 | goto fail; |
| 5520 | |
| 5521 | /* Now, forward the response and terminate the transaction */ |
| 5522 | s->txn->status = code; |
| 5523 | htx_to_buf(htx, &s->res.buf); |
| 5524 | if (!http_forward_proxy_resp(s, 1)) |
| 5525 | goto fail; |
| 5526 | |
| 5527 | return 1; |
| 5528 | |
| 5529 | fail: |
| 5530 | channel_htx_truncate(&s->res, htx); |
| 5531 | return 0; |
| 5532 | } |
| 5533 | |
| 5534 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5535 | * processing is just aborted. Nothing is returned to the client and all |
| 5536 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5537 | * is forwarded to the client before terminating the transaction. On success, |
| 5538 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5539 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5540 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5541 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5542 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5543 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5544 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5545 | struct stream *s; |
| 5546 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5547 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5548 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5549 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5550 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5551 | * just end the execution of the current lua code. */ |
| 5552 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5553 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5554 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5555 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5556 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5557 | struct channel *req = &s->req; |
| 5558 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5559 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5560 | channel_auto_read(req); |
| 5561 | channel_abort(req); |
| 5562 | channel_auto_close(req); |
| 5563 | channel_erase(req); |
| 5564 | |
| 5565 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5566 | channel_auto_read(res); |
| 5567 | channel_auto_close(res); |
| 5568 | channel_shutr_now(res); |
| 5569 | |
| 5570 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5571 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5572 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5573 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5574 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5575 | /* No reply or invalid reply */ |
| 5576 | s->txn->status = 0; |
| 5577 | http_reply_and_close(s, 0, NULL); |
| 5578 | } |
| 5579 | else { |
| 5580 | /* Remove extra args to have the reply on top of the stack */ |
| 5581 | if (lua_gettop(L) > 2) |
| 5582 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5583 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5584 | if (!hlua_txn_forward_reply(L, s)) { |
| 5585 | if (!(s->flags & SF_ERR_MASK)) |
| 5586 | s->flags |= SF_ERR_PRXCOND; |
| 5587 | lua_pushinteger(L, ACT_RET_ERR); |
| 5588 | WILL_LJMP(hlua_done(L)); |
| 5589 | return 0; /* Never reached */ |
| 5590 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5591 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5592 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5593 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5594 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5595 | /* let's log the request time */ |
| 5596 | s->logs.tv_request = now; |
| 5597 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5598 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5599 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5600 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5601 | done: |
| 5602 | if (!(s->flags & SF_ERR_MASK)) |
| 5603 | s->flags |= SF_ERR_LOCAL; |
| 5604 | if (!(s->flags & SF_FINST_MASK)) |
| 5605 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5606 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5607 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5608 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5609 | return 0; |
| 5610 | } |
| 5611 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5612 | /* |
| 5613 | * |
| 5614 | * |
| 5615 | * Class REPLY |
| 5616 | * |
| 5617 | * |
| 5618 | */ |
| 5619 | |
| 5620 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5621 | * free slots, the function fails and returns 0; |
| 5622 | */ |
| 5623 | static int hlua_txn_reply_new(lua_State *L) |
| 5624 | { |
| 5625 | struct hlua_txn *htxn; |
| 5626 | const char *reason, *body = NULL; |
| 5627 | int ret, status; |
| 5628 | |
| 5629 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5630 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5631 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5632 | WILL_LJMP(lua_error(L)); |
| 5633 | } |
| 5634 | |
| 5635 | /* Default value */ |
| 5636 | status = 200; |
| 5637 | reason = http_get_reason(status); |
| 5638 | |
| 5639 | if (lua_istable(L, 2)) { |
| 5640 | /* load status and reason from the table argument at index 2 */ |
| 5641 | ret = lua_getfield(L, 2, "status"); |
| 5642 | if (ret == LUA_TNIL) |
| 5643 | goto reason; |
| 5644 | else if (ret != LUA_TNUMBER) { |
| 5645 | /* invalid status: ignore the reason */ |
| 5646 | goto body; |
| 5647 | } |
| 5648 | status = lua_tointeger(L, -1); |
| 5649 | |
| 5650 | reason: |
| 5651 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5652 | ret = lua_getfield(L, 2, "reason"); |
| 5653 | if (ret == LUA_TSTRING) |
| 5654 | reason = lua_tostring(L, -1); |
| 5655 | |
| 5656 | body: |
| 5657 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5658 | ret = lua_getfield(L, 2, "body"); |
| 5659 | if (ret == LUA_TSTRING) |
| 5660 | body = lua_tostring(L, -1); |
| 5661 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5662 | } |
| 5663 | |
| 5664 | /* Create the Reply table */ |
| 5665 | lua_newtable(L); |
| 5666 | |
| 5667 | /* Add status element */ |
| 5668 | lua_pushstring(L, "status"); |
| 5669 | lua_pushinteger(L, status); |
| 5670 | lua_settable(L, -3); |
| 5671 | |
| 5672 | /* Add reason element */ |
| 5673 | reason = http_get_reason(status); |
| 5674 | lua_pushstring(L, "reason"); |
| 5675 | lua_pushstring(L, reason); |
| 5676 | lua_settable(L, -3); |
| 5677 | |
| 5678 | /* Add body element, nil if undefined */ |
| 5679 | lua_pushstring(L, "body"); |
| 5680 | if (body) |
| 5681 | lua_pushstring(L, body); |
| 5682 | else |
| 5683 | lua_pushnil(L); |
| 5684 | lua_settable(L, -3); |
| 5685 | |
| 5686 | /* Add headers element */ |
| 5687 | lua_pushstring(L, "headers"); |
| 5688 | lua_newtable(L); |
| 5689 | |
| 5690 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5691 | if (lua_istable(L, 2)) { |
| 5692 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5693 | ret = lua_getfield(L, 2, "headers"); |
| 5694 | if (ret == LUA_TTABLE) { |
| 5695 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5696 | lua_pushnil(L); |
| 5697 | while (lua_next(L, -2) != 0) { |
| 5698 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5699 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5700 | /* invalid value type, skip it */ |
| 5701 | lua_pop(L, 1); |
| 5702 | continue; |
| 5703 | } |
| 5704 | |
| 5705 | |
| 5706 | /* Duplicate the key and swap it with the value. */ |
| 5707 | lua_pushvalue(L, -2); |
| 5708 | lua_insert(L, -2); |
| 5709 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5710 | |
| 5711 | lua_newtable(L); |
| 5712 | lua_insert(L, -2); |
| 5713 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5714 | |
| 5715 | if (lua_isstring(L, -1)) { |
| 5716 | /* push the value in the inner table */ |
| 5717 | lua_rawseti(L, -2, 1); |
| 5718 | } |
| 5719 | else { /* table */ |
| 5720 | lua_pushnil(L); |
| 5721 | while (lua_next(L, -2) != 0) { |
| 5722 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5723 | if (!lua_isstring(L, -1)) { |
| 5724 | /* invalid value type, skip it*/ |
| 5725 | lua_pop(L, 1); |
| 5726 | continue; |
| 5727 | } |
| 5728 | /* push the value in the inner table */ |
| 5729 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5730 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5731 | } |
| 5732 | lua_pop(L, 1); |
| 5733 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5734 | } |
| 5735 | |
| 5736 | /* push (k,v) on the stack in the headers table: |
| 5737 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5738 | */ |
| 5739 | lua_settable(L, -5); |
| 5740 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5741 | } |
| 5742 | } |
| 5743 | lua_pop(L, 1); |
| 5744 | } |
| 5745 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5746 | lua_settable(L, -3); |
| 5747 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5748 | |
| 5749 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5750 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5751 | lua_setmetatable(L, -2); |
| 5752 | return 1; |
| 5753 | } |
| 5754 | |
| 5755 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5756 | * provided, the default one corresponding to the status code is used. |
| 5757 | */ |
| 5758 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5759 | { |
| 5760 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5761 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5762 | |
| 5763 | /* First argument (self) must be a table */ |
| 5764 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5765 | |
| 5766 | if (status < 100 || status > 599) { |
| 5767 | lua_pushboolean(L, 0); |
| 5768 | return 1; |
| 5769 | } |
| 5770 | if (!reason) |
| 5771 | reason = http_get_reason(status); |
| 5772 | |
| 5773 | lua_pushinteger(L, status); |
| 5774 | lua_setfield(L, 1, "status"); |
| 5775 | |
| 5776 | lua_pushstring(L, reason); |
| 5777 | lua_setfield(L, 1, "reason"); |
| 5778 | |
| 5779 | lua_pushboolean(L, 1); |
| 5780 | return 1; |
| 5781 | } |
| 5782 | |
| 5783 | /* Add a header into the reply object. Each header name is associated to an |
| 5784 | * array of values in the "headers" table. If the header name is not found, a |
| 5785 | * new entry is created. |
| 5786 | */ |
| 5787 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5788 | { |
| 5789 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5790 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5791 | int ret; |
| 5792 | |
| 5793 | /* First argument (self) must be a table */ |
| 5794 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5795 | |
| 5796 | /* Push in the stack the "headers" entry. */ |
| 5797 | ret = lua_getfield(L, 1, "headers"); |
| 5798 | if (ret != LUA_TTABLE) { |
| 5799 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5800 | WILL_LJMP(lua_error(L)); |
| 5801 | } |
| 5802 | |
| 5803 | /* check if the header is already registered. If not, register it. */ |
| 5804 | ret = lua_getfield(L, -1, name); |
| 5805 | if (ret == LUA_TNIL) { |
| 5806 | /* Entry not found. */ |
| 5807 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5808 | |
| 5809 | /* Insert the new header name in the array in the top of the stack. |
| 5810 | * It left the new array in the top of the stack. |
| 5811 | */ |
| 5812 | lua_newtable(L); |
| 5813 | lua_pushstring(L, name); |
| 5814 | lua_pushvalue(L, -2); |
| 5815 | lua_settable(L, -4); |
| 5816 | } |
| 5817 | else if (ret != LUA_TTABLE) { |
| 5818 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5819 | WILL_LJMP(lua_error(L)); |
| 5820 | } |
| 5821 | |
| 5822 | /* Now the top od thestack is an array of values. We push |
| 5823 | * the header value as new entry. |
| 5824 | */ |
| 5825 | lua_pushstring(L, value); |
| 5826 | ret = lua_rawlen(L, -2); |
| 5827 | lua_rawseti(L, -2, ret + 1); |
| 5828 | |
| 5829 | lua_pushboolean(L, 1); |
| 5830 | return 1; |
| 5831 | } |
| 5832 | |
| 5833 | /* Remove all occurrences of a given header name. */ |
| 5834 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5835 | { |
| 5836 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5837 | int ret; |
| 5838 | |
| 5839 | /* First argument (self) must be a table */ |
| 5840 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5841 | |
| 5842 | /* Push in the stack the "headers" entry. */ |
| 5843 | ret = lua_getfield(L, 1, "headers"); |
| 5844 | if (ret != LUA_TTABLE) { |
| 5845 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5846 | WILL_LJMP(lua_error(L)); |
| 5847 | } |
| 5848 | |
| 5849 | lua_pushstring(L, name); |
| 5850 | lua_pushnil(L); |
| 5851 | lua_settable(L, -3); |
| 5852 | |
| 5853 | lua_pushboolean(L, 1); |
| 5854 | return 1; |
| 5855 | } |
| 5856 | |
| 5857 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5858 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5859 | { |
| 5860 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5861 | |
| 5862 | /* First argument (self) must be a table */ |
| 5863 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5864 | |
| 5865 | lua_pushstring(L, payload); |
| 5866 | lua_setfield(L, 1, "body"); |
| 5867 | |
| 5868 | lua_pushboolean(L, 1); |
| 5869 | return 1; |
| 5870 | } |
| 5871 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5872 | __LJMP static int hlua_log(lua_State *L) |
| 5873 | { |
| 5874 | int level; |
| 5875 | const char *msg; |
| 5876 | |
| 5877 | MAY_LJMP(check_args(L, 2, "log")); |
| 5878 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5879 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5880 | |
| 5881 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5882 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5883 | |
| 5884 | hlua_sendlog(NULL, level, msg); |
| 5885 | return 0; |
| 5886 | } |
| 5887 | |
| 5888 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5889 | { |
| 5890 | const char *msg; |
| 5891 | |
| 5892 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5893 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5894 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5895 | return 0; |
| 5896 | } |
| 5897 | |
| 5898 | __LJMP static int hlua_log_info(lua_State *L) |
| 5899 | { |
| 5900 | const char *msg; |
| 5901 | |
| 5902 | MAY_LJMP(check_args(L, 1, "info")); |
| 5903 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5904 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5905 | return 0; |
| 5906 | } |
| 5907 | |
| 5908 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5909 | { |
| 5910 | const char *msg; |
| 5911 | |
| 5912 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5913 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5914 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5915 | return 0; |
| 5916 | } |
| 5917 | |
| 5918 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5919 | { |
| 5920 | const char *msg; |
| 5921 | |
| 5922 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5923 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5924 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5925 | return 0; |
| 5926 | } |
| 5927 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5928 | __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] | 5929 | { |
| 5930 | int wakeup_ms = lua_tointeger(L, -1); |
| 5931 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5932 | 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] | 5933 | return 0; |
| 5934 | } |
| 5935 | |
| 5936 | __LJMP static int hlua_sleep(lua_State *L) |
| 5937 | { |
| 5938 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5939 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5940 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5941 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5942 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5943 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5944 | wakeup_ms = tick_add(now_ms, delay); |
| 5945 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5946 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5947 | 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] | 5948 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5949 | } |
| 5950 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5951 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5952 | { |
| 5953 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5954 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5955 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5956 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5957 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5958 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5959 | wakeup_ms = tick_add(now_ms, delay); |
| 5960 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5961 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5962 | 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] | 5963 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5964 | } |
| 5965 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5966 | /* This functionis an LUA binding. it permits to give back |
| 5967 | * the hand at the HAProxy scheduler. It is used when the |
| 5968 | * LUA processing consumes a lot of time. |
| 5969 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5970 | __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] | 5971 | { |
| 5972 | return 0; |
| 5973 | } |
| 5974 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5975 | __LJMP static int hlua_yield(lua_State *L) |
| 5976 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5977 | 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] | 5978 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5979 | } |
| 5980 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5981 | /* This function change the nice of the currently executed |
| 5982 | * task. It is used set low or high priority at the current |
| 5983 | * task. |
| 5984 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5985 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5986 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5987 | struct hlua *hlua; |
| 5988 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5989 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5990 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5991 | hlua = hlua_gethlua(L); |
| 5992 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5993 | |
| 5994 | /* If he task is not set, I'm in a start mode. */ |
| 5995 | if (!hlua || !hlua->task) |
| 5996 | return 0; |
| 5997 | |
| 5998 | if (nice < -1024) |
| 5999 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6000 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6001 | nice = 1024; |
| 6002 | |
| 6003 | hlua->task->nice = nice; |
| 6004 | return 0; |
| 6005 | } |
| 6006 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6007 | /* 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] | 6008 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6009 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6010 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6011 | * |
| 6012 | * Task wrapper are longjmp safe because the only one Lua code |
| 6013 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6014 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6015 | struct task *hlua_process_task(struct task *task, void *context, unsigned short state) |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6016 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6017 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6018 | enum hlua_exec status; |
| 6019 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6020 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6021 | task_set_affinity(task, tid_bit); |
| 6022 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6023 | /* If it is the first call to the task, we must initialize the |
| 6024 | * execution timeouts. |
| 6025 | */ |
| 6026 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6027 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6028 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6029 | /* Execute the Lua code. */ |
| 6030 | status = hlua_ctx_resume(hlua, 1); |
| 6031 | |
| 6032 | switch (status) { |
| 6033 | /* finished or yield */ |
| 6034 | case HLUA_E_OK: |
| 6035 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6036 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6037 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6038 | break; |
| 6039 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6040 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6041 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6042 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6043 | break; |
| 6044 | |
| 6045 | /* finished with error. */ |
| 6046 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6047 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6048 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6049 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6050 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6051 | break; |
| 6052 | |
| 6053 | case HLUA_E_ERR: |
| 6054 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6055 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6056 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6057 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6058 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6059 | break; |
| 6060 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6061 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6062 | } |
| 6063 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6064 | /* This function is an LUA binding that register LUA function to be |
| 6065 | * executed after the HAProxy configuration parsing and before the |
| 6066 | * HAProxy scheduler starts. This function expect only one LUA |
| 6067 | * argument that is a function. This function returns nothing, but |
| 6068 | * throws if an error is encountered. |
| 6069 | */ |
| 6070 | __LJMP static int hlua_register_init(lua_State *L) |
| 6071 | { |
| 6072 | struct hlua_init_function *init; |
| 6073 | int ref; |
| 6074 | |
| 6075 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6076 | |
| 6077 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6078 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6079 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6080 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6081 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6082 | |
| 6083 | init->function_ref = ref; |
| 6084 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6085 | return 0; |
| 6086 | } |
| 6087 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6088 | /* This functio is an LUA binding. It permits to register a task |
| 6089 | * executed in parallel of the main HAroxy activity. The task is |
| 6090 | * created and it is set in the HAProxy scheduler. It can be called |
| 6091 | * from the "init" section, "post init" or during the runtime. |
| 6092 | * |
| 6093 | * Lua prototype: |
| 6094 | * |
| 6095 | * <none> core.register_task(<function>) |
| 6096 | */ |
| 6097 | static int hlua_register_task(lua_State *L) |
| 6098 | { |
| 6099 | struct hlua *hlua; |
| 6100 | struct task *task; |
| 6101 | int ref; |
| 6102 | |
| 6103 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6104 | |
| 6105 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6106 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6107 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6108 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6109 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6110 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6111 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6112 | if (!task) |
| 6113 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6114 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6115 | task->context = hlua; |
| 6116 | task->process = hlua_process_task; |
| 6117 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6118 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6119 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6120 | |
| 6121 | /* Restore the function in the stack. */ |
| 6122 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6123 | hlua->nargs = 0; |
| 6124 | |
| 6125 | /* Schedule task. */ |
| 6126 | task_schedule(task, now_ms); |
| 6127 | |
| 6128 | return 0; |
| 6129 | } |
| 6130 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6131 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6132 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6133 | * resume converters. |
| 6134 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6135 | 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] | 6136 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6137 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6138 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6139 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6140 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6141 | if (!stream) |
| 6142 | return 0; |
| 6143 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6144 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6145 | * Lua context can be not initialized. This behavior |
| 6146 | * permits to save performances because a systematic |
| 6147 | * Lua initialization cause 5% performances loss. |
| 6148 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6149 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6150 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6151 | if (!stream->hlua) { |
| 6152 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6153 | return 0; |
| 6154 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6155 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6156 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6157 | return 0; |
| 6158 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6159 | } |
| 6160 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6161 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6162 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6163 | |
| 6164 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6165 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6166 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6167 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6168 | else |
| 6169 | error = "critical error"; |
| 6170 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6171 | return 0; |
| 6172 | } |
| 6173 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6174 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6175 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6176 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6177 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6178 | return 0; |
| 6179 | } |
| 6180 | |
| 6181 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6182 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6183 | |
| 6184 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6186 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6187 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6188 | return 0; |
| 6189 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6190 | hlua_smp2lua(stream->hlua->T, smp); |
| 6191 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6192 | |
| 6193 | /* push keywords in the stack. */ |
| 6194 | if (arg_p) { |
| 6195 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6196 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6197 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6198 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6199 | return 0; |
| 6200 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6202 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6203 | } |
| 6204 | } |
| 6205 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6206 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6207 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6208 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6209 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6210 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6211 | } |
| 6212 | |
| 6213 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6214 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6215 | /* finished. */ |
| 6216 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6217 | /* If the stack is empty, the function fails. */ |
| 6218 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6219 | return 0; |
| 6220 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6221 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6222 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6223 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6224 | return 1; |
| 6225 | |
| 6226 | /* yield. */ |
| 6227 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6228 | 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] | 6229 | return 0; |
| 6230 | |
| 6231 | /* finished with error. */ |
| 6232 | case HLUA_E_ERRMSG: |
| 6233 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6234 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6235 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6236 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6237 | return 0; |
| 6238 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6239 | case HLUA_E_ETMOUT: |
| 6240 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6241 | return 0; |
| 6242 | |
| 6243 | case HLUA_E_NOMEM: |
| 6244 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6245 | return 0; |
| 6246 | |
| 6247 | case HLUA_E_YIELD: |
| 6248 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6249 | return 0; |
| 6250 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6251 | case HLUA_E_ERR: |
| 6252 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6253 | SEND_ERR(stream->be, "Lua converter '%s' returns an unknown error.\n", fcn->name); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6254 | |
| 6255 | default: |
| 6256 | return 0; |
| 6257 | } |
| 6258 | } |
| 6259 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6260 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6261 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6262 | * resume sample-fetches. This function will be called by the sample |
| 6263 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6264 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6265 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6266 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6267 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6268 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6269 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6270 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6271 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6272 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6273 | if (!stream) |
| 6274 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6275 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6276 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6277 | * Lua context can be not initialized. This behavior |
| 6278 | * permits to save performances because a systematic |
| 6279 | * Lua initialization cause 5% performances loss. |
| 6280 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6281 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6282 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6283 | if (!stream->hlua) { |
| 6284 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6285 | return 0; |
| 6286 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6287 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6288 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6289 | return 0; |
| 6290 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6291 | } |
| 6292 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6293 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6294 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6295 | |
| 6296 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6297 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6298 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6299 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6300 | else |
| 6301 | error = "critical error"; |
| 6302 | 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] | 6303 | return 0; |
| 6304 | } |
| 6305 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6306 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6307 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6308 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6309 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6310 | return 0; |
| 6311 | } |
| 6312 | |
| 6313 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6314 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6315 | |
| 6316 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6317 | 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] | 6318 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6319 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6320 | return 0; |
| 6321 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6322 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6323 | |
| 6324 | /* push keywords in the stack. */ |
| 6325 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6326 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6327 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6328 | SEND_ERR(smp->px, "Lua sample-fetch '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6329 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6330 | return 0; |
| 6331 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6332 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6333 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6334 | } |
| 6335 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6336 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6337 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6338 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6339 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6340 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6341 | } |
| 6342 | |
| 6343 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6344 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6345 | /* finished. */ |
| 6346 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6347 | /* If the stack is empty, the function fails. */ |
| 6348 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6349 | return 0; |
| 6350 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6351 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6352 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6353 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6354 | |
| 6355 | /* Set the end of execution flag. */ |
| 6356 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6357 | return 1; |
| 6358 | |
| 6359 | /* yield. */ |
| 6360 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6361 | 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] | 6362 | return 0; |
| 6363 | |
| 6364 | /* finished with error. */ |
| 6365 | case HLUA_E_ERRMSG: |
| 6366 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6367 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6368 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6369 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6370 | return 0; |
| 6371 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6372 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6373 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6374 | return 0; |
| 6375 | |
| 6376 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6377 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6378 | return 0; |
| 6379 | |
| 6380 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6381 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6382 | return 0; |
| 6383 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6384 | case HLUA_E_ERR: |
| 6385 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6386 | SEND_ERR(smp->px, "Lua sample-fetch '%s' returns an unknown error.\n", fcn->name); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6387 | |
| 6388 | default: |
| 6389 | return 0; |
| 6390 | } |
| 6391 | } |
| 6392 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6393 | /* This function is an LUA binding used for registering |
| 6394 | * "sample-conv" functions. It expects a converter name used |
| 6395 | * in the haproxy configuration file, and an LUA function. |
| 6396 | */ |
| 6397 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6398 | { |
| 6399 | struct sample_conv_kw_list *sck; |
| 6400 | const char *name; |
| 6401 | int ref; |
| 6402 | int len; |
| 6403 | struct hlua_function *fcn; |
| 6404 | |
| 6405 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6406 | |
| 6407 | /* First argument : converter name. */ |
| 6408 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6409 | |
| 6410 | /* Second argument : lua function. */ |
| 6411 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6412 | |
| 6413 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6414 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6415 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6416 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6417 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6418 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6419 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6420 | |
| 6421 | /* Fill fcn. */ |
| 6422 | fcn->name = strdup(name); |
| 6423 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6424 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6425 | fcn->function_ref = ref; |
| 6426 | |
| 6427 | /* List head */ |
| 6428 | sck->list.n = sck->list.p = NULL; |
| 6429 | |
| 6430 | /* converter keyword. */ |
| 6431 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6432 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6433 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6434 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6435 | |
| 6436 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6437 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6438 | 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] | 6439 | sck->kw[0].val_args = NULL; |
| 6440 | sck->kw[0].in_type = SMP_T_STR; |
| 6441 | sck->kw[0].out_type = SMP_T_STR; |
| 6442 | sck->kw[0].private = fcn; |
| 6443 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6444 | /* Register this new converter */ |
| 6445 | sample_register_convs(sck); |
| 6446 | |
| 6447 | return 0; |
| 6448 | } |
| 6449 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6450 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6451 | * "sample-fetch" functions. It expects a converter name used |
| 6452 | * in the haproxy configuration file, and an LUA function. |
| 6453 | */ |
| 6454 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6455 | { |
| 6456 | const char *name; |
| 6457 | int ref; |
| 6458 | int len; |
| 6459 | struct sample_fetch_kw_list *sfk; |
| 6460 | struct hlua_function *fcn; |
| 6461 | |
| 6462 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6463 | |
| 6464 | /* First argument : sample-fetch name. */ |
| 6465 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6466 | |
| 6467 | /* Second argument : lua function. */ |
| 6468 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6469 | |
| 6470 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6471 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6472 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6473 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6474 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6475 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6476 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6477 | |
| 6478 | /* Fill fcn. */ |
| 6479 | fcn->name = strdup(name); |
| 6480 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6481 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6482 | fcn->function_ref = ref; |
| 6483 | |
| 6484 | /* List head */ |
| 6485 | sfk->list.n = sfk->list.p = NULL; |
| 6486 | |
| 6487 | /* sample-fetch keyword. */ |
| 6488 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6489 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6490 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6491 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6492 | |
| 6493 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6494 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6495 | 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] | 6496 | sfk->kw[0].val_args = NULL; |
| 6497 | sfk->kw[0].out_type = SMP_T_STR; |
| 6498 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6499 | sfk->kw[0].val = 0; |
| 6500 | sfk->kw[0].private = fcn; |
| 6501 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6502 | /* Register this new fetch. */ |
| 6503 | sample_register_fetches(sfk); |
| 6504 | |
| 6505 | return 0; |
| 6506 | } |
| 6507 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6508 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6509 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6510 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6511 | { |
| 6512 | struct hlua *hlua = hlua_gethlua(L); |
| 6513 | unsigned int delay; |
| 6514 | unsigned int wakeup_ms; |
| 6515 | |
| 6516 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6517 | |
| 6518 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6519 | wakeup_ms = tick_add(now_ms, delay); |
| 6520 | hlua->wake_time = wakeup_ms; |
| 6521 | return 0; |
| 6522 | } |
| 6523 | |
| 6524 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6525 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6526 | * wrapper during the initialisation period. This function may return any |
| 6527 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6528 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6529 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6530 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6531 | 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] | 6532 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6533 | { |
| 6534 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6535 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6536 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6537 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6538 | |
| 6539 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6540 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6541 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6542 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6543 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6544 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6545 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6546 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6547 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6548 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6549 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6550 | * Lua context can be not initialized. This behavior |
| 6551 | * permits to save performances because a systematic |
| 6552 | * Lua initialization cause 5% performances loss. |
| 6553 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6554 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6555 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6556 | if (!s->hlua) { |
| 6557 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6558 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6559 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6560 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6561 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6562 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6563 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6564 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6565 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6566 | } |
| 6567 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6568 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6569 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6570 | |
| 6571 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6572 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6573 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6574 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6575 | else |
| 6576 | error = "critical error"; |
| 6577 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6578 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6579 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6580 | } |
| 6581 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6582 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6583 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6584 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6585 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6586 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6587 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6588 | } |
| 6589 | |
| 6590 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | lua_rawgeti(s->hlua->T, LUA_REGISTRYINDEX, rule->arg.hlua_rule->fcn.function_ref); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6592 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6593 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6594 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6595 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6596 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6597 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6598 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6599 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6600 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6601 | |
| 6602 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6603 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6604 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6605 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6606 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6607 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6608 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6609 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6610 | lua_pushstring(s->hlua->T, *arg); |
| 6611 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6612 | } |
| 6613 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6614 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6616 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6617 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6619 | } |
| 6620 | |
| 6621 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6622 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6623 | /* finished. */ |
| 6624 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6625 | /* Catch the return value */ |
| 6626 | if (lua_gettop(s->hlua->T) > 0) |
| 6627 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6628 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6629 | /* Set timeout in the required channel. */ |
| 6630 | if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) { |
| 6631 | if (dir == SMP_OPT_DIR_REQ) |
| 6632 | s->req.analyse_exp = s->hlua->wake_time; |
| 6633 | else |
| 6634 | s->res.analyse_exp = s->hlua->wake_time; |
| 6635 | } |
| 6636 | goto end; |
| 6637 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6638 | /* yield. */ |
| 6639 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6640 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6641 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6642 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6643 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6644 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6645 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6646 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6647 | /* Some actions can be wake up when a "write" event |
| 6648 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6649 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6650 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6651 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6652 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6653 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6654 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6655 | act_ret = ACT_RET_YIELD; |
| 6656 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6657 | |
| 6658 | /* finished with error. */ |
| 6659 | case HLUA_E_ERRMSG: |
| 6660 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6661 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6662 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6663 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6664 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6665 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6666 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6667 | 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] | 6668 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6669 | |
| 6670 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6671 | 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] | 6672 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6673 | |
| 6674 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6675 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6676 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6677 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6678 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6679 | case HLUA_E_ERR: |
| 6680 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6681 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6682 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6683 | |
| 6684 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6685 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6686 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6687 | |
| 6688 | end: |
| 6689 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6690 | } |
| 6691 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6692 | struct task *hlua_applet_wakeup(struct task *t, void *context, unsigned short state) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6693 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6694 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6695 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6696 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6697 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6698 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6699 | } |
| 6700 | |
| 6701 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6702 | { |
| 6703 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6704 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6705 | struct task *task; |
| 6706 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6707 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6708 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6709 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6710 | if (!hlua) { |
| 6711 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6712 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6713 | return 0; |
| 6714 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6715 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6716 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6717 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6718 | |
| 6719 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6720 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6721 | if (!task) { |
| 6722 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6723 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6724 | return 0; |
| 6725 | } |
| 6726 | task->nice = 0; |
| 6727 | task->context = ctx; |
| 6728 | task->process = hlua_applet_wakeup; |
| 6729 | ctx->ctx.hlua_apptcp.task = task; |
| 6730 | |
| 6731 | /* In the execution wrappers linked with a stream, the |
| 6732 | * Lua context can be not initialized. This behavior |
| 6733 | * permits to save performances because a systematic |
| 6734 | * Lua initialization cause 5% performances loss. |
| 6735 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6736 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6737 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6738 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6739 | return 0; |
| 6740 | } |
| 6741 | |
| 6742 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6743 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6744 | |
| 6745 | /* The following Lua calls can fail. */ |
| 6746 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6747 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6748 | error = lua_tostring(hlua->T, -1); |
| 6749 | else |
| 6750 | error = "critical error"; |
| 6751 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6752 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6753 | return 0; |
| 6754 | } |
| 6755 | |
| 6756 | /* Check stack available size. */ |
| 6757 | if (!lua_checkstack(hlua->T, 1)) { |
| 6758 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6759 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6760 | RESET_SAFE_LJMP(hlua->T); |
| 6761 | return 0; |
| 6762 | } |
| 6763 | |
| 6764 | /* Restore the function in the stack. */ |
| 6765 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6766 | |
| 6767 | /* Create and and push object stream in the stack. */ |
| 6768 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6769 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6770 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6771 | RESET_SAFE_LJMP(hlua->T); |
| 6772 | return 0; |
| 6773 | } |
| 6774 | hlua->nargs = 1; |
| 6775 | |
| 6776 | /* push keywords in the stack. */ |
| 6777 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6778 | if (!lua_checkstack(hlua->T, 1)) { |
| 6779 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6780 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6781 | RESET_SAFE_LJMP(hlua->T); |
| 6782 | return 0; |
| 6783 | } |
| 6784 | lua_pushstring(hlua->T, *arg); |
| 6785 | hlua->nargs++; |
| 6786 | } |
| 6787 | |
| 6788 | RESET_SAFE_LJMP(hlua->T); |
| 6789 | |
| 6790 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6791 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6792 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6793 | |
| 6794 | return 1; |
| 6795 | } |
| 6796 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6797 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6798 | { |
| 6799 | struct stream_interface *si = ctx->owner; |
| 6800 | struct stream *strm = si_strm(si); |
| 6801 | struct channel *res = si_ic(si); |
| 6802 | struct act_rule *rule = ctx->rule; |
| 6803 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6804 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6805 | |
| 6806 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6807 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6808 | /* eat the whole request */ |
| 6809 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6810 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6811 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6812 | |
| 6813 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6814 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6815 | return; |
| 6816 | |
| 6817 | /* Execute the function. */ |
| 6818 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6819 | /* finished. */ |
| 6820 | case HLUA_E_OK: |
| 6821 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6822 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6823 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6824 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6825 | res->flags |= CF_READ_NULL; |
| 6826 | si_shutr(si); |
| 6827 | return; |
| 6828 | |
| 6829 | /* yield. */ |
| 6830 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6831 | if (hlua->wake_time != TICK_ETERNITY) |
| 6832 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6833 | return; |
| 6834 | |
| 6835 | /* finished with error. */ |
| 6836 | case HLUA_E_ERRMSG: |
| 6837 | /* Display log. */ |
| 6838 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6839 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6840 | lua_pop(hlua->T, 1); |
| 6841 | goto error; |
| 6842 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6843 | case HLUA_E_ETMOUT: |
| 6844 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6845 | rule->arg.hlua_rule->fcn.name); |
| 6846 | goto error; |
| 6847 | |
| 6848 | case HLUA_E_NOMEM: |
| 6849 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6850 | rule->arg.hlua_rule->fcn.name); |
| 6851 | goto error; |
| 6852 | |
| 6853 | case HLUA_E_YIELD: /* unexpected */ |
| 6854 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6855 | rule->arg.hlua_rule->fcn.name); |
| 6856 | goto error; |
| 6857 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6858 | case HLUA_E_ERR: |
| 6859 | /* Display log. */ |
| 6860 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6861 | rule->arg.hlua_rule->fcn.name); |
| 6862 | goto error; |
| 6863 | |
| 6864 | default: |
| 6865 | goto error; |
| 6866 | } |
| 6867 | |
| 6868 | error: |
| 6869 | |
| 6870 | /* For all other cases, just close the stream. */ |
| 6871 | si_shutw(si); |
| 6872 | si_shutr(si); |
| 6873 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6874 | } |
| 6875 | |
| 6876 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6877 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6878 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6879 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6880 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6881 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6882 | } |
| 6883 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6884 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6885 | * an errors occurs and -1 if more data are required for initializing |
| 6886 | * the applet. |
| 6887 | */ |
| 6888 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6889 | { |
| 6890 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6891 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6892 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6893 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6894 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6895 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6896 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6897 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6898 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6899 | if (!hlua) { |
| 6900 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6901 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6902 | return 0; |
| 6903 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6904 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6905 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6906 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6907 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6908 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6909 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6910 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6911 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6912 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6913 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6914 | if (!task) { |
| 6915 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6916 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6917 | return 0; |
| 6918 | } |
| 6919 | task->nice = 0; |
| 6920 | task->context = ctx; |
| 6921 | task->process = hlua_applet_wakeup; |
| 6922 | ctx->ctx.hlua_apphttp.task = task; |
| 6923 | |
| 6924 | /* In the execution wrappers linked with a stream, the |
| 6925 | * Lua context can be not initialized. This behavior |
| 6926 | * permits to save performances because a systematic |
| 6927 | * Lua initialization cause 5% performances loss. |
| 6928 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6929 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6930 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6931 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6932 | return 0; |
| 6933 | } |
| 6934 | |
| 6935 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6936 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6937 | |
| 6938 | /* The following Lua calls can fail. */ |
| 6939 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6940 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6941 | error = lua_tostring(hlua->T, -1); |
| 6942 | else |
| 6943 | error = "critical error"; |
| 6944 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6945 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6946 | return 0; |
| 6947 | } |
| 6948 | |
| 6949 | /* Check stack available size. */ |
| 6950 | if (!lua_checkstack(hlua->T, 1)) { |
| 6951 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6952 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6953 | RESET_SAFE_LJMP(hlua->T); |
| 6954 | return 0; |
| 6955 | } |
| 6956 | |
| 6957 | /* Restore the function in the stack. */ |
| 6958 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6959 | |
| 6960 | /* Create and and push object stream in the stack. */ |
| 6961 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6962 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6963 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6964 | RESET_SAFE_LJMP(hlua->T); |
| 6965 | return 0; |
| 6966 | } |
| 6967 | hlua->nargs = 1; |
| 6968 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6969 | /* push keywords in the stack. */ |
| 6970 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6971 | if (!lua_checkstack(hlua->T, 1)) { |
| 6972 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6973 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6974 | RESET_SAFE_LJMP(hlua->T); |
| 6975 | return 0; |
| 6976 | } |
| 6977 | lua_pushstring(hlua->T, *arg); |
| 6978 | hlua->nargs++; |
| 6979 | } |
| 6980 | |
| 6981 | RESET_SAFE_LJMP(hlua->T); |
| 6982 | |
| 6983 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6984 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6985 | |
| 6986 | return 1; |
| 6987 | } |
| 6988 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6989 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6990 | { |
| 6991 | struct stream_interface *si = ctx->owner; |
| 6992 | struct stream *strm = si_strm(si); |
| 6993 | struct channel *req = si_oc(si); |
| 6994 | struct channel *res = si_ic(si); |
| 6995 | struct act_rule *rule = ctx->rule; |
| 6996 | struct proxy *px = strm->be; |
| 6997 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 6998 | struct htx *req_htx, *res_htx; |
| 6999 | |
| 7000 | res_htx = htx_from_buf(&res->buf); |
| 7001 | |
| 7002 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7003 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7004 | goto out; |
| 7005 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7006 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7007 | if (!b_size(&res->buf)) { |
| 7008 | si_rx_room_blk(si); |
| 7009 | goto out; |
| 7010 | } |
| 7011 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7012 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7013 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7014 | |
| 7015 | /* Set the currently running flag. */ |
| 7016 | if (!HLUA_IS_RUNNING(hlua) && |
| 7017 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7018 | struct htx_blk *blk; |
| 7019 | size_t count = co_data(req); |
| 7020 | |
| 7021 | if (!count) { |
| 7022 | si_cant_get(si); |
| 7023 | goto out; |
| 7024 | } |
| 7025 | |
| 7026 | /* We need to flush the request header. This left the body for |
| 7027 | * the Lua. |
| 7028 | */ |
| 7029 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7030 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7031 | while (count && blk) { |
| 7032 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7033 | uint32_t sz = htx_get_blksz(blk); |
| 7034 | |
| 7035 | if (sz > count) { |
| 7036 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7037 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7038 | goto out; |
| 7039 | } |
| 7040 | |
| 7041 | count -= sz; |
| 7042 | co_set_data(req, co_data(req) - sz); |
| 7043 | blk = htx_remove_blk(req_htx, blk); |
| 7044 | |
| 7045 | if (type == HTX_BLK_EOH) |
| 7046 | break; |
| 7047 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7048 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7049 | } |
| 7050 | |
| 7051 | /* Executes The applet if it is not done. */ |
| 7052 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7053 | |
| 7054 | /* Execute the function. */ |
| 7055 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7056 | /* finished. */ |
| 7057 | case HLUA_E_OK: |
| 7058 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7059 | break; |
| 7060 | |
| 7061 | /* yield. */ |
| 7062 | case HLUA_E_AGAIN: |
| 7063 | if (hlua->wake_time != TICK_ETERNITY) |
| 7064 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7065 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7066 | |
| 7067 | /* finished with error. */ |
| 7068 | case HLUA_E_ERRMSG: |
| 7069 | /* Display log. */ |
| 7070 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7071 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7072 | lua_pop(hlua->T, 1); |
| 7073 | goto error; |
| 7074 | |
| 7075 | case HLUA_E_ETMOUT: |
| 7076 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7077 | rule->arg.hlua_rule->fcn.name); |
| 7078 | goto error; |
| 7079 | |
| 7080 | case HLUA_E_NOMEM: |
| 7081 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7082 | rule->arg.hlua_rule->fcn.name); |
| 7083 | goto error; |
| 7084 | |
| 7085 | case HLUA_E_YIELD: /* unexpected */ |
| 7086 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7087 | rule->arg.hlua_rule->fcn.name); |
| 7088 | goto error; |
| 7089 | |
| 7090 | case HLUA_E_ERR: |
| 7091 | /* Display log. */ |
| 7092 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7093 | rule->arg.hlua_rule->fcn.name); |
| 7094 | goto error; |
| 7095 | |
| 7096 | default: |
| 7097 | goto error; |
| 7098 | } |
| 7099 | } |
| 7100 | |
| 7101 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7102 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7103 | goto done; |
| 7104 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7105 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7106 | goto error; |
| 7107 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7108 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7109 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7110 | si_rx_room_blk(si); |
| 7111 | goto out; |
| 7112 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7113 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7114 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7115 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7116 | } |
| 7117 | |
| 7118 | done: |
| 7119 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7120 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7121 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7122 | si_shutr(si); |
| 7123 | } |
| 7124 | |
| 7125 | /* eat the whole request */ |
| 7126 | if (co_data(req)) { |
| 7127 | req_htx = htx_from_buf(&req->buf); |
| 7128 | co_htx_skip(req, req_htx, co_data(req)); |
| 7129 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7130 | } |
| 7131 | } |
| 7132 | |
| 7133 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7134 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7135 | return; |
| 7136 | |
| 7137 | error: |
| 7138 | |
| 7139 | /* If we are in HTTP mode, and we are not send any |
| 7140 | * data, return a 500 server error in best effort: |
| 7141 | * if there is no room available in the buffer, |
| 7142 | * just close the connection. |
| 7143 | */ |
| 7144 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7145 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7146 | |
| 7147 | channel_erase(res); |
| 7148 | res->buf.data = b_data(err); |
| 7149 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7150 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7151 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7152 | } |
| 7153 | if (!(strm->flags & SF_ERR_MASK)) |
| 7154 | strm->flags |= SF_ERR_RESOURCE; |
| 7155 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7156 | goto done; |
| 7157 | } |
| 7158 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7159 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7160 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7161 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7162 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7163 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7164 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7165 | } |
| 7166 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7167 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7168 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7169 | * |
| 7170 | * This function can fail with an abort() due to an Lua critical error. |
| 7171 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7172 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7173 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7174 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7175 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7176 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7177 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7178 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7179 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7180 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7181 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7182 | if (!rule->arg.hlua_rule) { |
| 7183 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7184 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7185 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7186 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7187 | /* Memory for arguments. */ |
| 7188 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7189 | if (!rule->arg.hlua_rule->args) { |
| 7190 | memprintf(err, "out of memory error"); |
| 7191 | return ACT_RET_PRS_ERR; |
| 7192 | } |
| 7193 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7194 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7195 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7196 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7197 | /* Expect some arguments */ |
| 7198 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7199 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7200 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7201 | return ACT_RET_PRS_ERR; |
| 7202 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7203 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7204 | if (!rule->arg.hlua_rule->args[i]) { |
| 7205 | memprintf(err, "out of memory error"); |
| 7206 | return ACT_RET_PRS_ERR; |
| 7207 | } |
| 7208 | (*cur_arg)++; |
| 7209 | } |
| 7210 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7211 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7212 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7213 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7214 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7215 | } |
| 7216 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7217 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7218 | struct act_rule *rule, char **err) |
| 7219 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7220 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7221 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7222 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7223 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7224 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7225 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7226 | * the call of this analyzer. |
| 7227 | */ |
| 7228 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7229 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7230 | return ACT_RET_PRS_ERR; |
| 7231 | } |
| 7232 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7233 | /* Memory for the rule. */ |
| 7234 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7235 | if (!rule->arg.hlua_rule) { |
| 7236 | memprintf(err, "out of memory error"); |
| 7237 | return ACT_RET_PRS_ERR; |
| 7238 | } |
| 7239 | |
| 7240 | /* Reference the Lua function and store the reference. */ |
| 7241 | rule->arg.hlua_rule->fcn = *fcn; |
| 7242 | |
| 7243 | /* TODO: later accept arguments. */ |
| 7244 | rule->arg.hlua_rule->args = NULL; |
| 7245 | |
| 7246 | /* Add applet pointer in the rule. */ |
| 7247 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7248 | rule->applet.name = fcn->name; |
| 7249 | rule->applet.init = hlua_applet_http_init; |
| 7250 | rule->applet.fct = hlua_applet_http_fct; |
| 7251 | rule->applet.release = hlua_applet_http_release; |
| 7252 | rule->applet.timeout = hlua_timeout_applet; |
| 7253 | |
| 7254 | return ACT_RET_PRS_OK; |
| 7255 | } |
| 7256 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7257 | /* This function is an LUA binding used for registering |
| 7258 | * "sample-conv" functions. It expects a converter name used |
| 7259 | * in the haproxy configuration file, and an LUA function. |
| 7260 | */ |
| 7261 | __LJMP static int hlua_register_action(lua_State *L) |
| 7262 | { |
| 7263 | struct action_kw_list *akl; |
| 7264 | const char *name; |
| 7265 | int ref; |
| 7266 | int len; |
| 7267 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7268 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7269 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7270 | /* Initialise the number of expected arguments at 0. */ |
| 7271 | nargs = 0; |
| 7272 | |
| 7273 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7274 | 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] | 7275 | |
| 7276 | /* First argument : converter name. */ |
| 7277 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7278 | |
| 7279 | /* Second argument : environment. */ |
| 7280 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7281 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7282 | |
| 7283 | /* Third argument : lua function. */ |
| 7284 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7285 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7286 | /* 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] | 7287 | if (lua_gettop(L) >= 4) |
| 7288 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7289 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7290 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7291 | lua_pushnil(L); |
| 7292 | while (lua_next(L, 2) != 0) { |
| 7293 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7294 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7295 | |
| 7296 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7297 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7298 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7299 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7300 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7301 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7302 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7303 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7304 | |
| 7305 | /* Fill fcn. */ |
| 7306 | fcn->name = strdup(name); |
| 7307 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7308 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7309 | fcn->function_ref = ref; |
| 7310 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7311 | /* Set the expected number od arguments. */ |
| 7312 | fcn->nargs = nargs; |
| 7313 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7314 | /* List head */ |
| 7315 | akl->list.n = akl->list.p = NULL; |
| 7316 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7317 | /* action keyword. */ |
| 7318 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7319 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7320 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7321 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7322 | |
| 7323 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7324 | |
| 7325 | akl->kw[0].match_pfx = 0; |
| 7326 | akl->kw[0].private = fcn; |
| 7327 | akl->kw[0].parse = action_register_lua; |
| 7328 | |
| 7329 | /* select the action registering point. */ |
| 7330 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7331 | tcp_req_cont_keywords_register(akl); |
| 7332 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7333 | tcp_res_cont_keywords_register(akl); |
| 7334 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7335 | http_req_keywords_register(akl); |
| 7336 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7337 | http_res_keywords_register(akl); |
| 7338 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7339 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7340 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7341 | "are expected.", lua_tostring(L, -1))); |
| 7342 | |
| 7343 | /* pop the environment string. */ |
| 7344 | lua_pop(L, 1); |
| 7345 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7346 | return ACT_RET_PRS_OK; |
| 7347 | } |
| 7348 | |
| 7349 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7350 | struct act_rule *rule, char **err) |
| 7351 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7352 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7353 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7354 | if (px->mode == PR_MODE_HTTP) { |
| 7355 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7356 | return ACT_RET_PRS_ERR; |
| 7357 | } |
| 7358 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7359 | /* Memory for the rule. */ |
| 7360 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7361 | if (!rule->arg.hlua_rule) { |
| 7362 | memprintf(err, "out of memory error"); |
| 7363 | return ACT_RET_PRS_ERR; |
| 7364 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7365 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7366 | /* Reference the Lua function and store the reference. */ |
| 7367 | rule->arg.hlua_rule->fcn = *fcn; |
| 7368 | |
| 7369 | /* TODO: later accept arguments. */ |
| 7370 | rule->arg.hlua_rule->args = NULL; |
| 7371 | |
| 7372 | /* Add applet pointer in the rule. */ |
| 7373 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7374 | rule->applet.name = fcn->name; |
| 7375 | rule->applet.init = hlua_applet_tcp_init; |
| 7376 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7377 | rule->applet.release = hlua_applet_tcp_release; |
| 7378 | rule->applet.timeout = hlua_timeout_applet; |
| 7379 | |
| 7380 | return 0; |
| 7381 | } |
| 7382 | |
| 7383 | /* This function is an LUA binding used for registering |
| 7384 | * "sample-conv" functions. It expects a converter name used |
| 7385 | * in the haproxy configuration file, and an LUA function. |
| 7386 | */ |
| 7387 | __LJMP static int hlua_register_service(lua_State *L) |
| 7388 | { |
| 7389 | struct action_kw_list *akl; |
| 7390 | const char *name; |
| 7391 | const char *env; |
| 7392 | int ref; |
| 7393 | int len; |
| 7394 | struct hlua_function *fcn; |
| 7395 | |
| 7396 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7397 | |
| 7398 | /* First argument : converter name. */ |
| 7399 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7400 | |
| 7401 | /* Second argument : environment. */ |
| 7402 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7403 | |
| 7404 | /* Third argument : lua function. */ |
| 7405 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7406 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7407 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7408 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7409 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7410 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7411 | fcn = calloc(1, sizeof(*fcn)); |
| 7412 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7413 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7414 | |
| 7415 | /* Fill fcn. */ |
| 7416 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7417 | fcn->name = calloc(1, len); |
| 7418 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7419 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7420 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7421 | fcn->function_ref = ref; |
| 7422 | |
| 7423 | /* List head */ |
| 7424 | akl->list.n = akl->list.p = NULL; |
| 7425 | |
| 7426 | /* converter keyword. */ |
| 7427 | len = strlen("lua.") + strlen(name) + 1; |
| 7428 | akl->kw[0].kw = calloc(1, len); |
| 7429 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7430 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7431 | |
| 7432 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7433 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7434 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7435 | if (strcmp(env, "tcp") == 0) |
| 7436 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7437 | else if (strcmp(env, "http") == 0) |
| 7438 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7439 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7440 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7441 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7442 | |
| 7443 | akl->kw[0].match_pfx = 0; |
| 7444 | akl->kw[0].private = fcn; |
| 7445 | |
| 7446 | /* End of array. */ |
| 7447 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7448 | |
| 7449 | /* Register this new converter */ |
| 7450 | service_keywords_register(akl); |
| 7451 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7452 | return 0; |
| 7453 | } |
| 7454 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7455 | /* This function initialises Lua cli handler. It copies the |
| 7456 | * arguments in the Lua stack and create channel IO objects. |
| 7457 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7458 | 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] | 7459 | { |
| 7460 | struct hlua *hlua; |
| 7461 | struct hlua_function *fcn; |
| 7462 | int i; |
| 7463 | const char *error; |
| 7464 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7465 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7466 | appctx->ctx.hlua_cli.fcn = private; |
| 7467 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7468 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7469 | if (!hlua) { |
| 7470 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7471 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7472 | } |
| 7473 | HLUA_INIT(hlua); |
| 7474 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7475 | |
| 7476 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7477 | * 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] | 7478 | * applet_http. It is absolutely compatible. |
| 7479 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7480 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7481 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7482 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7483 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7484 | } |
| 7485 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7486 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7487 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7488 | |
| 7489 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7490 | if (!hlua_ctx_init(hlua, appctx->ctx.hlua_cli.task, 0)) { |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7491 | 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] | 7492 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7493 | } |
| 7494 | |
| 7495 | /* The following Lua calls can fail. */ |
| 7496 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7497 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7498 | error = lua_tostring(hlua->T, -1); |
| 7499 | else |
| 7500 | error = "critical error"; |
| 7501 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7502 | goto error; |
| 7503 | } |
| 7504 | |
| 7505 | /* Check stack available size. */ |
| 7506 | if (!lua_checkstack(hlua->T, 2)) { |
| 7507 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7508 | goto error; |
| 7509 | } |
| 7510 | |
| 7511 | /* Restore the function in the stack. */ |
| 7512 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7513 | |
| 7514 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7515 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7516 | */ |
| 7517 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7518 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7519 | goto error; |
| 7520 | } |
| 7521 | hlua->nargs = 1; |
| 7522 | |
| 7523 | /* push keywords in the stack. */ |
| 7524 | for (i = 0; *args[i]; i++) { |
| 7525 | /* Check stack available size. */ |
| 7526 | if (!lua_checkstack(hlua->T, 1)) { |
| 7527 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7528 | goto error; |
| 7529 | } |
| 7530 | lua_pushstring(hlua->T, args[i]); |
| 7531 | hlua->nargs++; |
| 7532 | } |
| 7533 | |
| 7534 | /* We must initialize the execution timeouts. */ |
| 7535 | hlua->max_time = hlua_timeout_session; |
| 7536 | |
| 7537 | /* At this point the execution is safe. */ |
| 7538 | RESET_SAFE_LJMP(hlua->T); |
| 7539 | |
| 7540 | /* It's ok */ |
| 7541 | return 0; |
| 7542 | |
| 7543 | /* It's not ok. */ |
| 7544 | error: |
| 7545 | RESET_SAFE_LJMP(hlua->T); |
| 7546 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7547 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7548 | return 1; |
| 7549 | } |
| 7550 | |
| 7551 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7552 | { |
| 7553 | struct hlua *hlua; |
| 7554 | struct stream_interface *si; |
| 7555 | struct hlua_function *fcn; |
| 7556 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7557 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7558 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7559 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7560 | |
| 7561 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7562 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7563 | return 1; |
| 7564 | |
| 7565 | /* Execute the function. */ |
| 7566 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7567 | |
| 7568 | /* finished. */ |
| 7569 | case HLUA_E_OK: |
| 7570 | return 1; |
| 7571 | |
| 7572 | /* yield. */ |
| 7573 | case HLUA_E_AGAIN: |
| 7574 | /* We want write. */ |
| 7575 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7576 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7577 | /* Set the timeout. */ |
| 7578 | if (hlua->wake_time != TICK_ETERNITY) |
| 7579 | task_schedule(hlua->task, hlua->wake_time); |
| 7580 | return 0; |
| 7581 | |
| 7582 | /* finished with error. */ |
| 7583 | case HLUA_E_ERRMSG: |
| 7584 | /* Display log. */ |
| 7585 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7586 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7587 | lua_pop(hlua->T, 1); |
| 7588 | return 1; |
| 7589 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7590 | case HLUA_E_ETMOUT: |
| 7591 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7592 | fcn->name); |
| 7593 | return 1; |
| 7594 | |
| 7595 | case HLUA_E_NOMEM: |
| 7596 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7597 | fcn->name); |
| 7598 | return 1; |
| 7599 | |
| 7600 | case HLUA_E_YIELD: /* unexpected */ |
| 7601 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7602 | fcn->name); |
| 7603 | return 1; |
| 7604 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7605 | case HLUA_E_ERR: |
| 7606 | /* Display log. */ |
| 7607 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7608 | fcn->name); |
| 7609 | return 1; |
| 7610 | |
| 7611 | default: |
| 7612 | return 1; |
| 7613 | } |
| 7614 | |
| 7615 | return 1; |
| 7616 | } |
| 7617 | |
| 7618 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7619 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7620 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7621 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7622 | } |
| 7623 | |
| 7624 | /* This function is an LUA binding used for registering |
| 7625 | * new keywords in the cli. It expects a list of keywords |
| 7626 | * which are the "path". It is limited to 5 keywords. A |
| 7627 | * description of the command, a function to be executed |
| 7628 | * for the parsing and a function for io handlers. |
| 7629 | */ |
| 7630 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7631 | { |
| 7632 | struct cli_kw_list *cli_kws; |
| 7633 | const char *message; |
| 7634 | int ref_io; |
| 7635 | int len; |
| 7636 | struct hlua_function *fcn; |
| 7637 | int index; |
| 7638 | int i; |
| 7639 | |
| 7640 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7641 | |
| 7642 | /* First argument : an array of maximum 5 keywords. */ |
| 7643 | if (!lua_istable(L, 1)) |
| 7644 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7645 | |
| 7646 | /* Second argument : string with contextual message. */ |
| 7647 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7648 | |
| 7649 | /* Third and fourth argument : lua function. */ |
| 7650 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7651 | |
| 7652 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7653 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7654 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7655 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7656 | fcn = calloc(1, sizeof(*fcn)); |
| 7657 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7658 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7659 | |
| 7660 | /* Fill path. */ |
| 7661 | index = 0; |
| 7662 | lua_pushnil(L); |
| 7663 | while(lua_next(L, 1) != 0) { |
| 7664 | if (index >= 5) |
| 7665 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7666 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7667 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7668 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7669 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7670 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7671 | index++; |
| 7672 | lua_pop(L, 1); |
| 7673 | } |
| 7674 | |
| 7675 | /* Copy help message. */ |
| 7676 | cli_kws->kw[0].usage = strdup(message); |
| 7677 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7678 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7679 | |
| 7680 | /* Fill fcn io handler. */ |
| 7681 | len = strlen("<lua.cli>") + 1; |
| 7682 | for (i = 0; i < index; i++) |
| 7683 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7684 | fcn->name = calloc(1, len); |
| 7685 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7686 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7687 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7688 | for (i = 0; i < index; i++) { |
| 7689 | strncat((char *)fcn->name, ".", len); |
| 7690 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7691 | } |
| 7692 | strncat((char *)fcn->name, ">", len); |
| 7693 | fcn->function_ref = ref_io; |
| 7694 | |
| 7695 | /* Fill last entries. */ |
| 7696 | cli_kws->kw[0].private = fcn; |
| 7697 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7698 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7699 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7700 | |
| 7701 | /* Register this new converter */ |
| 7702 | cli_register_kw(cli_kws); |
| 7703 | |
| 7704 | return 0; |
| 7705 | } |
| 7706 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7707 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7708 | struct proxy *defpx, const char *file, int line, |
| 7709 | char **err, unsigned int *timeout) |
| 7710 | { |
| 7711 | const char *error; |
| 7712 | |
| 7713 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7714 | if (error == PARSE_TIME_OVER) { |
| 7715 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7716 | args[1], args[0]); |
| 7717 | return -1; |
| 7718 | } |
| 7719 | else if (error == PARSE_TIME_UNDER) { |
| 7720 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7721 | args[1], args[0]); |
| 7722 | return -1; |
| 7723 | } |
| 7724 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7725 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7726 | return -1; |
| 7727 | } |
| 7728 | return 0; |
| 7729 | } |
| 7730 | |
| 7731 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7732 | struct proxy *defpx, const char *file, int line, |
| 7733 | char **err) |
| 7734 | { |
| 7735 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7736 | file, line, err, &hlua_timeout_session); |
| 7737 | } |
| 7738 | |
| 7739 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7740 | struct proxy *defpx, const char *file, int line, |
| 7741 | char **err) |
| 7742 | { |
| 7743 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7744 | file, line, err, &hlua_timeout_task); |
| 7745 | } |
| 7746 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7747 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7748 | struct proxy *defpx, const char *file, int line, |
| 7749 | char **err) |
| 7750 | { |
| 7751 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7752 | file, line, err, &hlua_timeout_applet); |
| 7753 | } |
| 7754 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7755 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7756 | struct proxy *defpx, const char *file, int line, |
| 7757 | char **err) |
| 7758 | { |
| 7759 | char *error; |
| 7760 | |
| 7761 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7762 | if (*error != '\0') { |
| 7763 | memprintf(err, "%s: invalid number", args[0]); |
| 7764 | return -1; |
| 7765 | } |
| 7766 | return 0; |
| 7767 | } |
| 7768 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7769 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7770 | struct proxy *defpx, const char *file, int line, |
| 7771 | char **err) |
| 7772 | { |
| 7773 | char *error; |
| 7774 | |
| 7775 | if (*(args[1]) == 0) { |
| 7776 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7777 | return -1; |
| 7778 | } |
| 7779 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7780 | if (*error != '\0') { |
| 7781 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7782 | return -1; |
| 7783 | } |
| 7784 | return 0; |
| 7785 | } |
| 7786 | |
| 7787 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7788 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7789 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7790 | * the main lua entry point. |
| 7791 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7792 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7793 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7794 | * |
| 7795 | * In some error case, LUA set an error message in top of the stack. This function |
| 7796 | * 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] | 7797 | * |
| 7798 | * This function can fail with an abort() due to an Lua critical error. |
| 7799 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7800 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7801 | */ |
| 7802 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7803 | struct proxy *defpx, const char *file, int line, |
| 7804 | char **err) |
| 7805 | { |
| 7806 | int error; |
| 7807 | |
| 7808 | /* Just load and compile the file. */ |
| 7809 | error = luaL_loadfile(gL.T, args[1]); |
| 7810 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7811 | memprintf(err, "error in Lua file '%s': %s", args[1], lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7812 | lua_pop(gL.T, 1); |
| 7813 | return -1; |
| 7814 | } |
| 7815 | |
| 7816 | /* If no syntax error where detected, execute the code. */ |
| 7817 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7818 | switch (error) { |
| 7819 | case LUA_OK: |
| 7820 | break; |
| 7821 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7822 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7823 | lua_pop(gL.T, 1); |
| 7824 | return -1; |
| 7825 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7826 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7827 | return -1; |
| 7828 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7829 | memprintf(err, "Lua message handler error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7830 | lua_pop(gL.T, 1); |
| 7831 | return -1; |
| 7832 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7833 | memprintf(err, "Lua garbage collector error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7834 | lua_pop(gL.T, 1); |
| 7835 | return -1; |
| 7836 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7837 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7838 | lua_pop(gL.T, 1); |
| 7839 | return -1; |
| 7840 | } |
| 7841 | |
| 7842 | return 0; |
| 7843 | } |
| 7844 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7845 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7846 | * in the given <ctx>. |
| 7847 | */ |
| 7848 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7849 | { |
| 7850 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7851 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7852 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7853 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7854 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7855 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7856 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7857 | |
| 7858 | return 0; |
| 7859 | } |
| 7860 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7861 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7862 | struct proxy *defpx, const char *file, int line, |
| 7863 | char **err) |
| 7864 | { |
| 7865 | char *path; |
| 7866 | char *type = "path"; |
| 7867 | if (too_many_args(2, args, err, NULL)) { |
| 7868 | return -1; |
| 7869 | } |
| 7870 | |
| 7871 | if (!(*args[1])) { |
| 7872 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7873 | return -1; |
| 7874 | } |
| 7875 | path = args[1]; |
| 7876 | |
| 7877 | if (*args[2]) { |
| 7878 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7879 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7880 | return -1; |
| 7881 | } |
| 7882 | type = args[2]; |
| 7883 | } |
| 7884 | |
| 7885 | return hlua_prepend_path(gL, type, path); |
| 7886 | } |
| 7887 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7888 | /* configuration keywords declaration */ |
| 7889 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7890 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7891 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7892 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7893 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7894 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7895 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7896 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7897 | { 0, NULL, NULL }, |
| 7898 | }}; |
| 7899 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7900 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7901 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7902 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7903 | /* This function can fail with an abort() due to an Lua critical error. |
| 7904 | * We are in the initialisation process of HAProxy, this abort() is |
| 7905 | * tolerated. |
| 7906 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7907 | int hlua_post_init() |
| 7908 | { |
| 7909 | struct hlua_init_function *init; |
| 7910 | const char *msg; |
| 7911 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7912 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7913 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7914 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7915 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7916 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7917 | error = lua_tostring(gL.T, -1); |
| 7918 | else |
| 7919 | error = "critical error"; |
| 7920 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7921 | exit(1); |
| 7922 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7923 | |
| 7924 | #if USE_OPENSSL |
| 7925 | /* Initialize SSL server. */ |
| 7926 | if (socket_ssl.xprt->prepare_srv) { |
| 7927 | int saved_used_backed = global.ssl_used_backend; |
| 7928 | // don't affect maxconn automatic computation |
| 7929 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7930 | global.ssl_used_backend = saved_used_backed; |
| 7931 | } |
| 7932 | #endif |
| 7933 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7934 | hlua_fcn_post_init(gL.T); |
| 7935 | RESET_SAFE_LJMP(gL.T); |
| 7936 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7937 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7938 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7939 | ret = hlua_ctx_resume(&gL, 0); |
| 7940 | switch (ret) { |
| 7941 | case HLUA_E_OK: |
| 7942 | lua_pop(gL.T, -1); |
| 7943 | return 1; |
| 7944 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7945 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7946 | return 0; |
| 7947 | case HLUA_E_ERRMSG: |
| 7948 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7949 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7950 | return 0; |
| 7951 | case HLUA_E_ERR: |
| 7952 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7953 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7954 | return 0; |
| 7955 | } |
| 7956 | } |
| 7957 | return 1; |
| 7958 | } |
| 7959 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7960 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7961 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7962 | * is the previously allocated size or the kind of object in case of a new |
| 7963 | * allocation. <nsize> is the requested new size. |
| 7964 | */ |
| 7965 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7966 | { |
| 7967 | struct hlua_mem_allocator *zone = ud; |
| 7968 | |
| 7969 | if (nsize == 0) { |
| 7970 | /* it's a free */ |
| 7971 | if (ptr) |
| 7972 | zone->allocated -= osize; |
| 7973 | free(ptr); |
| 7974 | return NULL; |
| 7975 | } |
| 7976 | |
| 7977 | if (!ptr) { |
| 7978 | /* it's a new allocation */ |
| 7979 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7980 | return NULL; |
| 7981 | |
| 7982 | ptr = malloc(nsize); |
| 7983 | if (ptr) |
| 7984 | zone->allocated += nsize; |
| 7985 | return ptr; |
| 7986 | } |
| 7987 | |
| 7988 | /* it's a realloc */ |
| 7989 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7990 | return NULL; |
| 7991 | |
| 7992 | ptr = realloc(ptr, nsize); |
| 7993 | if (ptr) |
| 7994 | zone->allocated += nsize - osize; |
| 7995 | return ptr; |
| 7996 | } |
| 7997 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7998 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7999 | * We are in the initialisation process of HAProxy, this abort() is |
| 8000 | * tolerated. |
| 8001 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8002 | void hlua_init(void) |
| 8003 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8004 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8005 | int idx; |
| 8006 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8007 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8008 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8009 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8010 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8011 | struct srv_kw *kw; |
| 8012 | int tmp_error; |
| 8013 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8014 | char *args[] = { /* SSL client configuration. */ |
| 8015 | "ssl", |
| 8016 | "verify", |
| 8017 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8018 | NULL |
| 8019 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8020 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8021 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8022 | /* Init main lua stack. */ |
| 8023 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8024 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8025 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8026 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8027 | hlua_sethlua(&gL); |
| 8028 | gL.Tref = LUA_REFNIL; |
| 8029 | gL.task = NULL; |
| 8030 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8031 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8032 | * the Lua function can fail with an abort. We are in the initialisation |
| 8033 | * process of HAProxy, this abort() is tolerated. |
| 8034 | */ |
| 8035 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8036 | /* Initialise lua. */ |
| 8037 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8038 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8039 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8040 | #ifdef HLUA_PREPEND_PATH |
| 8041 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8042 | #endif |
| 8043 | #ifdef HLUA_PREPEND_CPATH |
| 8044 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8045 | #endif |
| 8046 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8047 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8048 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8049 | /* Set safe environment for the initialisation. */ |
| 8050 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8051 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8052 | error_msg = lua_tostring(gL.T, -1); |
| 8053 | else |
| 8054 | error_msg = "critical error"; |
| 8055 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8056 | exit(1); |
| 8057 | } |
| 8058 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8059 | /* |
| 8060 | * |
| 8061 | * Create "core" object. |
| 8062 | * |
| 8063 | */ |
| 8064 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8065 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8066 | lua_newtable(gL.T); |
| 8067 | |
| 8068 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8069 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8070 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8071 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8072 | /* Register special functions. */ |
| 8073 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8074 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8075 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8076 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8077 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8078 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8079 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8080 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8081 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8082 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8083 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8084 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8085 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8086 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8087 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8088 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8089 | hlua_class_function(gL.T, "log", hlua_log); |
| 8090 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8091 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8092 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8093 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8094 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8095 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8096 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8097 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8098 | |
| 8099 | /* |
| 8100 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8101 | * Create "act" object. |
| 8102 | * |
| 8103 | */ |
| 8104 | |
| 8105 | /* This table entry is the object "act" base. */ |
| 8106 | lua_newtable(gL.T); |
| 8107 | |
| 8108 | /* push action return constants */ |
| 8109 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8110 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8111 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8112 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8113 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8114 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8115 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8116 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8117 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8118 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8119 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8120 | lua_setglobal(gL.T, "act"); |
| 8121 | |
| 8122 | /* |
| 8123 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8124 | * Register class Map |
| 8125 | * |
| 8126 | */ |
| 8127 | |
| 8128 | /* This table entry is the object "Map" base. */ |
| 8129 | lua_newtable(gL.T); |
| 8130 | |
| 8131 | /* register pattern types. */ |
| 8132 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8133 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8134 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8135 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8136 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8137 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8138 | |
| 8139 | /* register constructor. */ |
| 8140 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8141 | |
| 8142 | /* Create and fill the metatable. */ |
| 8143 | lua_newtable(gL.T); |
| 8144 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8145 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8146 | lua_pushstring(gL.T, "__index"); |
| 8147 | lua_newtable(gL.T); |
| 8148 | |
| 8149 | /* Register . */ |
| 8150 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8151 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8152 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8153 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8154 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8155 | /* Register previous table in the registry with reference and named entry. |
| 8156 | * The function hlua_register_metatable() pops the stack, so we |
| 8157 | * previously create a copy of the table. |
| 8158 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8159 | lua_pushvalue(gL.T, -1); /* Copy the -1 entry and push it on the stack. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8160 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8161 | |
| 8162 | /* Assign the metatable to the mai Map object. */ |
| 8163 | lua_setmetatable(gL.T, -2); |
| 8164 | |
| 8165 | /* Set a name to the table. */ |
| 8166 | lua_setglobal(gL.T, "Map"); |
| 8167 | |
| 8168 | /* |
| 8169 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8170 | * Register class Channel |
| 8171 | * |
| 8172 | */ |
| 8173 | |
| 8174 | /* Create and fill the metatable. */ |
| 8175 | lua_newtable(gL.T); |
| 8176 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8177 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8178 | lua_pushstring(gL.T, "__index"); |
| 8179 | lua_newtable(gL.T); |
| 8180 | |
| 8181 | /* Register . */ |
| 8182 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8183 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8184 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8185 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8186 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8187 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8188 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8189 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8190 | hlua_class_function(gL.T, "get_out_len", hlua_channel_get_out_len); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 8191 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8192 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8193 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8194 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8195 | |
| 8196 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8197 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8198 | |
| 8199 | /* |
| 8200 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8201 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8202 | * |
| 8203 | */ |
| 8204 | |
| 8205 | /* Create and fill the metatable. */ |
| 8206 | lua_newtable(gL.T); |
| 8207 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8208 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8209 | lua_pushstring(gL.T, "__index"); |
| 8210 | lua_newtable(gL.T); |
| 8211 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8212 | /* Browse existing fetches and create the associated |
| 8213 | * object method. |
| 8214 | */ |
| 8215 | sf = NULL; |
| 8216 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8217 | |
| 8218 | /* Dont register the keywork if the arguments check function are |
| 8219 | * not safe during the runtime. |
| 8220 | */ |
| 8221 | if ((sf->val_args != NULL) && |
| 8222 | (sf->val_args != val_payload_lv) && |
| 8223 | (sf->val_args != val_hdr)) |
| 8224 | continue; |
| 8225 | |
| 8226 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8227 | * by an underscore. |
| 8228 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8229 | strncpy(trash.area, sf->kw, trash.size); |
| 8230 | trash.area[trash.size - 1] = '\0'; |
| 8231 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8232 | if (*p == '.' || *p == '-' || *p == '+') |
| 8233 | *p = '_'; |
| 8234 | |
| 8235 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8236 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8237 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8238 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8239 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8240 | } |
| 8241 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8242 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8243 | |
| 8244 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8245 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8246 | |
| 8247 | /* |
| 8248 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8249 | * Register class Converters |
| 8250 | * |
| 8251 | */ |
| 8252 | |
| 8253 | /* Create and fill the metatable. */ |
| 8254 | lua_newtable(gL.T); |
| 8255 | |
| 8256 | /* Create and fill the __index entry. */ |
| 8257 | lua_pushstring(gL.T, "__index"); |
| 8258 | lua_newtable(gL.T); |
| 8259 | |
| 8260 | /* Browse existing converters and create the associated |
| 8261 | * object method. |
| 8262 | */ |
| 8263 | sc = NULL; |
| 8264 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8265 | /* Dont register the keywork if the arguments check function are |
| 8266 | * not safe during the runtime. |
| 8267 | */ |
| 8268 | if (sc->val_args != NULL) |
| 8269 | continue; |
| 8270 | |
| 8271 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8272 | * by an underscore. |
| 8273 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8274 | strncpy(trash.area, sc->kw, trash.size); |
| 8275 | trash.area[trash.size - 1] = '\0'; |
| 8276 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8277 | if (*p == '.' || *p == '-' || *p == '+') |
| 8278 | *p = '_'; |
| 8279 | |
| 8280 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8281 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8282 | lua_pushlightuserdata(gL.T, sc); |
| 8283 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8284 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8285 | } |
| 8286 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8287 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8288 | |
| 8289 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8290 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8291 | |
| 8292 | /* |
| 8293 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8294 | * Register class HTTP |
| 8295 | * |
| 8296 | */ |
| 8297 | |
| 8298 | /* Create and fill the metatable. */ |
| 8299 | lua_newtable(gL.T); |
| 8300 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8301 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8302 | lua_pushstring(gL.T, "__index"); |
| 8303 | lua_newtable(gL.T); |
| 8304 | |
| 8305 | /* Register Lua functions. */ |
| 8306 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8307 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8308 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8309 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8310 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8311 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8312 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8313 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8314 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8315 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8316 | |
| 8317 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8318 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8319 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8320 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8321 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8322 | hlua_class_function(gL.T, "res_set_header", hlua_http_res_set_hdr); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 8323 | hlua_class_function(gL.T, "res_set_status", hlua_http_res_set_status); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8324 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8325 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8326 | |
| 8327 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8328 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8329 | |
| 8330 | /* |
| 8331 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8332 | * Register class AppletTCP |
| 8333 | * |
| 8334 | */ |
| 8335 | |
| 8336 | /* Create and fill the metatable. */ |
| 8337 | lua_newtable(gL.T); |
| 8338 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8339 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8340 | lua_pushstring(gL.T, "__index"); |
| 8341 | lua_newtable(gL.T); |
| 8342 | |
| 8343 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8344 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8345 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8346 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8347 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8348 | hlua_class_function(gL.T, "get_priv", hlua_applet_tcp_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 8349 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8350 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8351 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8352 | |
| 8353 | lua_settable(gL.T, -3); |
| 8354 | |
| 8355 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8356 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8357 | |
| 8358 | /* |
| 8359 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8360 | * Register class AppletHTTP |
| 8361 | * |
| 8362 | */ |
| 8363 | |
| 8364 | /* Create and fill the metatable. */ |
| 8365 | lua_newtable(gL.T); |
| 8366 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8367 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8368 | lua_pushstring(gL.T, "__index"); |
| 8369 | lua_newtable(gL.T); |
| 8370 | |
| 8371 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8372 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8373 | hlua_class_function(gL.T, "get_priv", hlua_applet_http_get_priv); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 8374 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8375 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8376 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8377 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8378 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8379 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8380 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8381 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8382 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8383 | |
| 8384 | lua_settable(gL.T, -3); |
| 8385 | |
| 8386 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8387 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8388 | |
| 8389 | /* |
| 8390 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8391 | * Register class TXN |
| 8392 | * |
| 8393 | */ |
| 8394 | |
| 8395 | /* Create and fill the metatable. */ |
| 8396 | lua_newtable(gL.T); |
| 8397 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8398 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8399 | lua_pushstring(gL.T, "__index"); |
| 8400 | lua_newtable(gL.T); |
| 8401 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8402 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8403 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8404 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8405 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8406 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8407 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8408 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8409 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8410 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8411 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8412 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8413 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8414 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8415 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8416 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8417 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8418 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8419 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8420 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8421 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8422 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8423 | |
| 8424 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8425 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8426 | |
| 8427 | /* |
| 8428 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8429 | * Register class reply |
| 8430 | * |
| 8431 | */ |
| 8432 | lua_newtable(gL.T); |
| 8433 | lua_pushstring(gL.T, "__index"); |
| 8434 | lua_newtable(gL.T); |
| 8435 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8436 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8437 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8438 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8439 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8440 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8441 | |
| 8442 | |
| 8443 | /* |
| 8444 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8445 | * Register class Socket |
| 8446 | * |
| 8447 | */ |
| 8448 | |
| 8449 | /* Create and fill the metatable. */ |
| 8450 | lua_newtable(gL.T); |
| 8451 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8452 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8453 | lua_pushstring(gL.T, "__index"); |
| 8454 | lua_newtable(gL.T); |
| 8455 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8456 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8457 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8458 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8459 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8460 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8461 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8462 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8463 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8464 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8465 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8466 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8467 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8468 | lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8469 | |
| 8470 | /* Register the garbage collector entry. */ |
| 8471 | lua_pushstring(gL.T, "__gc"); |
| 8472 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8473 | lua_rawset(gL.T, -3); /* Push the last 2 entries in the table at index -3 */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8474 | |
| 8475 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8476 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8477 | |
| 8478 | /* Proxy and server configuration initialisation. */ |
| 8479 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8480 | init_new_proxy(&socket_proxy); |
| 8481 | socket_proxy.parent = NULL; |
| 8482 | socket_proxy.last_change = now.tv_sec; |
| 8483 | socket_proxy.id = "LUA-SOCKET"; |
| 8484 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8485 | socket_proxy.maxconn = 0; |
| 8486 | socket_proxy.accept = NULL; |
| 8487 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8488 | socket_proxy.srv = NULL; |
| 8489 | socket_proxy.conn_retries = 0; |
| 8490 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8491 | |
| 8492 | /* Init TCP server: unchanged parameters */ |
| 8493 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8494 | socket_tcp.next = NULL; |
| 8495 | socket_tcp.proxy = &socket_proxy; |
| 8496 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8497 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8498 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8499 | socket_tcp.idle_conns = NULL; |
| 8500 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8501 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8502 | socket_tcp.last_change = 0; |
| 8503 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8504 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8505 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8506 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8507 | |
| 8508 | /* XXX: Copy default parameter from default server, |
| 8509 | * but the default server is not initialized. |
| 8510 | */ |
| 8511 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8512 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8513 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8514 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8515 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8516 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8517 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8518 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8519 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8520 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8521 | |
| 8522 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8523 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8524 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8525 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8526 | socket_tcp.check.server = &socket_tcp; |
| 8527 | |
| 8528 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8529 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8530 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8531 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8532 | socket_tcp.agent.server = &socket_tcp; |
| 8533 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8534 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8535 | |
| 8536 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8537 | /* Init TCP server: unchanged parameters */ |
| 8538 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8539 | socket_ssl.next = NULL; |
| 8540 | socket_ssl.proxy = &socket_proxy; |
| 8541 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8542 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8543 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8544 | socket_ssl.idle_conns = NULL; |
| 8545 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8546 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8547 | socket_ssl.last_change = 0; |
| 8548 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8549 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8550 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8551 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8552 | |
| 8553 | /* XXX: Copy default parameter from default server, |
| 8554 | * but the default server is not initialized. |
| 8555 | */ |
| 8556 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8557 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8558 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8559 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8560 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8561 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8562 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8563 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8564 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8565 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8566 | |
| 8567 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8568 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8569 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8570 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8571 | socket_ssl.check.server = &socket_ssl; |
| 8572 | |
| 8573 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8574 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8575 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8576 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8577 | socket_ssl.agent.server = &socket_ssl; |
| 8578 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8579 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8580 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8581 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8582 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8583 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8584 | /* |
| 8585 | * |
| 8586 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8587 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8588 | * features like client certificates and ssl_verify. |
| 8589 | * |
| 8590 | */ |
| 8591 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8592 | if (tmp_error != 0) { |
| 8593 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8594 | abort(); /* This must be never arrives because the command line |
| 8595 | not editable by the user. */ |
| 8596 | } |
| 8597 | idx += kw->skip; |
| 8598 | } |
| 8599 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8600 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8601 | |
| 8602 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8603 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8604 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8605 | static void hlua_register_build_options(void) |
| 8606 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8607 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8608 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8609 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8610 | hap_register_build_opts(ptr, 1); |
| 8611 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8612 | |
| 8613 | INITCALL0(STG_REGISTER, hlua_register_build_options); |