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 | * |
| 114 | * Our main excution point of the Lua is the function lua_resume(). A |
| 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; } |
| 128 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 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; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 193 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 195 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 197 | * because a task may remain alive during all the haproxy execution. |
| 198 | */ |
| 199 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 200 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 201 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 203 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 204 | * it is used for preventing infinite loops. |
| 205 | * |
| 206 | * I test the scheer with an infinite loop containing one incrementation |
| 207 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 208 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 209 | * to one interrupt each 10 000 instructions. |
| 210 | * |
| 211 | * configured | Number of |
| 212 | * instructions | loops executed |
| 213 | * between two | in milions |
| 214 | * forced yields | |
| 215 | * ---------------+--------------- |
| 216 | * 10 | 160 |
| 217 | * 500 | 670 |
| 218 | * 1000 | 680 |
| 219 | * 5000 | 700 |
| 220 | * 7000 | 700 |
| 221 | * 8000 | 700 |
| 222 | * 9000 | 710 <- ceil |
| 223 | * 10000 | 710 |
| 224 | * 100000 | 710 |
| 225 | * 1000000 | 710 |
| 226 | * |
| 227 | */ |
| 228 | static unsigned int hlua_nb_instruction = 10000; |
| 229 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 230 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 231 | * be enforced on any memory allocation. |
| 232 | */ |
| 233 | struct hlua_mem_allocator { |
| 234 | size_t allocated; |
| 235 | size_t limit; |
| 236 | }; |
| 237 | |
| 238 | static struct hlua_mem_allocator hlua_global_allocator; |
| 239 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 240 | /* These functions converts types between HAProxy internal args or |
| 241 | * sample and LUA types. Another function permits to check if the |
| 242 | * LUA stack contains arguments according with an required ARG_T |
| 243 | * format. |
| 244 | */ |
| 245 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 246 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 247 | __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] | 248 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 249 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 250 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 251 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 252 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 253 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 254 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 255 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 256 | do { \ |
| 257 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 258 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 259 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 260 | } while (0) |
| 261 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 262 | /* Used to check an Lua function type in the stack. It creates and |
| 263 | * returns a reference of the function. This function throws an |
| 264 | * error if the rgument is not a "function". |
| 265 | */ |
| 266 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 267 | { |
| 268 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 269 | 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] | 270 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 271 | } |
| 272 | lua_pushvalue(L, argno); |
| 273 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 274 | } |
| 275 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 276 | /* Return the string that is of the top of the stack. */ |
| 277 | const char *hlua_get_top_error_string(lua_State *L) |
| 278 | { |
| 279 | if (lua_gettop(L) < 1) |
| 280 | return "unknown error"; |
| 281 | if (lua_type(L, -1) != LUA_TSTRING) |
| 282 | return "unknown error"; |
| 283 | return lua_tostring(L, -1); |
| 284 | } |
| 285 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 286 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 287 | { |
| 288 | lua_Debug ar; |
| 289 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 290 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 291 | int filled = 0; |
| 292 | |
| 293 | while (lua_getstack(L, level++, &ar)) { |
| 294 | |
| 295 | /* Add separator */ |
| 296 | if (filled) |
| 297 | chunk_appendf(msg, ", "); |
| 298 | filled = 1; |
| 299 | |
| 300 | /* Fill fields: |
| 301 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 302 | * 'l': fills in the field currentline; |
| 303 | * 'n': fills in the field name and namewhat; |
| 304 | * 't': fills in the field istailcall; |
| 305 | */ |
| 306 | lua_getinfo(L, "Slnt", &ar); |
| 307 | |
| 308 | /* Append code localisation */ |
| 309 | if (ar.currentline > 0) |
| 310 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 311 | else |
| 312 | chunk_appendf(msg, "%s ", ar.short_src); |
| 313 | |
| 314 | /* |
| 315 | * Get function name |
| 316 | * |
| 317 | * if namewhat is no empty, name is defined. |
| 318 | * what contains "Lua" for Lua function, "C" for C function, |
| 319 | * or "main" for main code. |
| 320 | */ |
| 321 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 322 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 323 | |
| 324 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 325 | chunk_appendf(msg, "main chunk"); |
| 326 | |
| 327 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 328 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 329 | |
| 330 | else /* nothing left... */ |
| 331 | chunk_appendf(msg, "?"); |
| 332 | |
| 333 | |
| 334 | /* Display tailed call */ |
| 335 | if (ar.istailcall) |
| 336 | chunk_appendf(msg, " ..."); |
| 337 | } |
| 338 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 339 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | /* This function check the number of arguments available in the |
| 344 | * stack. If the number of arguments available is not the same |
| 345 | * then <nb> an error is throwed. |
| 346 | */ |
| 347 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 348 | { |
| 349 | if (lua_gettop(L) == nb) |
| 350 | return; |
| 351 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 352 | } |
| 353 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 354 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 355 | * and the line number where the error is encountered. |
| 356 | */ |
| 357 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 358 | { |
| 359 | va_list argp; |
| 360 | va_start(argp, fmt); |
| 361 | luaL_where(L, 1); |
| 362 | lua_pushvfstring(L, fmt, argp); |
| 363 | va_end(argp); |
| 364 | lua_concat(L, 2); |
| 365 | return 1; |
| 366 | } |
| 367 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 368 | /* This functions is used with sample fetch and converters. It |
| 369 | * converts the HAProxy configuration argument in a lua stack |
| 370 | * values. |
| 371 | * |
| 372 | * It takes an array of "arg", and each entry of the array is |
| 373 | * converted and pushed in the LUA stack. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 376 | { |
| 377 | switch (arg->type) { |
| 378 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | case ARGT_TIME: |
| 380 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 381 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 385 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case ARGT_IPV4: |
| 389 | case ARGT_IPV6: |
| 390 | case ARGT_MSK4: |
| 391 | case ARGT_MSK6: |
| 392 | case ARGT_FE: |
| 393 | case ARGT_BE: |
| 394 | case ARGT_TAB: |
| 395 | case ARGT_SRV: |
| 396 | case ARGT_USR: |
| 397 | case ARGT_MAP: |
| 398 | default: |
| 399 | lua_pushnil(L); |
| 400 | break; |
| 401 | } |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | /* This function take one entrie in an LUA stack at the index "ud", |
| 406 | * and try to convert it in an HAProxy argument entry. This is useful |
| 407 | * with sample fetch wrappers. The input arguments are gived to the |
| 408 | * lua wrapper and converted as arg list by thi function. |
| 409 | */ |
| 410 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 411 | { |
| 412 | switch (lua_type(L, ud)) { |
| 413 | |
| 414 | case LUA_TNUMBER: |
| 415 | case LUA_TBOOLEAN: |
| 416 | arg->type = ARGT_SINT; |
| 417 | arg->data.sint = lua_tointeger(L, ud); |
| 418 | break; |
| 419 | |
| 420 | case LUA_TSTRING: |
| 421 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 422 | 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] | 423 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 424 | arg->data.str.size = arg->data.str.data; |
| 425 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | case LUA_TUSERDATA: |
| 429 | case LUA_TNIL: |
| 430 | case LUA_TTABLE: |
| 431 | case LUA_TFUNCTION: |
| 432 | case LUA_TTHREAD: |
| 433 | case LUA_TLIGHTUSERDATA: |
| 434 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 435 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /* the following functions are used to convert a struct sample |
| 442 | * in Lua type. This useful to convert the return of the |
| 443 | * fetchs or converters. |
| 444 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 445 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 447 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 450 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case SMP_T_BIN: |
| 454 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 455 | 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] | 456 | break; |
| 457 | |
| 458 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 459 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 460 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 461 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 462 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 463 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 464 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 465 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 466 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 467 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 468 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 469 | 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] | 470 | break; |
| 471 | default: |
| 472 | lua_pushnil(L); |
| 473 | break; |
| 474 | } |
| 475 | break; |
| 476 | |
| 477 | case SMP_T_IPV4: |
| 478 | case SMP_T_IPV6: |
| 479 | 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] | 480 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 481 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 482 | 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] | 483 | else |
| 484 | lua_pushnil(L); |
| 485 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | default: |
| 487 | lua_pushnil(L); |
| 488 | break; |
| 489 | } |
| 490 | return 1; |
| 491 | } |
| 492 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | /* the following functions are used to convert a struct sample |
| 494 | * in Lua strings. This is useful to convert the return of the |
| 495 | * fetchs or converters. |
| 496 | */ |
| 497 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 498 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 499 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 500 | |
| 501 | case SMP_T_BIN: |
| 502 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 503 | 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] | 504 | break; |
| 505 | |
| 506 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 507 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 508 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 509 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 510 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 511 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 512 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 513 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 514 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 515 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 516 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 517 | 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] | 518 | break; |
| 519 | default: |
| 520 | lua_pushstring(L, ""); |
| 521 | break; |
| 522 | } |
| 523 | break; |
| 524 | |
| 525 | case SMP_T_SINT: |
| 526 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 527 | case SMP_T_IPV4: |
| 528 | case SMP_T_IPV6: |
| 529 | 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] | 530 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 531 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 532 | 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] | 533 | else |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | default: |
| 537 | lua_pushstring(L, ""); |
| 538 | break; |
| 539 | } |
| 540 | return 1; |
| 541 | } |
| 542 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | /* the following functions are used to convert an Lua type in a |
| 544 | * struct sample. This is useful to provide data from a converter |
| 545 | * to the LUA code. |
| 546 | */ |
| 547 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 548 | { |
| 549 | switch (lua_type(L, ud)) { |
| 550 | |
| 551 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 552 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 553 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | |
| 557 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 558 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 563 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 565 | 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] | 566 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 567 | smp->data.u.str.size = smp->data.u.str.data; |
| 568 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case LUA_TUSERDATA: |
| 572 | case LUA_TNIL: |
| 573 | case LUA_TTABLE: |
| 574 | case LUA_TFUNCTION: |
| 575 | case LUA_TTHREAD: |
| 576 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 577 | case LUA_TNONE: |
| 578 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 579 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 580 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 587 | * 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] | 588 | * 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] | 589 | * |
| 590 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 591 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 593 | __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] | 594 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | { |
| 596 | int min_arg; |
| 597 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 598 | struct proxy *px; |
| 599 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 600 | |
| 601 | idx = 0; |
| 602 | min_arg = ARGM(mask); |
| 603 | mask >>= ARGM_BITS; |
| 604 | |
| 605 | while (1) { |
| 606 | |
| 607 | /* Check oversize. */ |
| 608 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 609 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* Check for mandatory arguments. */ |
| 613 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 614 | if (idx < min_arg) { |
| 615 | |
| 616 | /* If miss other argument than the first one, we return an error. */ |
| 617 | if (idx > 0) |
| 618 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 619 | |
| 620 | /* If first argument have a certain type, some default values |
| 621 | * may be used. See the function smp_resolve_args(). |
| 622 | */ |
| 623 | switch (mask & ARGT_MASK) { |
| 624 | |
| 625 | case ARGT_FE: |
| 626 | if (!(p->cap & PR_CAP_FE)) |
| 627 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 628 | argp[idx].data.prx = p; |
| 629 | argp[idx].type = ARGT_FE; |
| 630 | argp[idx+1].type = ARGT_STOP; |
| 631 | break; |
| 632 | |
| 633 | case ARGT_BE: |
| 634 | if (!(p->cap & PR_CAP_BE)) |
| 635 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 636 | argp[idx].data.prx = p; |
| 637 | argp[idx].type = ARGT_BE; |
| 638 | argp[idx+1].type = ARGT_STOP; |
| 639 | break; |
| 640 | |
| 641 | case ARGT_TAB: |
| 642 | argp[idx].data.prx = p; |
| 643 | argp[idx].type = ARGT_TAB; |
| 644 | argp[idx+1].type = ARGT_STOP; |
| 645 | break; |
| 646 | |
| 647 | default: |
| 648 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 649 | break; |
| 650 | } |
| 651 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* Check for exceed the number of requiered argument. */ |
| 656 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 657 | argp[idx].type != ARGT_STOP) { |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 659 | } |
| 660 | |
| 661 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 662 | argp[idx].type == ARGT_STOP) { |
| 663 | return 0; |
| 664 | } |
| 665 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | /* Convert some argument types. */ |
| 667 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | if (argp[idx].type != ARGT_SINT) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 671 | argp[idx].type = ARGT_SINT; |
| 672 | break; |
| 673 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 674 | case ARGT_TIME: |
| 675 | if (argp[idx].type != ARGT_SINT) |
| 676 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 677 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case ARGT_SIZE: |
| 681 | if (argp[idx].type != ARGT_SINT) |
| 682 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 683 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case ARGT_FE: |
| 687 | if (argp[idx].type != ARGT_STR) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 689 | memcpy(trash.area, argp[idx].data.str.area, |
| 690 | argp[idx].data.str.data); |
| 691 | trash.area[argp[idx].data.str.data] = 0; |
| 692 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 693 | if (!argp[idx].data.prx) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 695 | argp[idx].type = ARGT_FE; |
| 696 | break; |
| 697 | |
| 698 | case ARGT_BE: |
| 699 | if (argp[idx].type != ARGT_STR) |
| 700 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 701 | memcpy(trash.area, argp[idx].data.str.area, |
| 702 | argp[idx].data.str.data); |
| 703 | trash.area[argp[idx].data.str.data] = 0; |
| 704 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | if (!argp[idx].data.prx) |
| 706 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 707 | argp[idx].type = ARGT_BE; |
| 708 | break; |
| 709 | |
| 710 | case ARGT_TAB: |
| 711 | if (argp[idx].type != ARGT_STR) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | memcpy(trash.area, argp[idx].data.str.area, |
| 714 | argp[idx].data.str.data); |
| 715 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 716 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 717 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 718 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 719 | argp[idx].type = ARGT_TAB; |
| 720 | break; |
| 721 | |
| 722 | case ARGT_SRV: |
| 723 | if (argp[idx].type != ARGT_STR) |
| 724 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | memcpy(trash.area, argp[idx].data.str.area, |
| 726 | argp[idx].data.str.data); |
| 727 | trash.area[argp[idx].data.str.data] = 0; |
| 728 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 729 | if (sname) { |
| 730 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 731 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 732 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 733 | if (!px) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 735 | } |
| 736 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 737 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 738 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 740 | argp[idx].data.srv = findserver(px, sname); |
| 741 | if (!argp[idx].data.srv) |
| 742 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 743 | argp[idx].type = ARGT_SRV; |
| 744 | break; |
| 745 | |
| 746 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | memcpy(trash.area, argp[idx].data.str.area, |
| 748 | argp[idx].data.str.data); |
| 749 | trash.area[argp[idx].data.str.data] = 0; |
| 750 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 751 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 752 | argp[idx].type = ARGT_IPV4; |
| 753 | break; |
| 754 | |
| 755 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 756 | memcpy(trash.area, argp[idx].data.str.area, |
| 757 | argp[idx].data.str.data); |
| 758 | trash.area[argp[idx].data.str.data] = 0; |
| 759 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 760 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 761 | argp[idx].type = ARGT_MSK4; |
| 762 | break; |
| 763 | |
| 764 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 765 | memcpy(trash.area, argp[idx].data.str.area, |
| 766 | argp[idx].data.str.data); |
| 767 | trash.area[argp[idx].data.str.data] = 0; |
| 768 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 769 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 770 | argp[idx].type = ARGT_IPV6; |
| 771 | break; |
| 772 | |
| 773 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 774 | memcpy(trash.area, argp[idx].data.str.area, |
| 775 | argp[idx].data.str.data); |
| 776 | trash.area[argp[idx].data.str.data] = 0; |
| 777 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 778 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 779 | argp[idx].type = ARGT_MSK6; |
| 780 | break; |
| 781 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | case ARGT_MAP: |
| 783 | case ARGT_REG: |
| 784 | case ARGT_USR: |
| 785 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 786 | break; |
| 787 | } |
| 788 | |
| 789 | /* Check for type of argument. */ |
| 790 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 791 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 792 | arg_type_names[(mask & ARGT_MASK)], |
| 793 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 794 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 795 | } |
| 796 | |
| 797 | /* Next argument. */ |
| 798 | mask >>= ARGT_BITS; |
| 799 | idx++; |
| 800 | } |
| 801 | } |
| 802 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | /* |
| 804 | * The following functions are used to make correspondance between the the |
| 805 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * |
| 807 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 808 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 809 | */ |
| 810 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 811 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 812 | struct hlua **hlua = lua_getextraspace(L); |
| 813 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 814 | } |
| 815 | static inline void hlua_sethlua(struct hlua *hlua) |
| 816 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 817 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 818 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 821 | /* This function is used to send logs. It try to send on screen (stderr) |
| 822 | * and on the default syslog server. |
| 823 | */ |
| 824 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 825 | { |
| 826 | struct tm tm; |
| 827 | char *p; |
| 828 | |
| 829 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 830 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 831 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 832 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 833 | /* Break the message if exceed the buffer size. */ |
| 834 | *(p-4) = ' '; |
| 835 | *(p-3) = '.'; |
| 836 | *(p-2) = '.'; |
| 837 | *(p-1) = '.'; |
| 838 | break; |
| 839 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 840 | if (isprint(*msg)) |
| 841 | *p = *msg; |
| 842 | else |
| 843 | *p = '.'; |
| 844 | } |
| 845 | *p = '\0'; |
| 846 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 847 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 849 | get_localtime(date.tv_sec, &tm); |
| 850 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | 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] | 852 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 853 | fflush(stderr); |
| 854 | } |
| 855 | } |
| 856 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 857 | /* This function just ensure that the yield will be always |
| 858 | * returned with a timeout and permit to set some flags |
| 859 | */ |
| 860 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 861 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 862 | { |
| 863 | struct hlua *hlua = hlua_gethlua(L); |
| 864 | |
| 865 | /* Set the wake timeout. If timeout is required, we set |
| 866 | * the expiration time. |
| 867 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 868 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 870 | hlua->flags |= flags; |
| 871 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 873 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 876 | /* This function initialises the Lua environment stored in the stream. |
| 877 | * 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] | 878 | * 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] | 879 | * |
| 880 | * This function is particular. it initialises a new Lua thread. If the |
| 881 | * initialisation fails (example: out of memory error), the lua function |
| 882 | * throws an error (longjmp). |
| 883 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 884 | * In some case (at least one), this function can be called from safe |
| 885 | * environement, so we must not initialise it. While the support of |
| 886 | * threads appear, the safe environment set a lock to ensure only one |
| 887 | * Lua execution at a time. If we initialize safe environment in another |
| 888 | * safe environmenet, we have a dead lock. |
| 889 | * |
| 890 | * set "already_safe" true if the context is initialized form safe |
| 891 | * Lua fonction. |
| 892 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 893 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 894 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 895 | * MUST NOT manipulate the created thread stack state, because it is not |
| 896 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 897 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 898 | 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] | 899 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 900 | if (!already_safe) { |
| 901 | if (!SET_SAFE_LJMP(gL.T)) { |
| 902 | lua->Tref = LUA_REFNIL; |
| 903 | return 0; |
| 904 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 905 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 907 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 908 | lua->gc_count = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 909 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 910 | lua->T = lua_newthread(gL.T); |
| 911 | if (!lua->T) { |
| 912 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 913 | if (!already_safe) |
| 914 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 915 | return 0; |
| 916 | } |
| 917 | hlua_sethlua(lua); |
| 918 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 919 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 920 | if (!already_safe) |
| 921 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 922 | return 1; |
| 923 | } |
| 924 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 925 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 926 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 927 | * is not freed. |
| 928 | */ |
| 929 | void hlua_ctx_destroy(struct hlua *lua) |
| 930 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 931 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 932 | return; |
| 933 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 934 | if (!lua->T) |
| 935 | goto end; |
| 936 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 937 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 938 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 940 | if (!SET_SAFE_LJMP(lua->T)) |
| 941 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 942 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 943 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 944 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | if (!SET_SAFE_LJMP(gL.T)) |
| 946 | return; |
| 947 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 948 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 949 | /* Forces a garbage collecting process. If the Lua program is finished |
| 950 | * without error, we run the GC on the thread pointer. Its freed all |
| 951 | * the unused memory. |
| 952 | * If the thread is finnish with an error or is currently yielded, |
| 953 | * it seems that the GC applied on the thread doesn't clean anything, |
| 954 | * so e run the GC on the main thread. |
| 955 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 956 | * the garbage collection. |
| 957 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 958 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 959 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 960 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 961 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 962 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 963 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 964 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 965 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 966 | |
| 967 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 968 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 969 | } |
| 970 | |
| 971 | /* This function is used to restore the Lua context when a coroutine |
| 972 | * fails. This function copy the common memory between old coroutine |
| 973 | * and the new coroutine. The old coroutine is destroyed, and its |
| 974 | * replaced by the new coroutine. |
| 975 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 976 | * as string error message and it is copied in the new stack. |
| 977 | */ |
| 978 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 979 | { |
| 980 | lua_State *T; |
| 981 | int new_ref; |
| 982 | |
| 983 | /* Renew the main LUA stack doesn't have sense. */ |
| 984 | if (lua == &gL) |
| 985 | return 0; |
| 986 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 987 | /* New Lua coroutine. */ |
| 988 | T = lua_newthread(gL.T); |
| 989 | if (!T) |
| 990 | return 0; |
| 991 | |
| 992 | /* Copy last error message. */ |
| 993 | if (keep_msg) |
| 994 | lua_xmove(lua->T, T, 1); |
| 995 | |
| 996 | /* Copy data between the coroutines. */ |
| 997 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 998 | lua_xmove(lua->T, T, 1); |
| 999 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1000 | |
| 1001 | /* Destroy old data. */ |
| 1002 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1003 | |
| 1004 | /* The thread is garbage collected by Lua. */ |
| 1005 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1006 | |
| 1007 | /* Fill the struct with the new coroutine values. */ |
| 1008 | lua->Mref = new_ref; |
| 1009 | lua->T = T; |
| 1010 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1011 | |
| 1012 | /* Set context. */ |
| 1013 | hlua_sethlua(lua); |
| 1014 | |
| 1015 | return 1; |
| 1016 | } |
| 1017 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1018 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1019 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1020 | struct hlua *hlua = hlua_gethlua(L); |
| 1021 | |
| 1022 | /* Lua cannot yield when its returning from a function, |
| 1023 | * so, we can fix the interrupt hook to 1 instruction, |
| 1024 | * expecting that the function is finnished. |
| 1025 | */ |
| 1026 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1027 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1028 | return; |
| 1029 | } |
| 1030 | |
| 1031 | /* restore the interrupt condition. */ |
| 1032 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1033 | |
| 1034 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1035 | * If the state is not yieldable, trying yield causes an error. |
| 1036 | */ |
| 1037 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1038 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1039 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1040 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1041 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1042 | hlua->run_time += now_ms - hlua->start_time; |
| 1043 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1044 | lua_pushfstring(L, "execution timeout"); |
| 1045 | WILL_LJMP(lua_error(L)); |
| 1046 | } |
| 1047 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1048 | /* Update the start time. */ |
| 1049 | hlua->start_time = now_ms; |
| 1050 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1051 | /* Try to interrupt the process at the end of the current |
| 1052 | * unyieldable function. |
| 1053 | */ |
| 1054 | 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] | 1055 | } |
| 1056 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1057 | /* This function start or resumes the Lua stack execution. If the flag |
| 1058 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1059 | * The function return an error. |
| 1060 | * |
| 1061 | * The function can returns 4 values: |
| 1062 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1063 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1064 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1065 | * - 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] | 1066 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * 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] | 1070 | * LUA code. |
| 1071 | */ |
| 1072 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1073 | { |
| 1074 | int ret; |
| 1075 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1076 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1077 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1078 | /* Initialise run time counter. */ |
| 1079 | if (!HLUA_IS_RUNNING(lua)) |
| 1080 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1081 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1082 | /* Lock the whole Lua execution. This lock must be before the |
| 1083 | * label "resume_execution". |
| 1084 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1085 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1086 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1087 | resume_execution: |
| 1088 | |
| 1089 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1090 | * instructions. it is used for preventing infinite loops. |
| 1091 | */ |
| 1092 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1093 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1094 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1095 | HLUA_SET_RUN(lua); |
| 1096 | HLUA_CLR_CTRLYIELD(lua); |
| 1097 | HLUA_CLR_WAKERESWR(lua); |
| 1098 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1099 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1100 | /* Update the start time. */ |
| 1101 | lua->start_time = now_ms; |
| 1102 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1103 | /* Call the function. */ |
| 1104 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1105 | switch (ret) { |
| 1106 | |
| 1107 | case LUA_OK: |
| 1108 | ret = HLUA_E_OK; |
| 1109 | break; |
| 1110 | |
| 1111 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1112 | /* Check if the execution timeout is expired. It it is the case, we |
| 1113 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1114 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1115 | tv_update_date(0, 1); |
| 1116 | lua->run_time += now_ms - lua->start_time; |
| 1117 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1118 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1119 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1120 | break; |
| 1121 | } |
| 1122 | /* Process the forced yield. if the general yield is not allowed or |
| 1123 | * if no task were associated this the current Lua execution |
| 1124 | * coroutine, we resume the execution. Else we want to return in the |
| 1125 | * scheduler and we want to be waked up again, to continue the |
| 1126 | * current Lua execution. So we schedule our own task. |
| 1127 | */ |
| 1128 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1129 | if (!yield_allowed || !lua->task) |
| 1130 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1131 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1132 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1133 | if (!yield_allowed) { |
| 1134 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1135 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | break; |
| 1137 | } |
| 1138 | ret = HLUA_E_AGAIN; |
| 1139 | break; |
| 1140 | |
| 1141 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1142 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1143 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1144 | * because the errors ares the only one mean to return immediately |
| 1145 | * from and lua execution. |
| 1146 | */ |
| 1147 | if (lua->flags & HLUA_EXIT) { |
| 1148 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1149 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1150 | break; |
| 1151 | } |
| 1152 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1153 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1154 | if (!lua_checkstack(lua->T, 1)) { |
| 1155 | ret = HLUA_E_ERR; |
| 1156 | break; |
| 1157 | } |
| 1158 | msg = lua_tostring(lua->T, -1); |
| 1159 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1160 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1161 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1162 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1163 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1165 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | ret = HLUA_E_ERRMSG; |
| 1167 | break; |
| 1168 | |
| 1169 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1170 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1171 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1172 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1173 | break; |
| 1174 | |
| 1175 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1176 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1177 | if (!lua_checkstack(lua->T, 1)) { |
| 1178 | ret = HLUA_E_ERR; |
| 1179 | break; |
| 1180 | } |
| 1181 | msg = lua_tostring(lua->T, -1); |
| 1182 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1183 | lua_pop(lua->T, 1); |
| 1184 | if (msg) |
| 1185 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1186 | else |
| 1187 | lua_pushfstring(lua->T, "message handler error"); |
| 1188 | ret = HLUA_E_ERRMSG; |
| 1189 | break; |
| 1190 | |
| 1191 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1192 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1193 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1194 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1195 | break; |
| 1196 | } |
| 1197 | |
| 1198 | switch (ret) { |
| 1199 | case HLUA_E_AGAIN: |
| 1200 | break; |
| 1201 | |
| 1202 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1203 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1204 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1205 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1206 | break; |
| 1207 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1208 | case HLUA_E_ETMOUT: |
| 1209 | case HLUA_E_NOMEM: |
| 1210 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1211 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1212 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1213 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | hlua_ctx_renew(lua, 0); |
| 1215 | break; |
| 1216 | |
| 1217 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1218 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1219 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1220 | break; |
| 1221 | } |
| 1222 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1223 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1224 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1225 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1226 | return ret; |
| 1227 | } |
| 1228 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1229 | /* This function exit the current code. */ |
| 1230 | __LJMP static int hlua_done(lua_State *L) |
| 1231 | { |
| 1232 | struct hlua *hlua = hlua_gethlua(L); |
| 1233 | |
| 1234 | hlua->flags |= HLUA_EXIT; |
| 1235 | WILL_LJMP(lua_error(L)); |
| 1236 | |
| 1237 | return 0; |
| 1238 | } |
| 1239 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1240 | /* This function is an LUA binding. It provides a function |
| 1241 | * for deleting ACL from a referenced ACL file. |
| 1242 | */ |
| 1243 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1244 | { |
| 1245 | const char *name; |
| 1246 | const char *key; |
| 1247 | struct pat_ref *ref; |
| 1248 | |
| 1249 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1250 | |
| 1251 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1252 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1253 | |
| 1254 | ref = pat_ref_lookup(name); |
| 1255 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1256 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1257 | |
| 1258 | pat_ref_delete(ref, key); |
| 1259 | return 0; |
| 1260 | } |
| 1261 | |
| 1262 | /* This function is an LUA binding. It provides a function |
| 1263 | * for deleting map entry from a referenced map file. |
| 1264 | */ |
| 1265 | static int hlua_del_map(lua_State *L) |
| 1266 | { |
| 1267 | const char *name; |
| 1268 | const char *key; |
| 1269 | struct pat_ref *ref; |
| 1270 | |
| 1271 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1272 | |
| 1273 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1274 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1275 | |
| 1276 | ref = pat_ref_lookup(name); |
| 1277 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1278 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1279 | |
| 1280 | pat_ref_delete(ref, key); |
| 1281 | return 0; |
| 1282 | } |
| 1283 | |
| 1284 | /* This function is an LUA binding. It provides a function |
| 1285 | * for adding ACL pattern from a referenced ACL file. |
| 1286 | */ |
| 1287 | static int hlua_add_acl(lua_State *L) |
| 1288 | { |
| 1289 | const char *name; |
| 1290 | const char *key; |
| 1291 | struct pat_ref *ref; |
| 1292 | |
| 1293 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1294 | |
| 1295 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1296 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1297 | |
| 1298 | ref = pat_ref_lookup(name); |
| 1299 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1300 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1301 | |
| 1302 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1303 | pat_ref_add(ref, key, NULL, NULL); |
| 1304 | return 0; |
| 1305 | } |
| 1306 | |
| 1307 | /* This function is an LUA binding. It provides a function |
| 1308 | * for setting map pattern and sample from a referenced map |
| 1309 | * file. |
| 1310 | */ |
| 1311 | static int hlua_set_map(lua_State *L) |
| 1312 | { |
| 1313 | const char *name; |
| 1314 | const char *key; |
| 1315 | const char *value; |
| 1316 | struct pat_ref *ref; |
| 1317 | |
| 1318 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1319 | |
| 1320 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1321 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1322 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1323 | |
| 1324 | ref = pat_ref_lookup(name); |
| 1325 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1326 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1327 | |
| 1328 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1329 | pat_ref_set(ref, key, value, NULL); |
| 1330 | else |
| 1331 | pat_ref_add(ref, key, value, NULL); |
| 1332 | return 0; |
| 1333 | } |
| 1334 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1335 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1336 | * an integer or user data. This data is associated with a metatable. This |
| 1337 | * metatable have an original version registred in the global context with |
| 1338 | * the name of the object (_G[<name>] = <metable> ). |
| 1339 | * |
| 1340 | * A metable is a table that modify the standard behavior of a standard |
| 1341 | * access to the associated data. The entries of this new metatable are |
| 1342 | * defined as is: |
| 1343 | * |
| 1344 | * http://lua-users.org/wiki/MetatableEvents |
| 1345 | * |
| 1346 | * __index |
| 1347 | * |
| 1348 | * we access an absent field in a table, the result is nil. This is |
| 1349 | * true, but it is not the whole truth. Actually, such access triggers |
| 1350 | * the interpreter to look for an __index metamethod: If there is no |
| 1351 | * such method, as usually happens, then the access results in nil; |
| 1352 | * otherwise, the metamethod will provide the result. |
| 1353 | * |
| 1354 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1355 | * the key does not appear in the table, but the metatable has an __index |
| 1356 | * property: |
| 1357 | * |
| 1358 | * - if the value is a function, the function is called, passing in the |
| 1359 | * table and the key; the return value of that function is returned as |
| 1360 | * the result. |
| 1361 | * |
| 1362 | * - if the value is another table, the value of the key in that table is |
| 1363 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1364 | * table's metatable has an __index property, then it continues on up) |
| 1365 | * |
| 1366 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1367 | * |
| 1368 | * http://www.lua.org/pil/13.4.1.html |
| 1369 | * |
| 1370 | * __newindex |
| 1371 | * |
| 1372 | * Like __index, but control property assignment. |
| 1373 | * |
| 1374 | * __mode - Control weak references. A string value with one or both |
| 1375 | * of the characters 'k' and 'v' which specifies that the the |
| 1376 | * keys and/or values in the table are weak references. |
| 1377 | * |
| 1378 | * __call - Treat a table like a function. When a table is followed by |
| 1379 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1380 | * a __call key pointing to a function, that function is invoked |
| 1381 | * (passing any specified arguments) and the return value is |
| 1382 | * returned. |
| 1383 | * |
| 1384 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1385 | * called, if the metatable for myTable has a __metatable |
| 1386 | * key, the value of that key is returned instead of the |
| 1387 | * actual metatable. |
| 1388 | * |
| 1389 | * __tostring - Control string representation. When the builtin |
| 1390 | * "tostring( myTable )" function is called, if the metatable |
| 1391 | * for myTable has a __tostring property set to a function, |
| 1392 | * that function is invoked (passing myTable to it) and the |
| 1393 | * return value is used as the string representation. |
| 1394 | * |
| 1395 | * __len - Control table length. When the table length is requested using |
| 1396 | * the length operator ( '#' ), if the metatable for myTable has |
| 1397 | * a __len key pointing to a function, that function is invoked |
| 1398 | * (passing myTable to it) and the return value used as the value |
| 1399 | * of "#myTable". |
| 1400 | * |
| 1401 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1402 | * collected, if the metatable has a __gc field pointing to a |
| 1403 | * function, that function is first invoked, passing the userdata |
| 1404 | * to it. The __gc metamethod is not called for tables. |
| 1405 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1406 | * |
| 1407 | * Special metamethods for redefining standard operators: |
| 1408 | * http://www.lua.org/pil/13.1.html |
| 1409 | * |
| 1410 | * __add "+" |
| 1411 | * __sub "-" |
| 1412 | * __mul "*" |
| 1413 | * __div "/" |
| 1414 | * __unm "!" |
| 1415 | * __pow "^" |
| 1416 | * __concat ".." |
| 1417 | * |
| 1418 | * Special methods for redfining standar relations |
| 1419 | * http://www.lua.org/pil/13.2.html |
| 1420 | * |
| 1421 | * __eq "==" |
| 1422 | * __lt "<" |
| 1423 | * __le "<=" |
| 1424 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1425 | |
| 1426 | /* |
| 1427 | * |
| 1428 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1429 | * Class Map |
| 1430 | * |
| 1431 | * |
| 1432 | */ |
| 1433 | |
| 1434 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1435 | * a class session, otherwise it throws an error. |
| 1436 | */ |
| 1437 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1438 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1439 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1440 | } |
| 1441 | |
| 1442 | /* This function is the map constructor. It don't need |
| 1443 | * the class Map object. It creates and return a new Map |
| 1444 | * object. It must be called only during "body" or "init" |
| 1445 | * context because it process some filesystem accesses. |
| 1446 | */ |
| 1447 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1448 | { |
| 1449 | const char *fn; |
| 1450 | int match = PAT_MATCH_STR; |
| 1451 | struct sample_conv conv; |
| 1452 | const char *file = ""; |
| 1453 | int line = 0; |
| 1454 | lua_Debug ar; |
| 1455 | char *err = NULL; |
| 1456 | struct arg args[2]; |
| 1457 | |
| 1458 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1459 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1460 | |
| 1461 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1462 | |
| 1463 | if (lua_gettop(L) >= 2) { |
| 1464 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1465 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1466 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1467 | } |
| 1468 | |
| 1469 | /* Get Lua filename and line number. */ |
| 1470 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1471 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1472 | if (ar.currentline > 0) { /* is there info? */ |
| 1473 | file = ar.short_src; |
| 1474 | line = ar.currentline; |
| 1475 | } |
| 1476 | } |
| 1477 | |
| 1478 | /* fill fake sample_conv struct. */ |
| 1479 | conv.kw = ""; /* unused. */ |
| 1480 | conv.process = NULL; /* unused. */ |
| 1481 | conv.arg_mask = 0; /* unused. */ |
| 1482 | conv.val_args = NULL; /* unused. */ |
| 1483 | conv.out_type = SMP_T_STR; |
| 1484 | conv.private = (void *)(long)match; |
| 1485 | switch (match) { |
| 1486 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1487 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1488 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1489 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1490 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1491 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1492 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1493 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1494 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1495 | default: |
| 1496 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1497 | } |
| 1498 | |
| 1499 | /* fill fake args. */ |
| 1500 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1501 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1502 | args[1].type = ARGT_STOP; |
| 1503 | |
| 1504 | /* load the map. */ |
| 1505 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1506 | /* error case: we cant use luaL_error because we must |
| 1507 | * free the err variable. |
| 1508 | */ |
| 1509 | luaL_where(L, 1); |
| 1510 | lua_pushfstring(L, "'new': %s.", err); |
| 1511 | lua_concat(L, 2); |
| 1512 | free(err); |
| 1513 | WILL_LJMP(lua_error(L)); |
| 1514 | } |
| 1515 | |
| 1516 | /* create the lua object. */ |
| 1517 | lua_newtable(L); |
| 1518 | lua_pushlightuserdata(L, args[0].data.map); |
| 1519 | lua_rawseti(L, -2, 0); |
| 1520 | |
| 1521 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1522 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1523 | lua_setmetatable(L, -2); |
| 1524 | |
| 1525 | |
| 1526 | return 1; |
| 1527 | } |
| 1528 | |
| 1529 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1530 | { |
| 1531 | struct map_descriptor *desc; |
| 1532 | struct pattern *pat; |
| 1533 | struct sample smp; |
| 1534 | |
| 1535 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1536 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1537 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1538 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1539 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1540 | } |
| 1541 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1542 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1543 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1544 | 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] | 1545 | } |
| 1546 | |
| 1547 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1548 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1549 | if (str) |
| 1550 | lua_pushstring(L, ""); |
| 1551 | else |
| 1552 | lua_pushnil(L); |
| 1553 | return 1; |
| 1554 | } |
| 1555 | |
| 1556 | /* 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] | 1557 | 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] | 1558 | return 1; |
| 1559 | } |
| 1560 | |
| 1561 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1562 | { |
| 1563 | return _hlua_map_lookup(L, 0); |
| 1564 | } |
| 1565 | |
| 1566 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1567 | { |
| 1568 | return _hlua_map_lookup(L, 1); |
| 1569 | } |
| 1570 | |
| 1571 | /* |
| 1572 | * |
| 1573 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1574 | * Class Socket |
| 1575 | * |
| 1576 | * |
| 1577 | */ |
| 1578 | |
| 1579 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1580 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1581 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1582 | } |
| 1583 | |
| 1584 | /* 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] | 1585 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | * received. |
| 1587 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1588 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1589 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1590 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1591 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1592 | if (appctx->ctx.hlua_cosocket.die) { |
| 1593 | si_shutw(si); |
| 1594 | si_shutr(si); |
| 1595 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1596 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1597 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1598 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1599 | } |
| 1600 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1601 | /* If we cant write, wakeup the pending write signals. */ |
| 1602 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1603 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1604 | |
| 1605 | /* If we cant read, wakeup the pending read signals. */ |
| 1606 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1607 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1609 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1610 | * to be notified whenever the connection completes. |
| 1611 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1612 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1613 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1614 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1615 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1616 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1617 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1618 | |
| 1619 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1620 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1621 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1622 | /* 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] | 1623 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1624 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
| 1626 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1627 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1628 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1629 | |
| 1630 | /* Some data were injected in the buffer, notify the stream |
| 1631 | * interface. |
| 1632 | */ |
| 1633 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1634 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1635 | |
| 1636 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1637 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1638 | */ |
| 1639 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1640 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1641 | } |
| 1642 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1643 | /* This function is called when the "struct stream" is destroyed. |
| 1644 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | * Wake all the pending signals. |
| 1646 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1647 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1648 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1649 | struct xref *peer; |
| 1650 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1651 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1652 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1653 | if (peer) |
| 1654 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | |
| 1656 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1657 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1658 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | } |
| 1660 | |
| 1661 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1662 | * uses this object. If the stream does not exists, just quit. |
| 1663 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1664 | * pending signal can rest in the read and write lists. destroy |
| 1665 | * it. |
| 1666 | */ |
| 1667 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1668 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1669 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1670 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1671 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1672 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1673 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1674 | |
| 1675 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1676 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1677 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1678 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1679 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1680 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1681 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1682 | appctx->ctx.hlua_cosocket.die = 1; |
| 1683 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1685 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1686 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1687 | return 0; |
| 1688 | } |
| 1689 | |
| 1690 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1691 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1692 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1693 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1694 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1695 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1697 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1698 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1699 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1700 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1701 | |
| 1702 | /* Check if we run on the same thread than the xreator thread. |
| 1703 | * We cannot access to the socket if the thread is different. |
| 1704 | */ |
| 1705 | if (socket->tid != tid) |
| 1706 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1707 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1708 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1709 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1711 | |
| 1712 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1713 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1714 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1715 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1716 | appctx->ctx.hlua_cosocket.die = 1; |
| 1717 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1718 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1719 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1720 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | return 0; |
| 1722 | } |
| 1723 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1724 | /* The close function calls close_helper. |
| 1725 | */ |
| 1726 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1727 | { |
| 1728 | MAY_LJMP(check_args(L, 1, "close")); |
| 1729 | return hlua_socket_close_helper(L); |
| 1730 | } |
| 1731 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | /* This Lua function assumes that the stack contain three parameters. |
| 1733 | * 1 - USERDATA containing a struct socket |
| 1734 | * 2 - INTEGER with values of the macro defined below |
| 1735 | * If the integer is -1, we must read at most one line. |
| 1736 | * If the integer is -2, we ust read all the data until the |
| 1737 | * end of the stream. |
| 1738 | * If the integer is positive value, we must read a number of |
| 1739 | * bytes corresponding to this value. |
| 1740 | */ |
| 1741 | #define HLSR_READ_LINE (-1) |
| 1742 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1743 | __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] | 1744 | { |
| 1745 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1746 | int wanted = lua_tointeger(L, 2); |
| 1747 | struct hlua *hlua = hlua_gethlua(L); |
| 1748 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1749 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1750 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1751 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1752 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1753 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1754 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1755 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1756 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1757 | struct stream_interface *si; |
| 1758 | struct stream *s; |
| 1759 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1760 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1761 | |
| 1762 | /* Check if this lua stack is schedulable. */ |
| 1763 | if (!hlua || !hlua->task) |
| 1764 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1765 | "'frontend', 'backend' or 'task'")); |
| 1766 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1767 | /* Check if we run on the same thread than the xreator thread. |
| 1768 | * We cannot access to the socket if the thread is different. |
| 1769 | */ |
| 1770 | if (socket->tid != tid) |
| 1771 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1772 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1773 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1774 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1775 | if (!peer) |
| 1776 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1777 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1778 | si = appctx->owner; |
| 1779 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1780 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1782 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1784 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1785 | if (nblk < 0) /* Connection close. */ |
| 1786 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1787 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1789 | |
| 1790 | /* remove final \r\n. */ |
| 1791 | if (nblk == 1) { |
| 1792 | if (blk1[len1-1] == '\n') { |
| 1793 | len1--; |
| 1794 | skip_at_end++; |
| 1795 | if (blk1[len1-1] == '\r') { |
| 1796 | len1--; |
| 1797 | skip_at_end++; |
| 1798 | } |
| 1799 | } |
| 1800 | } |
| 1801 | else { |
| 1802 | if (blk2[len2-1] == '\n') { |
| 1803 | len2--; |
| 1804 | skip_at_end++; |
| 1805 | if (blk2[len2-1] == '\r') { |
| 1806 | len2--; |
| 1807 | skip_at_end++; |
| 1808 | } |
| 1809 | } |
| 1810 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1811 | } |
| 1812 | |
| 1813 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1814 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1815 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1816 | if (nblk < 0) /* Connection close. */ |
| 1817 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1818 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | goto connection_empty; |
| 1820 | } |
| 1821 | |
| 1822 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1823 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1824 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | if (nblk < 0) /* Connection close. */ |
| 1826 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1827 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1828 | goto connection_empty; |
| 1829 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1830 | missing_bytes = wanted - socket->b.n; |
| 1831 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1832 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1833 | len1 = missing_bytes; |
| 1834 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1835 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | } |
| 1837 | |
| 1838 | len = len1; |
| 1839 | |
| 1840 | luaL_addlstring(&socket->b, blk1, len1); |
| 1841 | if (nblk == 2) { |
| 1842 | len += len2; |
| 1843 | luaL_addlstring(&socket->b, blk2, len2); |
| 1844 | } |
| 1845 | |
| 1846 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1847 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1848 | |
| 1849 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1850 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1851 | |
| 1852 | /* If the pattern reclaim to read all the data |
| 1853 | * in the connection, got out. |
| 1854 | */ |
| 1855 | if (wanted == HLSR_READ_ALL) |
| 1856 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1857 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1858 | goto connection_empty; |
| 1859 | |
| 1860 | /* Return result. */ |
| 1861 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1862 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1863 | return 1; |
| 1864 | |
| 1865 | connection_closed: |
| 1866 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1867 | xref_unlock(&socket->xref, peer); |
| 1868 | |
| 1869 | no_peer: |
| 1870 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | /* If the buffer containds data. */ |
| 1872 | if (socket->b.n > 0) { |
| 1873 | luaL_pushresult(&socket->b); |
| 1874 | return 1; |
| 1875 | } |
| 1876 | lua_pushnil(L); |
| 1877 | lua_pushstring(L, "connection closed."); |
| 1878 | return 2; |
| 1879 | |
| 1880 | connection_empty: |
| 1881 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1882 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1883 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1884 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1885 | } |
| 1886 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1887 | 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] | 1888 | return 0; |
| 1889 | } |
| 1890 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1891 | /* This Lua function gets two parameters. The first one can be string |
| 1892 | * or a number. If the string is "*l", the user requires one line. If |
| 1893 | * 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] | 1894 | * If the value is a number, the user require a number of bytes equal |
| 1895 | * to the value. The default value is "*l" (a line). |
| 1896 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1897 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1898 | * integer takes this values: |
| 1899 | * -1 : read a line |
| 1900 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1901 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1902 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1903 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1904 | * concatenated with the read data. |
| 1905 | */ |
| 1906 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1907 | { |
| 1908 | int wanted = HLSR_READ_LINE; |
| 1909 | const char *pattern; |
| 1910 | int type; |
| 1911 | char *error; |
| 1912 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1913 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1914 | |
| 1915 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1916 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1917 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1918 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1919 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1920 | /* Check if we run on the same thread than the xreator thread. |
| 1921 | * We cannot access to the socket if the thread is different. |
| 1922 | */ |
| 1923 | if (socket->tid != tid) |
| 1924 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1925 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1926 | /* check for pattern. */ |
| 1927 | if (lua_gettop(L) >= 2) { |
| 1928 | type = lua_type(L, 2); |
| 1929 | if (type == LUA_TSTRING) { |
| 1930 | pattern = lua_tostring(L, 2); |
| 1931 | if (strcmp(pattern, "*a") == 0) |
| 1932 | wanted = HLSR_READ_ALL; |
| 1933 | else if (strcmp(pattern, "*l") == 0) |
| 1934 | wanted = HLSR_READ_LINE; |
| 1935 | else { |
| 1936 | wanted = strtoll(pattern, &error, 10); |
| 1937 | if (*error != '\0') |
| 1938 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1939 | } |
| 1940 | } |
| 1941 | else if (type == LUA_TNUMBER) { |
| 1942 | wanted = lua_tointeger(L, 2); |
| 1943 | if (wanted < 0) |
| 1944 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1945 | } |
| 1946 | } |
| 1947 | |
| 1948 | /* Set pattern. */ |
| 1949 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1950 | |
| 1951 | /* Check if we would replace the top by itself. */ |
| 1952 | if (lua_gettop(L) != 2) |
| 1953 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1954 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1955 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1956 | luaL_buffinit(L, &socket->b); |
| 1957 | |
| 1958 | /* Check prefix. */ |
| 1959 | if (lua_gettop(L) >= 3) { |
| 1960 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1961 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1962 | pattern = lua_tolstring(L, 3, &len); |
| 1963 | luaL_addlstring(&socket->b, pattern, len); |
| 1964 | } |
| 1965 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1966 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | } |
| 1968 | |
| 1969 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1970 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1971 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1972 | 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] | 1973 | { |
| 1974 | struct hlua_socket *socket; |
| 1975 | struct hlua *hlua = hlua_gethlua(L); |
| 1976 | struct appctx *appctx; |
| 1977 | size_t buf_len; |
| 1978 | const char *buf; |
| 1979 | int len; |
| 1980 | int send_len; |
| 1981 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1982 | struct xref *peer; |
| 1983 | struct stream_interface *si; |
| 1984 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1985 | |
| 1986 | /* Check if this lua stack is schedulable. */ |
| 1987 | if (!hlua || !hlua->task) |
| 1988 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1989 | "'frontend', 'backend' or 'task'")); |
| 1990 | |
| 1991 | /* Get object */ |
| 1992 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1993 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1994 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1995 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1996 | /* Check if we run on the same thread than the xreator thread. |
| 1997 | * We cannot access to the socket if the thread is different. |
| 1998 | */ |
| 1999 | if (socket->tid != tid) |
| 2000 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2001 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2002 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2003 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2004 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2005 | lua_pushinteger(L, -1); |
| 2006 | return 1; |
| 2007 | } |
| 2008 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2009 | si = appctx->owner; |
| 2010 | s = si_strm(si); |
| 2011 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2012 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2015 | lua_pushinteger(L, -1); |
| 2016 | return 1; |
| 2017 | } |
| 2018 | |
| 2019 | /* Update the input buffer data. */ |
| 2020 | buf += sent; |
| 2021 | send_len = buf_len - sent; |
| 2022 | |
| 2023 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2024 | if (sent >= buf_len) { |
| 2025 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2027 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2028 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2029 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2030 | * the request buffer if its not required. |
| 2031 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2032 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2033 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2034 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2035 | } |
| 2036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2037 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2038 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2039 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2040 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2041 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2042 | |
| 2043 | /* send data */ |
| 2044 | if (len < send_len) |
| 2045 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2046 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2047 | |
| 2048 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2049 | * the data" (-2) are not expected because the available length |
| 2050 | * is tested. |
| 2051 | * Other unknown error are also not expected. |
| 2052 | */ |
| 2053 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2054 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2055 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2056 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2057 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2059 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2060 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2061 | return 1; |
| 2062 | } |
| 2063 | |
| 2064 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2065 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2066 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2067 | s->req.rex = TICK_ETERNITY; |
| 2068 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | |
| 2070 | /* Update length sent. */ |
| 2071 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2072 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2073 | |
| 2074 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2075 | if (sent + len >= buf_len) { |
| 2076 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2078 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2079 | |
| 2080 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2081 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2082 | xref_unlock(&socket->xref, peer); |
| 2083 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2084 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2085 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2086 | 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] | 2087 | return 0; |
| 2088 | } |
| 2089 | |
| 2090 | /* This function initiate the send of data. It just check the input |
| 2091 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2092 | * 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] | 2093 | * "hlua_socket_write_yield" that can yield. |
| 2094 | * |
| 2095 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2096 | * the associated object. The second is a string buffer. The third is |
| 2097 | * a facultative integer that represents where is the buffer position |
| 2098 | * of the start of the data that can send. The first byte is the |
| 2099 | * position "1". The default value is "1". The fourth argument is a |
| 2100 | * facultative integer that represents where is the buffer position |
| 2101 | * of the end of the data that can send. The default is the last byte. |
| 2102 | */ |
| 2103 | static int hlua_socket_send(struct lua_State *L) |
| 2104 | { |
| 2105 | int i; |
| 2106 | int j; |
| 2107 | const char *buf; |
| 2108 | size_t buf_len; |
| 2109 | |
| 2110 | /* Check number of arguments. */ |
| 2111 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2112 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2113 | |
| 2114 | /* Get the string. */ |
| 2115 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2116 | |
| 2117 | /* Get and check j. */ |
| 2118 | if (lua_gettop(L) == 4) { |
| 2119 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2120 | if (j < 0) |
| 2121 | j = buf_len + j + 1; |
| 2122 | if (j > buf_len) |
| 2123 | j = buf_len + 1; |
| 2124 | lua_pop(L, 1); |
| 2125 | } |
| 2126 | else |
| 2127 | j = buf_len; |
| 2128 | |
| 2129 | /* Get and check i. */ |
| 2130 | if (lua_gettop(L) == 3) { |
| 2131 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2132 | if (i < 0) |
| 2133 | i = buf_len + i + 1; |
| 2134 | if (i > buf_len) |
| 2135 | i = buf_len + 1; |
| 2136 | lua_pop(L, 1); |
| 2137 | } else |
| 2138 | i = 1; |
| 2139 | |
| 2140 | /* Check bth i and j. */ |
| 2141 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2142 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2143 | return 1; |
| 2144 | } |
| 2145 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2146 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2147 | return 1; |
| 2148 | } |
| 2149 | if (i == 0) |
| 2150 | i = 1; |
| 2151 | if (j == 0) |
| 2152 | j = 1; |
| 2153 | |
| 2154 | /* Pop the string. */ |
| 2155 | lua_pop(L, 1); |
| 2156 | |
| 2157 | /* Update the buffer length. */ |
| 2158 | buf += i - 1; |
| 2159 | buf_len = j - i + 1; |
| 2160 | lua_pushlstring(L, buf, buf_len); |
| 2161 | |
| 2162 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2163 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2164 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2165 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2166 | } |
| 2167 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2168 | #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] | 2169 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2170 | { |
| 2171 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2172 | int ret; |
| 2173 | int len; |
| 2174 | char *p; |
| 2175 | |
| 2176 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2177 | if (ret <= 0) { |
| 2178 | lua_pushnil(L); |
| 2179 | return 1; |
| 2180 | } |
| 2181 | |
| 2182 | if (ret == AF_UNIX) { |
| 2183 | lua_pushstring(L, buffer+1); |
| 2184 | return 1; |
| 2185 | } |
| 2186 | else if (ret == AF_INET6) { |
| 2187 | buffer[0] = '['; |
| 2188 | len = strlen(buffer); |
| 2189 | buffer[len] = ']'; |
| 2190 | len++; |
| 2191 | buffer[len] = ':'; |
| 2192 | len++; |
| 2193 | p = buffer; |
| 2194 | } |
| 2195 | else if (ret == AF_INET) { |
| 2196 | p = buffer + 1; |
| 2197 | len = strlen(p); |
| 2198 | p[len] = ':'; |
| 2199 | len++; |
| 2200 | } |
| 2201 | else { |
| 2202 | lua_pushnil(L); |
| 2203 | return 1; |
| 2204 | } |
| 2205 | |
| 2206 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2207 | lua_pushnil(L); |
| 2208 | return 1; |
| 2209 | } |
| 2210 | |
| 2211 | lua_pushstring(L, p); |
| 2212 | return 1; |
| 2213 | } |
| 2214 | |
| 2215 | /* Returns information about the peer of the connection. */ |
| 2216 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2217 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2218 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2219 | struct xref *peer; |
| 2220 | struct appctx *appctx; |
| 2221 | struct stream_interface *si; |
| 2222 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2223 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2224 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2225 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2226 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2227 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2228 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2229 | /* Check if we run on the same thread than the xreator thread. |
| 2230 | * We cannot access to the socket if the thread is different. |
| 2231 | */ |
| 2232 | if (socket->tid != tid) |
| 2233 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2234 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2235 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2236 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2237 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2238 | lua_pushnil(L); |
| 2239 | return 1; |
| 2240 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2241 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2242 | si = appctx->owner; |
| 2243 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2244 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2245 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2246 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2247 | lua_pushnil(L); |
| 2248 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | } |
| 2250 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2251 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2252 | xref_unlock(&socket->xref, peer); |
| 2253 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2254 | } |
| 2255 | |
| 2256 | /* Returns information about my connection side. */ |
| 2257 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2258 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2259 | struct hlua_socket *socket; |
| 2260 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2261 | struct appctx *appctx; |
| 2262 | struct xref *peer; |
| 2263 | struct stream_interface *si; |
| 2264 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2265 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2266 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2267 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2268 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2269 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2271 | /* Check if we run on the same thread than the xreator thread. |
| 2272 | * We cannot access to the socket if the thread is different. |
| 2273 | */ |
| 2274 | if (socket->tid != tid) |
| 2275 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2276 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2277 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2278 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2279 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2280 | lua_pushnil(L); |
| 2281 | return 1; |
| 2282 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2283 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2284 | si = appctx->owner; |
| 2285 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2287 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2288 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2289 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2290 | lua_pushnil(L); |
| 2291 | return 1; |
| 2292 | } |
| 2293 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2294 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2295 | xref_unlock(&socket->xref, peer); |
| 2296 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | } |
| 2298 | |
| 2299 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2300 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | .obj_type = OBJ_TYPE_APPLET, |
| 2302 | .name = "<LUA_TCP>", |
| 2303 | .fct = hlua_socket_handler, |
| 2304 | .release = hlua_socket_release, |
| 2305 | }; |
| 2306 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2307 | __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] | 2308 | { |
| 2309 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2310 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2311 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2313 | struct stream_interface *si; |
| 2314 | struct stream *s; |
| 2315 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2316 | /* Check if we run on the same thread than the xreator thread. |
| 2317 | * We cannot access to the socket if the thread is different. |
| 2318 | */ |
| 2319 | if (socket->tid != tid) |
| 2320 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2321 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2323 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2324 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | lua_pushnil(L); |
| 2326 | lua_pushstring(L, "Can't connect"); |
| 2327 | return 2; |
| 2328 | } |
| 2329 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2330 | si = appctx->owner; |
| 2331 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2332 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2333 | /* Check if we run on the same thread than the xreator thread. |
| 2334 | * We cannot access to the socket if the thread is different. |
| 2335 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2336 | if (socket->tid != tid) { |
| 2337 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2338 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2339 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2340 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2341 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2342 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2343 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2344 | lua_pushnil(L); |
| 2345 | lua_pushstring(L, "Can't connect"); |
| 2346 | return 2; |
| 2347 | } |
| 2348 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2349 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2350 | |
| 2351 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2352 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2353 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | lua_pushinteger(L, 1); |
| 2355 | return 1; |
| 2356 | } |
| 2357 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2360 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | } |
| 2362 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2363 | 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] | 2364 | return 0; |
| 2365 | } |
| 2366 | |
| 2367 | /* This function fail or initite the connection. */ |
| 2368 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2369 | { |
| 2370 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2371 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2373 | struct hlua *hlua; |
| 2374 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2375 | int low, high; |
| 2376 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2377 | struct xref *peer; |
| 2378 | struct stream_interface *si; |
| 2379 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2380 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2381 | if (lua_gettop(L) < 2) |
| 2382 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | |
| 2384 | /* Get args. */ |
| 2385 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2386 | |
| 2387 | /* Check if we run on the same thread than the xreator thread. |
| 2388 | * We cannot access to the socket if the thread is different. |
| 2389 | */ |
| 2390 | if (socket->tid != tid) |
| 2391 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2392 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2393 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2394 | if (lua_gettop(L) >= 3) { |
| 2395 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2396 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2397 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2398 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2399 | * that are not enclosed within square brackets. |
| 2400 | */ |
| 2401 | if (port > 0) { |
| 2402 | luaL_buffinit(L, &b); |
| 2403 | luaL_addstring(&b, ip); |
| 2404 | luaL_addchar(&b, ':'); |
| 2405 | luaL_pushresult(&b); |
| 2406 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2407 | } |
| 2408 | } |
| 2409 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2410 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2411 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2412 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | lua_pushnil(L); |
| 2414 | return 1; |
| 2415 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | |
| 2417 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2418 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2419 | if (!addr) { |
| 2420 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2421 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | } |
| 2423 | if (low != high) { |
| 2424 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2425 | 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] | 2426 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2427 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2428 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2429 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2430 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2431 | if (port == -1) { |
| 2432 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2433 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2434 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2435 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2436 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | if (port == -1) { |
| 2438 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2439 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2440 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2441 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2442 | } |
| 2443 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2444 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2445 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2446 | si = appctx->owner; |
| 2447 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2448 | |
| 2449 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2450 | xref_unlock(&socket->xref, peer); |
| 2451 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2452 | } |
| 2453 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2454 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2455 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2456 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2457 | |
| 2458 | /* inform the stream that we want to be notified whenever the |
| 2459 | * connection completes. |
| 2460 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2461 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2462 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2463 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2464 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2465 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2466 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2467 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2468 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2469 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2470 | } |
| 2471 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2472 | |
| 2473 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2474 | /* Return yield waiting for connection. */ |
| 2475 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2476 | 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] | 2477 | |
| 2478 | return 0; |
| 2479 | } |
| 2480 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2481 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2482 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2483 | { |
| 2484 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2485 | struct xref *peer; |
| 2486 | struct appctx *appctx; |
| 2487 | struct stream_interface *si; |
| 2488 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2489 | |
| 2490 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2491 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2492 | |
| 2493 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2494 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2495 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2496 | lua_pushnil(L); |
| 2497 | return 1; |
| 2498 | } |
| 2499 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2500 | si = appctx->owner; |
| 2501 | s = si_strm(si); |
| 2502 | |
| 2503 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2504 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2505 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2506 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2507 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2508 | |
| 2509 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2510 | { |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
| 2514 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2515 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2516 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2517 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2518 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2519 | struct xref *peer; |
| 2520 | struct appctx *appctx; |
| 2521 | struct stream_interface *si; |
| 2522 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2523 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2524 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2525 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2526 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2527 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2528 | /* convert the timeout to millis */ |
| 2529 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2530 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2531 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2532 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2533 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2534 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2535 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2536 | 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] | 2537 | |
| 2538 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2539 | if (tmout == 0) |
| 2540 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2541 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2542 | /* Check if we run on the same thread than the xreator thread. |
| 2543 | * We cannot access to the socket if the thread is different. |
| 2544 | */ |
| 2545 | if (socket->tid != tid) |
| 2546 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2547 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2548 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2549 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2550 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2551 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2552 | WILL_LJMP(lua_error(L)); |
| 2553 | return 0; |
| 2554 | } |
| 2555 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2556 | si = appctx->owner; |
| 2557 | s = si_strm(si); |
| 2558 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2559 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2560 | s->req.rto = tmout; |
| 2561 | s->req.wto = tmout; |
| 2562 | s->res.rto = tmout; |
| 2563 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2564 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2565 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2566 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2567 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2568 | |
| 2569 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2570 | task_queue(s->task); |
| 2571 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2572 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2573 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2574 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2575 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2576 | } |
| 2577 | |
| 2578 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2579 | { |
| 2580 | struct hlua_socket *socket; |
| 2581 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2582 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2583 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | |
| 2585 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2586 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | hlua_pusherror(L, "socket: full stack"); |
| 2588 | goto out_fail_conf; |
| 2589 | } |
| 2590 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2591 | /* Create the object: obj[0] = userdata. */ |
| 2592 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2593 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2594 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2596 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2597 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2598 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2599 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2600 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2601 | goto out_fail_conf; |
| 2602 | } |
| 2603 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2604 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2605 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2606 | lua_setmetatable(L, -2); |
| 2607 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2608 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2609 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2610 | if (!appctx) { |
| 2611 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2612 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2613 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2615 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2616 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2617 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2618 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2619 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2620 | /* Now create a session, task and stream for this applet */ |
| 2621 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2622 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2623 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2624 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | } |
| 2626 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2627 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2628 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2629 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2630 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2631 | } |
| 2632 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2633 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2634 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2635 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | /* Configure "right" stream interface. this "si" is used to connect |
| 2637 | * and retrieve data from the server. The connection is initialized |
| 2638 | * with the "struct server". |
| 2639 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2640 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | |
| 2642 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2643 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2644 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2645 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | return 1; |
| 2647 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2648 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2649 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2650 | out_fail_sess: |
| 2651 | appctx_free(appctx); |
| 2652 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2653 | WILL_LJMP(lua_error(L)); |
| 2654 | return 0; |
| 2655 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2656 | |
| 2657 | /* |
| 2658 | * |
| 2659 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2660 | * Class Channel |
| 2661 | * |
| 2662 | * |
| 2663 | */ |
| 2664 | |
| 2665 | /* Returns the struct hlua_channel join to the class channel in the |
| 2666 | * stack entry "ud" or throws an argument error. |
| 2667 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2668 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2669 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2670 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2671 | } |
| 2672 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2673 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2674 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2675 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2676 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2677 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2678 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2679 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | return 0; |
| 2681 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2682 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2683 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2684 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2685 | |
| 2686 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2687 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2688 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | return 1; |
| 2690 | } |
| 2691 | |
| 2692 | /* Duplicate all the data present in the input channel and put it |
| 2693 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2694 | * the stack if the channel is closed and all the data are consumed, |
| 2695 | * returns 0 if no data are available, otherwise it returns the length |
| 2696 | * of the builded string. |
| 2697 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2698 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2699 | { |
| 2700 | char *blk1; |
| 2701 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2702 | size_t len1; |
| 2703 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2704 | int ret; |
| 2705 | luaL_Buffer b; |
| 2706 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2707 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2708 | if (unlikely(ret == 0)) |
| 2709 | return 0; |
| 2710 | |
| 2711 | if (unlikely(ret < 0)) { |
| 2712 | lua_pushnil(L); |
| 2713 | return -1; |
| 2714 | } |
| 2715 | |
| 2716 | luaL_buffinit(L, &b); |
| 2717 | luaL_addlstring(&b, blk1, len1); |
| 2718 | if (unlikely(ret == 2)) |
| 2719 | luaL_addlstring(&b, blk2, len2); |
| 2720 | luaL_pushresult(&b); |
| 2721 | |
| 2722 | if (unlikely(ret == 2)) |
| 2723 | return len1 + len2; |
| 2724 | return len1; |
| 2725 | } |
| 2726 | |
| 2727 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2728 | * a yield. This function keep the data in the buffer. |
| 2729 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2730 | __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] | 2731 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2732 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2733 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2734 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2735 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2736 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2737 | WILL_LJMP(lua_error(L)); |
| 2738 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2739 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2740 | 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] | 2741 | return 1; |
| 2742 | } |
| 2743 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2744 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2745 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2746 | { |
| 2747 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2748 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2749 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2750 | } |
| 2751 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2753 | * a yield. This function consumes the data in the buffer. It returns |
| 2754 | * a string containing the data or a nil pointer if no data are available |
| 2755 | * and the channel is closed. |
| 2756 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2757 | __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] | 2758 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2759 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2760 | int ret; |
| 2761 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2762 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2763 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2764 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2765 | WILL_LJMP(lua_error(L)); |
| 2766 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2767 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2768 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2769 | 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] | 2770 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2771 | if (unlikely(ret == -1)) |
| 2772 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2774 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2775 | return 1; |
| 2776 | } |
| 2777 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2778 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2779 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2780 | { |
| 2781 | MAY_LJMP(check_args(L, 1, "get")); |
| 2782 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2783 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2784 | } |
| 2785 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | /* This functions consumes and returns one line. If the channel is closed, |
| 2787 | * 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] | 2788 | * 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] | 2789 | * value. |
| 2790 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2791 | __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] | 2792 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2793 | char *blk1; |
| 2794 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2795 | size_t len1; |
| 2796 | size_t len2; |
| 2797 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2798 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2799 | int ret; |
| 2800 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2801 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2802 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2803 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2804 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2805 | WILL_LJMP(lua_error(L)); |
| 2806 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2807 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2808 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2809 | 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] | 2810 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2811 | if (ret == -1) { |
| 2812 | lua_pushnil(L); |
| 2813 | return 1; |
| 2814 | } |
| 2815 | |
| 2816 | luaL_buffinit(L, &b); |
| 2817 | luaL_addlstring(&b, blk1, len1); |
| 2818 | len = len1; |
| 2819 | if (unlikely(ret == 2)) { |
| 2820 | luaL_addlstring(&b, blk2, len2); |
| 2821 | len += len2; |
| 2822 | } |
| 2823 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2824 | 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] | 2825 | return 1; |
| 2826 | } |
| 2827 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2828 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2829 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2830 | { |
| 2831 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2832 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2833 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2834 | } |
| 2835 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2836 | /* This function takes a string as input, and append it at the |
| 2837 | * input side of channel. If the data is too big, but a space |
| 2838 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2839 | * yields. If the data is bigger than the buffer, or if the |
| 2840 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2841 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2842 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | __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] | 2844 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2845 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2846 | size_t len; |
| 2847 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2848 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2849 | int ret; |
| 2850 | int max; |
| 2851 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2852 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2853 | WILL_LJMP(lua_error(L)); |
| 2854 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2855 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2856 | * the request buffer if its not required. |
| 2857 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2858 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2859 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2860 | 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] | 2861 | } |
| 2862 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2863 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2864 | if (max > len - l) |
| 2865 | max = len - l; |
| 2866 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2867 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2868 | if (ret == -2 || ret == -3) { |
| 2869 | lua_pushinteger(L, -1); |
| 2870 | return 1; |
| 2871 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2872 | if (ret == -1) { |
| 2873 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2874 | 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] | 2875 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2876 | l += ret; |
| 2877 | lua_pop(L, 1); |
| 2878 | lua_pushinteger(L, l); |
| 2879 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2880 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2881 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2882 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2883 | * in this case, we cannot add more data, so we cannot yield, |
| 2884 | * we return the amount of copyied data. |
| 2885 | */ |
| 2886 | return 1; |
| 2887 | } |
| 2888 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2889 | 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] | 2890 | return 1; |
| 2891 | } |
| 2892 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2893 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2894 | * 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] | 2895 | * buffer size is too little for the data. |
| 2896 | */ |
| 2897 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2898 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2899 | size_t len; |
| 2900 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2901 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2902 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2903 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2904 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2905 | lua_pushinteger(L, 0); |
| 2906 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2907 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2908 | } |
| 2909 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2910 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2911 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2912 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2913 | * or -1 if the channel is closed or if the buffer size is too |
| 2914 | * little for the data. |
| 2915 | */ |
| 2916 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2917 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2918 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2919 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2921 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | lua_pushinteger(L, 0); |
| 2923 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2924 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2925 | WILL_LJMP(lua_error(L)); |
| 2926 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2927 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2928 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2929 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2930 | } |
| 2931 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2932 | /* Append data in the output side of the buffer. This data is immediately |
| 2933 | * sent. The function returns the amount of data written. If the buffer |
| 2934 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2935 | * if the channel is closed. |
| 2936 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2937 | __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] | 2938 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2939 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2940 | size_t len; |
| 2941 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2942 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2943 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2944 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2945 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2946 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2947 | WILL_LJMP(lua_error(L)); |
| 2948 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2949 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2950 | lua_pushinteger(L, -1); |
| 2951 | return 1; |
| 2952 | } |
| 2953 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2954 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2955 | * the request buffer if its not required. |
| 2956 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2957 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2958 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2959 | 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] | 2960 | } |
| 2961 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2962 | /* The written data will be immediately sent, so we can check |
| 2963 | * the available space without taking in account the reserve. |
| 2964 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2965 | * data, because the buffer will be flushed. |
| 2966 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2967 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2968 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2969 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2970 | * in this case, we cannot add more data, so we cannot yield, |
| 2971 | * we return the amount of copyied data. |
| 2972 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2973 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2974 | return 1; |
| 2975 | |
| 2976 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2977 | if (max > len - l) |
| 2978 | max = len - l; |
| 2979 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2980 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2981 | * detects a non contiguous buffer and realign it. |
| 2982 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2983 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2984 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2985 | |
| 2986 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2987 | 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] | 2988 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2989 | /* buffer replace considers that the input part is filled. |
| 2990 | * so, I must forward these new data in the output part. |
| 2991 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 2992 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | |
| 2994 | l += max; |
| 2995 | lua_pop(L, 1); |
| 2996 | lua_pushinteger(L, l); |
| 2997 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2998 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2999 | * in this case, we cannot add more data, so we cannot yield, |
| 3000 | * we return the amount of copyied data. |
| 3001 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3002 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3003 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3005 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3006 | if (l < len) { |
| 3007 | /* If we are waiting for space in the response buffer, we |
| 3008 | * must set the flag WAKERESWR. This flag required the task |
| 3009 | * wake up if any activity is detected on the response buffer. |
| 3010 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3011 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3012 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3013 | else |
| 3014 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3015 | 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] | 3016 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3017 | |
| 3018 | return 1; |
| 3019 | } |
| 3020 | |
| 3021 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3022 | * yield the LUA process, and resume it without checking the |
| 3023 | * input arguments. |
| 3024 | */ |
| 3025 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3026 | { |
| 3027 | MAY_LJMP(check_args(L, 2, "send")); |
| 3028 | lua_pushinteger(L, 0); |
| 3029 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3030 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3031 | } |
| 3032 | |
| 3033 | /* This function forward and amount of butes. The data pass from |
| 3034 | * the input side of the buffer to the output side, and can be |
| 3035 | * forwarded. This function never fails. |
| 3036 | * |
| 3037 | * The Lua function takes an amount of bytes to be forwarded in |
| 3038 | * imput. It returns the number of bytes forwarded. |
| 3039 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3040 | __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] | 3041 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3042 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3043 | int len; |
| 3044 | int l; |
| 3045 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3046 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3047 | |
| 3048 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3049 | |
| 3050 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3051 | WILL_LJMP(lua_error(L)); |
| 3052 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3053 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3054 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3055 | |
| 3056 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3057 | if (max > ci_data(chn)) |
| 3058 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3059 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3060 | l += max; |
| 3061 | |
| 3062 | lua_pop(L, 1); |
| 3063 | lua_pushinteger(L, l); |
| 3064 | |
| 3065 | /* Check if it miss bytes to forward. */ |
| 3066 | if (l < len) { |
| 3067 | /* The the input channel or the output channel are closed, we |
| 3068 | * must return the amount of data forwarded. |
| 3069 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3070 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | return 1; |
| 3072 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3073 | /* If we are waiting for space data in the response buffer, we |
| 3074 | * must set the flag WAKERESWR. This flag required the task |
| 3075 | * wake up if any activity is detected on the response buffer. |
| 3076 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3077 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3078 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3079 | else |
| 3080 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3081 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3083 | 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] | 3084 | } |
| 3085 | |
| 3086 | return 1; |
| 3087 | } |
| 3088 | |
| 3089 | /* Just check the input and prepare the stack for the previous |
| 3090 | * function "hlua_channel_forward_yield" |
| 3091 | */ |
| 3092 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3093 | { |
| 3094 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3095 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3096 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3097 | |
| 3098 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3099 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3100 | } |
| 3101 | |
| 3102 | /* Just returns the number of bytes available in the input |
| 3103 | * side of the buffer. This function never fails. |
| 3104 | */ |
| 3105 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3106 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3107 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3108 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3109 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3110 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3111 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3112 | struct htx *htx = htxbuf(&chn->buf); |
| 3113 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3114 | } |
| 3115 | else |
| 3116 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | return 1; |
| 3118 | } |
| 3119 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3120 | /* Returns true if the channel is full. */ |
| 3121 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3122 | { |
| 3123 | struct channel *chn; |
| 3124 | int rem; |
| 3125 | |
| 3126 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3127 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3128 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3129 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3130 | struct htx *htx = htxbuf(&chn->buf); |
| 3131 | |
| 3132 | rem = htx_free_data_space(htx); |
| 3133 | } |
| 3134 | else |
| 3135 | rem = b_room(&chn->buf); |
| 3136 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3137 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3138 | |
| 3139 | lua_pushboolean(L, rem <= 0); |
| 3140 | return 1; |
| 3141 | } |
| 3142 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3143 | /* Just returns the number of bytes available in the output |
| 3144 | * side of the buffer. This function never fails. |
| 3145 | */ |
| 3146 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3147 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3148 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3149 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3150 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3151 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3152 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3153 | return 1; |
| 3154 | } |
| 3155 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3156 | /* |
| 3157 | * |
| 3158 | * |
| 3159 | * Class Fetches |
| 3160 | * |
| 3161 | * |
| 3162 | */ |
| 3163 | |
| 3164 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3165 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3166 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3167 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3168 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3169 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3170 | } |
| 3171 | |
| 3172 | /* This function creates and push in the stack a fetch object according |
| 3173 | * with a current TXN. |
| 3174 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3175 | 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] | 3176 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3177 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3178 | |
| 3179 | /* Check stack size. */ |
| 3180 | if (!lua_checkstack(L, 3)) |
| 3181 | return 0; |
| 3182 | |
| 3183 | /* Create the object: obj[0] = userdata. |
| 3184 | * Note that the base of the Fetches object is the |
| 3185 | * transaction object. |
| 3186 | */ |
| 3187 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3188 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3189 | lua_rawseti(L, -2, 0); |
| 3190 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3191 | hsmp->s = txn->s; |
| 3192 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3193 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3194 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3195 | |
| 3196 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3197 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3198 | lua_setmetatable(L, -2); |
| 3199 | |
| 3200 | return 1; |
| 3201 | } |
| 3202 | |
| 3203 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3204 | * It uses closure argument to store the associated sample-fetch. It |
| 3205 | * returns only one argument or throws an error. An error is thrown |
| 3206 | * only if an error is encountered during the argument parsing. If |
| 3207 | * the "sample-fetch" function fails, nil is returned. |
| 3208 | */ |
| 3209 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3210 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3211 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3212 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3213 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3214 | int i; |
| 3215 | struct sample smp; |
| 3216 | |
| 3217 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3218 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3219 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3220 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3221 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3222 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3223 | /* Check execution authorization. */ |
| 3224 | if (f->use & SMP_USE_HTTP_ANY && |
| 3225 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3226 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3227 | "is not available in Lua services", f->kw); |
| 3228 | WILL_LJMP(lua_error(L)); |
| 3229 | } |
| 3230 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3231 | /* Get extra arguments. */ |
| 3232 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3233 | if (i >= ARGM_NBARGS) |
| 3234 | break; |
| 3235 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3236 | } |
| 3237 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3238 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3239 | |
| 3240 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3241 | 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] | 3242 | |
| 3243 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3244 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3245 | lua_pushfstring(L, "error in arguments"); |
| 3246 | WILL_LJMP(lua_error(L)); |
| 3247 | } |
| 3248 | |
| 3249 | /* Initialise the sample. */ |
| 3250 | memset(&smp, 0, sizeof(smp)); |
| 3251 | |
| 3252 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3253 | 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] | 3254 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3255 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3256 | lua_pushstring(L, ""); |
| 3257 | else |
| 3258 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3259 | return 1; |
| 3260 | } |
| 3261 | |
| 3262 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3263 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3264 | hlua_smp2lua_str(L, &smp); |
| 3265 | else |
| 3266 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3267 | return 1; |
| 3268 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3269 | |
| 3270 | /* |
| 3271 | * |
| 3272 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3273 | * Class Converters |
| 3274 | * |
| 3275 | * |
| 3276 | */ |
| 3277 | |
| 3278 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3279 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3280 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3281 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3282 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3283 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3284 | } |
| 3285 | |
| 3286 | /* This function creates and push in the stack a Converters object |
| 3287 | * according with a current TXN. |
| 3288 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3289 | 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] | 3290 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3291 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3292 | |
| 3293 | /* Check stack size. */ |
| 3294 | if (!lua_checkstack(L, 3)) |
| 3295 | return 0; |
| 3296 | |
| 3297 | /* Create the object: obj[0] = userdata. |
| 3298 | * Note that the base of the Converters object is the |
| 3299 | * same than the TXN object. |
| 3300 | */ |
| 3301 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3302 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3303 | lua_rawseti(L, -2, 0); |
| 3304 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3305 | hsmp->s = txn->s; |
| 3306 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3307 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3308 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3309 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3310 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3311 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3312 | lua_setmetatable(L, -2); |
| 3313 | |
| 3314 | return 1; |
| 3315 | } |
| 3316 | |
| 3317 | /* This function is an LUA binding. It is called with each converter. |
| 3318 | * It uses closure argument to store the associated converter. It |
| 3319 | * returns only one argument or throws an error. An error is thrown |
| 3320 | * only if an error is encountered during the argument parsing. If |
| 3321 | * the converter function function fails, nil is returned. |
| 3322 | */ |
| 3323 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3324 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3325 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3326 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3327 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3328 | int i; |
| 3329 | struct sample smp; |
| 3330 | |
| 3331 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3332 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3333 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3334 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3335 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3336 | |
| 3337 | /* Get extra arguments. */ |
| 3338 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3339 | if (i >= ARGM_NBARGS) |
| 3340 | break; |
| 3341 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3342 | } |
| 3343 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3344 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3345 | |
| 3346 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3347 | 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] | 3348 | |
| 3349 | /* Run the special args checker. */ |
| 3350 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3351 | hlua_pusherror(L, "error in arguments"); |
| 3352 | WILL_LJMP(lua_error(L)); |
| 3353 | } |
| 3354 | |
| 3355 | /* Initialise the sample. */ |
| 3356 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3357 | hlua_pusherror(L, "error in the input argument"); |
| 3358 | WILL_LJMP(lua_error(L)); |
| 3359 | } |
| 3360 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3361 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3362 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3363 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3364 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3365 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3366 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3367 | WILL_LJMP(lua_error(L)); |
| 3368 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3369 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3370 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3371 | hlua_pusherror(L, "error during the input argument casting"); |
| 3372 | WILL_LJMP(lua_error(L)); |
| 3373 | } |
| 3374 | |
| 3375 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3376 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3377 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3378 | lua_pushstring(L, ""); |
| 3379 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3380 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | return 1; |
| 3382 | } |
| 3383 | |
| 3384 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3385 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3386 | hlua_smp2lua_str(L, &smp); |
| 3387 | else |
| 3388 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3389 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3390 | } |
| 3391 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3392 | /* |
| 3393 | * |
| 3394 | * |
| 3395 | * Class AppletTCP |
| 3396 | * |
| 3397 | * |
| 3398 | */ |
| 3399 | |
| 3400 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3401 | * a class stream, otherwise it throws an error. |
| 3402 | */ |
| 3403 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3404 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3405 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3406 | } |
| 3407 | |
| 3408 | /* This function creates and push in the stack an Applet object |
| 3409 | * according with a current TXN. |
| 3410 | */ |
| 3411 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3412 | { |
| 3413 | struct hlua_appctx *appctx; |
| 3414 | struct stream_interface *si = ctx->owner; |
| 3415 | struct stream *s = si_strm(si); |
| 3416 | struct proxy *p = s->be; |
| 3417 | |
| 3418 | /* Check stack size. */ |
| 3419 | if (!lua_checkstack(L, 3)) |
| 3420 | return 0; |
| 3421 | |
| 3422 | /* Create the object: obj[0] = userdata. |
| 3423 | * Note that the base of the Converters object is the |
| 3424 | * same than the TXN object. |
| 3425 | */ |
| 3426 | lua_newtable(L); |
| 3427 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3428 | lua_rawseti(L, -2, 0); |
| 3429 | appctx->appctx = ctx; |
| 3430 | appctx->htxn.s = s; |
| 3431 | appctx->htxn.p = p; |
| 3432 | |
| 3433 | /* Create the "f" field that contains a list of fetches. */ |
| 3434 | lua_pushstring(L, "f"); |
| 3435 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3436 | return 0; |
| 3437 | lua_settable(L, -3); |
| 3438 | |
| 3439 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3440 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3441 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3442 | return 0; |
| 3443 | lua_settable(L, -3); |
| 3444 | |
| 3445 | /* Create the "c" field that contains a list of converters. */ |
| 3446 | lua_pushstring(L, "c"); |
| 3447 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3448 | return 0; |
| 3449 | lua_settable(L, -3); |
| 3450 | |
| 3451 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3452 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3453 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3454 | return 0; |
| 3455 | lua_settable(L, -3); |
| 3456 | |
| 3457 | /* Pop a class stream metatable and affect it to the table. */ |
| 3458 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3459 | lua_setmetatable(L, -2); |
| 3460 | |
| 3461 | return 1; |
| 3462 | } |
| 3463 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3464 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3465 | { |
| 3466 | struct hlua_appctx *appctx; |
| 3467 | struct stream *s; |
| 3468 | const char *name; |
| 3469 | size_t len; |
| 3470 | struct sample smp; |
| 3471 | |
| 3472 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3473 | |
| 3474 | /* It is useles to retrieve the stream, but this function |
| 3475 | * runs only in a stream context. |
| 3476 | */ |
| 3477 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3478 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3479 | s = appctx->htxn.s; |
| 3480 | |
| 3481 | /* Converts the third argument in a sample. */ |
| 3482 | hlua_lua2smp(L, 3, &smp); |
| 3483 | |
| 3484 | /* Store the sample in a variable. */ |
| 3485 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3486 | vars_set_by_name(name, len, &smp); |
| 3487 | return 0; |
| 3488 | } |
| 3489 | |
| 3490 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3491 | { |
| 3492 | struct hlua_appctx *appctx; |
| 3493 | struct stream *s; |
| 3494 | const char *name; |
| 3495 | size_t len; |
| 3496 | struct sample smp; |
| 3497 | |
| 3498 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3499 | |
| 3500 | /* It is useles to retrieve the stream, but this function |
| 3501 | * runs only in a stream context. |
| 3502 | */ |
| 3503 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3504 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3505 | s = appctx->htxn.s; |
| 3506 | |
| 3507 | /* Unset the variable. */ |
| 3508 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3509 | vars_unset_by_name(name, len, &smp); |
| 3510 | return 0; |
| 3511 | } |
| 3512 | |
| 3513 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3514 | { |
| 3515 | struct hlua_appctx *appctx; |
| 3516 | struct stream *s; |
| 3517 | const char *name; |
| 3518 | size_t len; |
| 3519 | struct sample smp; |
| 3520 | |
| 3521 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3522 | |
| 3523 | /* It is useles to retrieve the stream, but this function |
| 3524 | * runs only in a stream context. |
| 3525 | */ |
| 3526 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3527 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3528 | s = appctx->htxn.s; |
| 3529 | |
| 3530 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3531 | if (!vars_get_by_name(name, len, &smp)) { |
| 3532 | lua_pushnil(L); |
| 3533 | return 1; |
| 3534 | } |
| 3535 | |
| 3536 | return hlua_smp2lua(L, &smp); |
| 3537 | } |
| 3538 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3539 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3540 | { |
| 3541 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3542 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3543 | struct hlua *hlua; |
| 3544 | |
| 3545 | /* 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] | 3546 | if (!s->hlua) |
| 3547 | return 0; |
| 3548 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3549 | |
| 3550 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3551 | |
| 3552 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3553 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3554 | |
| 3555 | /* Get and store new value. */ |
| 3556 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3557 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3558 | |
| 3559 | return 0; |
| 3560 | } |
| 3561 | |
| 3562 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3563 | { |
| 3564 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3565 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3566 | struct hlua *hlua; |
| 3567 | |
| 3568 | /* 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] | 3569 | if (!s->hlua) { |
| 3570 | lua_pushnil(L); |
| 3571 | return 1; |
| 3572 | } |
| 3573 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3574 | |
| 3575 | /* Push configuration index in the stack. */ |
| 3576 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3577 | |
| 3578 | return 1; |
| 3579 | } |
| 3580 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3581 | /* If expected data not yet available, it returns a yield. This function |
| 3582 | * consumes the data in the buffer. It returns a string containing the |
| 3583 | * data. This string can be empty. |
| 3584 | */ |
| 3585 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3586 | { |
| 3587 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3588 | struct stream_interface *si = appctx->appctx->owner; |
| 3589 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3590 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3591 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3592 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3593 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3594 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3595 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3596 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3597 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3598 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3599 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3600 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3601 | 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] | 3602 | } |
| 3603 | |
| 3604 | /* End of data: commit the total strings and return. */ |
| 3605 | if (ret < 0) { |
| 3606 | luaL_pushresult(&appctx->b); |
| 3607 | return 1; |
| 3608 | } |
| 3609 | |
| 3610 | /* Ensure that the block 2 length is usable. */ |
| 3611 | if (ret == 1) |
| 3612 | len2 = 0; |
| 3613 | |
| 3614 | /* dont check the max length read and dont check. */ |
| 3615 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3616 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3617 | |
| 3618 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3619 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3620 | luaL_pushresult(&appctx->b); |
| 3621 | return 1; |
| 3622 | } |
| 3623 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3624 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3625 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3626 | { |
| 3627 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3628 | |
| 3629 | /* Initialise the string catenation. */ |
| 3630 | luaL_buffinit(L, &appctx->b); |
| 3631 | |
| 3632 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3633 | } |
| 3634 | |
| 3635 | /* If expected data not yet available, it returns a yield. This function |
| 3636 | * consumes the data in the buffer. It returns a string containing the |
| 3637 | * data. This string can be empty. |
| 3638 | */ |
| 3639 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3640 | { |
| 3641 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3642 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3643 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3644 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3645 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3646 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3647 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3648 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3649 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3650 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3651 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3652 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3653 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3654 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3655 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3656 | 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] | 3657 | } |
| 3658 | |
| 3659 | /* End of data: commit the total strings and return. */ |
| 3660 | if (ret < 0) { |
| 3661 | luaL_pushresult(&appctx->b); |
| 3662 | return 1; |
| 3663 | } |
| 3664 | |
| 3665 | /* Ensure that the block 2 length is usable. */ |
| 3666 | if (ret == 1) |
| 3667 | len2 = 0; |
| 3668 | |
| 3669 | if (len == -1) { |
| 3670 | |
| 3671 | /* If len == -1, catenate all the data avalaile and |
| 3672 | * yield because we want to get all the data until |
| 3673 | * the end of data stream. |
| 3674 | */ |
| 3675 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3676 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3677 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3678 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3679 | 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] | 3680 | |
| 3681 | } else { |
| 3682 | |
| 3683 | /* Copy the fisrt block caping to the length required. */ |
| 3684 | if (len1 > len) |
| 3685 | len1 = len; |
| 3686 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3687 | len -= len1; |
| 3688 | |
| 3689 | /* Copy the second block. */ |
| 3690 | if (len2 > len) |
| 3691 | len2 = len; |
| 3692 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3693 | len -= len2; |
| 3694 | |
| 3695 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3696 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3698 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3699 | if (len > 0) { |
| 3700 | lua_pushinteger(L, len); |
| 3701 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3702 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3703 | 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] | 3704 | } |
| 3705 | |
| 3706 | /* return the result. */ |
| 3707 | luaL_pushresult(&appctx->b); |
| 3708 | return 1; |
| 3709 | } |
| 3710 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3711 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3712 | hlua_pusherror(L, "Lua: internal error"); |
| 3713 | WILL_LJMP(lua_error(L)); |
| 3714 | return 0; |
| 3715 | } |
| 3716 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3717 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3718 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3719 | { |
| 3720 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3721 | int len = -1; |
| 3722 | |
| 3723 | if (lua_gettop(L) > 2) |
| 3724 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3725 | if (lua_gettop(L) >= 2) { |
| 3726 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3727 | lua_pop(L, 1); |
| 3728 | } |
| 3729 | |
| 3730 | /* Confirm or set the required length */ |
| 3731 | lua_pushinteger(L, len); |
| 3732 | |
| 3733 | /* Initialise the string catenation. */ |
| 3734 | luaL_buffinit(L, &appctx->b); |
| 3735 | |
| 3736 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3737 | } |
| 3738 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3739 | /* Append data in the output side of the buffer. This data is immediately |
| 3740 | * sent. The function returns the amount of data written. If the buffer |
| 3741 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3742 | * if the channel is closed. |
| 3743 | */ |
| 3744 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3745 | { |
| 3746 | size_t len; |
| 3747 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3748 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3749 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3750 | struct stream_interface *si = appctx->appctx->owner; |
| 3751 | struct channel *chn = si_ic(si); |
| 3752 | int max; |
| 3753 | |
| 3754 | /* Get the max amount of data which can write as input in the channel. */ |
| 3755 | max = channel_recv_max(chn); |
| 3756 | if (max > (len - l)) |
| 3757 | max = len - l; |
| 3758 | |
| 3759 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3760 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3761 | |
| 3762 | /* update counters. */ |
| 3763 | l += max; |
| 3764 | lua_pop(L, 1); |
| 3765 | lua_pushinteger(L, l); |
| 3766 | |
| 3767 | /* If some data is not send, declares the situation to the |
| 3768 | * applet, and returns a yield. |
| 3769 | */ |
| 3770 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3771 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3772 | 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] | 3773 | } |
| 3774 | |
| 3775 | return 1; |
| 3776 | } |
| 3777 | |
| 3778 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3779 | * yield the LUA process, and resume it without checking the |
| 3780 | * input arguments. |
| 3781 | */ |
| 3782 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3783 | { |
| 3784 | MAY_LJMP(check_args(L, 2, "send")); |
| 3785 | lua_pushinteger(L, 0); |
| 3786 | |
| 3787 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3788 | } |
| 3789 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3790 | /* |
| 3791 | * |
| 3792 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3793 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3794 | * |
| 3795 | * |
| 3796 | */ |
| 3797 | |
| 3798 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3799 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3800 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3801 | __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] | 3802 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3803 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3804 | } |
| 3805 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3806 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3807 | * according with a current TXN. |
| 3808 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3809 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3810 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3811 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3812 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3813 | struct stream_interface *si = ctx->owner; |
| 3814 | struct stream *s = si_strm(si); |
| 3815 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3816 | struct htx *htx; |
| 3817 | struct htx_blk *blk; |
| 3818 | struct htx_sl *sl; |
| 3819 | struct ist path; |
| 3820 | unsigned long long len = 0; |
| 3821 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3822 | |
| 3823 | /* Check stack size. */ |
| 3824 | if (!lua_checkstack(L, 3)) |
| 3825 | return 0; |
| 3826 | |
| 3827 | /* Create the object: obj[0] = userdata. |
| 3828 | * Note that the base of the Converters object is the |
| 3829 | * same than the TXN object. |
| 3830 | */ |
| 3831 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3832 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3833 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3834 | appctx->appctx = ctx; |
| 3835 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3836 | 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] | 3837 | appctx->htxn.s = s; |
| 3838 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3839 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3840 | /* Create the "f" field that contains a list of fetches. */ |
| 3841 | lua_pushstring(L, "f"); |
| 3842 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3843 | return 0; |
| 3844 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3845 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3846 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3847 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3848 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3849 | return 0; |
| 3850 | lua_settable(L, -3); |
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 "c" field that contains a list of converters. */ |
| 3853 | lua_pushstring(L, "c"); |
| 3854 | if (!hlua_converters_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 "sc" field that contains a list of stringsafe converters. */ |
| 3859 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3860 | if (!hlua_converters_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); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3863 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3864 | htx = htxbuf(&s->req.buf); |
| 3865 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3866 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3867 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3868 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3869 | /* Stores the request method. */ |
| 3870 | lua_pushstring(L, "method"); |
| 3871 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3872 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3873 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3874 | /* Stores the http version. */ |
| 3875 | lua_pushstring(L, "version"); |
| 3876 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3877 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3878 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3879 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3880 | * the array on the top of the stack. |
| 3881 | */ |
| 3882 | lua_pushstring(L, "headers"); |
| 3883 | htxn.s = s; |
| 3884 | htxn.p = px; |
| 3885 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3886 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3887 | return 0; |
| 3888 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3889 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3890 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3891 | if (path.ptr) { |
| 3892 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3894 | p = path.ptr; |
| 3895 | end = path.ptr + path.len; |
| 3896 | q = p; |
| 3897 | while (q < end && *q != '?') |
| 3898 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3899 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3900 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3901 | lua_pushstring(L, "path"); |
| 3902 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3903 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3904 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3905 | /* Stores the query string. */ |
| 3906 | lua_pushstring(L, "qs"); |
| 3907 | if (*q == '?') |
| 3908 | q++; |
| 3909 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3910 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3911 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3912 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3913 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3914 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3915 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3918 | break; |
| 3919 | if (type == HTX_BLK_DATA) |
| 3920 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3921 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3922 | if (htx->extra != ULLONG_MAX) |
| 3923 | len += htx->extra; |
| 3924 | |
| 3925 | /* Stores the request path. */ |
| 3926 | lua_pushstring(L, "length"); |
| 3927 | lua_pushinteger(L, len); |
| 3928 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3929 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3930 | /* Create an empty array of HTTP request headers. */ |
| 3931 | lua_pushstring(L, "response"); |
| 3932 | lua_newtable(L); |
| 3933 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3934 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3935 | /* Pop a class stream metatable and affect it to the table. */ |
| 3936 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3937 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3938 | |
| 3939 | return 1; |
| 3940 | } |
| 3941 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3942 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3943 | { |
| 3944 | struct hlua_appctx *appctx; |
| 3945 | struct stream *s; |
| 3946 | const char *name; |
| 3947 | size_t len; |
| 3948 | struct sample smp; |
| 3949 | |
| 3950 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3951 | |
| 3952 | /* It is useles to retrieve the stream, but this function |
| 3953 | * runs only in a stream context. |
| 3954 | */ |
| 3955 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3956 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3957 | s = appctx->htxn.s; |
| 3958 | |
| 3959 | /* Converts the third argument in a sample. */ |
| 3960 | hlua_lua2smp(L, 3, &smp); |
| 3961 | |
| 3962 | /* Store the sample in a variable. */ |
| 3963 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3964 | vars_set_by_name(name, len, &smp); |
| 3965 | return 0; |
| 3966 | } |
| 3967 | |
| 3968 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3969 | { |
| 3970 | struct hlua_appctx *appctx; |
| 3971 | struct stream *s; |
| 3972 | const char *name; |
| 3973 | size_t len; |
| 3974 | struct sample smp; |
| 3975 | |
| 3976 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3977 | |
| 3978 | /* It is useles to retrieve the stream, but this function |
| 3979 | * runs only in a stream context. |
| 3980 | */ |
| 3981 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3982 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3983 | s = appctx->htxn.s; |
| 3984 | |
| 3985 | /* Unset the variable. */ |
| 3986 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3987 | vars_unset_by_name(name, len, &smp); |
| 3988 | return 0; |
| 3989 | } |
| 3990 | |
| 3991 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 3992 | { |
| 3993 | struct hlua_appctx *appctx; |
| 3994 | struct stream *s; |
| 3995 | const char *name; |
| 3996 | size_t len; |
| 3997 | struct sample smp; |
| 3998 | |
| 3999 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4000 | |
| 4001 | /* It is useles to retrieve the stream, but this function |
| 4002 | * runs only in a stream context. |
| 4003 | */ |
| 4004 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4005 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4006 | s = appctx->htxn.s; |
| 4007 | |
| 4008 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4009 | if (!vars_get_by_name(name, len, &smp)) { |
| 4010 | lua_pushnil(L); |
| 4011 | return 1; |
| 4012 | } |
| 4013 | |
| 4014 | return hlua_smp2lua(L, &smp); |
| 4015 | } |
| 4016 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4017 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4018 | { |
| 4019 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4020 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4021 | struct hlua *hlua; |
| 4022 | |
| 4023 | /* 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] | 4024 | if (!s->hlua) |
| 4025 | return 0; |
| 4026 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4027 | |
| 4028 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4029 | |
| 4030 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4031 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4032 | |
| 4033 | /* Get and store new value. */ |
| 4034 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4035 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4036 | |
| 4037 | return 0; |
| 4038 | } |
| 4039 | |
| 4040 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4041 | { |
| 4042 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4043 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4044 | struct hlua *hlua; |
| 4045 | |
| 4046 | /* 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] | 4047 | if (!s->hlua) { |
| 4048 | lua_pushnil(L); |
| 4049 | return 1; |
| 4050 | } |
| 4051 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4052 | |
| 4053 | /* Push configuration index in the stack. */ |
| 4054 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4055 | |
| 4056 | return 1; |
| 4057 | } |
| 4058 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4059 | /* If expected data not yet available, it returns a yield. This function |
| 4060 | * consumes the data in the buffer. It returns a string containing the |
| 4061 | * data. This string can be empty. |
| 4062 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4063 | __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] | 4064 | { |
| 4065 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4066 | struct stream_interface *si = appctx->appctx->owner; |
| 4067 | struct channel *req = si_oc(si); |
| 4068 | struct htx *htx; |
| 4069 | struct htx_blk *blk; |
| 4070 | size_t count; |
| 4071 | int stop = 0; |
| 4072 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4073 | htx = htx_from_buf(&req->buf); |
| 4074 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4075 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4076 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4077 | while (count && !stop && blk) { |
| 4078 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4079 | uint32_t sz = htx_get_blksz(blk); |
| 4080 | struct ist v; |
| 4081 | uint32_t vlen; |
| 4082 | char *nl; |
| 4083 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4084 | if (type == HTX_BLK_EOM) { |
| 4085 | stop = 1; |
| 4086 | break; |
| 4087 | } |
| 4088 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4089 | vlen = sz; |
| 4090 | if (vlen > count) { |
| 4091 | if (type != HTX_BLK_DATA) |
| 4092 | break; |
| 4093 | vlen = count; |
| 4094 | } |
| 4095 | |
| 4096 | switch (type) { |
| 4097 | case HTX_BLK_UNUSED: |
| 4098 | break; |
| 4099 | |
| 4100 | case HTX_BLK_DATA: |
| 4101 | v = htx_get_blk_value(htx, blk); |
| 4102 | v.len = vlen; |
| 4103 | nl = istchr(v, '\n'); |
| 4104 | if (nl != NULL) { |
| 4105 | stop = 1; |
| 4106 | vlen = nl - v.ptr + 1; |
| 4107 | } |
| 4108 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4109 | break; |
| 4110 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4111 | case HTX_BLK_TLR: |
| 4112 | case HTX_BLK_EOM: |
| 4113 | stop = 1; |
| 4114 | break; |
| 4115 | |
| 4116 | default: |
| 4117 | break; |
| 4118 | } |
| 4119 | |
| 4120 | co_set_data(req, co_data(req) - vlen); |
| 4121 | count -= vlen; |
| 4122 | if (sz == vlen) |
| 4123 | blk = htx_remove_blk(htx, blk); |
| 4124 | else { |
| 4125 | htx_cut_data_blk(htx, blk, vlen); |
| 4126 | break; |
| 4127 | } |
| 4128 | } |
| 4129 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4130 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4131 | if (!stop) { |
| 4132 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4133 | 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] | 4134 | } |
| 4135 | |
| 4136 | /* return the result. */ |
| 4137 | luaL_pushresult(&appctx->b); |
| 4138 | return 1; |
| 4139 | } |
| 4140 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4141 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4142 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4143 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4144 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4145 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4146 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4147 | /* Initialise the string catenation. */ |
| 4148 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4149 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4150 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4151 | } |
| 4152 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4153 | /* If expected data not yet available, it returns a yield. This function |
| 4154 | * consumes the data in the buffer. It returns a string containing the |
| 4155 | * data. This string can be empty. |
| 4156 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4157 | __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] | 4158 | { |
| 4159 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4160 | struct stream_interface *si = appctx->appctx->owner; |
| 4161 | struct channel *req = si_oc(si); |
| 4162 | struct htx *htx; |
| 4163 | struct htx_blk *blk; |
| 4164 | size_t count; |
| 4165 | int len; |
| 4166 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4167 | htx = htx_from_buf(&req->buf); |
| 4168 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4169 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4170 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4171 | while (count && len && blk) { |
| 4172 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4173 | uint32_t sz = htx_get_blksz(blk); |
| 4174 | struct ist v; |
| 4175 | uint32_t vlen; |
| 4176 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4177 | if (type == HTX_BLK_EOM) { |
| 4178 | len = 0; |
| 4179 | break; |
| 4180 | } |
| 4181 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4182 | vlen = sz; |
| 4183 | if (len > 0 && vlen > len) |
| 4184 | vlen = len; |
| 4185 | if (vlen > count) { |
| 4186 | if (type != HTX_BLK_DATA) |
| 4187 | break; |
| 4188 | vlen = count; |
| 4189 | } |
| 4190 | |
| 4191 | switch (type) { |
| 4192 | case HTX_BLK_UNUSED: |
| 4193 | break; |
| 4194 | |
| 4195 | case HTX_BLK_DATA: |
| 4196 | v = htx_get_blk_value(htx, blk); |
| 4197 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4198 | break; |
| 4199 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4200 | case HTX_BLK_TLR: |
| 4201 | case HTX_BLK_EOM: |
| 4202 | len = 0; |
| 4203 | break; |
| 4204 | |
| 4205 | default: |
| 4206 | break; |
| 4207 | } |
| 4208 | |
| 4209 | co_set_data(req, co_data(req) - vlen); |
| 4210 | count -= vlen; |
| 4211 | if (len > 0) |
| 4212 | len -= vlen; |
| 4213 | if (sz == vlen) |
| 4214 | blk = htx_remove_blk(htx, blk); |
| 4215 | else { |
| 4216 | htx_cut_data_blk(htx, blk, vlen); |
| 4217 | break; |
| 4218 | } |
| 4219 | } |
| 4220 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4221 | htx_to_buf(htx, &req->buf); |
| 4222 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4223 | /* If we are no other data available, yield waiting for new data. */ |
| 4224 | if (len) { |
| 4225 | if (len > 0) { |
| 4226 | lua_pushinteger(L, len); |
| 4227 | lua_replace(L, 2); |
| 4228 | } |
| 4229 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4230 | 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] | 4231 | } |
| 4232 | |
| 4233 | /* return the result. */ |
| 4234 | luaL_pushresult(&appctx->b); |
| 4235 | return 1; |
| 4236 | } |
| 4237 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4238 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4239 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4240 | { |
| 4241 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4242 | int len = -1; |
| 4243 | |
| 4244 | /* Check arguments. */ |
| 4245 | if (lua_gettop(L) > 2) |
| 4246 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4247 | if (lua_gettop(L) >= 2) { |
| 4248 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4249 | lua_pop(L, 1); |
| 4250 | } |
| 4251 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4252 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4253 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4254 | /* Initialise the string catenation. */ |
| 4255 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4256 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4257 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4258 | } |
| 4259 | |
| 4260 | /* Append data in the output side of the buffer. This data is immediately |
| 4261 | * sent. The function returns the amount of data written. If the buffer |
| 4262 | * cannot contain the data, the function yields. The function returns -1 |
| 4263 | * if the channel is closed. |
| 4264 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4265 | __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] | 4266 | { |
| 4267 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4268 | struct stream_interface *si = appctx->appctx->owner; |
| 4269 | struct channel *res = si_ic(si); |
| 4270 | struct htx *htx = htx_from_buf(&res->buf); |
| 4271 | const char *data; |
| 4272 | size_t len; |
| 4273 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4274 | int max; |
| 4275 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4276 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4277 | if (!max) |
| 4278 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4279 | |
| 4280 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4281 | |
| 4282 | /* Get the max amount of data which can write as input in the channel. */ |
| 4283 | if (max > (len - l)) |
| 4284 | max = len - l; |
| 4285 | |
| 4286 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4287 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4288 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4289 | |
| 4290 | /* update counters. */ |
| 4291 | l += max; |
| 4292 | lua_pop(L, 1); |
| 4293 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4294 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4295 | /* If some data is not send, declares the situation to the |
| 4296 | * applet, and returns a yield. |
| 4297 | */ |
| 4298 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4299 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4300 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4301 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4302 | 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] | 4303 | } |
| 4304 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4305 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4306 | return 1; |
| 4307 | } |
| 4308 | |
| 4309 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4310 | * yield the LUA process, and resume it without checking the |
| 4311 | * input arguments. |
| 4312 | */ |
| 4313 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4314 | { |
| 4315 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4316 | |
| 4317 | /* We want to send some data. Headers must be sent. */ |
| 4318 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4319 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4320 | WILL_LJMP(lua_error(L)); |
| 4321 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4322 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4323 | /* This interger is used for followinf the amount of data sent. */ |
| 4324 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4325 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4326 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4327 | } |
| 4328 | |
| 4329 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4330 | { |
| 4331 | const char *name; |
| 4332 | int ret; |
| 4333 | |
| 4334 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4335 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4336 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4337 | |
| 4338 | /* Push in the stack the "response" entry. */ |
| 4339 | ret = lua_getfield(L, 1, "response"); |
| 4340 | if (ret != LUA_TTABLE) { |
| 4341 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4342 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4343 | WILL_LJMP(lua_error(L)); |
| 4344 | } |
| 4345 | |
| 4346 | /* check if the header is already registered if it is not |
| 4347 | * the case, register it. |
| 4348 | */ |
| 4349 | ret = lua_getfield(L, -1, name); |
| 4350 | if (ret == LUA_TNIL) { |
| 4351 | |
| 4352 | /* Entry not found. */ |
| 4353 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4354 | |
| 4355 | /* Insert the new header name in the array in the top of the stack. |
| 4356 | * It left the new array in the top of the stack. |
| 4357 | */ |
| 4358 | lua_newtable(L); |
| 4359 | lua_pushvalue(L, 2); |
| 4360 | lua_pushvalue(L, -2); |
| 4361 | lua_settable(L, -4); |
| 4362 | |
| 4363 | } else if (ret != LUA_TTABLE) { |
| 4364 | |
| 4365 | /* corruption error. */ |
| 4366 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4367 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4368 | WILL_LJMP(lua_error(L)); |
| 4369 | } |
| 4370 | |
| 4371 | /* Now the top od thestack is an array of values. We push |
| 4372 | * the header value as new entry. |
| 4373 | */ |
| 4374 | lua_pushvalue(L, 3); |
| 4375 | ret = lua_rawlen(L, -2); |
| 4376 | lua_rawseti(L, -2, ret + 1); |
| 4377 | lua_pushboolean(L, 1); |
| 4378 | return 1; |
| 4379 | } |
| 4380 | |
| 4381 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4382 | { |
| 4383 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4384 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4385 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4386 | |
| 4387 | if (status < 100 || status > 599) { |
| 4388 | lua_pushboolean(L, 0); |
| 4389 | return 1; |
| 4390 | } |
| 4391 | |
| 4392 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4393 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4394 | lua_pushboolean(L, 1); |
| 4395 | return 1; |
| 4396 | } |
| 4397 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4398 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4399 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4400 | { |
| 4401 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4402 | struct stream_interface *si = appctx->appctx->owner; |
| 4403 | struct channel *res = si_ic(si); |
| 4404 | struct htx *htx; |
| 4405 | struct htx_sl *sl; |
| 4406 | struct h1m h1m; |
| 4407 | const char *status, *reason; |
| 4408 | const char *name, *value; |
| 4409 | size_t nlen, vlen; |
| 4410 | unsigned int flags; |
| 4411 | |
| 4412 | /* Send the message at once. */ |
| 4413 | htx = htx_from_buf(&res->buf); |
| 4414 | h1m_init_res(&h1m); |
| 4415 | |
| 4416 | /* Use the same http version than the request. */ |
| 4417 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4418 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4419 | if (reason == NULL) |
| 4420 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4421 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4422 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4423 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4424 | } |
| 4425 | else { |
| 4426 | flags = HTX_SL_F_IS_RESP; |
| 4427 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4428 | } |
| 4429 | if (!sl) { |
| 4430 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4431 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4432 | WILL_LJMP(lua_error(L)); |
| 4433 | } |
| 4434 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4435 | |
| 4436 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4437 | lua_pushvalue(L, 0); |
| 4438 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4439 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4440 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4441 | WILL_LJMP(lua_error(L)); |
| 4442 | } |
| 4443 | |
| 4444 | /* Browse the list of headers. */ |
| 4445 | lua_pushnil(L); |
| 4446 | while(lua_next(L, -2) != 0) { |
| 4447 | /* We expect a string as -2. */ |
| 4448 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4449 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4450 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4451 | lua_typename(L, lua_type(L, -2))); |
| 4452 | WILL_LJMP(lua_error(L)); |
| 4453 | } |
| 4454 | name = lua_tolstring(L, -2, &nlen); |
| 4455 | |
| 4456 | /* We expect an array as -1. */ |
| 4457 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4458 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4459 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4460 | name, |
| 4461 | lua_typename(L, lua_type(L, -1))); |
| 4462 | WILL_LJMP(lua_error(L)); |
| 4463 | } |
| 4464 | |
| 4465 | /* Browse the table who is on the top of the stack. */ |
| 4466 | lua_pushnil(L); |
| 4467 | while(lua_next(L, -2) != 0) { |
| 4468 | int id; |
| 4469 | |
| 4470 | /* We expect a number as -2. */ |
| 4471 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4472 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4473 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4474 | name, |
| 4475 | lua_typename(L, lua_type(L, -2))); |
| 4476 | WILL_LJMP(lua_error(L)); |
| 4477 | } |
| 4478 | id = lua_tointeger(L, -2); |
| 4479 | |
| 4480 | /* We expect a string as -2. */ |
| 4481 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4482 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4483 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4484 | name, id, |
| 4485 | lua_typename(L, lua_type(L, -1))); |
| 4486 | WILL_LJMP(lua_error(L)); |
| 4487 | } |
| 4488 | value = lua_tolstring(L, -1, &vlen); |
| 4489 | |
| 4490 | /* Simple Protocol checks. */ |
| 4491 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4492 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4493 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4494 | struct ist v = ist2(value, vlen); |
| 4495 | int ret; |
| 4496 | |
| 4497 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4498 | if (ret < 0) { |
| 4499 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4500 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4501 | name); |
| 4502 | WILL_LJMP(lua_error(L)); |
| 4503 | } |
| 4504 | else if (ret == 0) |
| 4505 | goto next; /* Skip it */ |
| 4506 | } |
| 4507 | |
| 4508 | /* Add a new header */ |
| 4509 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4510 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4511 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4512 | name); |
| 4513 | WILL_LJMP(lua_error(L)); |
| 4514 | } |
| 4515 | next: |
| 4516 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4517 | lua_pop(L, 1); |
| 4518 | } |
| 4519 | |
| 4520 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4521 | lua_pop(L, 1); |
| 4522 | } |
| 4523 | |
| 4524 | if (h1m.flags & H1_MF_CHNK) |
| 4525 | h1m.flags &= ~H1_MF_CLEN; |
| 4526 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4527 | h1m.flags |= H1_MF_XFER_LEN; |
| 4528 | |
| 4529 | /* Uset HTX start-line flags */ |
| 4530 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4531 | flags |= HTX_SL_F_XFER_ENC; |
| 4532 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4533 | flags |= HTX_SL_F_XFER_LEN; |
| 4534 | if (h1m.flags & H1_MF_CHNK) |
| 4535 | flags |= HTX_SL_F_CHNK; |
| 4536 | else if (h1m.flags & H1_MF_CLEN) |
| 4537 | flags |= HTX_SL_F_CLEN; |
| 4538 | if (h1m.body_len == 0) |
| 4539 | flags |= HTX_SL_F_BODYLESS; |
| 4540 | } |
| 4541 | sl->flags |= flags; |
| 4542 | |
| 4543 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4544 | * and the status code implies the presence of a message body, we must |
| 4545 | * announce a transfer encoding chunked. This is required by haproxy |
| 4546 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4547 | * chunked itslef, don't do anything. |
| 4548 | */ |
| 4549 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4550 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4551 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4552 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4553 | /* Add a new header */ |
| 4554 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4555 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4556 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4557 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4558 | WILL_LJMP(lua_error(L)); |
| 4559 | } |
| 4560 | } |
| 4561 | |
| 4562 | /* Finalize headers. */ |
| 4563 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4564 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4565 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4566 | WILL_LJMP(lua_error(L)); |
| 4567 | } |
| 4568 | |
| 4569 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4570 | b_reset(&res->buf); |
| 4571 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4572 | WILL_LJMP(lua_error(L)); |
| 4573 | } |
| 4574 | |
| 4575 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4576 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4577 | |
| 4578 | /* Headers sent, set the flag. */ |
| 4579 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4580 | return 0; |
| 4581 | |
| 4582 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4583 | /* We will build the status line and the headers of the HTTP response. |
| 4584 | * We will try send at once if its not possible, we give back the hand |
| 4585 | * waiting for more room. |
| 4586 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4587 | __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] | 4588 | { |
| 4589 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4590 | struct stream_interface *si = appctx->appctx->owner; |
| 4591 | struct channel *res = si_ic(si); |
| 4592 | |
| 4593 | if (co_data(res)) { |
| 4594 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4595 | 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] | 4596 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4597 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4598 | } |
| 4599 | |
| 4600 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4601 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4602 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4603 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4604 | } |
| 4605 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4606 | /* |
| 4607 | * |
| 4608 | * |
| 4609 | * Class HTTP |
| 4610 | * |
| 4611 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4612 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4613 | |
| 4614 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4615 | * a class stream, otherwise it throws an error. |
| 4616 | */ |
| 4617 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4618 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4619 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4620 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4621 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4622 | /* This function creates and push in the stack a HTTP object |
| 4623 | * according with a current TXN. |
| 4624 | */ |
| 4625 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4626 | { |
| 4627 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4628 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4629 | /* Check stack size. */ |
| 4630 | if (!lua_checkstack(L, 3)) |
| 4631 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4632 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4633 | /* Create the object: obj[0] = userdata. |
| 4634 | * Note that the base of the Converters object is the |
| 4635 | * same than the TXN object. |
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 | lua_newtable(L); |
| 4638 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4639 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4640 | |
| 4641 | htxn->s = txn->s; |
| 4642 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4643 | htxn->dir = txn->dir; |
| 4644 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4645 | |
| 4646 | /* Pop a class stream metatable and affect it to the table. */ |
| 4647 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4648 | lua_setmetatable(L, -2); |
| 4649 | |
| 4650 | return 1; |
| 4651 | } |
| 4652 | |
| 4653 | /* This function creates ans returns an array of HTTP headers. |
| 4654 | * This function does not fails. It is used as wrapper with the |
| 4655 | * 2 following functions. |
| 4656 | */ |
| 4657 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4658 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4659 | struct htx *htx; |
| 4660 | int32_t pos; |
| 4661 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | /* Create the table. */ |
| 4663 | lua_newtable(L); |
| 4664 | |
| 4665 | if (!htxn->s->txn) |
| 4666 | return 1; |
| 4667 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4668 | htx = htxbuf(&msg->chn->buf); |
| 4669 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4670 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4671 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4672 | struct ist n, v; |
| 4673 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4674 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4675 | if (type == HTX_BLK_HDR) { |
| 4676 | n = htx_get_blk_name(htx,blk); |
| 4677 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4678 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4679 | else if (type == HTX_BLK_EOH) |
| 4680 | break; |
| 4681 | else |
| 4682 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4683 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4684 | /* Check for existing entry: |
| 4685 | * assume that the table is on the top of the stack, and |
| 4686 | * push the key in the stack, the function lua_gettable() |
| 4687 | * perform the lookup. |
| 4688 | */ |
| 4689 | lua_pushlstring(L, n.ptr, n.len); |
| 4690 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4691 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4692 | switch (lua_type(L, -1)) { |
| 4693 | case LUA_TNIL: |
| 4694 | /* Table not found, create it. */ |
| 4695 | lua_pop(L, 1); /* remove the nil value. */ |
| 4696 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4697 | lua_newtable(L); /* create and push empty table. */ |
| 4698 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4699 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4700 | 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] | 4701 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4702 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4703 | case LUA_TTABLE: |
| 4704 | /* Entry found: push the value in the table. */ |
| 4705 | len = lua_rawlen(L, -1); |
| 4706 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4707 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4708 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4709 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4711 | default: |
| 4712 | /* Other cases are errors. */ |
| 4713 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4714 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4715 | } |
| 4716 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4717 | return 1; |
| 4718 | } |
| 4719 | |
| 4720 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4721 | { |
| 4722 | struct hlua_txn *htxn; |
| 4723 | |
| 4724 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4725 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4726 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4727 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4728 | WILL_LJMP(lua_error(L)); |
| 4729 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4730 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4731 | } |
| 4732 | |
| 4733 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4734 | { |
| 4735 | struct hlua_txn *htxn; |
| 4736 | |
| 4737 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4738 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4739 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4740 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4741 | WILL_LJMP(lua_error(L)); |
| 4742 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4743 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4744 | } |
| 4745 | |
| 4746 | /* This function replace full header, or just a value in |
| 4747 | * the request or in the response. It is a wrapper fir the |
| 4748 | * 4 following functions. |
| 4749 | */ |
| 4750 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4751 | struct http_msg *msg, int full) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4752 | { |
| 4753 | size_t name_len; |
| 4754 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4755 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4756 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4757 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4758 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4759 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4760 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4761 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4762 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4763 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4764 | http_replace_hdrs(htxn->s, htx, ist2(name, name_len), value, re, full); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4765 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4766 | return 0; |
| 4767 | } |
| 4768 | |
| 4769 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4770 | { |
| 4771 | struct hlua_txn *htxn; |
| 4772 | |
| 4773 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4774 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4775 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4776 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4777 | WILL_LJMP(lua_error(L)); |
| 4778 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4779 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4780 | } |
| 4781 | |
| 4782 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4783 | { |
| 4784 | struct hlua_txn *htxn; |
| 4785 | |
| 4786 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4787 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4788 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4789 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4790 | WILL_LJMP(lua_error(L)); |
| 4791 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4792 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4793 | } |
| 4794 | |
| 4795 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4796 | { |
| 4797 | struct hlua_txn *htxn; |
| 4798 | |
| 4799 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4800 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4801 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4802 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4803 | WILL_LJMP(lua_error(L)); |
| 4804 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4805 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4806 | } |
| 4807 | |
| 4808 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4809 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4810 | struct hlua_txn *htxn; |
| 4811 | |
| 4812 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4813 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4814 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4815 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4816 | WILL_LJMP(lua_error(L)); |
| 4817 | |
Christopher Faulet | 92d34fe | 2019-12-17 09:20:34 +0100 | [diff] [blame] | 4818 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4819 | } |
| 4820 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4821 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4822 | * It is a wrapper for the 2 following functions. |
| 4823 | */ |
| 4824 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4825 | { |
| 4826 | size_t len; |
| 4827 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4828 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4829 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4830 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4831 | ctx.blk = NULL; |
| 4832 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4833 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4834 | return 0; |
| 4835 | } |
| 4836 | |
| 4837 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4838 | { |
| 4839 | struct hlua_txn *htxn; |
| 4840 | |
| 4841 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4842 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4843 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4844 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4845 | WILL_LJMP(lua_error(L)); |
| 4846 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4847 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4848 | } |
| 4849 | |
| 4850 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4851 | { |
| 4852 | struct hlua_txn *htxn; |
| 4853 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4854 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4855 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4856 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4857 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4858 | WILL_LJMP(lua_error(L)); |
| 4859 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4860 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4861 | } |
| 4862 | |
| 4863 | /* This function adds an header. It is a wrapper used by |
| 4864 | * the 2 following functions. |
| 4865 | */ |
| 4866 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4867 | { |
| 4868 | size_t name_len; |
| 4869 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4870 | size_t value_len; |
| 4871 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4872 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4873 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4874 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4875 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4876 | return 0; |
| 4877 | } |
| 4878 | |
| 4879 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4880 | { |
| 4881 | struct hlua_txn *htxn; |
| 4882 | |
| 4883 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4884 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4885 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4886 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4887 | WILL_LJMP(lua_error(L)); |
| 4888 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4889 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4890 | } |
| 4891 | |
| 4892 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4893 | { |
| 4894 | struct hlua_txn *htxn; |
| 4895 | |
| 4896 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4897 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4898 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4899 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4900 | WILL_LJMP(lua_error(L)); |
| 4901 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4902 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4903 | } |
| 4904 | |
| 4905 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4906 | { |
| 4907 | struct hlua_txn *htxn; |
| 4908 | |
| 4909 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4910 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4911 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4912 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4913 | WILL_LJMP(lua_error(L)); |
| 4914 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4915 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4916 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4920 | { |
| 4921 | struct hlua_txn *htxn; |
| 4922 | |
| 4923 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4924 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4925 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4926 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4927 | WILL_LJMP(lua_error(L)); |
| 4928 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4929 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4930 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4931 | } |
| 4932 | |
| 4933 | /* This function set the method. */ |
| 4934 | static int hlua_http_req_set_meth(lua_State *L) |
| 4935 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4936 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4937 | size_t name_len; |
| 4938 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4939 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4940 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4941 | WILL_LJMP(lua_error(L)); |
| 4942 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4943 | 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] | 4944 | return 1; |
| 4945 | } |
| 4946 | |
| 4947 | /* This function set the method. */ |
| 4948 | static int hlua_http_req_set_path(lua_State *L) |
| 4949 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4950 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4951 | size_t name_len; |
| 4952 | 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] | 4953 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4954 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4955 | WILL_LJMP(lua_error(L)); |
| 4956 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4957 | 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] | 4958 | return 1; |
| 4959 | } |
| 4960 | |
| 4961 | /* This function set the query-string. */ |
| 4962 | static int hlua_http_req_set_query(lua_State *L) |
| 4963 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4964 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | size_t name_len; |
| 4966 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4967 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4968 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4969 | WILL_LJMP(lua_error(L)); |
| 4970 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4971 | /* Check length. */ |
| 4972 | if (name_len > trash.size - 1) { |
| 4973 | lua_pushboolean(L, 0); |
| 4974 | return 1; |
| 4975 | } |
| 4976 | |
| 4977 | /* Add the mark question as prefix. */ |
| 4978 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4979 | trash.area[trash.data++] = '?'; |
| 4980 | memcpy(trash.area + trash.data, name, name_len); |
| 4981 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4982 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 4983 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4984 | 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] | 4985 | return 1; |
| 4986 | } |
| 4987 | |
| 4988 | /* This function set the uri. */ |
| 4989 | static int hlua_http_req_set_uri(lua_State *L) |
| 4990 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4991 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | size_t name_len; |
| 4993 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4994 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4995 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4996 | WILL_LJMP(lua_error(L)); |
| 4997 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4998 | 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] | 4999 | return 1; |
| 5000 | } |
| 5001 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5002 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5003 | static int hlua_http_res_set_status(lua_State *L) |
| 5004 | { |
| 5005 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5006 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5007 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5008 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5009 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5010 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
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 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5014 | return 0; |
| 5015 | } |
| 5016 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | /* |
| 5018 | * |
| 5019 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5020 | * Class TXN |
| 5021 | * |
| 5022 | * |
| 5023 | */ |
| 5024 | |
| 5025 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5026 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5027 | */ |
| 5028 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5029 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5030 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5031 | } |
| 5032 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5033 | __LJMP static int hlua_set_var(lua_State *L) |
| 5034 | { |
| 5035 | struct hlua_txn *htxn; |
| 5036 | const char *name; |
| 5037 | size_t len; |
| 5038 | struct sample smp; |
| 5039 | |
| 5040 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5041 | |
| 5042 | /* It is useles to retrieve the stream, but this function |
| 5043 | * runs only in a stream context. |
| 5044 | */ |
| 5045 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5046 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5047 | |
| 5048 | /* Converts the third argument in a sample. */ |
| 5049 | hlua_lua2smp(L, 3, &smp); |
| 5050 | |
| 5051 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5052 | 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] | 5053 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5054 | return 0; |
| 5055 | } |
| 5056 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5057 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5058 | { |
| 5059 | struct hlua_txn *htxn; |
| 5060 | const char *name; |
| 5061 | size_t len; |
| 5062 | struct sample smp; |
| 5063 | |
| 5064 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5065 | |
| 5066 | /* It is useles to retrieve the stream, but this function |
| 5067 | * runs only in a stream context. |
| 5068 | */ |
| 5069 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5070 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5071 | |
| 5072 | /* Unset the variable. */ |
| 5073 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5074 | vars_unset_by_name(name, len, &smp); |
| 5075 | return 0; |
| 5076 | } |
| 5077 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5078 | __LJMP static int hlua_get_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, "get_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 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5093 | 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] | 5094 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5095 | lua_pushnil(L); |
| 5096 | return 1; |
| 5097 | } |
| 5098 | |
| 5099 | return hlua_smp2lua(L, &smp); |
| 5100 | } |
| 5101 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5102 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5103 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5104 | struct hlua *hlua; |
| 5105 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5106 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5107 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5108 | /* It is useles to retrieve the stream, but this function |
| 5109 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5110 | */ |
| 5111 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5112 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5113 | |
| 5114 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5115 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5116 | |
| 5117 | /* Get and store new value. */ |
| 5118 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5119 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5120 | |
| 5121 | return 0; |
| 5122 | } |
| 5123 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5124 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5125 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5126 | struct hlua *hlua; |
| 5127 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5128 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5129 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5130 | /* It is useles to retrieve the stream, but this function |
| 5131 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5132 | */ |
| 5133 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5134 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5135 | |
| 5136 | /* Push configuration index in the stack. */ |
| 5137 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5138 | |
| 5139 | return 1; |
| 5140 | } |
| 5141 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5142 | /* Create stack entry containing a class TXN. This function |
| 5143 | * return 0 if the stack does not contains free slots, |
| 5144 | * otherwise it returns 1. |
| 5145 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5146 | 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] | 5147 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5148 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5149 | |
| 5150 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5151 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5152 | return 0; |
| 5153 | |
| 5154 | /* NOTE: The allocation never fails. The failure |
| 5155 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5156 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5157 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5158 | /* Create the object: obj[0] = userdata. */ |
| 5159 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5160 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5161 | lua_rawseti(L, -2, 0); |
| 5162 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5163 | htxn->s = s; |
| 5164 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5165 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5166 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5167 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5168 | /* Create the "f" field that contains a list of fetches. */ |
| 5169 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5170 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5171 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5172 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5173 | |
| 5174 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5175 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5176 | 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] | 5177 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5178 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5179 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5180 | /* Create the "c" field that contains a list of converters. */ |
| 5181 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5182 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5183 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5184 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5185 | |
| 5186 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5187 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5188 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5189 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5190 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5191 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5192 | /* Create the "req" field that contains the request channel object. */ |
| 5193 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5194 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5195 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5196 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5197 | |
| 5198 | /* Create the "res" field that contains the response channel object. */ |
| 5199 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5200 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5201 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5202 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5203 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5204 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5205 | lua_pushstring(L, "http"); |
| 5206 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5207 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5208 | return 0; |
| 5209 | } |
| 5210 | else |
| 5211 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5212 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5213 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5214 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5215 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5216 | lua_setmetatable(L, -2); |
| 5217 | |
| 5218 | return 1; |
| 5219 | } |
| 5220 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5221 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5222 | { |
| 5223 | const char *msg; |
| 5224 | struct hlua_txn *htxn; |
| 5225 | |
| 5226 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5227 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5228 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5229 | |
| 5230 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5231 | return 0; |
| 5232 | } |
| 5233 | |
| 5234 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5235 | { |
| 5236 | int level; |
| 5237 | const char *msg; |
| 5238 | struct hlua_txn *htxn; |
| 5239 | |
| 5240 | MAY_LJMP(check_args(L, 3, "log")); |
| 5241 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5242 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5243 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5244 | |
| 5245 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5246 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5247 | |
| 5248 | hlua_sendlog(htxn->s->be, level, msg); |
| 5249 | return 0; |
| 5250 | } |
| 5251 | |
| 5252 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5253 | { |
| 5254 | const char *msg; |
| 5255 | struct hlua_txn *htxn; |
| 5256 | |
| 5257 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5258 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5259 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5260 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5261 | return 0; |
| 5262 | } |
| 5263 | |
| 5264 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5265 | { |
| 5266 | const char *msg; |
| 5267 | struct hlua_txn *htxn; |
| 5268 | |
| 5269 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5270 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5271 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5272 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5273 | return 0; |
| 5274 | } |
| 5275 | |
| 5276 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5277 | { |
| 5278 | const char *msg; |
| 5279 | struct hlua_txn *htxn; |
| 5280 | |
| 5281 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5282 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5283 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5284 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5285 | return 0; |
| 5286 | } |
| 5287 | |
| 5288 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5289 | { |
| 5290 | const char *msg; |
| 5291 | struct hlua_txn *htxn; |
| 5292 | |
| 5293 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5294 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5295 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5296 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5297 | return 0; |
| 5298 | } |
| 5299 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5300 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5301 | { |
| 5302 | struct hlua_txn *htxn; |
| 5303 | int ll; |
| 5304 | |
| 5305 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5306 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5307 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5308 | |
| 5309 | if (ll < 0 || ll > 7) |
| 5310 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5311 | |
| 5312 | htxn->s->logs.level = ll; |
| 5313 | return 0; |
| 5314 | } |
| 5315 | |
| 5316 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5317 | { |
| 5318 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5319 | int tos; |
| 5320 | |
| 5321 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5322 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5323 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5324 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5325 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5326 | return 0; |
| 5327 | } |
| 5328 | |
| 5329 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5330 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5331 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5332 | int mark; |
| 5333 | |
| 5334 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5335 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5336 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5337 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5338 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5339 | return 0; |
| 5340 | } |
| 5341 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5342 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5343 | { |
| 5344 | struct hlua_txn *htxn; |
| 5345 | |
| 5346 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5347 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5348 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5349 | return 0; |
| 5350 | } |
| 5351 | |
| 5352 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5353 | { |
| 5354 | struct hlua_txn *htxn; |
| 5355 | |
| 5356 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5357 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5358 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5359 | return 0; |
| 5360 | } |
| 5361 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5362 | /* This function is an Lua binding that send pending data |
| 5363 | * to the client, and close the stream interface. |
| 5364 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5365 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5366 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5367 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5368 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5369 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5370 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5371 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5372 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5373 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5374 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5375 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5376 | * session, so we just end the execution of the current |
| 5377 | * lua code. |
| 5378 | */ |
| 5379 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5380 | WILL_LJMP(hlua_done(L)); |
| 5381 | return 0; |
| 5382 | } |
| 5383 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5384 | ic = &htxn->s->req; |
| 5385 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5386 | |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5387 | if (IS_HTX_STRM(htxn->s)) { |
| 5388 | htxn->s->txn->status = 0; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5389 | http_reply_and_close(htxn->s, 0, NULL); |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5390 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5391 | else { |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5392 | channel_auto_read(ic); |
| 5393 | channel_abort(ic); |
| 5394 | channel_auto_close(ic); |
| 5395 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5396 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5397 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 5398 | channel_auto_read(oc); |
| 5399 | channel_auto_close(oc); |
| 5400 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5401 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5402 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5403 | |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5404 | ic->analysers &= AN_REQ_FLT_END; |
| 5405 | oc->analysers &= AN_RES_FLT_END; |
| 5406 | |
| 5407 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 5408 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 5409 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5410 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5411 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5412 | return 0; |
| 5413 | } |
| 5414 | |
| 5415 | __LJMP static int hlua_log(lua_State *L) |
| 5416 | { |
| 5417 | int level; |
| 5418 | const char *msg; |
| 5419 | |
| 5420 | MAY_LJMP(check_args(L, 2, "log")); |
| 5421 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5422 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5423 | |
| 5424 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5425 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5426 | |
| 5427 | hlua_sendlog(NULL, level, msg); |
| 5428 | return 0; |
| 5429 | } |
| 5430 | |
| 5431 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5432 | { |
| 5433 | const char *msg; |
| 5434 | |
| 5435 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5436 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5437 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5438 | return 0; |
| 5439 | } |
| 5440 | |
| 5441 | __LJMP static int hlua_log_info(lua_State *L) |
| 5442 | { |
| 5443 | const char *msg; |
| 5444 | |
| 5445 | MAY_LJMP(check_args(L, 1, "info")); |
| 5446 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5447 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5448 | return 0; |
| 5449 | } |
| 5450 | |
| 5451 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5452 | { |
| 5453 | const char *msg; |
| 5454 | |
| 5455 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5456 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5457 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5458 | return 0; |
| 5459 | } |
| 5460 | |
| 5461 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5462 | { |
| 5463 | const char *msg; |
| 5464 | |
| 5465 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5466 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5467 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5468 | return 0; |
| 5469 | } |
| 5470 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5471 | __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] | 5472 | { |
| 5473 | int wakeup_ms = lua_tointeger(L, -1); |
| 5474 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5475 | 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] | 5476 | return 0; |
| 5477 | } |
| 5478 | |
| 5479 | __LJMP static int hlua_sleep(lua_State *L) |
| 5480 | { |
| 5481 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5482 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5483 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5484 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5485 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5486 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5487 | wakeup_ms = tick_add(now_ms, delay); |
| 5488 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5489 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5490 | 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] | 5491 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5492 | } |
| 5493 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5494 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5495 | { |
| 5496 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5497 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5498 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5499 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5500 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5501 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5502 | wakeup_ms = tick_add(now_ms, delay); |
| 5503 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5504 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5505 | 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] | 5506 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5507 | } |
| 5508 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5509 | /* This functionis an LUA binding. it permits to give back |
| 5510 | * the hand at the HAProxy scheduler. It is used when the |
| 5511 | * LUA processing consumes a lot of time. |
| 5512 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5513 | __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] | 5514 | { |
| 5515 | return 0; |
| 5516 | } |
| 5517 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5518 | __LJMP static int hlua_yield(lua_State *L) |
| 5519 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5520 | 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] | 5521 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5522 | } |
| 5523 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5524 | /* This function change the nice of the currently executed |
| 5525 | * task. It is used set low or high priority at the current |
| 5526 | * task. |
| 5527 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5528 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5529 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5530 | struct hlua *hlua; |
| 5531 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5532 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5533 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5534 | hlua = hlua_gethlua(L); |
| 5535 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5536 | |
| 5537 | /* If he task is not set, I'm in a start mode. */ |
| 5538 | if (!hlua || !hlua->task) |
| 5539 | return 0; |
| 5540 | |
| 5541 | if (nice < -1024) |
| 5542 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5543 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5544 | nice = 1024; |
| 5545 | |
| 5546 | hlua->task->nice = nice; |
| 5547 | return 0; |
| 5548 | } |
| 5549 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5550 | /* 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] | 5551 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5552 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5553 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5554 | * |
| 5555 | * Task wrapper are longjmp safe because the only one Lua code |
| 5556 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5557 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 5558 | 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] | 5559 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5560 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5561 | enum hlua_exec status; |
| 5562 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5563 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5564 | task_set_affinity(task, tid_bit); |
| 5565 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5566 | /* If it is the first call to the task, we must initialize the |
| 5567 | * execution timeouts. |
| 5568 | */ |
| 5569 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5570 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5571 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5572 | /* Execute the Lua code. */ |
| 5573 | status = hlua_ctx_resume(hlua, 1); |
| 5574 | |
| 5575 | switch (status) { |
| 5576 | /* finished or yield */ |
| 5577 | case HLUA_E_OK: |
| 5578 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5579 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5580 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5581 | break; |
| 5582 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5583 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5584 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5585 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5586 | break; |
| 5587 | |
| 5588 | /* finished with error. */ |
| 5589 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5590 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5591 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5592 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5593 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5594 | break; |
| 5595 | |
| 5596 | case HLUA_E_ERR: |
| 5597 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5598 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5599 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5600 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5601 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5602 | break; |
| 5603 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5604 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5605 | } |
| 5606 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5607 | /* This function is an LUA binding that register LUA function to be |
| 5608 | * executed after the HAProxy configuration parsing and before the |
| 5609 | * HAProxy scheduler starts. This function expect only one LUA |
| 5610 | * argument that is a function. This function returns nothing, but |
| 5611 | * throws if an error is encountered. |
| 5612 | */ |
| 5613 | __LJMP static int hlua_register_init(lua_State *L) |
| 5614 | { |
| 5615 | struct hlua_init_function *init; |
| 5616 | int ref; |
| 5617 | |
| 5618 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5619 | |
| 5620 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5621 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5622 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5623 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5624 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5625 | |
| 5626 | init->function_ref = ref; |
| 5627 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5628 | return 0; |
| 5629 | } |
| 5630 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5631 | /* This functio is an LUA binding. It permits to register a task |
| 5632 | * executed in parallel of the main HAroxy activity. The task is |
| 5633 | * created and it is set in the HAProxy scheduler. It can be called |
| 5634 | * from the "init" section, "post init" or during the runtime. |
| 5635 | * |
| 5636 | * Lua prototype: |
| 5637 | * |
| 5638 | * <none> core.register_task(<function>) |
| 5639 | */ |
| 5640 | static int hlua_register_task(lua_State *L) |
| 5641 | { |
| 5642 | struct hlua *hlua; |
| 5643 | struct task *task; |
| 5644 | int ref; |
| 5645 | |
| 5646 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5647 | |
| 5648 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5649 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5650 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5651 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5652 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5653 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5654 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5655 | if (!task) |
| 5656 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5657 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5658 | task->context = hlua; |
| 5659 | task->process = hlua_process_task; |
| 5660 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5661 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5662 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5663 | |
| 5664 | /* Restore the function in the stack. */ |
| 5665 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5666 | hlua->nargs = 0; |
| 5667 | |
| 5668 | /* Schedule task. */ |
| 5669 | task_schedule(task, now_ms); |
| 5670 | |
| 5671 | return 0; |
| 5672 | } |
| 5673 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5674 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5675 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5676 | * resume converters. |
| 5677 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5678 | 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] | 5679 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5680 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5681 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5682 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5683 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5684 | if (!stream) |
| 5685 | return 0; |
| 5686 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5687 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5688 | * Lua context can be not initialized. This behavior |
| 5689 | * permits to save performances because a systematic |
| 5690 | * Lua initialization cause 5% performances loss. |
| 5691 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5692 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5693 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5694 | if (!stream->hlua) { |
| 5695 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5696 | return 0; |
| 5697 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5698 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5699 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5700 | return 0; |
| 5701 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5702 | } |
| 5703 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5704 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5705 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5706 | |
| 5707 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5708 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5709 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5710 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5711 | else |
| 5712 | error = "critical error"; |
| 5713 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5714 | return 0; |
| 5715 | } |
| 5716 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5717 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5718 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5719 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5720 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5721 | return 0; |
| 5722 | } |
| 5723 | |
| 5724 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5725 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5726 | |
| 5727 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5728 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5729 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5730 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5731 | return 0; |
| 5732 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5733 | hlua_smp2lua(stream->hlua->T, smp); |
| 5734 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5735 | |
| 5736 | /* push keywords in the stack. */ |
| 5737 | if (arg_p) { |
| 5738 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5739 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5740 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5741 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5744 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5745 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5746 | } |
| 5747 | } |
| 5748 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5749 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5750 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5751 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5752 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5753 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5754 | } |
| 5755 | |
| 5756 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5757 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5758 | /* finished. */ |
| 5759 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5760 | /* If the stack is empty, the function fails. */ |
| 5761 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5762 | return 0; |
| 5763 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5764 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5765 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5766 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5767 | return 1; |
| 5768 | |
| 5769 | /* yield. */ |
| 5770 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5771 | 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] | 5772 | return 0; |
| 5773 | |
| 5774 | /* finished with error. */ |
| 5775 | case HLUA_E_ERRMSG: |
| 5776 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5777 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5778 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5779 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5780 | return 0; |
| 5781 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5782 | case HLUA_E_ETMOUT: |
| 5783 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5784 | return 0; |
| 5785 | |
| 5786 | case HLUA_E_NOMEM: |
| 5787 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5788 | return 0; |
| 5789 | |
| 5790 | case HLUA_E_YIELD: |
| 5791 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5792 | return 0; |
| 5793 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5794 | case HLUA_E_ERR: |
| 5795 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5796 | 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] | 5797 | |
| 5798 | default: |
| 5799 | return 0; |
| 5800 | } |
| 5801 | } |
| 5802 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5803 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5804 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5805 | * resume sample-fetches. This function will be called by the sample |
| 5806 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5807 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5808 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5809 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5810 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5811 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5812 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5813 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5814 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5815 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5816 | if (!stream) |
| 5817 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5818 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5819 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5820 | * Lua context can be not initialized. This behavior |
| 5821 | * permits to save performances because a systematic |
| 5822 | * Lua initialization cause 5% performances loss. |
| 5823 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5824 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5825 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5826 | if (!stream->hlua) { |
| 5827 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5828 | return 0; |
| 5829 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5830 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5831 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5832 | return 0; |
| 5833 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5834 | } |
| 5835 | |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5836 | if (stream->be->mode == PR_MODE_HTTP) { |
| 5837 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 5838 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 5839 | else |
| 5840 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 5841 | } |
| 5842 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5843 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5844 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5845 | |
| 5846 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5847 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5848 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5849 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5850 | else |
| 5851 | error = "critical error"; |
| 5852 | 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] | 5853 | return 0; |
| 5854 | } |
| 5855 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5856 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5857 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5858 | 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] | 5859 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5860 | return 0; |
| 5861 | } |
| 5862 | |
| 5863 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5864 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5865 | |
| 5866 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5867 | 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] | 5868 | 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] | 5869 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5870 | return 0; |
| 5871 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5872 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5873 | |
| 5874 | /* push keywords in the stack. */ |
| 5875 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5876 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5877 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5878 | 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] | 5879 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5880 | return 0; |
| 5881 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5882 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5883 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5884 | } |
| 5885 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5886 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5887 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5888 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5889 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5890 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5891 | } |
| 5892 | |
| 5893 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5894 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5895 | /* finished. */ |
| 5896 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5897 | /* If the stack is empty, the function fails. */ |
| 5898 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5899 | return 0; |
| 5900 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5901 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5902 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5903 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5904 | |
| 5905 | /* Set the end of execution flag. */ |
| 5906 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5907 | return 1; |
| 5908 | |
| 5909 | /* yield. */ |
| 5910 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5911 | 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] | 5912 | return 0; |
| 5913 | |
| 5914 | /* finished with error. */ |
| 5915 | case HLUA_E_ERRMSG: |
| 5916 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5917 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5918 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5919 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5920 | return 0; |
| 5921 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5922 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5923 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5924 | return 0; |
| 5925 | |
| 5926 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5927 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5928 | return 0; |
| 5929 | |
| 5930 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5931 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5932 | return 0; |
| 5933 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5934 | case HLUA_E_ERR: |
| 5935 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5936 | 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] | 5937 | |
| 5938 | default: |
| 5939 | return 0; |
| 5940 | } |
| 5941 | } |
| 5942 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5943 | /* This function is an LUA binding used for registering |
| 5944 | * "sample-conv" functions. It expects a converter name used |
| 5945 | * in the haproxy configuration file, and an LUA function. |
| 5946 | */ |
| 5947 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5948 | { |
| 5949 | struct sample_conv_kw_list *sck; |
| 5950 | const char *name; |
| 5951 | int ref; |
| 5952 | int len; |
| 5953 | struct hlua_function *fcn; |
| 5954 | |
| 5955 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5956 | |
| 5957 | /* First argument : converter name. */ |
| 5958 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5959 | |
| 5960 | /* Second argument : lua function. */ |
| 5961 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5962 | |
| 5963 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5964 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5965 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5966 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5967 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5968 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5969 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5970 | |
| 5971 | /* Fill fcn. */ |
| 5972 | fcn->name = strdup(name); |
| 5973 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5974 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5975 | fcn->function_ref = ref; |
| 5976 | |
| 5977 | /* List head */ |
| 5978 | sck->list.n = sck->list.p = NULL; |
| 5979 | |
| 5980 | /* converter keyword. */ |
| 5981 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5982 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5983 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5984 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5985 | |
| 5986 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 5987 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 5988 | 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] | 5989 | sck->kw[0].val_args = NULL; |
| 5990 | sck->kw[0].in_type = SMP_T_STR; |
| 5991 | sck->kw[0].out_type = SMP_T_STR; |
| 5992 | sck->kw[0].private = fcn; |
| 5993 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5994 | /* Register this new converter */ |
| 5995 | sample_register_convs(sck); |
| 5996 | |
| 5997 | return 0; |
| 5998 | } |
| 5999 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6000 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6001 | * "sample-fetch" functions. It expects a converter name used |
| 6002 | * in the haproxy configuration file, and an LUA function. |
| 6003 | */ |
| 6004 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6005 | { |
| 6006 | const char *name; |
| 6007 | int ref; |
| 6008 | int len; |
| 6009 | struct sample_fetch_kw_list *sfk; |
| 6010 | struct hlua_function *fcn; |
| 6011 | |
| 6012 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6013 | |
| 6014 | /* First argument : sample-fetch name. */ |
| 6015 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6016 | |
| 6017 | /* Second argument : lua function. */ |
| 6018 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6019 | |
| 6020 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6021 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6022 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6023 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6024 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6025 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6026 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6027 | |
| 6028 | /* Fill fcn. */ |
| 6029 | fcn->name = strdup(name); |
| 6030 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6031 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6032 | fcn->function_ref = ref; |
| 6033 | |
| 6034 | /* List head */ |
| 6035 | sfk->list.n = sfk->list.p = NULL; |
| 6036 | |
| 6037 | /* sample-fetch keyword. */ |
| 6038 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6039 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6040 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6041 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6042 | |
| 6043 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6044 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6045 | 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] | 6046 | sfk->kw[0].val_args = NULL; |
| 6047 | sfk->kw[0].out_type = SMP_T_STR; |
| 6048 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6049 | sfk->kw[0].val = 0; |
| 6050 | sfk->kw[0].private = fcn; |
| 6051 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6052 | /* Register this new fetch. */ |
| 6053 | sample_register_fetches(sfk); |
| 6054 | |
| 6055 | return 0; |
| 6056 | } |
| 6057 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6058 | /* This function is a wrapper to execute each LUA function declared |
| 6059 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6060 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6061 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6062 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6063 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6064 | 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] | 6065 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6066 | { |
| 6067 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6068 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6069 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6070 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6071 | |
| 6072 | switch (rule->from) { |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6073 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6074 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6075 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6076 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6077 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6078 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6079 | return ACT_RET_CONT; |
| 6080 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6081 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6082 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6083 | * Lua context can be not initialized. This behavior |
| 6084 | * permits to save performances because a systematic |
| 6085 | * Lua initialization cause 5% performances loss. |
| 6086 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6087 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6088 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6089 | if (!s->hlua) { |
| 6090 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6091 | rule->arg.hlua_rule->fcn.name); |
| 6092 | return ACT_RET_CONT; |
| 6093 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6094 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6095 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6096 | rule->arg.hlua_rule->fcn.name); |
| 6097 | return ACT_RET_CONT; |
| 6098 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6099 | } |
| 6100 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6101 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6102 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6103 | |
| 6104 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6105 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6106 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6107 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6108 | else |
| 6109 | error = "critical error"; |
| 6110 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6111 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6112 | return ACT_RET_CONT; |
| 6113 | } |
| 6114 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6115 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6116 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6117 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6118 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6119 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6120 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6121 | } |
| 6122 | |
| 6123 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6124 | 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] | 6125 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6126 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6127 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6128 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6129 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6130 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6131 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6132 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6133 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6134 | |
| 6135 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6136 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6137 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6138 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6139 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6140 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6141 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6142 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6143 | lua_pushstring(s->hlua->T, *arg); |
| 6144 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6145 | } |
| 6146 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6147 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6148 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6149 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6150 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6151 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6152 | } |
| 6153 | |
| 6154 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6155 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6156 | /* finished. */ |
| 6157 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6158 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6159 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6160 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6161 | |
| 6162 | /* yield. */ |
| 6163 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6164 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6165 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6166 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6167 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6168 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6169 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6170 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6171 | /* Some actions can be wake up when a "write" event |
| 6172 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6173 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6174 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6175 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6176 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6177 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6178 | s->req.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6179 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6180 | |
| 6181 | /* finished with error. */ |
| 6182 | case HLUA_E_ERRMSG: |
| 6183 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6184 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6186 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6187 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6188 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6189 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6190 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6191 | return 0; |
| 6192 | |
| 6193 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6194 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6195 | return 0; |
| 6196 | |
| 6197 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6198 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6199 | rule->arg.hlua_rule->fcn.name); |
| 6200 | return 0; |
| 6201 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6202 | case HLUA_E_ERR: |
| 6203 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6204 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6205 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6206 | |
| 6207 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6208 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6209 | } |
| 6210 | } |
| 6211 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6212 | 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] | 6213 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6214 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6215 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6216 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6217 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6218 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6222 | { |
| 6223 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6224 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6225 | struct task *task; |
| 6226 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6227 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6228 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6229 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6230 | if (!hlua) { |
| 6231 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6232 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6233 | return 0; |
| 6234 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6235 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6236 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6237 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6238 | |
| 6239 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6240 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6241 | if (!task) { |
| 6242 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6243 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6244 | return 0; |
| 6245 | } |
| 6246 | task->nice = 0; |
| 6247 | task->context = ctx; |
| 6248 | task->process = hlua_applet_wakeup; |
| 6249 | ctx->ctx.hlua_apptcp.task = task; |
| 6250 | |
| 6251 | /* In the execution wrappers linked with a stream, the |
| 6252 | * Lua context can be not initialized. This behavior |
| 6253 | * permits to save performances because a systematic |
| 6254 | * Lua initialization cause 5% performances loss. |
| 6255 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6256 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6257 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6258 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6259 | return 0; |
| 6260 | } |
| 6261 | |
| 6262 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6263 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6264 | |
| 6265 | /* The following Lua calls can fail. */ |
| 6266 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6267 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6268 | error = lua_tostring(hlua->T, -1); |
| 6269 | else |
| 6270 | error = "critical error"; |
| 6271 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6272 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6273 | return 0; |
| 6274 | } |
| 6275 | |
| 6276 | /* Check stack available size. */ |
| 6277 | if (!lua_checkstack(hlua->T, 1)) { |
| 6278 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6279 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6280 | RESET_SAFE_LJMP(hlua->T); |
| 6281 | return 0; |
| 6282 | } |
| 6283 | |
| 6284 | /* Restore the function in the stack. */ |
| 6285 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6286 | |
| 6287 | /* Create and and push object stream in the stack. */ |
| 6288 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6289 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6290 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6291 | RESET_SAFE_LJMP(hlua->T); |
| 6292 | return 0; |
| 6293 | } |
| 6294 | hlua->nargs = 1; |
| 6295 | |
| 6296 | /* push keywords in the stack. */ |
| 6297 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6298 | if (!lua_checkstack(hlua->T, 1)) { |
| 6299 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6300 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6301 | RESET_SAFE_LJMP(hlua->T); |
| 6302 | return 0; |
| 6303 | } |
| 6304 | lua_pushstring(hlua->T, *arg); |
| 6305 | hlua->nargs++; |
| 6306 | } |
| 6307 | |
| 6308 | RESET_SAFE_LJMP(hlua->T); |
| 6309 | |
| 6310 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6311 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6312 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6313 | |
| 6314 | return 1; |
| 6315 | } |
| 6316 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6317 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6318 | { |
| 6319 | struct stream_interface *si = ctx->owner; |
| 6320 | struct stream *strm = si_strm(si); |
| 6321 | struct channel *res = si_ic(si); |
| 6322 | struct act_rule *rule = ctx->rule; |
| 6323 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6324 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6325 | |
| 6326 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6327 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6328 | /* eat the whole request */ |
| 6329 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6330 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6331 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6332 | |
| 6333 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6334 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6335 | return; |
| 6336 | |
| 6337 | /* Execute the function. */ |
| 6338 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6339 | /* finished. */ |
| 6340 | case HLUA_E_OK: |
| 6341 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6342 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6343 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6344 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6345 | res->flags |= CF_READ_NULL; |
| 6346 | si_shutr(si); |
| 6347 | return; |
| 6348 | |
| 6349 | /* yield. */ |
| 6350 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6351 | if (hlua->wake_time != TICK_ETERNITY) |
| 6352 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6353 | return; |
| 6354 | |
| 6355 | /* finished with error. */ |
| 6356 | case HLUA_E_ERRMSG: |
| 6357 | /* Display log. */ |
| 6358 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6359 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6360 | lua_pop(hlua->T, 1); |
| 6361 | goto error; |
| 6362 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6363 | case HLUA_E_ETMOUT: |
| 6364 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6365 | rule->arg.hlua_rule->fcn.name); |
| 6366 | goto error; |
| 6367 | |
| 6368 | case HLUA_E_NOMEM: |
| 6369 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6370 | rule->arg.hlua_rule->fcn.name); |
| 6371 | goto error; |
| 6372 | |
| 6373 | case HLUA_E_YIELD: /* unexpected */ |
| 6374 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6375 | rule->arg.hlua_rule->fcn.name); |
| 6376 | goto error; |
| 6377 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6378 | case HLUA_E_ERR: |
| 6379 | /* Display log. */ |
| 6380 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6381 | rule->arg.hlua_rule->fcn.name); |
| 6382 | goto error; |
| 6383 | |
| 6384 | default: |
| 6385 | goto error; |
| 6386 | } |
| 6387 | |
| 6388 | error: |
| 6389 | |
| 6390 | /* For all other cases, just close the stream. */ |
| 6391 | si_shutw(si); |
| 6392 | si_shutr(si); |
| 6393 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6394 | } |
| 6395 | |
| 6396 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6397 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6398 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6399 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6400 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6401 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6402 | } |
| 6403 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6404 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6405 | * an errors occurs and -1 if more data are required for initializing |
| 6406 | * the applet. |
| 6407 | */ |
| 6408 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6409 | { |
| 6410 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6411 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6412 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6413 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6414 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6415 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6416 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6417 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6418 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6419 | if (!hlua) { |
| 6420 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6421 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6422 | return 0; |
| 6423 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6424 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6425 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6426 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6427 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6428 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6429 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6430 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6431 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6432 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6433 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6434 | if (!task) { |
| 6435 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6436 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6437 | return 0; |
| 6438 | } |
| 6439 | task->nice = 0; |
| 6440 | task->context = ctx; |
| 6441 | task->process = hlua_applet_wakeup; |
| 6442 | ctx->ctx.hlua_apphttp.task = task; |
| 6443 | |
| 6444 | /* In the execution wrappers linked with a stream, the |
| 6445 | * Lua context can be not initialized. This behavior |
| 6446 | * permits to save performances because a systematic |
| 6447 | * Lua initialization cause 5% performances loss. |
| 6448 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6449 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6450 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6451 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6452 | return 0; |
| 6453 | } |
| 6454 | |
| 6455 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6456 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6457 | |
| 6458 | /* The following Lua calls can fail. */ |
| 6459 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6460 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6461 | error = lua_tostring(hlua->T, -1); |
| 6462 | else |
| 6463 | error = "critical error"; |
| 6464 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6465 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6466 | return 0; |
| 6467 | } |
| 6468 | |
| 6469 | /* Check stack available size. */ |
| 6470 | if (!lua_checkstack(hlua->T, 1)) { |
| 6471 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6472 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6473 | RESET_SAFE_LJMP(hlua->T); |
| 6474 | return 0; |
| 6475 | } |
| 6476 | |
| 6477 | /* Restore the function in the stack. */ |
| 6478 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6479 | |
| 6480 | /* Create and and push object stream in the stack. */ |
| 6481 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6482 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6483 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6484 | RESET_SAFE_LJMP(hlua->T); |
| 6485 | return 0; |
| 6486 | } |
| 6487 | hlua->nargs = 1; |
| 6488 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6489 | /* push keywords in the stack. */ |
| 6490 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6491 | if (!lua_checkstack(hlua->T, 1)) { |
| 6492 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6493 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6494 | RESET_SAFE_LJMP(hlua->T); |
| 6495 | return 0; |
| 6496 | } |
| 6497 | lua_pushstring(hlua->T, *arg); |
| 6498 | hlua->nargs++; |
| 6499 | } |
| 6500 | |
| 6501 | RESET_SAFE_LJMP(hlua->T); |
| 6502 | |
| 6503 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6504 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6505 | |
| 6506 | return 1; |
| 6507 | } |
| 6508 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6509 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6510 | { |
| 6511 | struct stream_interface *si = ctx->owner; |
| 6512 | struct stream *strm = si_strm(si); |
| 6513 | struct channel *req = si_oc(si); |
| 6514 | struct channel *res = si_ic(si); |
| 6515 | struct act_rule *rule = ctx->rule; |
| 6516 | struct proxy *px = strm->be; |
| 6517 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 6518 | struct htx *req_htx, *res_htx; |
| 6519 | |
| 6520 | res_htx = htx_from_buf(&res->buf); |
| 6521 | |
| 6522 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6523 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6524 | goto out; |
| 6525 | |
| 6526 | /* Check if the input buffer is avalaible. */ |
| 6527 | if (!b_size(&res->buf)) { |
| 6528 | si_rx_room_blk(si); |
| 6529 | goto out; |
| 6530 | } |
| 6531 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6532 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6533 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6534 | |
| 6535 | /* Set the currently running flag. */ |
| 6536 | if (!HLUA_IS_RUNNING(hlua) && |
| 6537 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6538 | struct htx_blk *blk; |
| 6539 | size_t count = co_data(req); |
| 6540 | |
| 6541 | if (!count) { |
| 6542 | si_cant_get(si); |
| 6543 | goto out; |
| 6544 | } |
| 6545 | |
| 6546 | /* We need to flush the request header. This left the body for |
| 6547 | * the Lua. |
| 6548 | */ |
| 6549 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 6550 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6551 | while (count && blk) { |
| 6552 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6553 | uint32_t sz = htx_get_blksz(blk); |
| 6554 | |
| 6555 | if (sz > count) { |
| 6556 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6557 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6558 | goto out; |
| 6559 | } |
| 6560 | |
| 6561 | count -= sz; |
| 6562 | co_set_data(req, co_data(req) - sz); |
| 6563 | blk = htx_remove_blk(req_htx, blk); |
| 6564 | |
| 6565 | if (type == HTX_BLK_EOH) |
| 6566 | break; |
| 6567 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6568 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6569 | } |
| 6570 | |
| 6571 | /* Executes The applet if it is not done. */ |
| 6572 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6573 | |
| 6574 | /* Execute the function. */ |
| 6575 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6576 | /* finished. */ |
| 6577 | case HLUA_E_OK: |
| 6578 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6579 | break; |
| 6580 | |
| 6581 | /* yield. */ |
| 6582 | case HLUA_E_AGAIN: |
| 6583 | if (hlua->wake_time != TICK_ETERNITY) |
| 6584 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6585 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6586 | |
| 6587 | /* finished with error. */ |
| 6588 | case HLUA_E_ERRMSG: |
| 6589 | /* Display log. */ |
| 6590 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6591 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6592 | lua_pop(hlua->T, 1); |
| 6593 | goto error; |
| 6594 | |
| 6595 | case HLUA_E_ETMOUT: |
| 6596 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6597 | rule->arg.hlua_rule->fcn.name); |
| 6598 | goto error; |
| 6599 | |
| 6600 | case HLUA_E_NOMEM: |
| 6601 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6602 | rule->arg.hlua_rule->fcn.name); |
| 6603 | goto error; |
| 6604 | |
| 6605 | case HLUA_E_YIELD: /* unexpected */ |
| 6606 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6607 | rule->arg.hlua_rule->fcn.name); |
| 6608 | goto error; |
| 6609 | |
| 6610 | case HLUA_E_ERR: |
| 6611 | /* Display log. */ |
| 6612 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6613 | rule->arg.hlua_rule->fcn.name); |
| 6614 | goto error; |
| 6615 | |
| 6616 | default: |
| 6617 | goto error; |
| 6618 | } |
| 6619 | } |
| 6620 | |
| 6621 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6622 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 6623 | goto done; |
| 6624 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6625 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 6626 | goto error; |
| 6627 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 6628 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6629 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 6630 | si_rx_room_blk(si); |
| 6631 | goto out; |
| 6632 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6633 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6634 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6635 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6636 | } |
| 6637 | |
| 6638 | done: |
| 6639 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6640 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6641 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6642 | si_shutr(si); |
| 6643 | } |
| 6644 | |
| 6645 | /* eat the whole request */ |
| 6646 | if (co_data(req)) { |
| 6647 | req_htx = htx_from_buf(&req->buf); |
| 6648 | co_htx_skip(req, req_htx, co_data(req)); |
| 6649 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6650 | } |
| 6651 | } |
| 6652 | |
| 6653 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6654 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6655 | return; |
| 6656 | |
| 6657 | error: |
| 6658 | |
| 6659 | /* If we are in HTTP mode, and we are not send any |
| 6660 | * data, return a 500 server error in best effort: |
| 6661 | * if there is no room available in the buffer, |
| 6662 | * just close the connection. |
| 6663 | */ |
| 6664 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 6665 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6666 | |
| 6667 | channel_erase(res); |
| 6668 | res->buf.data = b_data(err); |
| 6669 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 6670 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6671 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6672 | } |
| 6673 | if (!(strm->flags & SF_ERR_MASK)) |
| 6674 | strm->flags |= SF_ERR_RESOURCE; |
| 6675 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6676 | goto done; |
| 6677 | } |
| 6678 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6679 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6680 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6681 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6682 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6683 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6684 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6685 | } |
| 6686 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6687 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6688 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6689 | * |
| 6690 | * This function can fail with an abort() due to an Lua critical error. |
| 6691 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6692 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6693 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6694 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6695 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6696 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6697 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6698 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6699 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6700 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6701 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6702 | if (!rule->arg.hlua_rule) { |
| 6703 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6704 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6705 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6706 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6707 | /* Memory for arguments. */ |
| 6708 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6709 | if (!rule->arg.hlua_rule->args) { |
| 6710 | memprintf(err, "out of memory error"); |
| 6711 | return ACT_RET_PRS_ERR; |
| 6712 | } |
| 6713 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6714 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6715 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6716 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6717 | /* Expect some arguments */ |
| 6718 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6719 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6720 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6721 | return ACT_RET_PRS_ERR; |
| 6722 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6723 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6724 | if (!rule->arg.hlua_rule->args[i]) { |
| 6725 | memprintf(err, "out of memory error"); |
| 6726 | return ACT_RET_PRS_ERR; |
| 6727 | } |
| 6728 | (*cur_arg)++; |
| 6729 | } |
| 6730 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6731 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6732 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6733 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6734 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6735 | } |
| 6736 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6737 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6738 | struct act_rule *rule, char **err) |
| 6739 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6740 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6741 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6742 | /* HTTP applets are forbidden in tcp-request rules. |
| 6743 | * HTTP applet request requires everything initilized by |
| 6744 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6745 | * The applet will be immediately initilized, but its before |
| 6746 | * the call of this analyzer. |
| 6747 | */ |
| 6748 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6749 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6750 | return ACT_RET_PRS_ERR; |
| 6751 | } |
| 6752 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6753 | /* Memory for the rule. */ |
| 6754 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6755 | if (!rule->arg.hlua_rule) { |
| 6756 | memprintf(err, "out of memory error"); |
| 6757 | return ACT_RET_PRS_ERR; |
| 6758 | } |
| 6759 | |
| 6760 | /* Reference the Lua function and store the reference. */ |
| 6761 | rule->arg.hlua_rule->fcn = *fcn; |
| 6762 | |
| 6763 | /* TODO: later accept arguments. */ |
| 6764 | rule->arg.hlua_rule->args = NULL; |
| 6765 | |
| 6766 | /* Add applet pointer in the rule. */ |
| 6767 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6768 | rule->applet.name = fcn->name; |
| 6769 | rule->applet.init = hlua_applet_http_init; |
| 6770 | rule->applet.fct = hlua_applet_http_fct; |
| 6771 | rule->applet.release = hlua_applet_http_release; |
| 6772 | rule->applet.timeout = hlua_timeout_applet; |
| 6773 | |
| 6774 | return ACT_RET_PRS_OK; |
| 6775 | } |
| 6776 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6777 | /* This function is an LUA binding used for registering |
| 6778 | * "sample-conv" functions. It expects a converter name used |
| 6779 | * in the haproxy configuration file, and an LUA function. |
| 6780 | */ |
| 6781 | __LJMP static int hlua_register_action(lua_State *L) |
| 6782 | { |
| 6783 | struct action_kw_list *akl; |
| 6784 | const char *name; |
| 6785 | int ref; |
| 6786 | int len; |
| 6787 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6788 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6789 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6790 | /* Initialise the number of expected arguments at 0. */ |
| 6791 | nargs = 0; |
| 6792 | |
| 6793 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6794 | 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] | 6795 | |
| 6796 | /* First argument : converter name. */ |
| 6797 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6798 | |
| 6799 | /* Second argument : environment. */ |
| 6800 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6801 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6802 | |
| 6803 | /* Third argument : lua function. */ |
| 6804 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6805 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6806 | /* 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] | 6807 | if (lua_gettop(L) >= 4) |
| 6808 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6809 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6810 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6811 | lua_pushnil(L); |
| 6812 | while (lua_next(L, 2) != 0) { |
| 6813 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6814 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6815 | |
| 6816 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6817 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6818 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6819 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6820 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6821 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6822 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6823 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6824 | |
| 6825 | /* Fill fcn. */ |
| 6826 | fcn->name = strdup(name); |
| 6827 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6828 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6829 | fcn->function_ref = ref; |
| 6830 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6831 | /* Set the expected number od arguments. */ |
| 6832 | fcn->nargs = nargs; |
| 6833 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6834 | /* List head */ |
| 6835 | akl->list.n = akl->list.p = NULL; |
| 6836 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6837 | /* action keyword. */ |
| 6838 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6839 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6840 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6841 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6842 | |
| 6843 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6844 | |
| 6845 | akl->kw[0].match_pfx = 0; |
| 6846 | akl->kw[0].private = fcn; |
| 6847 | akl->kw[0].parse = action_register_lua; |
| 6848 | |
| 6849 | /* select the action registering point. */ |
| 6850 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6851 | tcp_req_cont_keywords_register(akl); |
| 6852 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6853 | tcp_res_cont_keywords_register(akl); |
| 6854 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6855 | http_req_keywords_register(akl); |
| 6856 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6857 | http_res_keywords_register(akl); |
| 6858 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6859 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6860 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6861 | "are expected.", lua_tostring(L, -1))); |
| 6862 | |
| 6863 | /* pop the environment string. */ |
| 6864 | lua_pop(L, 1); |
| 6865 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6866 | return ACT_RET_PRS_OK; |
| 6867 | } |
| 6868 | |
| 6869 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6870 | struct act_rule *rule, char **err) |
| 6871 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6872 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6873 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 6874 | if (px->mode == PR_MODE_HTTP) { |
| 6875 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6876 | return ACT_RET_PRS_ERR; |
| 6877 | } |
| 6878 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6879 | /* Memory for the rule. */ |
| 6880 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6881 | if (!rule->arg.hlua_rule) { |
| 6882 | memprintf(err, "out of memory error"); |
| 6883 | return ACT_RET_PRS_ERR; |
| 6884 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6885 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6886 | /* Reference the Lua function and store the reference. */ |
| 6887 | rule->arg.hlua_rule->fcn = *fcn; |
| 6888 | |
| 6889 | /* TODO: later accept arguments. */ |
| 6890 | rule->arg.hlua_rule->args = NULL; |
| 6891 | |
| 6892 | /* Add applet pointer in the rule. */ |
| 6893 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6894 | rule->applet.name = fcn->name; |
| 6895 | rule->applet.init = hlua_applet_tcp_init; |
| 6896 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6897 | rule->applet.release = hlua_applet_tcp_release; |
| 6898 | rule->applet.timeout = hlua_timeout_applet; |
| 6899 | |
| 6900 | return 0; |
| 6901 | } |
| 6902 | |
| 6903 | /* This function is an LUA binding used for registering |
| 6904 | * "sample-conv" functions. It expects a converter name used |
| 6905 | * in the haproxy configuration file, and an LUA function. |
| 6906 | */ |
| 6907 | __LJMP static int hlua_register_service(lua_State *L) |
| 6908 | { |
| 6909 | struct action_kw_list *akl; |
| 6910 | const char *name; |
| 6911 | const char *env; |
| 6912 | int ref; |
| 6913 | int len; |
| 6914 | struct hlua_function *fcn; |
| 6915 | |
| 6916 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6917 | |
| 6918 | /* First argument : converter name. */ |
| 6919 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6920 | |
| 6921 | /* Second argument : environment. */ |
| 6922 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6923 | |
| 6924 | /* Third argument : lua function. */ |
| 6925 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6926 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6927 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6928 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6929 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6930 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6931 | fcn = calloc(1, sizeof(*fcn)); |
| 6932 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6933 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6934 | |
| 6935 | /* Fill fcn. */ |
| 6936 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6937 | fcn->name = calloc(1, len); |
| 6938 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6939 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6940 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 6941 | fcn->function_ref = ref; |
| 6942 | |
| 6943 | /* List head */ |
| 6944 | akl->list.n = akl->list.p = NULL; |
| 6945 | |
| 6946 | /* converter keyword. */ |
| 6947 | len = strlen("lua.") + strlen(name) + 1; |
| 6948 | akl->kw[0].kw = calloc(1, len); |
| 6949 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6950 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6951 | |
| 6952 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6953 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 6954 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6955 | if (strcmp(env, "tcp") == 0) |
| 6956 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6957 | else if (strcmp(env, "http") == 0) |
| 6958 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6959 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6960 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 6961 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | |
| 6963 | akl->kw[0].match_pfx = 0; |
| 6964 | akl->kw[0].private = fcn; |
| 6965 | |
| 6966 | /* End of array. */ |
| 6967 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 6968 | |
| 6969 | /* Register this new converter */ |
| 6970 | service_keywords_register(akl); |
| 6971 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6972 | return 0; |
| 6973 | } |
| 6974 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6975 | /* This function initialises Lua cli handler. It copies the |
| 6976 | * arguments in the Lua stack and create channel IO objects. |
| 6977 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 6978 | 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] | 6979 | { |
| 6980 | struct hlua *hlua; |
| 6981 | struct hlua_function *fcn; |
| 6982 | int i; |
| 6983 | const char *error; |
| 6984 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6985 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6986 | appctx->ctx.hlua_cli.fcn = private; |
| 6987 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6988 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6989 | if (!hlua) { |
| 6990 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 6991 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6992 | } |
| 6993 | HLUA_INIT(hlua); |
| 6994 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 6995 | |
| 6996 | /* Create task used by signal to wakeup applets. |
| 6997 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 6998 | * applet_http. It is absolutely compatible. |
| 6999 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7000 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7001 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7002 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7003 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7004 | } |
| 7005 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7006 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7007 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7008 | |
| 7009 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7010 | 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] | 7011 | 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] | 7012 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7013 | } |
| 7014 | |
| 7015 | /* The following Lua calls can fail. */ |
| 7016 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7017 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7018 | error = lua_tostring(hlua->T, -1); |
| 7019 | else |
| 7020 | error = "critical error"; |
| 7021 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7022 | goto error; |
| 7023 | } |
| 7024 | |
| 7025 | /* Check stack available size. */ |
| 7026 | if (!lua_checkstack(hlua->T, 2)) { |
| 7027 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7028 | goto error; |
| 7029 | } |
| 7030 | |
| 7031 | /* Restore the function in the stack. */ |
| 7032 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7033 | |
| 7034 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7035 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7036 | */ |
| 7037 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7038 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7039 | goto error; |
| 7040 | } |
| 7041 | hlua->nargs = 1; |
| 7042 | |
| 7043 | /* push keywords in the stack. */ |
| 7044 | for (i = 0; *args[i]; i++) { |
| 7045 | /* Check stack available size. */ |
| 7046 | if (!lua_checkstack(hlua->T, 1)) { |
| 7047 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7048 | goto error; |
| 7049 | } |
| 7050 | lua_pushstring(hlua->T, args[i]); |
| 7051 | hlua->nargs++; |
| 7052 | } |
| 7053 | |
| 7054 | /* We must initialize the execution timeouts. */ |
| 7055 | hlua->max_time = hlua_timeout_session; |
| 7056 | |
| 7057 | /* At this point the execution is safe. */ |
| 7058 | RESET_SAFE_LJMP(hlua->T); |
| 7059 | |
| 7060 | /* It's ok */ |
| 7061 | return 0; |
| 7062 | |
| 7063 | /* It's not ok. */ |
| 7064 | error: |
| 7065 | RESET_SAFE_LJMP(hlua->T); |
| 7066 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7067 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7068 | return 1; |
| 7069 | } |
| 7070 | |
| 7071 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7072 | { |
| 7073 | struct hlua *hlua; |
| 7074 | struct stream_interface *si; |
| 7075 | struct hlua_function *fcn; |
| 7076 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7077 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7078 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7079 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7080 | |
| 7081 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7082 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7083 | return 1; |
| 7084 | |
| 7085 | /* Execute the function. */ |
| 7086 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7087 | |
| 7088 | /* finished. */ |
| 7089 | case HLUA_E_OK: |
| 7090 | return 1; |
| 7091 | |
| 7092 | /* yield. */ |
| 7093 | case HLUA_E_AGAIN: |
| 7094 | /* We want write. */ |
| 7095 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7096 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7097 | /* Set the timeout. */ |
| 7098 | if (hlua->wake_time != TICK_ETERNITY) |
| 7099 | task_schedule(hlua->task, hlua->wake_time); |
| 7100 | return 0; |
| 7101 | |
| 7102 | /* finished with error. */ |
| 7103 | case HLUA_E_ERRMSG: |
| 7104 | /* Display log. */ |
| 7105 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7106 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7107 | lua_pop(hlua->T, 1); |
| 7108 | return 1; |
| 7109 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7110 | case HLUA_E_ETMOUT: |
| 7111 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7112 | fcn->name); |
| 7113 | return 1; |
| 7114 | |
| 7115 | case HLUA_E_NOMEM: |
| 7116 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7117 | fcn->name); |
| 7118 | return 1; |
| 7119 | |
| 7120 | case HLUA_E_YIELD: /* unexpected */ |
| 7121 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7122 | fcn->name); |
| 7123 | return 1; |
| 7124 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7125 | case HLUA_E_ERR: |
| 7126 | /* Display log. */ |
| 7127 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7128 | fcn->name); |
| 7129 | return 1; |
| 7130 | |
| 7131 | default: |
| 7132 | return 1; |
| 7133 | } |
| 7134 | |
| 7135 | return 1; |
| 7136 | } |
| 7137 | |
| 7138 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7139 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7140 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7141 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7142 | } |
| 7143 | |
| 7144 | /* This function is an LUA binding used for registering |
| 7145 | * new keywords in the cli. It expects a list of keywords |
| 7146 | * which are the "path". It is limited to 5 keywords. A |
| 7147 | * description of the command, a function to be executed |
| 7148 | * for the parsing and a function for io handlers. |
| 7149 | */ |
| 7150 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7151 | { |
| 7152 | struct cli_kw_list *cli_kws; |
| 7153 | const char *message; |
| 7154 | int ref_io; |
| 7155 | int len; |
| 7156 | struct hlua_function *fcn; |
| 7157 | int index; |
| 7158 | int i; |
| 7159 | |
| 7160 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7161 | |
| 7162 | /* First argument : an array of maximum 5 keywords. */ |
| 7163 | if (!lua_istable(L, 1)) |
| 7164 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7165 | |
| 7166 | /* Second argument : string with contextual message. */ |
| 7167 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7168 | |
| 7169 | /* Third and fourth argument : lua function. */ |
| 7170 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7171 | |
| 7172 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7173 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7174 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7175 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7176 | fcn = calloc(1, sizeof(*fcn)); |
| 7177 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7178 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7179 | |
| 7180 | /* Fill path. */ |
| 7181 | index = 0; |
| 7182 | lua_pushnil(L); |
| 7183 | while(lua_next(L, 1) != 0) { |
| 7184 | if (index >= 5) |
| 7185 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7186 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7187 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7188 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7189 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7190 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7191 | index++; |
| 7192 | lua_pop(L, 1); |
| 7193 | } |
| 7194 | |
| 7195 | /* Copy help message. */ |
| 7196 | cli_kws->kw[0].usage = strdup(message); |
| 7197 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7198 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7199 | |
| 7200 | /* Fill fcn io handler. */ |
| 7201 | len = strlen("<lua.cli>") + 1; |
| 7202 | for (i = 0; i < index; i++) |
| 7203 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7204 | fcn->name = calloc(1, len); |
| 7205 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7206 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7207 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7208 | for (i = 0; i < index; i++) { |
| 7209 | strncat((char *)fcn->name, ".", len); |
| 7210 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7211 | } |
| 7212 | strncat((char *)fcn->name, ">", len); |
| 7213 | fcn->function_ref = ref_io; |
| 7214 | |
| 7215 | /* Fill last entries. */ |
| 7216 | cli_kws->kw[0].private = fcn; |
| 7217 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7218 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7219 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7220 | |
| 7221 | /* Register this new converter */ |
| 7222 | cli_register_kw(cli_kws); |
| 7223 | |
| 7224 | return 0; |
| 7225 | } |
| 7226 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7227 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7228 | struct proxy *defpx, const char *file, int line, |
| 7229 | char **err, unsigned int *timeout) |
| 7230 | { |
| 7231 | const char *error; |
| 7232 | |
| 7233 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7234 | if (error == PARSE_TIME_OVER) { |
| 7235 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7236 | args[1], args[0]); |
| 7237 | return -1; |
| 7238 | } |
| 7239 | else if (error == PARSE_TIME_UNDER) { |
| 7240 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7241 | args[1], args[0]); |
| 7242 | return -1; |
| 7243 | } |
| 7244 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7245 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7246 | return -1; |
| 7247 | } |
| 7248 | return 0; |
| 7249 | } |
| 7250 | |
| 7251 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7252 | struct proxy *defpx, const char *file, int line, |
| 7253 | char **err) |
| 7254 | { |
| 7255 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7256 | file, line, err, &hlua_timeout_session); |
| 7257 | } |
| 7258 | |
| 7259 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7260 | struct proxy *defpx, const char *file, int line, |
| 7261 | char **err) |
| 7262 | { |
| 7263 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7264 | file, line, err, &hlua_timeout_task); |
| 7265 | } |
| 7266 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7267 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7268 | struct proxy *defpx, const char *file, int line, |
| 7269 | char **err) |
| 7270 | { |
| 7271 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7272 | file, line, err, &hlua_timeout_applet); |
| 7273 | } |
| 7274 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7275 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7276 | struct proxy *defpx, const char *file, int line, |
| 7277 | char **err) |
| 7278 | { |
| 7279 | char *error; |
| 7280 | |
| 7281 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7282 | if (*error != '\0') { |
| 7283 | memprintf(err, "%s: invalid number", args[0]); |
| 7284 | return -1; |
| 7285 | } |
| 7286 | return 0; |
| 7287 | } |
| 7288 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7289 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7290 | struct proxy *defpx, const char *file, int line, |
| 7291 | char **err) |
| 7292 | { |
| 7293 | char *error; |
| 7294 | |
| 7295 | if (*(args[1]) == 0) { |
| 7296 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7297 | return -1; |
| 7298 | } |
| 7299 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7300 | if (*error != '\0') { |
| 7301 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7302 | return -1; |
| 7303 | } |
| 7304 | return 0; |
| 7305 | } |
| 7306 | |
| 7307 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7308 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7309 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7310 | * the main lua entry point. |
| 7311 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7312 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7313 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7314 | * |
| 7315 | * In some error case, LUA set an error message in top of the stack. This function |
| 7316 | * 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] | 7317 | * |
| 7318 | * This function can fail with an abort() due to an Lua critical error. |
| 7319 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7320 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7321 | */ |
| 7322 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7323 | struct proxy *defpx, const char *file, int line, |
| 7324 | char **err) |
| 7325 | { |
| 7326 | int error; |
| 7327 | |
| 7328 | /* Just load and compile the file. */ |
| 7329 | error = luaL_loadfile(gL.T, args[1]); |
| 7330 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7331 | 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] | 7332 | lua_pop(gL.T, 1); |
| 7333 | return -1; |
| 7334 | } |
| 7335 | |
| 7336 | /* If no syntax error where detected, execute the code. */ |
| 7337 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7338 | switch (error) { |
| 7339 | case LUA_OK: |
| 7340 | break; |
| 7341 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7342 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7343 | lua_pop(gL.T, 1); |
| 7344 | return -1; |
| 7345 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7346 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7347 | return -1; |
| 7348 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7349 | 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] | 7350 | lua_pop(gL.T, 1); |
| 7351 | return -1; |
| 7352 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7353 | 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] | 7354 | lua_pop(gL.T, 1); |
| 7355 | return -1; |
| 7356 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7357 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7358 | lua_pop(gL.T, 1); |
| 7359 | return -1; |
| 7360 | } |
| 7361 | |
| 7362 | return 0; |
| 7363 | } |
| 7364 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7365 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7366 | * in the given <ctx>. |
| 7367 | */ |
| 7368 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7369 | { |
| 7370 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7371 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7372 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7373 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7374 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7375 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7376 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7377 | |
| 7378 | return 0; |
| 7379 | } |
| 7380 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7381 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7382 | struct proxy *defpx, const char *file, int line, |
| 7383 | char **err) |
| 7384 | { |
| 7385 | char *path; |
| 7386 | char *type = "path"; |
| 7387 | if (too_many_args(2, args, err, NULL)) { |
| 7388 | return -1; |
| 7389 | } |
| 7390 | |
| 7391 | if (!(*args[1])) { |
| 7392 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7393 | return -1; |
| 7394 | } |
| 7395 | path = args[1]; |
| 7396 | |
| 7397 | if (*args[2]) { |
| 7398 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7399 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7400 | return -1; |
| 7401 | } |
| 7402 | type = args[2]; |
| 7403 | } |
| 7404 | |
| 7405 | return hlua_prepend_path(gL, type, path); |
| 7406 | } |
| 7407 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7408 | /* configuration keywords declaration */ |
| 7409 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7410 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7411 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7412 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7413 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7414 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7415 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7416 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7417 | { 0, NULL, NULL }, |
| 7418 | }}; |
| 7419 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7420 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7421 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7422 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7423 | /* This function can fail with an abort() due to an Lua critical error. |
| 7424 | * We are in the initialisation process of HAProxy, this abort() is |
| 7425 | * tolerated. |
| 7426 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7427 | int hlua_post_init() |
| 7428 | { |
| 7429 | struct hlua_init_function *init; |
| 7430 | const char *msg; |
| 7431 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7432 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7433 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7434 | /* Call post initialisation function in safe environement. */ |
| 7435 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7436 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7437 | error = lua_tostring(gL.T, -1); |
| 7438 | else |
| 7439 | error = "critical error"; |
| 7440 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7441 | exit(1); |
| 7442 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7443 | |
| 7444 | #if USE_OPENSSL |
| 7445 | /* Initialize SSL server. */ |
| 7446 | if (socket_ssl.xprt->prepare_srv) { |
| 7447 | int saved_used_backed = global.ssl_used_backend; |
| 7448 | // don't affect maxconn automatic computation |
| 7449 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7450 | global.ssl_used_backend = saved_used_backed; |
| 7451 | } |
| 7452 | #endif |
| 7453 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7454 | hlua_fcn_post_init(gL.T); |
| 7455 | RESET_SAFE_LJMP(gL.T); |
| 7456 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7457 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7458 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7459 | ret = hlua_ctx_resume(&gL, 0); |
| 7460 | switch (ret) { |
| 7461 | case HLUA_E_OK: |
| 7462 | lua_pop(gL.T, -1); |
| 7463 | return 1; |
| 7464 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7465 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7466 | return 0; |
| 7467 | case HLUA_E_ERRMSG: |
| 7468 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7469 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7470 | return 0; |
| 7471 | case HLUA_E_ERR: |
| 7472 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7473 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7474 | return 0; |
| 7475 | } |
| 7476 | } |
| 7477 | return 1; |
| 7478 | } |
| 7479 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7480 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7481 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7482 | * is the previously allocated size or the kind of object in case of a new |
| 7483 | * allocation. <nsize> is the requested new size. |
| 7484 | */ |
| 7485 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7486 | { |
| 7487 | struct hlua_mem_allocator *zone = ud; |
| 7488 | |
| 7489 | if (nsize == 0) { |
| 7490 | /* it's a free */ |
| 7491 | if (ptr) |
| 7492 | zone->allocated -= osize; |
| 7493 | free(ptr); |
| 7494 | return NULL; |
| 7495 | } |
| 7496 | |
| 7497 | if (!ptr) { |
| 7498 | /* it's a new allocation */ |
| 7499 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7500 | return NULL; |
| 7501 | |
| 7502 | ptr = malloc(nsize); |
| 7503 | if (ptr) |
| 7504 | zone->allocated += nsize; |
| 7505 | return ptr; |
| 7506 | } |
| 7507 | |
| 7508 | /* it's a realloc */ |
| 7509 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7510 | return NULL; |
| 7511 | |
| 7512 | ptr = realloc(ptr, nsize); |
| 7513 | if (ptr) |
| 7514 | zone->allocated += nsize - osize; |
| 7515 | return ptr; |
| 7516 | } |
| 7517 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7518 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7519 | * We are in the initialisation process of HAProxy, this abort() is |
| 7520 | * tolerated. |
| 7521 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7522 | void hlua_init(void) |
| 7523 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7524 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7525 | int idx; |
| 7526 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7527 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7528 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7529 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7530 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7531 | struct srv_kw *kw; |
| 7532 | int tmp_error; |
| 7533 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7534 | char *args[] = { /* SSL client configuration. */ |
| 7535 | "ssl", |
| 7536 | "verify", |
| 7537 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7538 | NULL |
| 7539 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7540 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7541 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7542 | /* Init main lua stack. */ |
| 7543 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7544 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7545 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7546 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7547 | hlua_sethlua(&gL); |
| 7548 | gL.Tref = LUA_REFNIL; |
| 7549 | gL.task = NULL; |
| 7550 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7551 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7552 | * the Lua function can fail with an abort. We are in the initialisation |
| 7553 | * process of HAProxy, this abort() is tolerated. |
| 7554 | */ |
| 7555 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7556 | /* Initialise lua. */ |
| 7557 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 7558 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 7559 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 7560 | #ifdef HLUA_PREPEND_PATH |
| 7561 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 7562 | #endif |
| 7563 | #ifdef HLUA_PREPEND_CPATH |
| 7564 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 7565 | #endif |
| 7566 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 7567 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7568 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7569 | /* Set safe environment for the initialisation. */ |
| 7570 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7571 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7572 | error_msg = lua_tostring(gL.T, -1); |
| 7573 | else |
| 7574 | error_msg = "critical error"; |
| 7575 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7576 | exit(1); |
| 7577 | } |
| 7578 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7579 | /* |
| 7580 | * |
| 7581 | * Create "core" object. |
| 7582 | * |
| 7583 | */ |
| 7584 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7585 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7586 | lua_newtable(gL.T); |
| 7587 | |
| 7588 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7589 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7590 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7591 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7592 | /* Register special functions. */ |
| 7593 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7594 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7595 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7596 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7597 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7598 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7599 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7600 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7601 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7602 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7603 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7604 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7605 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7606 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7607 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7608 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7609 | hlua_class_function(gL.T, "log", hlua_log); |
| 7610 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7611 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7612 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7613 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7614 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7615 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7616 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7617 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7618 | |
| 7619 | /* |
| 7620 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7621 | * Register class Map |
| 7622 | * |
| 7623 | */ |
| 7624 | |
| 7625 | /* This table entry is the object "Map" base. */ |
| 7626 | lua_newtable(gL.T); |
| 7627 | |
| 7628 | /* register pattern types. */ |
| 7629 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7630 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7631 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7632 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7633 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7634 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7635 | |
| 7636 | /* register constructor. */ |
| 7637 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7638 | |
| 7639 | /* Create and fill the metatable. */ |
| 7640 | lua_newtable(gL.T); |
| 7641 | |
| 7642 | /* Create and fille the __index entry. */ |
| 7643 | lua_pushstring(gL.T, "__index"); |
| 7644 | lua_newtable(gL.T); |
| 7645 | |
| 7646 | /* Register . */ |
| 7647 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7648 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7649 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7650 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7651 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7652 | /* Register previous table in the registry with reference and named entry. |
| 7653 | * The function hlua_register_metatable() pops the stack, so we |
| 7654 | * previously create a copy of the table. |
| 7655 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7656 | 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] | 7657 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7658 | |
| 7659 | /* Assign the metatable to the mai Map object. */ |
| 7660 | lua_setmetatable(gL.T, -2); |
| 7661 | |
| 7662 | /* Set a name to the table. */ |
| 7663 | lua_setglobal(gL.T, "Map"); |
| 7664 | |
| 7665 | /* |
| 7666 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7667 | * Register class Channel |
| 7668 | * |
| 7669 | */ |
| 7670 | |
| 7671 | /* Create and fill the metatable. */ |
| 7672 | lua_newtable(gL.T); |
| 7673 | |
| 7674 | /* Create and fille the __index entry. */ |
| 7675 | lua_pushstring(gL.T, "__index"); |
| 7676 | lua_newtable(gL.T); |
| 7677 | |
| 7678 | /* Register . */ |
| 7679 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7680 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7681 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7682 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7683 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7684 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7685 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7686 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7687 | 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] | 7688 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7689 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7690 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7691 | |
| 7692 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7693 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7694 | |
| 7695 | /* |
| 7696 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7697 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7698 | * |
| 7699 | */ |
| 7700 | |
| 7701 | /* Create and fill the metatable. */ |
| 7702 | lua_newtable(gL.T); |
| 7703 | |
| 7704 | /* Create and fille the __index entry. */ |
| 7705 | lua_pushstring(gL.T, "__index"); |
| 7706 | lua_newtable(gL.T); |
| 7707 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7708 | /* Browse existing fetches and create the associated |
| 7709 | * object method. |
| 7710 | */ |
| 7711 | sf = NULL; |
| 7712 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7713 | |
| 7714 | /* Dont register the keywork if the arguments check function are |
| 7715 | * not safe during the runtime. |
| 7716 | */ |
| 7717 | if ((sf->val_args != NULL) && |
| 7718 | (sf->val_args != val_payload_lv) && |
| 7719 | (sf->val_args != val_hdr)) |
| 7720 | continue; |
| 7721 | |
| 7722 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7723 | * by an underscore. |
| 7724 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7725 | strncpy(trash.area, sf->kw, trash.size); |
| 7726 | trash.area[trash.size - 1] = '\0'; |
| 7727 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7728 | if (*p == '.' || *p == '-' || *p == '+') |
| 7729 | *p = '_'; |
| 7730 | |
| 7731 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7732 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7733 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7734 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7735 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7736 | } |
| 7737 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7738 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7739 | |
| 7740 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7741 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7742 | |
| 7743 | /* |
| 7744 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7745 | * Register class Converters |
| 7746 | * |
| 7747 | */ |
| 7748 | |
| 7749 | /* Create and fill the metatable. */ |
| 7750 | lua_newtable(gL.T); |
| 7751 | |
| 7752 | /* Create and fill the __index entry. */ |
| 7753 | lua_pushstring(gL.T, "__index"); |
| 7754 | lua_newtable(gL.T); |
| 7755 | |
| 7756 | /* Browse existing converters and create the associated |
| 7757 | * object method. |
| 7758 | */ |
| 7759 | sc = NULL; |
| 7760 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7761 | /* Dont register the keywork if the arguments check function are |
| 7762 | * not safe during the runtime. |
| 7763 | */ |
| 7764 | if (sc->val_args != NULL) |
| 7765 | continue; |
| 7766 | |
| 7767 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7768 | * by an underscore. |
| 7769 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7770 | strncpy(trash.area, sc->kw, trash.size); |
| 7771 | trash.area[trash.size - 1] = '\0'; |
| 7772 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7773 | if (*p == '.' || *p == '-' || *p == '+') |
| 7774 | *p = '_'; |
| 7775 | |
| 7776 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7777 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7778 | lua_pushlightuserdata(gL.T, sc); |
| 7779 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7780 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7781 | } |
| 7782 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7783 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7784 | |
| 7785 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7786 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7787 | |
| 7788 | /* |
| 7789 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7790 | * Register class HTTP |
| 7791 | * |
| 7792 | */ |
| 7793 | |
| 7794 | /* Create and fill the metatable. */ |
| 7795 | lua_newtable(gL.T); |
| 7796 | |
| 7797 | /* Create and fille the __index entry. */ |
| 7798 | lua_pushstring(gL.T, "__index"); |
| 7799 | lua_newtable(gL.T); |
| 7800 | |
| 7801 | /* Register Lua functions. */ |
| 7802 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7803 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7804 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7805 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7806 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7807 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7808 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7809 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7810 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7811 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7812 | |
| 7813 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7814 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7815 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7816 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7817 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7818 | 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] | 7819 | 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] | 7820 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7821 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7822 | |
| 7823 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7824 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7825 | |
| 7826 | /* |
| 7827 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7828 | * Register class AppletTCP |
| 7829 | * |
| 7830 | */ |
| 7831 | |
| 7832 | /* Create and fill the metatable. */ |
| 7833 | lua_newtable(gL.T); |
| 7834 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7835 | /* Create and fille the __index entry. */ |
| 7836 | lua_pushstring(gL.T, "__index"); |
| 7837 | lua_newtable(gL.T); |
| 7838 | |
| 7839 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7840 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7841 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7842 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7843 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7844 | 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] | 7845 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7846 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7847 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7848 | |
| 7849 | lua_settable(gL.T, -3); |
| 7850 | |
| 7851 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7852 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7853 | |
| 7854 | /* |
| 7855 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7856 | * Register class AppletHTTP |
| 7857 | * |
| 7858 | */ |
| 7859 | |
| 7860 | /* Create and fill the metatable. */ |
| 7861 | lua_newtable(gL.T); |
| 7862 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7863 | /* Create and fille the __index entry. */ |
| 7864 | lua_pushstring(gL.T, "__index"); |
| 7865 | lua_newtable(gL.T); |
| 7866 | |
| 7867 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7868 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7869 | 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] | 7870 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7871 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7872 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7873 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7874 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7875 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7876 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7877 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7878 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7879 | |
| 7880 | lua_settable(gL.T, -3); |
| 7881 | |
| 7882 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7883 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7884 | |
| 7885 | /* |
| 7886 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7887 | * Register class TXN |
| 7888 | * |
| 7889 | */ |
| 7890 | |
| 7891 | /* Create and fill the metatable. */ |
| 7892 | lua_newtable(gL.T); |
| 7893 | |
| 7894 | /* Create and fille the __index entry. */ |
| 7895 | lua_pushstring(gL.T, "__index"); |
| 7896 | lua_newtable(gL.T); |
| 7897 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7898 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7899 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7900 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 7901 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 7902 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 7903 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 7904 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 7905 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 7906 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7907 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 7908 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 7909 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 7910 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7911 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7912 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7913 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7914 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7915 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7916 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7917 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7918 | |
| 7919 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7920 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7921 | |
| 7922 | /* |
| 7923 | * |
| 7924 | * Register class Socket |
| 7925 | * |
| 7926 | */ |
| 7927 | |
| 7928 | /* Create and fill the metatable. */ |
| 7929 | lua_newtable(gL.T); |
| 7930 | |
| 7931 | /* Create and fille the __index entry. */ |
| 7932 | lua_pushstring(gL.T, "__index"); |
| 7933 | lua_newtable(gL.T); |
| 7934 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7935 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7936 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7937 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7938 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7939 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7940 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7941 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7942 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7943 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7944 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7945 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7946 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7947 | 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] | 7948 | |
| 7949 | /* Register the garbage collector entry. */ |
| 7950 | lua_pushstring(gL.T, "__gc"); |
| 7951 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7952 | 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] | 7953 | |
| 7954 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7955 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7956 | |
| 7957 | /* Proxy and server configuration initialisation. */ |
| 7958 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7959 | init_new_proxy(&socket_proxy); |
| 7960 | socket_proxy.parent = NULL; |
| 7961 | socket_proxy.last_change = now.tv_sec; |
| 7962 | socket_proxy.id = "LUA-SOCKET"; |
| 7963 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7964 | socket_proxy.maxconn = 0; |
| 7965 | socket_proxy.accept = NULL; |
| 7966 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7967 | socket_proxy.srv = NULL; |
| 7968 | socket_proxy.conn_retries = 0; |
| 7969 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7970 | |
| 7971 | /* Init TCP server: unchanged parameters */ |
| 7972 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7973 | socket_tcp.next = NULL; |
| 7974 | socket_tcp.proxy = &socket_proxy; |
| 7975 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7976 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7977 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7978 | socket_tcp.priv_conns = NULL; |
| 7979 | socket_tcp.idle_conns = NULL; |
| 7980 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7981 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7982 | socket_tcp.last_change = 0; |
| 7983 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7984 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7985 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7986 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7987 | |
| 7988 | /* XXX: Copy default parameter from default server, |
| 7989 | * but the default server is not initialized. |
| 7990 | */ |
| 7991 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7992 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7993 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 7994 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 7995 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 7996 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 7997 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 7998 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 7999 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8000 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8001 | |
| 8002 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8003 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8004 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8005 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8006 | socket_tcp.check.server = &socket_tcp; |
| 8007 | |
| 8008 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8009 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8010 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8011 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8012 | socket_tcp.agent.server = &socket_tcp; |
| 8013 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8014 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8015 | |
| 8016 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8017 | /* Init TCP server: unchanged parameters */ |
| 8018 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8019 | socket_ssl.next = NULL; |
| 8020 | socket_ssl.proxy = &socket_proxy; |
| 8021 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8022 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8023 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8024 | socket_ssl.priv_conns = NULL; |
| 8025 | socket_ssl.idle_conns = NULL; |
| 8026 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8027 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8028 | socket_ssl.last_change = 0; |
| 8029 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8030 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8031 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8032 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8033 | |
| 8034 | /* XXX: Copy default parameter from default server, |
| 8035 | * but the default server is not initialized. |
| 8036 | */ |
| 8037 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8038 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8039 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8040 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8041 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8042 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8043 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8044 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8045 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8046 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8047 | |
| 8048 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8049 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8050 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8051 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8052 | socket_ssl.check.server = &socket_ssl; |
| 8053 | |
| 8054 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8055 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8056 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8057 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8058 | socket_ssl.agent.server = &socket_ssl; |
| 8059 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8060 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8061 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8062 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8063 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8064 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8065 | /* |
| 8066 | * |
| 8067 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8068 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8069 | * features like client certificates and ssl_verify. |
| 8070 | * |
| 8071 | */ |
| 8072 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8073 | if (tmp_error != 0) { |
| 8074 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8075 | abort(); /* This must be never arrives because the command line |
| 8076 | not editable by the user. */ |
| 8077 | } |
| 8078 | idx += kw->skip; |
| 8079 | } |
| 8080 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8081 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8082 | |
| 8083 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8084 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8085 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8086 | static void hlua_register_build_options(void) |
| 8087 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8088 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8089 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8090 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8091 | hap_register_build_opts(ptr, 1); |
| 8092 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8093 | |
| 8094 | INITCALL0(STG_REGISTER, hlua_register_build_options); |