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; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 422 | arg->data.str.area = (char *)lua_tolstring(L, ud, (size_t *)&arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 423 | break; |
| 424 | |
| 425 | case LUA_TUSERDATA: |
| 426 | case LUA_TNIL: |
| 427 | case LUA_TTABLE: |
| 428 | case LUA_TFUNCTION: |
| 429 | case LUA_TTHREAD: |
| 430 | case LUA_TLIGHTUSERDATA: |
| 431 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 432 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 433 | break; |
| 434 | } |
| 435 | return 1; |
| 436 | } |
| 437 | |
| 438 | /* the following functions are used to convert a struct sample |
| 439 | * in Lua type. This useful to convert the return of the |
| 440 | * fetchs or converters. |
| 441 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 442 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 443 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 444 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 447 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | break; |
| 449 | |
| 450 | case SMP_T_BIN: |
| 451 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 452 | 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] | 453 | break; |
| 454 | |
| 455 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 456 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 458 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 459 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 460 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 461 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 462 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 463 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 464 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 465 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 466 | 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] | 467 | break; |
| 468 | default: |
| 469 | lua_pushnil(L); |
| 470 | break; |
| 471 | } |
| 472 | break; |
| 473 | |
| 474 | case SMP_T_IPV4: |
| 475 | case SMP_T_IPV6: |
| 476 | 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] | 477 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 478 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 479 | 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] | 480 | else |
| 481 | lua_pushnil(L); |
| 482 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 483 | default: |
| 484 | lua_pushnil(L); |
| 485 | break; |
| 486 | } |
| 487 | return 1; |
| 488 | } |
| 489 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 490 | /* the following functions are used to convert a struct sample |
| 491 | * in Lua strings. This is useful to convert the return of the |
| 492 | * fetchs or converters. |
| 493 | */ |
| 494 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 495 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 496 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | |
| 498 | case SMP_T_BIN: |
| 499 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 500 | 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] | 501 | break; |
| 502 | |
| 503 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 504 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 506 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 507 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 508 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 509 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 510 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 511 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 512 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 513 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 514 | 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] | 515 | break; |
| 516 | default: |
| 517 | lua_pushstring(L, ""); |
| 518 | break; |
| 519 | } |
| 520 | break; |
| 521 | |
| 522 | case SMP_T_SINT: |
| 523 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 524 | case SMP_T_IPV4: |
| 525 | case SMP_T_IPV6: |
| 526 | 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] | 527 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 528 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 529 | 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] | 530 | else |
| 531 | lua_pushstring(L, ""); |
| 532 | break; |
| 533 | default: |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | } |
| 537 | return 1; |
| 538 | } |
| 539 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 540 | /* the following functions are used to convert an Lua type in a |
| 541 | * struct sample. This is useful to provide data from a converter |
| 542 | * to the LUA code. |
| 543 | */ |
| 544 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 545 | { |
| 546 | switch (lua_type(L, ud)) { |
| 547 | |
| 548 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 549 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 550 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 551 | break; |
| 552 | |
| 553 | |
| 554 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 555 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 556 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 557 | break; |
| 558 | |
| 559 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 560 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | smp->flags |= SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 562 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, (size_t *)&smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 563 | break; |
| 564 | |
| 565 | case LUA_TUSERDATA: |
| 566 | case LUA_TNIL: |
| 567 | case LUA_TTABLE: |
| 568 | case LUA_TFUNCTION: |
| 569 | case LUA_TTHREAD: |
| 570 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 571 | case LUA_TNONE: |
| 572 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 573 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 574 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 575 | break; |
| 576 | } |
| 577 | return 1; |
| 578 | } |
| 579 | |
| 580 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 581 | * 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] | 582 | * 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] | 583 | * |
| 584 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 585 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 586 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 587 | __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] | 588 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | { |
| 590 | int min_arg; |
| 591 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 592 | struct proxy *px; |
| 593 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 594 | |
| 595 | idx = 0; |
| 596 | min_arg = ARGM(mask); |
| 597 | mask >>= ARGM_BITS; |
| 598 | |
| 599 | while (1) { |
| 600 | |
| 601 | /* Check oversize. */ |
| 602 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 603 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 604 | } |
| 605 | |
| 606 | /* Check for mandatory arguments. */ |
| 607 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 608 | if (idx < min_arg) { |
| 609 | |
| 610 | /* If miss other argument than the first one, we return an error. */ |
| 611 | if (idx > 0) |
| 612 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 613 | |
| 614 | /* If first argument have a certain type, some default values |
| 615 | * may be used. See the function smp_resolve_args(). |
| 616 | */ |
| 617 | switch (mask & ARGT_MASK) { |
| 618 | |
| 619 | case ARGT_FE: |
| 620 | if (!(p->cap & PR_CAP_FE)) |
| 621 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 622 | argp[idx].data.prx = p; |
| 623 | argp[idx].type = ARGT_FE; |
| 624 | argp[idx+1].type = ARGT_STOP; |
| 625 | break; |
| 626 | |
| 627 | case ARGT_BE: |
| 628 | if (!(p->cap & PR_CAP_BE)) |
| 629 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 630 | argp[idx].data.prx = p; |
| 631 | argp[idx].type = ARGT_BE; |
| 632 | argp[idx+1].type = ARGT_STOP; |
| 633 | break; |
| 634 | |
| 635 | case ARGT_TAB: |
| 636 | argp[idx].data.prx = p; |
| 637 | argp[idx].type = ARGT_TAB; |
| 638 | argp[idx+1].type = ARGT_STOP; |
| 639 | break; |
| 640 | |
| 641 | default: |
| 642 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 643 | break; |
| 644 | } |
| 645 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 646 | return 0; |
| 647 | } |
| 648 | |
| 649 | /* Check for exceed the number of requiered argument. */ |
| 650 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 651 | argp[idx].type != ARGT_STOP) { |
| 652 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 653 | } |
| 654 | |
| 655 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 656 | argp[idx].type == ARGT_STOP) { |
| 657 | return 0; |
| 658 | } |
| 659 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 660 | /* Convert some argument types. */ |
| 661 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 662 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 663 | if (argp[idx].type != ARGT_SINT) |
| 664 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 665 | argp[idx].type = ARGT_SINT; |
| 666 | break; |
| 667 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 668 | case ARGT_TIME: |
| 669 | if (argp[idx].type != ARGT_SINT) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 671 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 672 | break; |
| 673 | |
| 674 | case ARGT_SIZE: |
| 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_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case ARGT_FE: |
| 681 | if (argp[idx].type != ARGT_STR) |
| 682 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 683 | memcpy(trash.area, argp[idx].data.str.area, |
| 684 | argp[idx].data.str.data); |
| 685 | trash.area[argp[idx].data.str.data] = 0; |
| 686 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 687 | if (!argp[idx].data.prx) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 689 | argp[idx].type = ARGT_FE; |
| 690 | break; |
| 691 | |
| 692 | case ARGT_BE: |
| 693 | if (argp[idx].type != ARGT_STR) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 695 | memcpy(trash.area, argp[idx].data.str.area, |
| 696 | argp[idx].data.str.data); |
| 697 | trash.area[argp[idx].data.str.data] = 0; |
| 698 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 699 | if (!argp[idx].data.prx) |
| 700 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 701 | argp[idx].type = ARGT_BE; |
| 702 | break; |
| 703 | |
| 704 | case ARGT_TAB: |
| 705 | if (argp[idx].type != ARGT_STR) |
| 706 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 707 | memcpy(trash.area, argp[idx].data.str.area, |
| 708 | argp[idx].data.str.data); |
| 709 | trash.area[argp[idx].data.str.data] = 0; |
| 710 | argp[idx].data.prx = proxy_tbl_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 711 | if (!argp[idx].data.prx) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 713 | argp[idx].type = ARGT_TAB; |
| 714 | break; |
| 715 | |
| 716 | case ARGT_SRV: |
| 717 | if (argp[idx].type != ARGT_STR) |
| 718 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 719 | memcpy(trash.area, argp[idx].data.str.area, |
| 720 | argp[idx].data.str.data); |
| 721 | trash.area[argp[idx].data.str.data] = 0; |
| 722 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 723 | if (sname) { |
| 724 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 726 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 727 | if (!px) |
| 728 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 729 | } |
| 730 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 731 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 732 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 733 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 734 | argp[idx].data.srv = findserver(px, sname); |
| 735 | if (!argp[idx].data.srv) |
| 736 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 737 | argp[idx].type = ARGT_SRV; |
| 738 | break; |
| 739 | |
| 740 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 741 | memcpy(trash.area, argp[idx].data.str.area, |
| 742 | argp[idx].data.str.data); |
| 743 | trash.area[argp[idx].data.str.data] = 0; |
| 744 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 745 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 746 | argp[idx].type = ARGT_IPV4; |
| 747 | break; |
| 748 | |
| 749 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 750 | memcpy(trash.area, argp[idx].data.str.area, |
| 751 | argp[idx].data.str.data); |
| 752 | trash.area[argp[idx].data.str.data] = 0; |
| 753 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 754 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 755 | argp[idx].type = ARGT_MSK4; |
| 756 | break; |
| 757 | |
| 758 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 759 | memcpy(trash.area, argp[idx].data.str.area, |
| 760 | argp[idx].data.str.data); |
| 761 | trash.area[argp[idx].data.str.data] = 0; |
| 762 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 763 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 764 | argp[idx].type = ARGT_IPV6; |
| 765 | break; |
| 766 | |
| 767 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 768 | memcpy(trash.area, argp[idx].data.str.area, |
| 769 | argp[idx].data.str.data); |
| 770 | trash.area[argp[idx].data.str.data] = 0; |
| 771 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 772 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 773 | argp[idx].type = ARGT_MSK6; |
| 774 | break; |
| 775 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 776 | case ARGT_MAP: |
| 777 | case ARGT_REG: |
| 778 | case ARGT_USR: |
| 779 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 780 | break; |
| 781 | } |
| 782 | |
| 783 | /* Check for type of argument. */ |
| 784 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 785 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 786 | arg_type_names[(mask & ARGT_MASK)], |
| 787 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 788 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 789 | } |
| 790 | |
| 791 | /* Next argument. */ |
| 792 | mask >>= ARGT_BITS; |
| 793 | idx++; |
| 794 | } |
| 795 | } |
| 796 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 797 | /* |
| 798 | * The following functions are used to make correspondance between the the |
| 799 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 800 | * |
| 801 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 802 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 803 | */ |
| 804 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 805 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 806 | struct hlua **hlua = lua_getextraspace(L); |
| 807 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 808 | } |
| 809 | static inline void hlua_sethlua(struct hlua *hlua) |
| 810 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 811 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 812 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 813 | } |
| 814 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 815 | /* This function is used to send logs. It try to send on screen (stderr) |
| 816 | * and on the default syslog server. |
| 817 | */ |
| 818 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 819 | { |
| 820 | struct tm tm; |
| 821 | char *p; |
| 822 | |
| 823 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 824 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 825 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 826 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 827 | /* Break the message if exceed the buffer size. */ |
| 828 | *(p-4) = ' '; |
| 829 | *(p-3) = '.'; |
| 830 | *(p-2) = '.'; |
| 831 | *(p-1) = '.'; |
| 832 | break; |
| 833 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 834 | if (isprint(*msg)) |
| 835 | *p = *msg; |
| 836 | else |
| 837 | *p = '.'; |
| 838 | } |
| 839 | *p = '\0'; |
| 840 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 841 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 842 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 843 | get_localtime(date.tv_sec, &tm); |
| 844 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 845 | 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] | 846 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 847 | fflush(stderr); |
| 848 | } |
| 849 | } |
| 850 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 851 | /* This function just ensure that the yield will be always |
| 852 | * returned with a timeout and permit to set some flags |
| 853 | */ |
| 854 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 855 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 856 | { |
| 857 | struct hlua *hlua = hlua_gethlua(L); |
| 858 | |
| 859 | /* Set the wake timeout. If timeout is required, we set |
| 860 | * the expiration time. |
| 861 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 862 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 864 | hlua->flags |= flags; |
| 865 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 866 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 867 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 868 | } |
| 869 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 870 | /* This function initialises the Lua environment stored in the stream. |
| 871 | * 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] | 872 | * 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] | 873 | * |
| 874 | * This function is particular. it initialises a new Lua thread. If the |
| 875 | * initialisation fails (example: out of memory error), the lua function |
| 876 | * throws an error (longjmp). |
| 877 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 878 | * In some case (at least one), this function can be called from safe |
| 879 | * environement, so we must not initialise it. While the support of |
| 880 | * threads appear, the safe environment set a lock to ensure only one |
| 881 | * Lua execution at a time. If we initialize safe environment in another |
| 882 | * safe environmenet, we have a dead lock. |
| 883 | * |
| 884 | * set "already_safe" true if the context is initialized form safe |
| 885 | * Lua fonction. |
| 886 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 887 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 888 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 889 | * MUST NOT manipulate the created thread stack state, because it is not |
| 890 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 891 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 892 | 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] | 893 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 894 | if (!already_safe) { |
| 895 | if (!SET_SAFE_LJMP(gL.T)) { |
| 896 | lua->Tref = LUA_REFNIL; |
| 897 | return 0; |
| 898 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 899 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 901 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 902 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 903 | lua->T = lua_newthread(gL.T); |
| 904 | if (!lua->T) { |
| 905 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 906 | if (!already_safe) |
| 907 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 908 | return 0; |
| 909 | } |
| 910 | hlua_sethlua(lua); |
| 911 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 912 | lua->task = task; |
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 1; |
| 916 | } |
| 917 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 918 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 919 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 920 | * is not freed. |
| 921 | */ |
| 922 | void hlua_ctx_destroy(struct hlua *lua) |
| 923 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 924 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 925 | return; |
| 926 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 927 | if (!lua->T) |
| 928 | goto end; |
| 929 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 930 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 931 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 932 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 933 | if (!SET_SAFE_LJMP(lua->T)) |
| 934 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 935 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 936 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 937 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 938 | if (!SET_SAFE_LJMP(gL.T)) |
| 939 | return; |
| 940 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 941 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 942 | /* Forces a garbage collecting process. If the Lua program is finished |
| 943 | * without error, we run the GC on the thread pointer. Its freed all |
| 944 | * the unused memory. |
| 945 | * If the thread is finnish with an error or is currently yielded, |
| 946 | * it seems that the GC applied on the thread doesn't clean anything, |
| 947 | * so e run the GC on the main thread. |
| 948 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 949 | * the garbage collection. |
| 950 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 951 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 952 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 953 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 954 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 955 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 956 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 957 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 958 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 959 | |
| 960 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 961 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 962 | } |
| 963 | |
| 964 | /* This function is used to restore the Lua context when a coroutine |
| 965 | * fails. This function copy the common memory between old coroutine |
| 966 | * and the new coroutine. The old coroutine is destroyed, and its |
| 967 | * replaced by the new coroutine. |
| 968 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 969 | * as string error message and it is copied in the new stack. |
| 970 | */ |
| 971 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 972 | { |
| 973 | lua_State *T; |
| 974 | int new_ref; |
| 975 | |
| 976 | /* Renew the main LUA stack doesn't have sense. */ |
| 977 | if (lua == &gL) |
| 978 | return 0; |
| 979 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 980 | /* New Lua coroutine. */ |
| 981 | T = lua_newthread(gL.T); |
| 982 | if (!T) |
| 983 | return 0; |
| 984 | |
| 985 | /* Copy last error message. */ |
| 986 | if (keep_msg) |
| 987 | lua_xmove(lua->T, T, 1); |
| 988 | |
| 989 | /* Copy data between the coroutines. */ |
| 990 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 991 | lua_xmove(lua->T, T, 1); |
| 992 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 993 | |
| 994 | /* Destroy old data. */ |
| 995 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 996 | |
| 997 | /* The thread is garbage collected by Lua. */ |
| 998 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 999 | |
| 1000 | /* Fill the struct with the new coroutine values. */ |
| 1001 | lua->Mref = new_ref; |
| 1002 | lua->T = T; |
| 1003 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1004 | |
| 1005 | /* Set context. */ |
| 1006 | hlua_sethlua(lua); |
| 1007 | |
| 1008 | return 1; |
| 1009 | } |
| 1010 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1011 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1012 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1013 | struct hlua *hlua = hlua_gethlua(L); |
| 1014 | |
| 1015 | /* Lua cannot yield when its returning from a function, |
| 1016 | * so, we can fix the interrupt hook to 1 instruction, |
| 1017 | * expecting that the function is finnished. |
| 1018 | */ |
| 1019 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1020 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1021 | return; |
| 1022 | } |
| 1023 | |
| 1024 | /* restore the interrupt condition. */ |
| 1025 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1026 | |
| 1027 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1028 | * If the state is not yieldable, trying yield causes an error. |
| 1029 | */ |
| 1030 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1031 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1032 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1033 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1034 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1035 | hlua->run_time += now_ms - hlua->start_time; |
| 1036 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1037 | lua_pushfstring(L, "execution timeout"); |
| 1038 | WILL_LJMP(lua_error(L)); |
| 1039 | } |
| 1040 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1041 | /* Update the start time. */ |
| 1042 | hlua->start_time = now_ms; |
| 1043 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1044 | /* Try to interrupt the process at the end of the current |
| 1045 | * unyieldable function. |
| 1046 | */ |
| 1047 | 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] | 1048 | } |
| 1049 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1050 | /* This function start or resumes the Lua stack execution. If the flag |
| 1051 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1052 | * The function return an error. |
| 1053 | * |
| 1054 | * The function can returns 4 values: |
| 1055 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1056 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1057 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1058 | * - 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] | 1059 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1060 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1061 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1062 | * 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] | 1063 | * LUA code. |
| 1064 | */ |
| 1065 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1066 | { |
| 1067 | int ret; |
| 1068 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1069 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1071 | /* Initialise run time counter. */ |
| 1072 | if (!HLUA_IS_RUNNING(lua)) |
| 1073 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1074 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1075 | /* Lock the whole Lua execution. This lock must be before the |
| 1076 | * label "resume_execution". |
| 1077 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1078 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1079 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1080 | resume_execution: |
| 1081 | |
| 1082 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1083 | * instructions. it is used for preventing infinite loops. |
| 1084 | */ |
| 1085 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1086 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1087 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1088 | HLUA_SET_RUN(lua); |
| 1089 | HLUA_CLR_CTRLYIELD(lua); |
| 1090 | HLUA_CLR_WAKERESWR(lua); |
| 1091 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1092 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1093 | /* Update the start time. */ |
| 1094 | lua->start_time = now_ms; |
| 1095 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1096 | /* Call the function. */ |
| 1097 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1098 | switch (ret) { |
| 1099 | |
| 1100 | case LUA_OK: |
| 1101 | ret = HLUA_E_OK; |
| 1102 | break; |
| 1103 | |
| 1104 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1105 | /* Check if the execution timeout is expired. It it is the case, we |
| 1106 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1107 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1108 | tv_update_date(0, 1); |
| 1109 | lua->run_time += now_ms - lua->start_time; |
| 1110 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1111 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1112 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1113 | break; |
| 1114 | } |
| 1115 | /* Process the forced yield. if the general yield is not allowed or |
| 1116 | * if no task were associated this the current Lua execution |
| 1117 | * coroutine, we resume the execution. Else we want to return in the |
| 1118 | * scheduler and we want to be waked up again, to continue the |
| 1119 | * current Lua execution. So we schedule our own task. |
| 1120 | */ |
| 1121 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1122 | if (!yield_allowed || !lua->task) |
| 1123 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1124 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1125 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1126 | if (!yield_allowed) { |
| 1127 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1128 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1129 | break; |
| 1130 | } |
| 1131 | ret = HLUA_E_AGAIN; |
| 1132 | break; |
| 1133 | |
| 1134 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1135 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1136 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1137 | * because the errors ares the only one mean to return immediately |
| 1138 | * from and lua execution. |
| 1139 | */ |
| 1140 | if (lua->flags & HLUA_EXIT) { |
| 1141 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1142 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1143 | break; |
| 1144 | } |
| 1145 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1146 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1147 | if (!lua_checkstack(lua->T, 1)) { |
| 1148 | ret = HLUA_E_ERR; |
| 1149 | break; |
| 1150 | } |
| 1151 | msg = lua_tostring(lua->T, -1); |
| 1152 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1153 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1154 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1155 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1156 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1158 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1159 | ret = HLUA_E_ERRMSG; |
| 1160 | break; |
| 1161 | |
| 1162 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1163 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1164 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1165 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1166 | break; |
| 1167 | |
| 1168 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1169 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1170 | if (!lua_checkstack(lua->T, 1)) { |
| 1171 | ret = HLUA_E_ERR; |
| 1172 | break; |
| 1173 | } |
| 1174 | msg = lua_tostring(lua->T, -1); |
| 1175 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1176 | lua_pop(lua->T, 1); |
| 1177 | if (msg) |
| 1178 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1179 | else |
| 1180 | lua_pushfstring(lua->T, "message handler error"); |
| 1181 | ret = HLUA_E_ERRMSG; |
| 1182 | break; |
| 1183 | |
| 1184 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1185 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1186 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1187 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1188 | break; |
| 1189 | } |
| 1190 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1191 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1192 | if (lua->flags & HLUA_MUST_GC && |
| 1193 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1194 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1195 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | switch (ret) { |
| 1197 | case HLUA_E_AGAIN: |
| 1198 | break; |
| 1199 | |
| 1200 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1201 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1203 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1204 | break; |
| 1205 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1206 | case HLUA_E_ETMOUT: |
| 1207 | case HLUA_E_NOMEM: |
| 1208 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1210 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1211 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1212 | hlua_ctx_renew(lua, 0); |
| 1213 | break; |
| 1214 | |
| 1215 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1216 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1217 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1218 | break; |
| 1219 | } |
| 1220 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1221 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1222 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1223 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1224 | return ret; |
| 1225 | } |
| 1226 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1227 | /* This function exit the current code. */ |
| 1228 | __LJMP static int hlua_done(lua_State *L) |
| 1229 | { |
| 1230 | struct hlua *hlua = hlua_gethlua(L); |
| 1231 | |
| 1232 | hlua->flags |= HLUA_EXIT; |
| 1233 | WILL_LJMP(lua_error(L)); |
| 1234 | |
| 1235 | return 0; |
| 1236 | } |
| 1237 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1238 | /* This function is an LUA binding. It provides a function |
| 1239 | * for deleting ACL from a referenced ACL file. |
| 1240 | */ |
| 1241 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1242 | { |
| 1243 | const char *name; |
| 1244 | const char *key; |
| 1245 | struct pat_ref *ref; |
| 1246 | |
| 1247 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1248 | |
| 1249 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1250 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1251 | |
| 1252 | ref = pat_ref_lookup(name); |
| 1253 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1254 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1255 | |
| 1256 | pat_ref_delete(ref, key); |
| 1257 | return 0; |
| 1258 | } |
| 1259 | |
| 1260 | /* This function is an LUA binding. It provides a function |
| 1261 | * for deleting map entry from a referenced map file. |
| 1262 | */ |
| 1263 | static int hlua_del_map(lua_State *L) |
| 1264 | { |
| 1265 | const char *name; |
| 1266 | const char *key; |
| 1267 | struct pat_ref *ref; |
| 1268 | |
| 1269 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1270 | |
| 1271 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1272 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1273 | |
| 1274 | ref = pat_ref_lookup(name); |
| 1275 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1276 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1277 | |
| 1278 | pat_ref_delete(ref, key); |
| 1279 | return 0; |
| 1280 | } |
| 1281 | |
| 1282 | /* This function is an LUA binding. It provides a function |
| 1283 | * for adding ACL pattern from a referenced ACL file. |
| 1284 | */ |
| 1285 | static int hlua_add_acl(lua_State *L) |
| 1286 | { |
| 1287 | const char *name; |
| 1288 | const char *key; |
| 1289 | struct pat_ref *ref; |
| 1290 | |
| 1291 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1292 | |
| 1293 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1294 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1295 | |
| 1296 | ref = pat_ref_lookup(name); |
| 1297 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1298 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1299 | |
| 1300 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1301 | pat_ref_add(ref, key, NULL, NULL); |
| 1302 | return 0; |
| 1303 | } |
| 1304 | |
| 1305 | /* This function is an LUA binding. It provides a function |
| 1306 | * for setting map pattern and sample from a referenced map |
| 1307 | * file. |
| 1308 | */ |
| 1309 | static int hlua_set_map(lua_State *L) |
| 1310 | { |
| 1311 | const char *name; |
| 1312 | const char *key; |
| 1313 | const char *value; |
| 1314 | struct pat_ref *ref; |
| 1315 | |
| 1316 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1317 | |
| 1318 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1319 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1320 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1321 | |
| 1322 | ref = pat_ref_lookup(name); |
| 1323 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1324 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1325 | |
| 1326 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1327 | pat_ref_set(ref, key, value, NULL); |
| 1328 | else |
| 1329 | pat_ref_add(ref, key, value, NULL); |
| 1330 | return 0; |
| 1331 | } |
| 1332 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1333 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1334 | * an integer or user data. This data is associated with a metatable. This |
| 1335 | * metatable have an original version registred in the global context with |
| 1336 | * the name of the object (_G[<name>] = <metable> ). |
| 1337 | * |
| 1338 | * A metable is a table that modify the standard behavior of a standard |
| 1339 | * access to the associated data. The entries of this new metatable are |
| 1340 | * defined as is: |
| 1341 | * |
| 1342 | * http://lua-users.org/wiki/MetatableEvents |
| 1343 | * |
| 1344 | * __index |
| 1345 | * |
| 1346 | * we access an absent field in a table, the result is nil. This is |
| 1347 | * true, but it is not the whole truth. Actually, such access triggers |
| 1348 | * the interpreter to look for an __index metamethod: If there is no |
| 1349 | * such method, as usually happens, then the access results in nil; |
| 1350 | * otherwise, the metamethod will provide the result. |
| 1351 | * |
| 1352 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1353 | * the key does not appear in the table, but the metatable has an __index |
| 1354 | * property: |
| 1355 | * |
| 1356 | * - if the value is a function, the function is called, passing in the |
| 1357 | * table and the key; the return value of that function is returned as |
| 1358 | * the result. |
| 1359 | * |
| 1360 | * - if the value is another table, the value of the key in that table is |
| 1361 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1362 | * table's metatable has an __index property, then it continues on up) |
| 1363 | * |
| 1364 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1365 | * |
| 1366 | * http://www.lua.org/pil/13.4.1.html |
| 1367 | * |
| 1368 | * __newindex |
| 1369 | * |
| 1370 | * Like __index, but control property assignment. |
| 1371 | * |
| 1372 | * __mode - Control weak references. A string value with one or both |
| 1373 | * of the characters 'k' and 'v' which specifies that the the |
| 1374 | * keys and/or values in the table are weak references. |
| 1375 | * |
| 1376 | * __call - Treat a table like a function. When a table is followed by |
| 1377 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1378 | * a __call key pointing to a function, that function is invoked |
| 1379 | * (passing any specified arguments) and the return value is |
| 1380 | * returned. |
| 1381 | * |
| 1382 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1383 | * called, if the metatable for myTable has a __metatable |
| 1384 | * key, the value of that key is returned instead of the |
| 1385 | * actual metatable. |
| 1386 | * |
| 1387 | * __tostring - Control string representation. When the builtin |
| 1388 | * "tostring( myTable )" function is called, if the metatable |
| 1389 | * for myTable has a __tostring property set to a function, |
| 1390 | * that function is invoked (passing myTable to it) and the |
| 1391 | * return value is used as the string representation. |
| 1392 | * |
| 1393 | * __len - Control table length. When the table length is requested using |
| 1394 | * the length operator ( '#' ), if the metatable for myTable has |
| 1395 | * a __len key pointing to a function, that function is invoked |
| 1396 | * (passing myTable to it) and the return value used as the value |
| 1397 | * of "#myTable". |
| 1398 | * |
| 1399 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1400 | * collected, if the metatable has a __gc field pointing to a |
| 1401 | * function, that function is first invoked, passing the userdata |
| 1402 | * to it. The __gc metamethod is not called for tables. |
| 1403 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1404 | * |
| 1405 | * Special metamethods for redefining standard operators: |
| 1406 | * http://www.lua.org/pil/13.1.html |
| 1407 | * |
| 1408 | * __add "+" |
| 1409 | * __sub "-" |
| 1410 | * __mul "*" |
| 1411 | * __div "/" |
| 1412 | * __unm "!" |
| 1413 | * __pow "^" |
| 1414 | * __concat ".." |
| 1415 | * |
| 1416 | * Special methods for redfining standar relations |
| 1417 | * http://www.lua.org/pil/13.2.html |
| 1418 | * |
| 1419 | * __eq "==" |
| 1420 | * __lt "<" |
| 1421 | * __le "<=" |
| 1422 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1423 | |
| 1424 | /* |
| 1425 | * |
| 1426 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1427 | * Class Map |
| 1428 | * |
| 1429 | * |
| 1430 | */ |
| 1431 | |
| 1432 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1433 | * a class session, otherwise it throws an error. |
| 1434 | */ |
| 1435 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1436 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1437 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
| 1440 | /* This function is the map constructor. It don't need |
| 1441 | * the class Map object. It creates and return a new Map |
| 1442 | * object. It must be called only during "body" or "init" |
| 1443 | * context because it process some filesystem accesses. |
| 1444 | */ |
| 1445 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1446 | { |
| 1447 | const char *fn; |
| 1448 | int match = PAT_MATCH_STR; |
| 1449 | struct sample_conv conv; |
| 1450 | const char *file = ""; |
| 1451 | int line = 0; |
| 1452 | lua_Debug ar; |
| 1453 | char *err = NULL; |
| 1454 | struct arg args[2]; |
| 1455 | |
| 1456 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1457 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1458 | |
| 1459 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1460 | |
| 1461 | if (lua_gettop(L) >= 2) { |
| 1462 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1463 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1464 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1465 | } |
| 1466 | |
| 1467 | /* Get Lua filename and line number. */ |
| 1468 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1469 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1470 | if (ar.currentline > 0) { /* is there info? */ |
| 1471 | file = ar.short_src; |
| 1472 | line = ar.currentline; |
| 1473 | } |
| 1474 | } |
| 1475 | |
| 1476 | /* fill fake sample_conv struct. */ |
| 1477 | conv.kw = ""; /* unused. */ |
| 1478 | conv.process = NULL; /* unused. */ |
| 1479 | conv.arg_mask = 0; /* unused. */ |
| 1480 | conv.val_args = NULL; /* unused. */ |
| 1481 | conv.out_type = SMP_T_STR; |
| 1482 | conv.private = (void *)(long)match; |
| 1483 | switch (match) { |
| 1484 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1485 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1486 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1487 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1488 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1489 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1490 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1491 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1492 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1493 | default: |
| 1494 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1495 | } |
| 1496 | |
| 1497 | /* fill fake args. */ |
| 1498 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1499 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1500 | args[1].type = ARGT_STOP; |
| 1501 | |
| 1502 | /* load the map. */ |
| 1503 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1504 | /* error case: we cant use luaL_error because we must |
| 1505 | * free the err variable. |
| 1506 | */ |
| 1507 | luaL_where(L, 1); |
| 1508 | lua_pushfstring(L, "'new': %s.", err); |
| 1509 | lua_concat(L, 2); |
| 1510 | free(err); |
| 1511 | WILL_LJMP(lua_error(L)); |
| 1512 | } |
| 1513 | |
| 1514 | /* create the lua object. */ |
| 1515 | lua_newtable(L); |
| 1516 | lua_pushlightuserdata(L, args[0].data.map); |
| 1517 | lua_rawseti(L, -2, 0); |
| 1518 | |
| 1519 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1520 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1521 | lua_setmetatable(L, -2); |
| 1522 | |
| 1523 | |
| 1524 | return 1; |
| 1525 | } |
| 1526 | |
| 1527 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1528 | { |
| 1529 | struct map_descriptor *desc; |
| 1530 | struct pattern *pat; |
| 1531 | struct sample smp; |
| 1532 | |
| 1533 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1534 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1535 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1536 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1537 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1538 | } |
| 1539 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1540 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1541 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1542 | 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] | 1543 | } |
| 1544 | |
| 1545 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1546 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1547 | if (str) |
| 1548 | lua_pushstring(L, ""); |
| 1549 | else |
| 1550 | lua_pushnil(L); |
| 1551 | return 1; |
| 1552 | } |
| 1553 | |
| 1554 | /* 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] | 1555 | 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] | 1556 | return 1; |
| 1557 | } |
| 1558 | |
| 1559 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1560 | { |
| 1561 | return _hlua_map_lookup(L, 0); |
| 1562 | } |
| 1563 | |
| 1564 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1565 | { |
| 1566 | return _hlua_map_lookup(L, 1); |
| 1567 | } |
| 1568 | |
| 1569 | /* |
| 1570 | * |
| 1571 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1572 | * Class Socket |
| 1573 | * |
| 1574 | * |
| 1575 | */ |
| 1576 | |
| 1577 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1578 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1579 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1580 | } |
| 1581 | |
| 1582 | /* 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] | 1583 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1584 | * received. |
| 1585 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1586 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1587 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1588 | struct stream_interface *si = appctx->owner; |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 1589 | struct connection *c = cs_conn(objt_cs(si_opposite(si)->end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1591 | if (appctx->ctx.hlua_cosocket.die) { |
| 1592 | si_shutw(si); |
| 1593 | si_shutr(si); |
| 1594 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1595 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1596 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1597 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1598 | } |
| 1599 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1600 | /* If the connection object is not available, close all the |
| 1601 | * streams and wakeup everything waiting for. |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1602 | */ |
| 1603 | if (!c) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1604 | si_shutw(si); |
| 1605 | si_shutr(si); |
Willy Tarreau | 2bb4a96 | 2014-11-28 11:11:05 +0100 | [diff] [blame] | 1606 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1607 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1608 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1609 | return; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1610 | } |
| 1611 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1612 | /* If we cant write, wakeup the pending write signals. */ |
| 1613 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | |
| 1616 | /* If we cant read, wakeup the pending read signals. */ |
| 1617 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1618 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1619 | |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1620 | /* if the connection is not estabkished, inform the stream that we want |
| 1621 | * to be notified whenever the connection completes. |
| 1622 | */ |
| 1623 | if (!(c->flags & CO_FL_CONNECTED)) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1624 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1625 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1626 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1627 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1628 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1631 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1633 | /* 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] | 1634 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1635 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1636 | |
| 1637 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1638 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1639 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* Some data were injected in the buffer, notify the stream |
| 1642 | * interface. |
| 1643 | */ |
| 1644 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1645 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1646 | |
| 1647 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1648 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1649 | */ |
| 1650 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1651 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1654 | /* This function is called when the "struct stream" is destroyed. |
| 1655 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1656 | * Wake all the pending signals. |
| 1657 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1658 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1660 | struct xref *peer; |
| 1661 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1663 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1664 | if (peer) |
| 1665 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | |
| 1667 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1668 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1669 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1673 | * uses this object. If the stream does not exists, just quit. |
| 1674 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | * pending signal can rest in the read and write lists. destroy |
| 1676 | * it. |
| 1677 | */ |
| 1678 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1679 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1680 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1682 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1684 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1685 | |
| 1686 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1687 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1688 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1690 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1693 | appctx->ctx.hlua_cosocket.die = 1; |
| 1694 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1696 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1697 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1702 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1704 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1706 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1708 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1709 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1710 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1711 | |
| 1712 | /* Check if we run on the same thread than the xreator thread. |
| 1713 | * We cannot access to the socket if the thread is different. |
| 1714 | */ |
| 1715 | if (socket->tid != tid) |
| 1716 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1717 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1718 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1719 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1720 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1721 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1723 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1724 | appctx->ctx.hlua_cosocket.die = 1; |
| 1725 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1726 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1727 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1728 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1729 | return 0; |
| 1730 | } |
| 1731 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1732 | /* The close function calls close_helper. |
| 1733 | */ |
| 1734 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1735 | { |
| 1736 | MAY_LJMP(check_args(L, 1, "close")); |
| 1737 | return hlua_socket_close_helper(L); |
| 1738 | } |
| 1739 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1740 | /* This Lua function assumes that the stack contain three parameters. |
| 1741 | * 1 - USERDATA containing a struct socket |
| 1742 | * 2 - INTEGER with values of the macro defined below |
| 1743 | * If the integer is -1, we must read at most one line. |
| 1744 | * If the integer is -2, we ust read all the data until the |
| 1745 | * end of the stream. |
| 1746 | * If the integer is positive value, we must read a number of |
| 1747 | * bytes corresponding to this value. |
| 1748 | */ |
| 1749 | #define HLSR_READ_LINE (-1) |
| 1750 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1751 | __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] | 1752 | { |
| 1753 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1754 | int wanted = lua_tointeger(L, 2); |
| 1755 | struct hlua *hlua = hlua_gethlua(L); |
| 1756 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1757 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1758 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1759 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1760 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1761 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1762 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1763 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1764 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1765 | struct stream_interface *si; |
| 1766 | struct stream *s; |
| 1767 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1768 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1769 | |
| 1770 | /* Check if this lua stack is schedulable. */ |
| 1771 | if (!hlua || !hlua->task) |
| 1772 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1773 | "'frontend', 'backend' or 'task'")); |
| 1774 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1775 | /* Check if we run on the same thread than the xreator thread. |
| 1776 | * We cannot access to the socket if the thread is different. |
| 1777 | */ |
| 1778 | if (socket->tid != tid) |
| 1779 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1780 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1781 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1782 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1783 | if (!peer) |
| 1784 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1785 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1786 | si = appctx->owner; |
| 1787 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1788 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1789 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1790 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1792 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | if (nblk < 0) /* Connection close. */ |
| 1794 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1795 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1797 | |
| 1798 | /* remove final \r\n. */ |
| 1799 | if (nblk == 1) { |
| 1800 | if (blk1[len1-1] == '\n') { |
| 1801 | len1--; |
| 1802 | skip_at_end++; |
| 1803 | if (blk1[len1-1] == '\r') { |
| 1804 | len1--; |
| 1805 | skip_at_end++; |
| 1806 | } |
| 1807 | } |
| 1808 | } |
| 1809 | else { |
| 1810 | if (blk2[len2-1] == '\n') { |
| 1811 | len2--; |
| 1812 | skip_at_end++; |
| 1813 | if (blk2[len2-1] == '\r') { |
| 1814 | len2--; |
| 1815 | skip_at_end++; |
| 1816 | } |
| 1817 | } |
| 1818 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1819 | } |
| 1820 | |
| 1821 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1823 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | if (nblk < 0) /* Connection close. */ |
| 1825 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1826 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | goto connection_empty; |
| 1828 | } |
| 1829 | |
| 1830 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1831 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1832 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | if (nblk < 0) /* Connection close. */ |
| 1834 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1835 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | goto connection_empty; |
| 1837 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1838 | missing_bytes = wanted - socket->b.n; |
| 1839 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1840 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1841 | len1 = missing_bytes; |
| 1842 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1843 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | len = len1; |
| 1847 | |
| 1848 | luaL_addlstring(&socket->b, blk1, len1); |
| 1849 | if (nblk == 2) { |
| 1850 | len += len2; |
| 1851 | luaL_addlstring(&socket->b, blk2, len2); |
| 1852 | } |
| 1853 | |
| 1854 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1855 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1856 | |
| 1857 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1858 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | |
| 1860 | /* If the pattern reclaim to read all the data |
| 1861 | * in the connection, got out. |
| 1862 | */ |
| 1863 | if (wanted == HLSR_READ_ALL) |
| 1864 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1865 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1866 | goto connection_empty; |
| 1867 | |
| 1868 | /* Return result. */ |
| 1869 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1870 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1871 | return 1; |
| 1872 | |
| 1873 | connection_closed: |
| 1874 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1875 | xref_unlock(&socket->xref, peer); |
| 1876 | |
| 1877 | no_peer: |
| 1878 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1879 | /* If the buffer containds data. */ |
| 1880 | if (socket->b.n > 0) { |
| 1881 | luaL_pushresult(&socket->b); |
| 1882 | return 1; |
| 1883 | } |
| 1884 | lua_pushnil(L); |
| 1885 | lua_pushstring(L, "connection closed."); |
| 1886 | return 2; |
| 1887 | |
| 1888 | connection_empty: |
| 1889 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1890 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1891 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1892 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1893 | } |
| 1894 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1895 | 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] | 1896 | return 0; |
| 1897 | } |
| 1898 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1899 | /* This Lua function gets two parameters. The first one can be string |
| 1900 | * or a number. If the string is "*l", the user requires one line. If |
| 1901 | * 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] | 1902 | * If the value is a number, the user require a number of bytes equal |
| 1903 | * to the value. The default value is "*l" (a line). |
| 1904 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1905 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1906 | * integer takes this values: |
| 1907 | * -1 : read a line |
| 1908 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1909 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1910 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1911 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1912 | * concatenated with the read data. |
| 1913 | */ |
| 1914 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1915 | { |
| 1916 | int wanted = HLSR_READ_LINE; |
| 1917 | const char *pattern; |
| 1918 | int type; |
| 1919 | char *error; |
| 1920 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1921 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1922 | |
| 1923 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1924 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1925 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1926 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1928 | /* Check if we run on the same thread than the xreator thread. |
| 1929 | * We cannot access to the socket if the thread is different. |
| 1930 | */ |
| 1931 | if (socket->tid != tid) |
| 1932 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1933 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1934 | /* check for pattern. */ |
| 1935 | if (lua_gettop(L) >= 2) { |
| 1936 | type = lua_type(L, 2); |
| 1937 | if (type == LUA_TSTRING) { |
| 1938 | pattern = lua_tostring(L, 2); |
| 1939 | if (strcmp(pattern, "*a") == 0) |
| 1940 | wanted = HLSR_READ_ALL; |
| 1941 | else if (strcmp(pattern, "*l") == 0) |
| 1942 | wanted = HLSR_READ_LINE; |
| 1943 | else { |
| 1944 | wanted = strtoll(pattern, &error, 10); |
| 1945 | if (*error != '\0') |
| 1946 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1947 | } |
| 1948 | } |
| 1949 | else if (type == LUA_TNUMBER) { |
| 1950 | wanted = lua_tointeger(L, 2); |
| 1951 | if (wanted < 0) |
| 1952 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1953 | } |
| 1954 | } |
| 1955 | |
| 1956 | /* Set pattern. */ |
| 1957 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1958 | |
| 1959 | /* Check if we would replace the top by itself. */ |
| 1960 | if (lua_gettop(L) != 2) |
| 1961 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1962 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1963 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1964 | luaL_buffinit(L, &socket->b); |
| 1965 | |
| 1966 | /* Check prefix. */ |
| 1967 | if (lua_gettop(L) >= 3) { |
| 1968 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1969 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1970 | pattern = lua_tolstring(L, 3, &len); |
| 1971 | luaL_addlstring(&socket->b, pattern, len); |
| 1972 | } |
| 1973 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1974 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1975 | } |
| 1976 | |
| 1977 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1978 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1979 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1980 | 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] | 1981 | { |
| 1982 | struct hlua_socket *socket; |
| 1983 | struct hlua *hlua = hlua_gethlua(L); |
| 1984 | struct appctx *appctx; |
| 1985 | size_t buf_len; |
| 1986 | const char *buf; |
| 1987 | int len; |
| 1988 | int send_len; |
| 1989 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1990 | struct xref *peer; |
| 1991 | struct stream_interface *si; |
| 1992 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1993 | |
| 1994 | /* Check if this lua stack is schedulable. */ |
| 1995 | if (!hlua || !hlua->task) |
| 1996 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1997 | "'frontend', 'backend' or 'task'")); |
| 1998 | |
| 1999 | /* Get object */ |
| 2000 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2001 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2002 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2003 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2004 | /* Check if we run on the same thread than the xreator thread. |
| 2005 | * We cannot access to the socket if the thread is different. |
| 2006 | */ |
| 2007 | if (socket->tid != tid) |
| 2008 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2009 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2010 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2011 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2012 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | lua_pushinteger(L, -1); |
| 2014 | return 1; |
| 2015 | } |
| 2016 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2017 | si = appctx->owner; |
| 2018 | s = si_strm(si); |
| 2019 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2020 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2021 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2022 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | lua_pushinteger(L, -1); |
| 2024 | return 1; |
| 2025 | } |
| 2026 | |
| 2027 | /* Update the input buffer data. */ |
| 2028 | buf += sent; |
| 2029 | send_len = buf_len - sent; |
| 2030 | |
| 2031 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2032 | if (sent >= buf_len) { |
| 2033 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2034 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2035 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2036 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2037 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2038 | * the request buffer if its not required. |
| 2039 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2040 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2041 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2042 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2043 | } |
| 2044 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2045 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2046 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2047 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2049 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2050 | |
| 2051 | /* send data */ |
| 2052 | if (len < send_len) |
| 2053 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2054 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2055 | |
| 2056 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2057 | * the data" (-2) are not expected because the available length |
| 2058 | * is tested. |
| 2059 | * Other unknown error are also not expected. |
| 2060 | */ |
| 2061 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2062 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2063 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2064 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2065 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2066 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2067 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2068 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | return 1; |
| 2070 | } |
| 2071 | |
| 2072 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2073 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2074 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2075 | s->req.rex = TICK_ETERNITY; |
| 2076 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2077 | |
| 2078 | /* Update length sent. */ |
| 2079 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2080 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2081 | |
| 2082 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2083 | if (sent + len >= buf_len) { |
| 2084 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2085 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2086 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2087 | |
| 2088 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2089 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2090 | xref_unlock(&socket->xref, peer); |
| 2091 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2092 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2093 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2094 | 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] | 2095 | return 0; |
| 2096 | } |
| 2097 | |
| 2098 | /* This function initiate the send of data. It just check the input |
| 2099 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2100 | * 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] | 2101 | * "hlua_socket_write_yield" that can yield. |
| 2102 | * |
| 2103 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2104 | * the associated object. The second is a string buffer. The third is |
| 2105 | * a facultative integer that represents where is the buffer position |
| 2106 | * of the start of the data that can send. The first byte is the |
| 2107 | * position "1". The default value is "1". The fourth argument is a |
| 2108 | * facultative integer that represents where is the buffer position |
| 2109 | * of the end of the data that can send. The default is the last byte. |
| 2110 | */ |
| 2111 | static int hlua_socket_send(struct lua_State *L) |
| 2112 | { |
| 2113 | int i; |
| 2114 | int j; |
| 2115 | const char *buf; |
| 2116 | size_t buf_len; |
| 2117 | |
| 2118 | /* Check number of arguments. */ |
| 2119 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2120 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2121 | |
| 2122 | /* Get the string. */ |
| 2123 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2124 | |
| 2125 | /* Get and check j. */ |
| 2126 | if (lua_gettop(L) == 4) { |
| 2127 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2128 | if (j < 0) |
| 2129 | j = buf_len + j + 1; |
| 2130 | if (j > buf_len) |
| 2131 | j = buf_len + 1; |
| 2132 | lua_pop(L, 1); |
| 2133 | } |
| 2134 | else |
| 2135 | j = buf_len; |
| 2136 | |
| 2137 | /* Get and check i. */ |
| 2138 | if (lua_gettop(L) == 3) { |
| 2139 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2140 | if (i < 0) |
| 2141 | i = buf_len + i + 1; |
| 2142 | if (i > buf_len) |
| 2143 | i = buf_len + 1; |
| 2144 | lua_pop(L, 1); |
| 2145 | } else |
| 2146 | i = 1; |
| 2147 | |
| 2148 | /* Check bth i and j. */ |
| 2149 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2150 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2151 | return 1; |
| 2152 | } |
| 2153 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2154 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2155 | return 1; |
| 2156 | } |
| 2157 | if (i == 0) |
| 2158 | i = 1; |
| 2159 | if (j == 0) |
| 2160 | j = 1; |
| 2161 | |
| 2162 | /* Pop the string. */ |
| 2163 | lua_pop(L, 1); |
| 2164 | |
| 2165 | /* Update the buffer length. */ |
| 2166 | buf += i - 1; |
| 2167 | buf_len = j - i + 1; |
| 2168 | lua_pushlstring(L, buf, buf_len); |
| 2169 | |
| 2170 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2171 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2172 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2173 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2174 | } |
| 2175 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2176 | #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] | 2177 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2178 | { |
| 2179 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2180 | int ret; |
| 2181 | int len; |
| 2182 | char *p; |
| 2183 | |
| 2184 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2185 | if (ret <= 0) { |
| 2186 | lua_pushnil(L); |
| 2187 | return 1; |
| 2188 | } |
| 2189 | |
| 2190 | if (ret == AF_UNIX) { |
| 2191 | lua_pushstring(L, buffer+1); |
| 2192 | return 1; |
| 2193 | } |
| 2194 | else if (ret == AF_INET6) { |
| 2195 | buffer[0] = '['; |
| 2196 | len = strlen(buffer); |
| 2197 | buffer[len] = ']'; |
| 2198 | len++; |
| 2199 | buffer[len] = ':'; |
| 2200 | len++; |
| 2201 | p = buffer; |
| 2202 | } |
| 2203 | else if (ret == AF_INET) { |
| 2204 | p = buffer + 1; |
| 2205 | len = strlen(p); |
| 2206 | p[len] = ':'; |
| 2207 | len++; |
| 2208 | } |
| 2209 | else { |
| 2210 | lua_pushnil(L); |
| 2211 | return 1; |
| 2212 | } |
| 2213 | |
| 2214 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2215 | lua_pushnil(L); |
| 2216 | return 1; |
| 2217 | } |
| 2218 | |
| 2219 | lua_pushstring(L, p); |
| 2220 | return 1; |
| 2221 | } |
| 2222 | |
| 2223 | /* Returns information about the peer of the connection. */ |
| 2224 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2225 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2226 | struct hlua_socket *socket; |
| 2227 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2228 | struct xref *peer; |
| 2229 | struct appctx *appctx; |
| 2230 | struct stream_interface *si; |
| 2231 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2232 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2233 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2234 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2235 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2236 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2237 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2238 | /* Check if we run on the same thread than the xreator thread. |
| 2239 | * We cannot access to the socket if the thread is different. |
| 2240 | */ |
| 2241 | if (socket->tid != tid) |
| 2242 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2243 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2244 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2245 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2246 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2247 | lua_pushnil(L); |
| 2248 | return 1; |
| 2249 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2250 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2251 | si = appctx->owner; |
| 2252 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2253 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2254 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2256 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2257 | lua_pushnil(L); |
| 2258 | return 1; |
| 2259 | } |
| 2260 | |
Willy Tarreau | 428d8e3 | 2019-07-17 11:51:35 +0200 | [diff] [blame] | 2261 | if (!conn_get_dst(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2262 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2263 | lua_pushnil(L); |
| 2264 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2267 | ret = MAY_LJMP(hlua_socket_info(L, &conn->addr.to)); |
| 2268 | xref_unlock(&socket->xref, peer); |
| 2269 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2270 | } |
| 2271 | |
| 2272 | /* Returns information about my connection side. */ |
| 2273 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2274 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2275 | struct hlua_socket *socket; |
| 2276 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2277 | struct appctx *appctx; |
| 2278 | struct xref *peer; |
| 2279 | struct stream_interface *si; |
| 2280 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2281 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2282 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2283 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2284 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2285 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2286 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2287 | /* Check if we run on the same thread than the xreator thread. |
| 2288 | * We cannot access to the socket if the thread is different. |
| 2289 | */ |
| 2290 | if (socket->tid != tid) |
| 2291 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2292 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2293 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2294 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2295 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2296 | lua_pushnil(L); |
| 2297 | return 1; |
| 2298 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2299 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2300 | si = appctx->owner; |
| 2301 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2303 | conn = cs_conn(objt_cs(s->si[1].end)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2304 | if (!conn) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2305 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2306 | lua_pushnil(L); |
| 2307 | return 1; |
| 2308 | } |
| 2309 | |
Willy Tarreau | 428d8e3 | 2019-07-17 11:51:35 +0200 | [diff] [blame] | 2310 | if (!conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2311 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2312 | lua_pushnil(L); |
| 2313 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2314 | } |
| 2315 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2316 | ret = hlua_socket_info(L, &conn->addr.from); |
| 2317 | xref_unlock(&socket->xref, peer); |
| 2318 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2319 | } |
| 2320 | |
| 2321 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2322 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | .obj_type = OBJ_TYPE_APPLET, |
| 2324 | .name = "<LUA_TCP>", |
| 2325 | .fct = hlua_socket_handler, |
| 2326 | .release = hlua_socket_release, |
| 2327 | }; |
| 2328 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2329 | __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] | 2330 | { |
| 2331 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2332 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2333 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2334 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2335 | struct stream_interface *si; |
| 2336 | struct stream *s; |
| 2337 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2338 | /* Check if we run on the same thread than the xreator thread. |
| 2339 | * We cannot access to the socket if the thread is different. |
| 2340 | */ |
| 2341 | if (socket->tid != tid) |
| 2342 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2343 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2344 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2345 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2346 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2347 | lua_pushnil(L); |
| 2348 | lua_pushstring(L, "Can't connect"); |
| 2349 | return 2; |
| 2350 | } |
| 2351 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2352 | si = appctx->owner; |
| 2353 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2354 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2355 | /* Check if we run on the same thread than the xreator thread. |
| 2356 | * We cannot access to the socket if the thread is different. |
| 2357 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2358 | if (socket->tid != tid) { |
| 2359 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2360 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2361 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2362 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2363 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2364 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2365 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2366 | lua_pushnil(L); |
| 2367 | lua_pushstring(L, "Can't connect"); |
| 2368 | return 2; |
| 2369 | } |
| 2370 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2371 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2372 | |
| 2373 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2374 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2375 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2376 | lua_pushinteger(L, 1); |
| 2377 | return 1; |
| 2378 | } |
| 2379 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2380 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2381 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2382 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2383 | } |
| 2384 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2385 | 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] | 2386 | return 0; |
| 2387 | } |
| 2388 | |
| 2389 | /* This function fail or initite the connection. */ |
| 2390 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2391 | { |
| 2392 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2393 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | const char *ip; |
| 2395 | struct connection *conn; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2396 | struct hlua *hlua; |
| 2397 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2398 | int low, high; |
| 2399 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2400 | struct xref *peer; |
| 2401 | struct stream_interface *si; |
| 2402 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2403 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2404 | if (lua_gettop(L) < 2) |
| 2405 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2406 | |
| 2407 | /* Get args. */ |
| 2408 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2409 | |
| 2410 | /* Check if we run on the same thread than the xreator thread. |
| 2411 | * We cannot access to the socket if the thread is different. |
| 2412 | */ |
| 2413 | if (socket->tid != tid) |
| 2414 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2415 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2416 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2417 | if (lua_gettop(L) >= 3) { |
| 2418 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2419 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2420 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2421 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2422 | * that are not enclosed within square brackets. |
| 2423 | */ |
| 2424 | if (port > 0) { |
| 2425 | luaL_buffinit(L, &b); |
| 2426 | luaL_addstring(&b, ip); |
| 2427 | luaL_addchar(&b, ':'); |
| 2428 | luaL_pushresult(&b); |
| 2429 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2430 | } |
| 2431 | } |
| 2432 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2433 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2434 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2435 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2436 | lua_pushnil(L); |
| 2437 | return 1; |
| 2438 | } |
| 2439 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2440 | si = appctx->owner; |
| 2441 | s = si_strm(si); |
| 2442 | |
| 2443 | /* Initialise connection. */ |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2444 | conn = cs_conn(si_alloc_cs(&s->si[1], NULL)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | if (!conn) { |
| 2446 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2447 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2449 | |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2450 | /* needed for the connection not to be closed */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2451 | conn->target = s->target; |
Willy Tarreau | 3adac08 | 2015-09-26 17:51:09 +0200 | [diff] [blame] | 2452 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2453 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2454 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2455 | if (!addr) { |
| 2456 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2457 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2458 | } |
| 2459 | if (low != high) { |
| 2460 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2461 | 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] | 2462 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2463 | memcpy(&conn->addr.to, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2464 | |
| 2465 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2466 | if (low == 0) { |
| 2467 | if (conn->addr.to.ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2468 | if (port == -1) { |
| 2469 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2470 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | ((struct sockaddr_in *)&conn->addr.to)->sin_port = htons(port); |
| 2473 | } else if (conn->addr.to.ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2474 | if (port == -1) { |
| 2475 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2476 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2477 | } |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2478 | ((struct sockaddr_in6 *)&conn->addr.to)->sin6_port = htons(port); |
| 2479 | } |
| 2480 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2481 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2482 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2483 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2484 | |
| 2485 | /* inform the stream that we want to be notified whenever the |
| 2486 | * connection completes. |
| 2487 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2488 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2489 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2490 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2491 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2492 | hlua->flags |= HLUA_MUST_GC; |
| 2493 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2494 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2495 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2496 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2497 | } |
| 2498 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2499 | |
| 2500 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2501 | /* Return yield waiting for connection. */ |
| 2502 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2503 | 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] | 2504 | |
| 2505 | return 0; |
| 2506 | } |
| 2507 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2508 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2510 | { |
| 2511 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2512 | struct xref *peer; |
| 2513 | struct appctx *appctx; |
| 2514 | struct stream_interface *si; |
| 2515 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | |
| 2517 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2518 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2519 | |
| 2520 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2521 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2522 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2523 | lua_pushnil(L); |
| 2524 | return 1; |
| 2525 | } |
| 2526 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2527 | si = appctx->owner; |
| 2528 | s = si_strm(si); |
| 2529 | |
| 2530 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2531 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2532 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2533 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2534 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | |
| 2536 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2537 | { |
| 2538 | return 0; |
| 2539 | } |
| 2540 | |
| 2541 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2542 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2543 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2544 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2545 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2546 | struct xref *peer; |
| 2547 | struct appctx *appctx; |
| 2548 | struct stream_interface *si; |
| 2549 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2550 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2551 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2552 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2553 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2554 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2555 | /* convert the timeout to millis */ |
| 2556 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2558 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2559 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2560 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2561 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2562 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2563 | 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] | 2564 | |
| 2565 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2566 | if (tmout == 0) |
| 2567 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2568 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2569 | /* Check if we run on the same thread than the xreator thread. |
| 2570 | * We cannot access to the socket if the thread is different. |
| 2571 | */ |
| 2572 | if (socket->tid != tid) |
| 2573 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2574 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2575 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2576 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2577 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2578 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2579 | WILL_LJMP(lua_error(L)); |
| 2580 | return 0; |
| 2581 | } |
| 2582 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2583 | si = appctx->owner; |
| 2584 | s = si_strm(si); |
| 2585 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2586 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2587 | s->req.rto = tmout; |
| 2588 | s->req.wto = tmout; |
| 2589 | s->res.rto = tmout; |
| 2590 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2591 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2592 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2593 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2594 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2595 | |
| 2596 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2597 | task_queue(s->task); |
| 2598 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2599 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2600 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2601 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2602 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2606 | { |
| 2607 | struct hlua_socket *socket; |
| 2608 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2609 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2610 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2611 | |
| 2612 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2613 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2614 | hlua_pusherror(L, "socket: full stack"); |
| 2615 | goto out_fail_conf; |
| 2616 | } |
| 2617 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2618 | /* Create the object: obj[0] = userdata. */ |
| 2619 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2621 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2622 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2623 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2625 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2626 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2627 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2628 | goto out_fail_conf; |
| 2629 | } |
| 2630 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2631 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2633 | lua_setmetatable(L, -2); |
| 2634 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2635 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2636 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2637 | if (!appctx) { |
| 2638 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2639 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2640 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2641 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2642 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2643 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2644 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2645 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2646 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2647 | /* Now create a session, task and stream for this applet */ |
| 2648 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2649 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2650 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2651 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | } |
| 2653 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2654 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2655 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2657 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2660 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2661 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2663 | /* Configure "right" stream interface. this "si" is used to connect |
| 2664 | * and retrieve data from the server. The connection is initialized |
| 2665 | * with the "struct server". |
| 2666 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2667 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | |
| 2669 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2670 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2671 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2672 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2673 | return 1; |
| 2674 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2675 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2676 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2677 | out_fail_sess: |
| 2678 | appctx_free(appctx); |
| 2679 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2680 | WILL_LJMP(lua_error(L)); |
| 2681 | return 0; |
| 2682 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2683 | |
| 2684 | /* |
| 2685 | * |
| 2686 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2687 | * Class Channel |
| 2688 | * |
| 2689 | * |
| 2690 | */ |
| 2691 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2692 | /* This function is called before the Lua execution. It stores |
| 2693 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2694 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2695 | static inline void consistency_set(struct stream *stream, int opt, struct hlua_consistency *c) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2696 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2697 | c->mode = stream->be->mode; |
| 2698 | switch (c->mode) { |
| 2699 | case PR_MODE_HTTP: |
| 2700 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2701 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2702 | c->data.http.state = stream->txn->req.msg_state; |
| 2703 | else |
| 2704 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2705 | break; |
| 2706 | default: |
| 2707 | break; |
| 2708 | } |
| 2709 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2710 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2711 | /* This function is called after the Lua execution. it |
| 2712 | * returns true if the parser state is consistent, otherwise, |
| 2713 | * it return false. |
| 2714 | * |
| 2715 | * In HTTP mode, the parser state must be in the same state |
| 2716 | * or greater when we exit the function. Even if we do a |
| 2717 | * control yield. This prevent to break the HTTP message |
| 2718 | * from the Lua code. |
| 2719 | */ |
| 2720 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2721 | { |
| 2722 | if (c->mode != stream->be->mode) |
| 2723 | return 0; |
| 2724 | |
| 2725 | switch (c->mode) { |
| 2726 | case PR_MODE_HTTP: |
| 2727 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2728 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2729 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2730 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2731 | else |
| 2732 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2733 | default: |
| 2734 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2735 | } |
| 2736 | return 1; |
| 2737 | } |
| 2738 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2739 | /* Returns the struct hlua_channel join to the class channel in the |
| 2740 | * stack entry "ud" or throws an argument error. |
| 2741 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2742 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2743 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2744 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2745 | } |
| 2746 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2747 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2748 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2750 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2753 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2754 | return 0; |
| 2755 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2756 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2757 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2758 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2759 | |
| 2760 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2761 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2762 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | return 1; |
| 2764 | } |
| 2765 | |
| 2766 | /* Duplicate all the data present in the input channel and put it |
| 2767 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2768 | * the stack if the channel is closed and all the data are consumed, |
| 2769 | * returns 0 if no data are available, otherwise it returns the length |
| 2770 | * of the builded string. |
| 2771 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2772 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2773 | { |
| 2774 | char *blk1; |
| 2775 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2776 | size_t len1; |
| 2777 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2778 | int ret; |
| 2779 | luaL_Buffer b; |
| 2780 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2781 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | if (unlikely(ret == 0)) |
| 2783 | return 0; |
| 2784 | |
| 2785 | if (unlikely(ret < 0)) { |
| 2786 | lua_pushnil(L); |
| 2787 | return -1; |
| 2788 | } |
| 2789 | |
| 2790 | luaL_buffinit(L, &b); |
| 2791 | luaL_addlstring(&b, blk1, len1); |
| 2792 | if (unlikely(ret == 2)) |
| 2793 | luaL_addlstring(&b, blk2, len2); |
| 2794 | luaL_pushresult(&b); |
| 2795 | |
| 2796 | if (unlikely(ret == 2)) |
| 2797 | return len1 + len2; |
| 2798 | return len1; |
| 2799 | } |
| 2800 | |
| 2801 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2802 | * a yield. This function keep the data in the buffer. |
| 2803 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2804 | __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] | 2805 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2806 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2807 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2808 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2809 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2810 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2811 | WILL_LJMP(lua_error(L)); |
| 2812 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2813 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2814 | 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] | 2815 | return 1; |
| 2816 | } |
| 2817 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2818 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2819 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2820 | { |
| 2821 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2822 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2823 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2824 | } |
| 2825 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2826 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2827 | * a yield. This function consumes the data in the buffer. It returns |
| 2828 | * a string containing the data or a nil pointer if no data are available |
| 2829 | * and the channel is closed. |
| 2830 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2831 | __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] | 2832 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2833 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2834 | int ret; |
| 2835 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2836 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2838 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2839 | WILL_LJMP(lua_error(L)); |
| 2840 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2841 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2842 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2843 | 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] | 2844 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2845 | if (unlikely(ret == -1)) |
| 2846 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2848 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2849 | return 1; |
| 2850 | } |
| 2851 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2852 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2853 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2854 | { |
| 2855 | MAY_LJMP(check_args(L, 1, "get")); |
| 2856 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2857 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2858 | } |
| 2859 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2860 | /* This functions consumes and returns one line. If the channel is closed, |
| 2861 | * 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] | 2862 | * 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] | 2863 | * value. |
| 2864 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2865 | __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] | 2866 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2867 | char *blk1; |
| 2868 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2869 | size_t len1; |
| 2870 | size_t len2; |
| 2871 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2872 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2873 | int ret; |
| 2874 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2875 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2876 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2877 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2878 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2879 | WILL_LJMP(lua_error(L)); |
| 2880 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2881 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2882 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2883 | 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] | 2884 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2885 | if (ret == -1) { |
| 2886 | lua_pushnil(L); |
| 2887 | return 1; |
| 2888 | } |
| 2889 | |
| 2890 | luaL_buffinit(L, &b); |
| 2891 | luaL_addlstring(&b, blk1, len1); |
| 2892 | len = len1; |
| 2893 | if (unlikely(ret == 2)) { |
| 2894 | luaL_addlstring(&b, blk2, len2); |
| 2895 | len += len2; |
| 2896 | } |
| 2897 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2898 | 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] | 2899 | return 1; |
| 2900 | } |
| 2901 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2902 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2903 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2904 | { |
| 2905 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2906 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2907 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2908 | } |
| 2909 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2910 | /* This function takes a string as input, and append it at the |
| 2911 | * input side of channel. If the data is too big, but a space |
| 2912 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2913 | * yields. If the data is bigger than the buffer, or if the |
| 2914 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2915 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2917 | __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] | 2918 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2919 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2920 | size_t len; |
| 2921 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2922 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2923 | int ret; |
| 2924 | int max; |
| 2925 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2926 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2927 | WILL_LJMP(lua_error(L)); |
| 2928 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2929 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2930 | * the request buffer if its not required. |
| 2931 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2932 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2933 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2934 | 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] | 2935 | } |
| 2936 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2937 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2938 | if (max > len - l) |
| 2939 | max = len - l; |
| 2940 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2941 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2942 | if (ret == -2 || ret == -3) { |
| 2943 | lua_pushinteger(L, -1); |
| 2944 | return 1; |
| 2945 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2946 | if (ret == -1) { |
| 2947 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2948 | 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] | 2949 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2950 | l += ret; |
| 2951 | lua_pop(L, 1); |
| 2952 | lua_pushinteger(L, l); |
| 2953 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2954 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2955 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2956 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2957 | * in this case, we cannot add more data, so we cannot yield, |
| 2958 | * we return the amount of copyied data. |
| 2959 | */ |
| 2960 | return 1; |
| 2961 | } |
| 2962 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2963 | 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] | 2964 | return 1; |
| 2965 | } |
| 2966 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2967 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2968 | * 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] | 2969 | * buffer size is too little for the data. |
| 2970 | */ |
| 2971 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2972 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2973 | size_t len; |
| 2974 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2975 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2976 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2977 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2978 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2979 | lua_pushinteger(L, 0); |
| 2980 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2981 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2982 | } |
| 2983 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2984 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2985 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2986 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2987 | * or -1 if the channel is closed or if the buffer size is too |
| 2988 | * little for the data. |
| 2989 | */ |
| 2990 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2991 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2992 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2993 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2994 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2995 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2996 | lua_pushinteger(L, 0); |
| 2997 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2998 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2999 | WILL_LJMP(lua_error(L)); |
| 3000 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3001 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3003 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | } |
| 3005 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3006 | /* Append data in the output side of the buffer. This data is immediately |
| 3007 | * sent. The function returns the amount of data written. If the buffer |
| 3008 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3009 | * if the channel is closed. |
| 3010 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3011 | __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] | 3012 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3013 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3014 | size_t len; |
| 3015 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3016 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3017 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3018 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3019 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3020 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3021 | WILL_LJMP(lua_error(L)); |
| 3022 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3023 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3024 | lua_pushinteger(L, -1); |
| 3025 | return 1; |
| 3026 | } |
| 3027 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3028 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3029 | * the request buffer if its not required. |
| 3030 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3031 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3032 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3033 | 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] | 3034 | } |
| 3035 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3036 | /* The written data will be immediately sent, so we can check |
| 3037 | * the available space without taking in account the reserve. |
| 3038 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3039 | * data, because the buffer will be flushed. |
| 3040 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3041 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3042 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3043 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3044 | * in this case, we cannot add more data, so we cannot yield, |
| 3045 | * we return the amount of copyied data. |
| 3046 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3047 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3048 | return 1; |
| 3049 | |
| 3050 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3051 | if (max > len - l) |
| 3052 | max = len - l; |
| 3053 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3054 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3055 | * detects a non contiguous buffer and realign it. |
| 3056 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3057 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3058 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3059 | |
| 3060 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3061 | 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] | 3062 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3063 | /* buffer replace considers that the input part is filled. |
| 3064 | * so, I must forward these new data in the output part. |
| 3065 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3066 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3067 | |
| 3068 | l += max; |
| 3069 | lua_pop(L, 1); |
| 3070 | lua_pushinteger(L, l); |
| 3071 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3072 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3073 | * in this case, we cannot add more data, so we cannot yield, |
| 3074 | * we return the amount of copyied data. |
| 3075 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3076 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3077 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3078 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3079 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3080 | if (l < len) { |
| 3081 | /* If we are waiting for space in the response buffer, we |
| 3082 | * must set the flag WAKERESWR. This flag required the task |
| 3083 | * wake up if any activity is detected on the response buffer. |
| 3084 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3085 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3086 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3087 | else |
| 3088 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3089 | 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] | 3090 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | |
| 3092 | return 1; |
| 3093 | } |
| 3094 | |
| 3095 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3096 | * yield the LUA process, and resume it without checking the |
| 3097 | * input arguments. |
| 3098 | */ |
| 3099 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3100 | { |
| 3101 | MAY_LJMP(check_args(L, 2, "send")); |
| 3102 | lua_pushinteger(L, 0); |
| 3103 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3104 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3105 | } |
| 3106 | |
| 3107 | /* This function forward and amount of butes. The data pass from |
| 3108 | * the input side of the buffer to the output side, and can be |
| 3109 | * forwarded. This function never fails. |
| 3110 | * |
| 3111 | * The Lua function takes an amount of bytes to be forwarded in |
| 3112 | * imput. It returns the number of bytes forwarded. |
| 3113 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3114 | __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] | 3115 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3116 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3117 | int len; |
| 3118 | int l; |
| 3119 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3120 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3121 | |
| 3122 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3123 | |
| 3124 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3125 | WILL_LJMP(lua_error(L)); |
| 3126 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3128 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3129 | |
| 3130 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3131 | if (max > ci_data(chn)) |
| 3132 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3133 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3134 | l += max; |
| 3135 | |
| 3136 | lua_pop(L, 1); |
| 3137 | lua_pushinteger(L, l); |
| 3138 | |
| 3139 | /* Check if it miss bytes to forward. */ |
| 3140 | if (l < len) { |
| 3141 | /* The the input channel or the output channel are closed, we |
| 3142 | * must return the amount of data forwarded. |
| 3143 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3144 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3145 | return 1; |
| 3146 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3147 | /* If we are waiting for space data in the response buffer, we |
| 3148 | * must set the flag WAKERESWR. This flag required the task |
| 3149 | * wake up if any activity is detected on the response buffer. |
| 3150 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3151 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3152 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3153 | else |
| 3154 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3155 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3156 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3157 | 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] | 3158 | } |
| 3159 | |
| 3160 | return 1; |
| 3161 | } |
| 3162 | |
| 3163 | /* Just check the input and prepare the stack for the previous |
| 3164 | * function "hlua_channel_forward_yield" |
| 3165 | */ |
| 3166 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3167 | { |
| 3168 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3169 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3170 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3171 | |
| 3172 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3173 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3174 | } |
| 3175 | |
| 3176 | /* Just returns the number of bytes available in the input |
| 3177 | * side of the buffer. This function never fails. |
| 3178 | */ |
| 3179 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3180 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3181 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3182 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3183 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3184 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3185 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3186 | struct htx *htx = htxbuf(&chn->buf); |
| 3187 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3188 | } |
| 3189 | else |
| 3190 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3191 | return 1; |
| 3192 | } |
| 3193 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3194 | /* Returns true if the channel is full. */ |
| 3195 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3196 | { |
| 3197 | struct channel *chn; |
| 3198 | int rem; |
| 3199 | |
| 3200 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3201 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3202 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3203 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3204 | struct htx *htx = htxbuf(&chn->buf); |
| 3205 | |
| 3206 | rem = htx_free_data_space(htx); |
| 3207 | } |
| 3208 | else |
| 3209 | rem = b_room(&chn->buf); |
| 3210 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3211 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3212 | |
| 3213 | lua_pushboolean(L, rem <= 0); |
| 3214 | return 1; |
| 3215 | } |
| 3216 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3217 | /* Just returns the number of bytes available in the output |
| 3218 | * side of the buffer. This function never fails. |
| 3219 | */ |
| 3220 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3221 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3222 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3223 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3224 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3225 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3226 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3227 | return 1; |
| 3228 | } |
| 3229 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3230 | /* |
| 3231 | * |
| 3232 | * |
| 3233 | * Class Fetches |
| 3234 | * |
| 3235 | * |
| 3236 | */ |
| 3237 | |
| 3238 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3239 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3240 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3241 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3242 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3243 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3244 | } |
| 3245 | |
| 3246 | /* This function creates and push in the stack a fetch object according |
| 3247 | * with a current TXN. |
| 3248 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3249 | 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] | 3250 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3251 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3252 | |
| 3253 | /* Check stack size. */ |
| 3254 | if (!lua_checkstack(L, 3)) |
| 3255 | return 0; |
| 3256 | |
| 3257 | /* Create the object: obj[0] = userdata. |
| 3258 | * Note that the base of the Fetches object is the |
| 3259 | * transaction object. |
| 3260 | */ |
| 3261 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3262 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3263 | lua_rawseti(L, -2, 0); |
| 3264 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3265 | hsmp->s = txn->s; |
| 3266 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3267 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3268 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3269 | |
| 3270 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3271 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3272 | lua_setmetatable(L, -2); |
| 3273 | |
| 3274 | return 1; |
| 3275 | } |
| 3276 | |
| 3277 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3278 | * It uses closure argument to store the associated sample-fetch. It |
| 3279 | * returns only one argument or throws an error. An error is thrown |
| 3280 | * only if an error is encountered during the argument parsing. If |
| 3281 | * the "sample-fetch" function fails, nil is returned. |
| 3282 | */ |
| 3283 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3284 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3285 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3286 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3287 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3288 | int i; |
| 3289 | struct sample smp; |
| 3290 | |
| 3291 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3292 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3293 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3294 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3295 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3296 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3297 | /* Check execution authorization. */ |
| 3298 | if (f->use & SMP_USE_HTTP_ANY && |
| 3299 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3300 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3301 | "is not available in Lua services", f->kw); |
| 3302 | WILL_LJMP(lua_error(L)); |
| 3303 | } |
| 3304 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3305 | /* Get extra arguments. */ |
| 3306 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3307 | if (i >= ARGM_NBARGS) |
| 3308 | break; |
| 3309 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3310 | } |
| 3311 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3312 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3313 | |
| 3314 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3315 | 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] | 3316 | |
| 3317 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3318 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3319 | lua_pushfstring(L, "error in arguments"); |
| 3320 | WILL_LJMP(lua_error(L)); |
| 3321 | } |
| 3322 | |
| 3323 | /* Initialise the sample. */ |
| 3324 | memset(&smp, 0, sizeof(smp)); |
| 3325 | |
| 3326 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3327 | 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] | 3328 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3329 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3330 | lua_pushstring(L, ""); |
| 3331 | else |
| 3332 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3333 | return 1; |
| 3334 | } |
| 3335 | |
| 3336 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3337 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3338 | hlua_smp2lua_str(L, &smp); |
| 3339 | else |
| 3340 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3341 | return 1; |
| 3342 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3343 | |
| 3344 | /* |
| 3345 | * |
| 3346 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3347 | * Class Converters |
| 3348 | * |
| 3349 | * |
| 3350 | */ |
| 3351 | |
| 3352 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3353 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3354 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3355 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3356 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3357 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3358 | } |
| 3359 | |
| 3360 | /* This function creates and push in the stack a Converters object |
| 3361 | * according with a current TXN. |
| 3362 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3363 | 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] | 3364 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3365 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3366 | |
| 3367 | /* Check stack size. */ |
| 3368 | if (!lua_checkstack(L, 3)) |
| 3369 | return 0; |
| 3370 | |
| 3371 | /* Create the object: obj[0] = userdata. |
| 3372 | * Note that the base of the Converters object is the |
| 3373 | * same than the TXN object. |
| 3374 | */ |
| 3375 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3376 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | lua_rawseti(L, -2, 0); |
| 3378 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3379 | hsmp->s = txn->s; |
| 3380 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3381 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3382 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3383 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3384 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3385 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3386 | lua_setmetatable(L, -2); |
| 3387 | |
| 3388 | return 1; |
| 3389 | } |
| 3390 | |
| 3391 | /* This function is an LUA binding. It is called with each converter. |
| 3392 | * It uses closure argument to store the associated converter. It |
| 3393 | * returns only one argument or throws an error. An error is thrown |
| 3394 | * only if an error is encountered during the argument parsing. If |
| 3395 | * the converter function function fails, nil is returned. |
| 3396 | */ |
| 3397 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3398 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3399 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3400 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3401 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3402 | int i; |
| 3403 | struct sample smp; |
| 3404 | |
| 3405 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3406 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3407 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3408 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3409 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3410 | |
| 3411 | /* Get extra arguments. */ |
| 3412 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3413 | if (i >= ARGM_NBARGS) |
| 3414 | break; |
| 3415 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3416 | } |
| 3417 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3418 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3419 | |
| 3420 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3421 | 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] | 3422 | |
| 3423 | /* Run the special args checker. */ |
| 3424 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3425 | hlua_pusherror(L, "error in arguments"); |
| 3426 | WILL_LJMP(lua_error(L)); |
| 3427 | } |
| 3428 | |
| 3429 | /* Initialise the sample. */ |
| 3430 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3431 | hlua_pusherror(L, "error in the input argument"); |
| 3432 | WILL_LJMP(lua_error(L)); |
| 3433 | } |
| 3434 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3435 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3436 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3437 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3438 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3439 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3440 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3441 | WILL_LJMP(lua_error(L)); |
| 3442 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3443 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3444 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3445 | hlua_pusherror(L, "error during the input argument casting"); |
| 3446 | WILL_LJMP(lua_error(L)); |
| 3447 | } |
| 3448 | |
| 3449 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3450 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3451 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3452 | lua_pushstring(L, ""); |
| 3453 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3454 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3455 | return 1; |
| 3456 | } |
| 3457 | |
| 3458 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3459 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3460 | hlua_smp2lua_str(L, &smp); |
| 3461 | else |
| 3462 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3463 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3464 | } |
| 3465 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3466 | /* |
| 3467 | * |
| 3468 | * |
| 3469 | * Class AppletTCP |
| 3470 | * |
| 3471 | * |
| 3472 | */ |
| 3473 | |
| 3474 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3475 | * a class stream, otherwise it throws an error. |
| 3476 | */ |
| 3477 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3478 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3479 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3480 | } |
| 3481 | |
| 3482 | /* This function creates and push in the stack an Applet object |
| 3483 | * according with a current TXN. |
| 3484 | */ |
| 3485 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3486 | { |
| 3487 | struct hlua_appctx *appctx; |
| 3488 | struct stream_interface *si = ctx->owner; |
| 3489 | struct stream *s = si_strm(si); |
| 3490 | struct proxy *p = s->be; |
| 3491 | |
| 3492 | /* Check stack size. */ |
| 3493 | if (!lua_checkstack(L, 3)) |
| 3494 | return 0; |
| 3495 | |
| 3496 | /* Create the object: obj[0] = userdata. |
| 3497 | * Note that the base of the Converters object is the |
| 3498 | * same than the TXN object. |
| 3499 | */ |
| 3500 | lua_newtable(L); |
| 3501 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3502 | lua_rawseti(L, -2, 0); |
| 3503 | appctx->appctx = ctx; |
| 3504 | appctx->htxn.s = s; |
| 3505 | appctx->htxn.p = p; |
| 3506 | |
| 3507 | /* Create the "f" field that contains a list of fetches. */ |
| 3508 | lua_pushstring(L, "f"); |
| 3509 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3510 | return 0; |
| 3511 | lua_settable(L, -3); |
| 3512 | |
| 3513 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3514 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3515 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3516 | return 0; |
| 3517 | lua_settable(L, -3); |
| 3518 | |
| 3519 | /* Create the "c" field that contains a list of converters. */ |
| 3520 | lua_pushstring(L, "c"); |
| 3521 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3522 | return 0; |
| 3523 | lua_settable(L, -3); |
| 3524 | |
| 3525 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3526 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3527 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3528 | return 0; |
| 3529 | lua_settable(L, -3); |
| 3530 | |
| 3531 | /* Pop a class stream metatable and affect it to the table. */ |
| 3532 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3533 | lua_setmetatable(L, -2); |
| 3534 | |
| 3535 | return 1; |
| 3536 | } |
| 3537 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3538 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3539 | { |
| 3540 | struct hlua_appctx *appctx; |
| 3541 | struct stream *s; |
| 3542 | const char *name; |
| 3543 | size_t len; |
| 3544 | struct sample smp; |
| 3545 | |
| 3546 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3547 | |
| 3548 | /* It is useles to retrieve the stream, but this function |
| 3549 | * runs only in a stream context. |
| 3550 | */ |
| 3551 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3552 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3553 | s = appctx->htxn.s; |
| 3554 | |
| 3555 | /* Converts the third argument in a sample. */ |
| 3556 | hlua_lua2smp(L, 3, &smp); |
| 3557 | |
| 3558 | /* Store the sample in a variable. */ |
| 3559 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3560 | vars_set_by_name(name, len, &smp); |
| 3561 | return 0; |
| 3562 | } |
| 3563 | |
| 3564 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3565 | { |
| 3566 | struct hlua_appctx *appctx; |
| 3567 | struct stream *s; |
| 3568 | const char *name; |
| 3569 | size_t len; |
| 3570 | struct sample smp; |
| 3571 | |
| 3572 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3573 | |
| 3574 | /* It is useles to retrieve the stream, but this function |
| 3575 | * runs only in a stream context. |
| 3576 | */ |
| 3577 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3578 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3579 | s = appctx->htxn.s; |
| 3580 | |
| 3581 | /* Unset the variable. */ |
| 3582 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3583 | vars_unset_by_name(name, len, &smp); |
| 3584 | return 0; |
| 3585 | } |
| 3586 | |
| 3587 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3588 | { |
| 3589 | struct hlua_appctx *appctx; |
| 3590 | struct stream *s; |
| 3591 | const char *name; |
| 3592 | size_t len; |
| 3593 | struct sample smp; |
| 3594 | |
| 3595 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3596 | |
| 3597 | /* It is useles to retrieve the stream, but this function |
| 3598 | * runs only in a stream context. |
| 3599 | */ |
| 3600 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3601 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3602 | s = appctx->htxn.s; |
| 3603 | |
| 3604 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3605 | if (!vars_get_by_name(name, len, &smp)) { |
| 3606 | lua_pushnil(L); |
| 3607 | return 1; |
| 3608 | } |
| 3609 | |
| 3610 | return hlua_smp2lua(L, &smp); |
| 3611 | } |
| 3612 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3613 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3614 | { |
| 3615 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3616 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3617 | struct hlua *hlua; |
| 3618 | |
| 3619 | /* 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] | 3620 | if (!s->hlua) |
| 3621 | return 0; |
| 3622 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3623 | |
| 3624 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3625 | |
| 3626 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3627 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3628 | |
| 3629 | /* Get and store new value. */ |
| 3630 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3631 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3632 | |
| 3633 | return 0; |
| 3634 | } |
| 3635 | |
| 3636 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3637 | { |
| 3638 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3639 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3640 | struct hlua *hlua; |
| 3641 | |
| 3642 | /* 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] | 3643 | if (!s->hlua) { |
| 3644 | lua_pushnil(L); |
| 3645 | return 1; |
| 3646 | } |
| 3647 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3648 | |
| 3649 | /* Push configuration index in the stack. */ |
| 3650 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3651 | |
| 3652 | return 1; |
| 3653 | } |
| 3654 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3655 | /* If expected data not yet available, it returns a yield. This function |
| 3656 | * consumes the data in the buffer. It returns a string containing the |
| 3657 | * data. This string can be empty. |
| 3658 | */ |
| 3659 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3660 | { |
| 3661 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3662 | struct stream_interface *si = appctx->appctx->owner; |
| 3663 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3664 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3665 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3666 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3667 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3668 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3669 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3670 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3671 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3672 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3673 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3674 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3675 | 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] | 3676 | } |
| 3677 | |
| 3678 | /* End of data: commit the total strings and return. */ |
| 3679 | if (ret < 0) { |
| 3680 | luaL_pushresult(&appctx->b); |
| 3681 | return 1; |
| 3682 | } |
| 3683 | |
| 3684 | /* Ensure that the block 2 length is usable. */ |
| 3685 | if (ret == 1) |
| 3686 | len2 = 0; |
| 3687 | |
| 3688 | /* dont check the max length read and dont check. */ |
| 3689 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3690 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3691 | |
| 3692 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3693 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3694 | luaL_pushresult(&appctx->b); |
| 3695 | return 1; |
| 3696 | } |
| 3697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3698 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3699 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3700 | { |
| 3701 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3702 | |
| 3703 | /* Initialise the string catenation. */ |
| 3704 | luaL_buffinit(L, &appctx->b); |
| 3705 | |
| 3706 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3707 | } |
| 3708 | |
| 3709 | /* If expected data not yet available, it returns a yield. This function |
| 3710 | * consumes the data in the buffer. It returns a string containing the |
| 3711 | * data. This string can be empty. |
| 3712 | */ |
| 3713 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3714 | { |
| 3715 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3716 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3717 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3718 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3719 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3720 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3721 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3722 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3723 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3724 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3725 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3726 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3727 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3728 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3729 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3730 | 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] | 3731 | } |
| 3732 | |
| 3733 | /* End of data: commit the total strings and return. */ |
| 3734 | if (ret < 0) { |
| 3735 | luaL_pushresult(&appctx->b); |
| 3736 | return 1; |
| 3737 | } |
| 3738 | |
| 3739 | /* Ensure that the block 2 length is usable. */ |
| 3740 | if (ret == 1) |
| 3741 | len2 = 0; |
| 3742 | |
| 3743 | if (len == -1) { |
| 3744 | |
| 3745 | /* If len == -1, catenate all the data avalaile and |
| 3746 | * yield because we want to get all the data until |
| 3747 | * the end of data stream. |
| 3748 | */ |
| 3749 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3750 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3751 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3752 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3753 | 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] | 3754 | |
| 3755 | } else { |
| 3756 | |
| 3757 | /* Copy the fisrt block caping to the length required. */ |
| 3758 | if (len1 > len) |
| 3759 | len1 = len; |
| 3760 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3761 | len -= len1; |
| 3762 | |
| 3763 | /* Copy the second block. */ |
| 3764 | if (len2 > len) |
| 3765 | len2 = len; |
| 3766 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3767 | len -= len2; |
| 3768 | |
| 3769 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3770 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3771 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3772 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3773 | if (len > 0) { |
| 3774 | lua_pushinteger(L, len); |
| 3775 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3776 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3777 | 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] | 3778 | } |
| 3779 | |
| 3780 | /* return the result. */ |
| 3781 | luaL_pushresult(&appctx->b); |
| 3782 | return 1; |
| 3783 | } |
| 3784 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3785 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3786 | hlua_pusherror(L, "Lua: internal error"); |
| 3787 | WILL_LJMP(lua_error(L)); |
| 3788 | return 0; |
| 3789 | } |
| 3790 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3791 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3792 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3793 | { |
| 3794 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3795 | int len = -1; |
| 3796 | |
| 3797 | if (lua_gettop(L) > 2) |
| 3798 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3799 | if (lua_gettop(L) >= 2) { |
| 3800 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3801 | lua_pop(L, 1); |
| 3802 | } |
| 3803 | |
| 3804 | /* Confirm or set the required length */ |
| 3805 | lua_pushinteger(L, len); |
| 3806 | |
| 3807 | /* Initialise the string catenation. */ |
| 3808 | luaL_buffinit(L, &appctx->b); |
| 3809 | |
| 3810 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3811 | } |
| 3812 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3813 | /* Append data in the output side of the buffer. This data is immediately |
| 3814 | * sent. The function returns the amount of data written. If the buffer |
| 3815 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3816 | * if the channel is closed. |
| 3817 | */ |
| 3818 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3819 | { |
| 3820 | size_t len; |
| 3821 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3822 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3823 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3824 | struct stream_interface *si = appctx->appctx->owner; |
| 3825 | struct channel *chn = si_ic(si); |
| 3826 | int max; |
| 3827 | |
| 3828 | /* Get the max amount of data which can write as input in the channel. */ |
| 3829 | max = channel_recv_max(chn); |
| 3830 | if (max > (len - l)) |
| 3831 | max = len - l; |
| 3832 | |
| 3833 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3834 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3835 | |
| 3836 | /* update counters. */ |
| 3837 | l += max; |
| 3838 | lua_pop(L, 1); |
| 3839 | lua_pushinteger(L, l); |
| 3840 | |
| 3841 | /* If some data is not send, declares the situation to the |
| 3842 | * applet, and returns a yield. |
| 3843 | */ |
| 3844 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3845 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3846 | 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] | 3847 | } |
| 3848 | |
| 3849 | return 1; |
| 3850 | } |
| 3851 | |
| 3852 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3853 | * yield the LUA process, and resume it without checking the |
| 3854 | * input arguments. |
| 3855 | */ |
| 3856 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3857 | { |
| 3858 | MAY_LJMP(check_args(L, 2, "send")); |
| 3859 | lua_pushinteger(L, 0); |
| 3860 | |
| 3861 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3862 | } |
| 3863 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3864 | /* |
| 3865 | * |
| 3866 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3867 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3868 | * |
| 3869 | * |
| 3870 | */ |
| 3871 | |
| 3872 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3873 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3875 | __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] | 3876 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3877 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3878 | } |
| 3879 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3880 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3881 | * according with a current TXN. |
| 3882 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3883 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3884 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3885 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3886 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3887 | struct stream_interface *si = ctx->owner; |
| 3888 | struct stream *s = si_strm(si); |
| 3889 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3890 | struct htx *htx; |
| 3891 | struct htx_blk *blk; |
| 3892 | struct htx_sl *sl; |
| 3893 | struct ist path; |
| 3894 | unsigned long long len = 0; |
| 3895 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3896 | |
| 3897 | /* Check stack size. */ |
| 3898 | if (!lua_checkstack(L, 3)) |
| 3899 | return 0; |
| 3900 | |
| 3901 | /* Create the object: obj[0] = userdata. |
| 3902 | * Note that the base of the Converters object is the |
| 3903 | * same than the TXN object. |
| 3904 | */ |
| 3905 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3906 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3907 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3908 | appctx->appctx = ctx; |
| 3909 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3910 | 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] | 3911 | appctx->htxn.s = s; |
| 3912 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3913 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3914 | /* Create the "f" field that contains a list of fetches. */ |
| 3915 | lua_pushstring(L, "f"); |
| 3916 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3917 | return 0; |
| 3918 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3919 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3920 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3921 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3922 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3923 | return 0; |
| 3924 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3925 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3926 | /* Create the "c" field that contains a list of converters. */ |
| 3927 | lua_pushstring(L, "c"); |
| 3928 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3929 | return 0; |
| 3930 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3931 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3932 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3933 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3934 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3935 | return 0; |
| 3936 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3937 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3938 | htx = htxbuf(&s->req.buf); |
| 3939 | blk = htx_get_first_blk(htx); |
| 3940 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 3941 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3942 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3943 | /* Stores the request method. */ |
| 3944 | lua_pushstring(L, "method"); |
| 3945 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3946 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3947 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3948 | /* Stores the http version. */ |
| 3949 | lua_pushstring(L, "version"); |
| 3950 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3951 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3952 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3953 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3954 | * the array on the top of the stack. |
| 3955 | */ |
| 3956 | lua_pushstring(L, "headers"); |
| 3957 | htxn.s = s; |
| 3958 | htxn.p = px; |
| 3959 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3960 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3961 | return 0; |
| 3962 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3963 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3964 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3965 | if (path.ptr) { |
| 3966 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3967 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3968 | p = path.ptr; |
| 3969 | end = path.ptr + path.len; |
| 3970 | q = p; |
| 3971 | while (q < end && *q != '?') |
| 3972 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3973 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3974 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3975 | lua_pushstring(L, "path"); |
| 3976 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3977 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3978 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3979 | /* Stores the query string. */ |
| 3980 | lua_pushstring(L, "qs"); |
| 3981 | if (*q == '?') |
| 3982 | q++; |
| 3983 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3984 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3985 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3986 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3987 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3988 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3989 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3990 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3991 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3992 | break; |
| 3993 | if (type == HTX_BLK_DATA) |
| 3994 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3995 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3996 | if (htx->extra != ULLONG_MAX) |
| 3997 | len += htx->extra; |
| 3998 | |
| 3999 | /* Stores the request path. */ |
| 4000 | lua_pushstring(L, "length"); |
| 4001 | lua_pushinteger(L, len); |
| 4002 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4003 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4004 | /* Create an empty array of HTTP request headers. */ |
| 4005 | lua_pushstring(L, "response"); |
| 4006 | lua_newtable(L); |
| 4007 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4008 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4009 | /* Pop a class stream metatable and affect it to the table. */ |
| 4010 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4011 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4012 | |
| 4013 | return 1; |
| 4014 | } |
| 4015 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4016 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4017 | { |
| 4018 | struct hlua_appctx *appctx; |
| 4019 | struct stream *s; |
| 4020 | const char *name; |
| 4021 | size_t len; |
| 4022 | struct sample smp; |
| 4023 | |
| 4024 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4025 | |
| 4026 | /* It is useles to retrieve the stream, but this function |
| 4027 | * runs only in a stream context. |
| 4028 | */ |
| 4029 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4030 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4031 | s = appctx->htxn.s; |
| 4032 | |
| 4033 | /* Converts the third argument in a sample. */ |
| 4034 | hlua_lua2smp(L, 3, &smp); |
| 4035 | |
| 4036 | /* Store the sample in a variable. */ |
| 4037 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4038 | vars_set_by_name(name, len, &smp); |
| 4039 | return 0; |
| 4040 | } |
| 4041 | |
| 4042 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4043 | { |
| 4044 | struct hlua_appctx *appctx; |
| 4045 | struct stream *s; |
| 4046 | const char *name; |
| 4047 | size_t len; |
| 4048 | struct sample smp; |
| 4049 | |
| 4050 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4051 | |
| 4052 | /* It is useles to retrieve the stream, but this function |
| 4053 | * runs only in a stream context. |
| 4054 | */ |
| 4055 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4056 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4057 | s = appctx->htxn.s; |
| 4058 | |
| 4059 | /* Unset the variable. */ |
| 4060 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4061 | vars_unset_by_name(name, len, &smp); |
| 4062 | return 0; |
| 4063 | } |
| 4064 | |
| 4065 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4066 | { |
| 4067 | struct hlua_appctx *appctx; |
| 4068 | struct stream *s; |
| 4069 | const char *name; |
| 4070 | size_t len; |
| 4071 | struct sample smp; |
| 4072 | |
| 4073 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4074 | |
| 4075 | /* It is useles to retrieve the stream, but this function |
| 4076 | * runs only in a stream context. |
| 4077 | */ |
| 4078 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4079 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4080 | s = appctx->htxn.s; |
| 4081 | |
| 4082 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4083 | if (!vars_get_by_name(name, len, &smp)) { |
| 4084 | lua_pushnil(L); |
| 4085 | return 1; |
| 4086 | } |
| 4087 | |
| 4088 | return hlua_smp2lua(L, &smp); |
| 4089 | } |
| 4090 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4091 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4092 | { |
| 4093 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4094 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4095 | struct hlua *hlua; |
| 4096 | |
| 4097 | /* 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] | 4098 | if (!s->hlua) |
| 4099 | return 0; |
| 4100 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4101 | |
| 4102 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4103 | |
| 4104 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4105 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4106 | |
| 4107 | /* Get and store new value. */ |
| 4108 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4109 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4110 | |
| 4111 | return 0; |
| 4112 | } |
| 4113 | |
| 4114 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4115 | { |
| 4116 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4117 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4118 | struct hlua *hlua; |
| 4119 | |
| 4120 | /* 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] | 4121 | if (!s->hlua) { |
| 4122 | lua_pushnil(L); |
| 4123 | return 1; |
| 4124 | } |
| 4125 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4126 | |
| 4127 | /* Push configuration index in the stack. */ |
| 4128 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4129 | |
| 4130 | return 1; |
| 4131 | } |
| 4132 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4133 | /* If expected data not yet available, it returns a yield. This function |
| 4134 | * consumes the data in the buffer. It returns a string containing the |
| 4135 | * data. This string can be empty. |
| 4136 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4137 | __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] | 4138 | { |
| 4139 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4140 | struct stream_interface *si = appctx->appctx->owner; |
| 4141 | struct channel *req = si_oc(si); |
| 4142 | struct htx *htx; |
| 4143 | struct htx_blk *blk; |
| 4144 | size_t count; |
| 4145 | int stop = 0; |
| 4146 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4147 | htx = htx_from_buf(&req->buf); |
| 4148 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4149 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4150 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4151 | while (count && !stop && blk) { |
| 4152 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4153 | uint32_t sz = htx_get_blksz(blk); |
| 4154 | struct ist v; |
| 4155 | uint32_t vlen; |
| 4156 | char *nl; |
| 4157 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4158 | if (type == HTX_BLK_EOM) { |
| 4159 | stop = 1; |
| 4160 | break; |
| 4161 | } |
| 4162 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4163 | vlen = sz; |
| 4164 | if (vlen > count) { |
| 4165 | if (type != HTX_BLK_DATA) |
| 4166 | break; |
| 4167 | vlen = count; |
| 4168 | } |
| 4169 | |
| 4170 | switch (type) { |
| 4171 | case HTX_BLK_UNUSED: |
| 4172 | break; |
| 4173 | |
| 4174 | case HTX_BLK_DATA: |
| 4175 | v = htx_get_blk_value(htx, blk); |
| 4176 | v.len = vlen; |
| 4177 | nl = istchr(v, '\n'); |
| 4178 | if (nl != NULL) { |
| 4179 | stop = 1; |
| 4180 | vlen = nl - v.ptr + 1; |
| 4181 | } |
| 4182 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4183 | break; |
| 4184 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4185 | case HTX_BLK_TLR: |
| 4186 | case HTX_BLK_EOM: |
| 4187 | stop = 1; |
| 4188 | break; |
| 4189 | |
| 4190 | default: |
| 4191 | break; |
| 4192 | } |
| 4193 | |
| 4194 | co_set_data(req, co_data(req) - vlen); |
| 4195 | count -= vlen; |
| 4196 | if (sz == vlen) |
| 4197 | blk = htx_remove_blk(htx, blk); |
| 4198 | else { |
| 4199 | htx_cut_data_blk(htx, blk, vlen); |
| 4200 | break; |
| 4201 | } |
| 4202 | } |
| 4203 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4204 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4205 | if (!stop) { |
| 4206 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4207 | 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] | 4208 | } |
| 4209 | |
| 4210 | /* return the result. */ |
| 4211 | luaL_pushresult(&appctx->b); |
| 4212 | return 1; |
| 4213 | } |
| 4214 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4215 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4216 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4217 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4218 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4219 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4220 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4221 | /* Initialise the string catenation. */ |
| 4222 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4223 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4224 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4225 | } |
| 4226 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4227 | /* If expected data not yet available, it returns a yield. This function |
| 4228 | * consumes the data in the buffer. It returns a string containing the |
| 4229 | * data. This string can be empty. |
| 4230 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4231 | __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] | 4232 | { |
| 4233 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4234 | struct stream_interface *si = appctx->appctx->owner; |
| 4235 | struct channel *req = si_oc(si); |
| 4236 | struct htx *htx; |
| 4237 | struct htx_blk *blk; |
| 4238 | size_t count; |
| 4239 | int len; |
| 4240 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4241 | htx = htx_from_buf(&req->buf); |
| 4242 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4243 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4244 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4245 | while (count && len && blk) { |
| 4246 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4247 | uint32_t sz = htx_get_blksz(blk); |
| 4248 | struct ist v; |
| 4249 | uint32_t vlen; |
| 4250 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4251 | if (type == HTX_BLK_EOM) { |
| 4252 | len = 0; |
| 4253 | break; |
| 4254 | } |
| 4255 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4256 | vlen = sz; |
| 4257 | if (len > 0 && vlen > len) |
| 4258 | vlen = len; |
| 4259 | if (vlen > count) { |
| 4260 | if (type != HTX_BLK_DATA) |
| 4261 | break; |
| 4262 | vlen = count; |
| 4263 | } |
| 4264 | |
| 4265 | switch (type) { |
| 4266 | case HTX_BLK_UNUSED: |
| 4267 | break; |
| 4268 | |
| 4269 | case HTX_BLK_DATA: |
| 4270 | v = htx_get_blk_value(htx, blk); |
| 4271 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4272 | break; |
| 4273 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4274 | case HTX_BLK_TLR: |
| 4275 | case HTX_BLK_EOM: |
| 4276 | len = 0; |
| 4277 | break; |
| 4278 | |
| 4279 | default: |
| 4280 | break; |
| 4281 | } |
| 4282 | |
| 4283 | co_set_data(req, co_data(req) - vlen); |
| 4284 | count -= vlen; |
| 4285 | if (len > 0) |
| 4286 | len -= vlen; |
| 4287 | if (sz == vlen) |
| 4288 | blk = htx_remove_blk(htx, blk); |
| 4289 | else { |
| 4290 | htx_cut_data_blk(htx, blk, vlen); |
| 4291 | break; |
| 4292 | } |
| 4293 | } |
| 4294 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4295 | htx_to_buf(htx, &req->buf); |
| 4296 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4297 | /* If we are no other data available, yield waiting for new data. */ |
| 4298 | if (len) { |
| 4299 | if (len > 0) { |
| 4300 | lua_pushinteger(L, len); |
| 4301 | lua_replace(L, 2); |
| 4302 | } |
| 4303 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4304 | 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] | 4305 | } |
| 4306 | |
| 4307 | /* return the result. */ |
| 4308 | luaL_pushresult(&appctx->b); |
| 4309 | return 1; |
| 4310 | } |
| 4311 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4312 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4313 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4314 | { |
| 4315 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4316 | int len = -1; |
| 4317 | |
| 4318 | /* Check arguments. */ |
| 4319 | if (lua_gettop(L) > 2) |
| 4320 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4321 | if (lua_gettop(L) >= 2) { |
| 4322 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4323 | lua_pop(L, 1); |
| 4324 | } |
| 4325 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4326 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4327 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4328 | /* Initialise the string catenation. */ |
| 4329 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4330 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4331 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4332 | } |
| 4333 | |
| 4334 | /* Append data in the output side of the buffer. This data is immediately |
| 4335 | * sent. The function returns the amount of data written. If the buffer |
| 4336 | * cannot contain the data, the function yields. The function returns -1 |
| 4337 | * if the channel is closed. |
| 4338 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4339 | __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] | 4340 | { |
| 4341 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4342 | struct stream_interface *si = appctx->appctx->owner; |
| 4343 | struct channel *res = si_ic(si); |
| 4344 | struct htx *htx = htx_from_buf(&res->buf); |
| 4345 | const char *data; |
| 4346 | size_t len; |
| 4347 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4348 | int max; |
| 4349 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4350 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4351 | if (!max) |
| 4352 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4353 | |
| 4354 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4355 | |
| 4356 | /* Get the max amount of data which can write as input in the channel. */ |
| 4357 | if (max > (len - l)) |
| 4358 | max = len - l; |
| 4359 | |
| 4360 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4361 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4362 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4363 | |
| 4364 | /* update counters. */ |
| 4365 | l += max; |
| 4366 | lua_pop(L, 1); |
| 4367 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4368 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4369 | /* If some data is not send, declares the situation to the |
| 4370 | * applet, and returns a yield. |
| 4371 | */ |
| 4372 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4373 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4374 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4375 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4376 | 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] | 4377 | } |
| 4378 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4379 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4380 | return 1; |
| 4381 | } |
| 4382 | |
| 4383 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4384 | * yield the LUA process, and resume it without checking the |
| 4385 | * input arguments. |
| 4386 | */ |
| 4387 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4388 | { |
| 4389 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4390 | |
| 4391 | /* We want to send some data. Headers must be sent. */ |
| 4392 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4393 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4394 | WILL_LJMP(lua_error(L)); |
| 4395 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4396 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4397 | /* This interger is used for followinf the amount of data sent. */ |
| 4398 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4399 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4400 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4401 | } |
| 4402 | |
| 4403 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4404 | { |
| 4405 | const char *name; |
| 4406 | int ret; |
| 4407 | |
| 4408 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4409 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4410 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4411 | |
| 4412 | /* Push in the stack the "response" entry. */ |
| 4413 | ret = lua_getfield(L, 1, "response"); |
| 4414 | if (ret != LUA_TTABLE) { |
| 4415 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4416 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4417 | WILL_LJMP(lua_error(L)); |
| 4418 | } |
| 4419 | |
| 4420 | /* check if the header is already registered if it is not |
| 4421 | * the case, register it. |
| 4422 | */ |
| 4423 | ret = lua_getfield(L, -1, name); |
| 4424 | if (ret == LUA_TNIL) { |
| 4425 | |
| 4426 | /* Entry not found. */ |
| 4427 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4428 | |
| 4429 | /* Insert the new header name in the array in the top of the stack. |
| 4430 | * It left the new array in the top of the stack. |
| 4431 | */ |
| 4432 | lua_newtable(L); |
| 4433 | lua_pushvalue(L, 2); |
| 4434 | lua_pushvalue(L, -2); |
| 4435 | lua_settable(L, -4); |
| 4436 | |
| 4437 | } else if (ret != LUA_TTABLE) { |
| 4438 | |
| 4439 | /* corruption error. */ |
| 4440 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4441 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4442 | WILL_LJMP(lua_error(L)); |
| 4443 | } |
| 4444 | |
| 4445 | /* Now the top od thestack is an array of values. We push |
| 4446 | * the header value as new entry. |
| 4447 | */ |
| 4448 | lua_pushvalue(L, 3); |
| 4449 | ret = lua_rawlen(L, -2); |
| 4450 | lua_rawseti(L, -2, ret + 1); |
| 4451 | lua_pushboolean(L, 1); |
| 4452 | return 1; |
| 4453 | } |
| 4454 | |
| 4455 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4456 | { |
| 4457 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4458 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4459 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4460 | |
| 4461 | if (status < 100 || status > 599) { |
| 4462 | lua_pushboolean(L, 0); |
| 4463 | return 1; |
| 4464 | } |
| 4465 | |
| 4466 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4467 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4468 | lua_pushboolean(L, 1); |
| 4469 | return 1; |
| 4470 | } |
| 4471 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4472 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4473 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4474 | { |
| 4475 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4476 | struct stream_interface *si = appctx->appctx->owner; |
| 4477 | struct channel *res = si_ic(si); |
| 4478 | struct htx *htx; |
| 4479 | struct htx_sl *sl; |
| 4480 | struct h1m h1m; |
| 4481 | const char *status, *reason; |
| 4482 | const char *name, *value; |
| 4483 | size_t nlen, vlen; |
| 4484 | unsigned int flags; |
| 4485 | |
| 4486 | /* Send the message at once. */ |
| 4487 | htx = htx_from_buf(&res->buf); |
| 4488 | h1m_init_res(&h1m); |
| 4489 | |
| 4490 | /* Use the same http version than the request. */ |
| 4491 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4492 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4493 | if (reason == NULL) |
| 4494 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4495 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4496 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4497 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4498 | } |
| 4499 | else { |
| 4500 | flags = HTX_SL_F_IS_RESP; |
| 4501 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4502 | } |
| 4503 | if (!sl) { |
| 4504 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4505 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4506 | WILL_LJMP(lua_error(L)); |
| 4507 | } |
| 4508 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4509 | |
| 4510 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4511 | lua_pushvalue(L, 0); |
| 4512 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4513 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4514 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4515 | WILL_LJMP(lua_error(L)); |
| 4516 | } |
| 4517 | |
| 4518 | /* Browse the list of headers. */ |
| 4519 | lua_pushnil(L); |
| 4520 | while(lua_next(L, -2) != 0) { |
| 4521 | /* We expect a string as -2. */ |
| 4522 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4523 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4524 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4525 | lua_typename(L, lua_type(L, -2))); |
| 4526 | WILL_LJMP(lua_error(L)); |
| 4527 | } |
| 4528 | name = lua_tolstring(L, -2, &nlen); |
| 4529 | |
| 4530 | /* We expect an array as -1. */ |
| 4531 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4532 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4533 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4534 | name, |
| 4535 | lua_typename(L, lua_type(L, -1))); |
| 4536 | WILL_LJMP(lua_error(L)); |
| 4537 | } |
| 4538 | |
| 4539 | /* Browse the table who is on the top of the stack. */ |
| 4540 | lua_pushnil(L); |
| 4541 | while(lua_next(L, -2) != 0) { |
| 4542 | int id; |
| 4543 | |
| 4544 | /* We expect a number as -2. */ |
| 4545 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4546 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4547 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4548 | name, |
| 4549 | lua_typename(L, lua_type(L, -2))); |
| 4550 | WILL_LJMP(lua_error(L)); |
| 4551 | } |
| 4552 | id = lua_tointeger(L, -2); |
| 4553 | |
| 4554 | /* We expect a string as -2. */ |
| 4555 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4556 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4557 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4558 | name, id, |
| 4559 | lua_typename(L, lua_type(L, -1))); |
| 4560 | WILL_LJMP(lua_error(L)); |
| 4561 | } |
| 4562 | value = lua_tolstring(L, -1, &vlen); |
| 4563 | |
| 4564 | /* Simple Protocol checks. */ |
| 4565 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4566 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4567 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4568 | struct ist v = ist2(value, vlen); |
| 4569 | int ret; |
| 4570 | |
| 4571 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4572 | if (ret < 0) { |
| 4573 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4574 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4575 | name); |
| 4576 | WILL_LJMP(lua_error(L)); |
| 4577 | } |
| 4578 | else if (ret == 0) |
| 4579 | goto next; /* Skip it */ |
| 4580 | } |
| 4581 | |
| 4582 | /* Add a new header */ |
| 4583 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4584 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4585 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4586 | name); |
| 4587 | WILL_LJMP(lua_error(L)); |
| 4588 | } |
| 4589 | next: |
| 4590 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4591 | lua_pop(L, 1); |
| 4592 | } |
| 4593 | |
| 4594 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4595 | lua_pop(L, 1); |
| 4596 | } |
| 4597 | |
| 4598 | if (h1m.flags & H1_MF_CHNK) |
| 4599 | h1m.flags &= ~H1_MF_CLEN; |
| 4600 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4601 | h1m.flags |= H1_MF_XFER_LEN; |
| 4602 | |
| 4603 | /* Uset HTX start-line flags */ |
| 4604 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4605 | flags |= HTX_SL_F_XFER_ENC; |
| 4606 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4607 | flags |= HTX_SL_F_XFER_LEN; |
| 4608 | if (h1m.flags & H1_MF_CHNK) |
| 4609 | flags |= HTX_SL_F_CHNK; |
| 4610 | else if (h1m.flags & H1_MF_CLEN) |
| 4611 | flags |= HTX_SL_F_CLEN; |
| 4612 | if (h1m.body_len == 0) |
| 4613 | flags |= HTX_SL_F_BODYLESS; |
| 4614 | } |
| 4615 | sl->flags |= flags; |
| 4616 | |
| 4617 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4618 | * and the status code implies the presence of a message body, we must |
| 4619 | * announce a transfer encoding chunked. This is required by haproxy |
| 4620 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4621 | * chunked itslef, don't do anything. |
| 4622 | */ |
| 4623 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4624 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4625 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4626 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4627 | /* Add a new header */ |
| 4628 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4629 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4630 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4631 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4632 | WILL_LJMP(lua_error(L)); |
| 4633 | } |
| 4634 | } |
| 4635 | |
| 4636 | /* Finalize headers. */ |
| 4637 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4638 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4639 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4640 | WILL_LJMP(lua_error(L)); |
| 4641 | } |
| 4642 | |
| 4643 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4644 | b_reset(&res->buf); |
| 4645 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4646 | WILL_LJMP(lua_error(L)); |
| 4647 | } |
| 4648 | |
| 4649 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4650 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4651 | |
| 4652 | /* Headers sent, set the flag. */ |
| 4653 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4654 | return 0; |
| 4655 | |
| 4656 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4657 | /* We will build the status line and the headers of the HTTP response. |
| 4658 | * We will try send at once if its not possible, we give back the hand |
| 4659 | * waiting for more room. |
| 4660 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4661 | __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] | 4662 | { |
| 4663 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4664 | struct stream_interface *si = appctx->appctx->owner; |
| 4665 | struct channel *res = si_ic(si); |
| 4666 | |
| 4667 | if (co_data(res)) { |
| 4668 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4669 | 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] | 4670 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4671 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4672 | } |
| 4673 | |
| 4674 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4675 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4676 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | } |
| 4679 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4680 | /* |
| 4681 | * |
| 4682 | * |
| 4683 | * Class HTTP |
| 4684 | * |
| 4685 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4686 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4687 | |
| 4688 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4689 | * a class stream, otherwise it throws an error. |
| 4690 | */ |
| 4691 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4692 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4693 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4694 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4695 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4696 | /* This function creates and push in the stack a HTTP object |
| 4697 | * according with a current TXN. |
| 4698 | */ |
| 4699 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4700 | { |
| 4701 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4702 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4703 | /* Check stack size. */ |
| 4704 | if (!lua_checkstack(L, 3)) |
| 4705 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4706 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4707 | /* Create the object: obj[0] = userdata. |
| 4708 | * Note that the base of the Converters object is the |
| 4709 | * same than the TXN object. |
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 | lua_newtable(L); |
| 4712 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4713 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4714 | |
| 4715 | htxn->s = txn->s; |
| 4716 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4717 | htxn->dir = txn->dir; |
| 4718 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4719 | |
| 4720 | /* Pop a class stream metatable and affect it to the table. */ |
| 4721 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4722 | lua_setmetatable(L, -2); |
| 4723 | |
| 4724 | return 1; |
| 4725 | } |
| 4726 | |
| 4727 | /* This function creates ans returns an array of HTTP headers. |
| 4728 | * This function does not fails. It is used as wrapper with the |
| 4729 | * 2 following functions. |
| 4730 | */ |
| 4731 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4732 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4733 | struct htx *htx; |
| 4734 | int32_t pos; |
| 4735 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4736 | /* Create the table. */ |
| 4737 | lua_newtable(L); |
| 4738 | |
| 4739 | if (!htxn->s->txn) |
| 4740 | return 1; |
| 4741 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4742 | htx = htxbuf(&msg->chn->buf); |
| 4743 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4744 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4745 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4746 | struct ist n, v; |
| 4747 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4748 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4749 | if (type == HTX_BLK_HDR) { |
| 4750 | n = htx_get_blk_name(htx,blk); |
| 4751 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4752 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4753 | else if (type == HTX_BLK_EOH) |
| 4754 | break; |
| 4755 | else |
| 4756 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4757 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4758 | /* Check for existing entry: |
| 4759 | * assume that the table is on the top of the stack, and |
| 4760 | * push the key in the stack, the function lua_gettable() |
| 4761 | * perform the lookup. |
| 4762 | */ |
| 4763 | lua_pushlstring(L, n.ptr, n.len); |
| 4764 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4765 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4766 | switch (lua_type(L, -1)) { |
| 4767 | case LUA_TNIL: |
| 4768 | /* Table not found, create it. */ |
| 4769 | lua_pop(L, 1); /* remove the nil value. */ |
| 4770 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4771 | lua_newtable(L); /* create and push empty table. */ |
| 4772 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4773 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4774 | 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] | 4775 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4776 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4777 | case LUA_TTABLE: |
| 4778 | /* Entry found: push the value in the table. */ |
| 4779 | len = lua_rawlen(L, -1); |
| 4780 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4781 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4782 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4783 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4785 | default: |
| 4786 | /* Other cases are errors. */ |
| 4787 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4788 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4789 | } |
| 4790 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | return 1; |
| 4792 | } |
| 4793 | |
| 4794 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4795 | { |
| 4796 | struct hlua_txn *htxn; |
| 4797 | |
| 4798 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4799 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4800 | |
| 4801 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4802 | } |
| 4803 | |
| 4804 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4805 | { |
| 4806 | struct hlua_txn *htxn; |
| 4807 | |
| 4808 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4809 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4810 | |
| 4811 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4812 | } |
| 4813 | |
| 4814 | /* This function replace full header, or just a value in |
| 4815 | * the request or in the response. It is a wrapper fir the |
| 4816 | * 4 following functions. |
| 4817 | */ |
| 4818 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4819 | struct http_msg *msg, int action) |
| 4820 | { |
| 4821 | size_t name_len; |
| 4822 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4823 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4824 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4825 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4826 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4827 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4828 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4829 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4830 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4831 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4832 | http_transform_header_str(htxn->s, msg->chn, htx, ist2(name, name_len), value, re, action); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4833 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4834 | return 0; |
| 4835 | } |
| 4836 | |
| 4837 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4838 | { |
| 4839 | struct hlua_txn *htxn; |
| 4840 | |
| 4841 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4842 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4843 | |
| 4844 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4845 | } |
| 4846 | |
| 4847 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4848 | { |
| 4849 | struct hlua_txn *htxn; |
| 4850 | |
| 4851 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4852 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4853 | |
| 4854 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4855 | } |
| 4856 | |
| 4857 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4858 | { |
| 4859 | struct hlua_txn *htxn; |
| 4860 | |
| 4861 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4862 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4863 | |
| 4864 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4865 | } |
| 4866 | |
| 4867 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4868 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4869 | struct hlua_txn *htxn; |
| 4870 | |
| 4871 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4872 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4873 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4874 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_VAL)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4875 | } |
| 4876 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4877 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4878 | * It is a wrapper for the 2 following functions. |
| 4879 | */ |
| 4880 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4881 | { |
| 4882 | size_t len; |
| 4883 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4884 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4885 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4886 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4887 | ctx.blk = NULL; |
| 4888 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4889 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4890 | return 0; |
| 4891 | } |
| 4892 | |
| 4893 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4894 | { |
| 4895 | struct hlua_txn *htxn; |
| 4896 | |
| 4897 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4898 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4899 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4900 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4901 | } |
| 4902 | |
| 4903 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4904 | { |
| 4905 | struct hlua_txn *htxn; |
| 4906 | |
| 4907 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4908 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4909 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4910 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4911 | } |
| 4912 | |
| 4913 | /* This function adds an header. It is a wrapper used by |
| 4914 | * the 2 following functions. |
| 4915 | */ |
| 4916 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4917 | { |
| 4918 | size_t name_len; |
| 4919 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4920 | size_t value_len; |
| 4921 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4923 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4924 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4925 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4926 | return 0; |
| 4927 | } |
| 4928 | |
| 4929 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4930 | { |
| 4931 | struct hlua_txn *htxn; |
| 4932 | |
| 4933 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4934 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4935 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4936 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4937 | } |
| 4938 | |
| 4939 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4940 | { |
| 4941 | struct hlua_txn *htxn; |
| 4942 | |
| 4943 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4944 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4945 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4946 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4947 | } |
| 4948 | |
| 4949 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4950 | { |
| 4951 | struct hlua_txn *htxn; |
| 4952 | |
| 4953 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4954 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4955 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4956 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4957 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4958 | } |
| 4959 | |
| 4960 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4961 | { |
| 4962 | struct hlua_txn *htxn; |
| 4963 | |
| 4964 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4965 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4966 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4967 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4968 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4969 | } |
| 4970 | |
| 4971 | /* This function set the method. */ |
| 4972 | static int hlua_http_req_set_meth(lua_State *L) |
| 4973 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4974 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4975 | size_t name_len; |
| 4976 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4977 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4978 | 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] | 4979 | return 1; |
| 4980 | } |
| 4981 | |
| 4982 | /* This function set the method. */ |
| 4983 | static int hlua_http_req_set_path(lua_State *L) |
| 4984 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4985 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4986 | size_t name_len; |
| 4987 | 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] | 4988 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4989 | 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] | 4990 | return 1; |
| 4991 | } |
| 4992 | |
| 4993 | /* This function set the query-string. */ |
| 4994 | static int hlua_http_req_set_query(lua_State *L) |
| 4995 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4996 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4997 | size_t name_len; |
| 4998 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4999 | |
| 5000 | /* Check length. */ |
| 5001 | if (name_len > trash.size - 1) { |
| 5002 | lua_pushboolean(L, 0); |
| 5003 | return 1; |
| 5004 | } |
| 5005 | |
| 5006 | /* Add the mark question as prefix. */ |
| 5007 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5008 | trash.area[trash.data++] = '?'; |
| 5009 | memcpy(trash.area + trash.data, name, name_len); |
| 5010 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5011 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5012 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5013 | 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] | 5014 | return 1; |
| 5015 | } |
| 5016 | |
| 5017 | /* This function set the uri. */ |
| 5018 | static int hlua_http_req_set_uri(lua_State *L) |
| 5019 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5020 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5021 | size_t name_len; |
| 5022 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5023 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5024 | 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] | 5025 | return 1; |
| 5026 | } |
| 5027 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5028 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5029 | static int hlua_http_res_set_status(lua_State *L) |
| 5030 | { |
| 5031 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5032 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5033 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5034 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5035 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5036 | return 0; |
| 5037 | } |
| 5038 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5039 | /* |
| 5040 | * |
| 5041 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5042 | * Class TXN |
| 5043 | * |
| 5044 | * |
| 5045 | */ |
| 5046 | |
| 5047 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5048 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5049 | */ |
| 5050 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5051 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5052 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5053 | } |
| 5054 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5055 | __LJMP static int hlua_set_var(lua_State *L) |
| 5056 | { |
| 5057 | struct hlua_txn *htxn; |
| 5058 | const char *name; |
| 5059 | size_t len; |
| 5060 | struct sample smp; |
| 5061 | |
| 5062 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5063 | |
| 5064 | /* It is useles to retrieve the stream, but this function |
| 5065 | * runs only in a stream context. |
| 5066 | */ |
| 5067 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5068 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5069 | |
| 5070 | /* Converts the third argument in a sample. */ |
| 5071 | hlua_lua2smp(L, 3, &smp); |
| 5072 | |
| 5073 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5074 | 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] | 5075 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5076 | return 0; |
| 5077 | } |
| 5078 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5079 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5080 | { |
| 5081 | struct hlua_txn *htxn; |
| 5082 | const char *name; |
| 5083 | size_t len; |
| 5084 | struct sample smp; |
| 5085 | |
| 5086 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5087 | |
| 5088 | /* It is useles to retrieve the stream, but this function |
| 5089 | * runs only in a stream context. |
| 5090 | */ |
| 5091 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5092 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5093 | |
| 5094 | /* Unset the variable. */ |
| 5095 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5096 | vars_unset_by_name(name, len, &smp); |
| 5097 | return 0; |
| 5098 | } |
| 5099 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5100 | __LJMP static int hlua_get_var(lua_State *L) |
| 5101 | { |
| 5102 | struct hlua_txn *htxn; |
| 5103 | const char *name; |
| 5104 | size_t len; |
| 5105 | struct sample smp; |
| 5106 | |
| 5107 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5108 | |
| 5109 | /* It is useles to retrieve the stream, but this function |
| 5110 | * runs only in a stream context. |
| 5111 | */ |
| 5112 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5113 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5114 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5115 | 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] | 5116 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5117 | lua_pushnil(L); |
| 5118 | return 1; |
| 5119 | } |
| 5120 | |
| 5121 | return hlua_smp2lua(L, &smp); |
| 5122 | } |
| 5123 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5124 | __LJMP static int hlua_set_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, 2, "set_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 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5137 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5138 | |
| 5139 | /* Get and store new value. */ |
| 5140 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5141 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5142 | |
| 5143 | return 0; |
| 5144 | } |
| 5145 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5146 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5147 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5148 | struct hlua *hlua; |
| 5149 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5150 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5152 | /* It is useles to retrieve the stream, but this function |
| 5153 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5154 | */ |
| 5155 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5156 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5157 | |
| 5158 | /* Push configuration index in the stack. */ |
| 5159 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5160 | |
| 5161 | return 1; |
| 5162 | } |
| 5163 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5164 | /* Create stack entry containing a class TXN. This function |
| 5165 | * return 0 if the stack does not contains free slots, |
| 5166 | * otherwise it returns 1. |
| 5167 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5168 | 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] | 5169 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5170 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5171 | |
| 5172 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5173 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5174 | return 0; |
| 5175 | |
| 5176 | /* NOTE: The allocation never fails. The failure |
| 5177 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5178 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5179 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5180 | /* Create the object: obj[0] = userdata. */ |
| 5181 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5182 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5183 | lua_rawseti(L, -2, 0); |
| 5184 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5185 | htxn->s = s; |
| 5186 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5187 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5188 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5189 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5190 | /* Create the "f" field that contains a list of fetches. */ |
| 5191 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5192 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5193 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5194 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5195 | |
| 5196 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5197 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5198 | 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] | 5199 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5200 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5201 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5202 | /* Create the "c" field that contains a list of converters. */ |
| 5203 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5204 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5205 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5206 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5207 | |
| 5208 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5209 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5210 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5211 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5212 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5213 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5214 | /* Create the "req" field that contains the request channel object. */ |
| 5215 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5216 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5217 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5218 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5219 | |
| 5220 | /* Create the "res" field that contains the response channel object. */ |
| 5221 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5222 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5223 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5224 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5225 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5226 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5227 | lua_pushstring(L, "http"); |
| 5228 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5229 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5230 | return 0; |
| 5231 | } |
| 5232 | else |
| 5233 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5234 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5235 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5236 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5237 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5238 | lua_setmetatable(L, -2); |
| 5239 | |
| 5240 | return 1; |
| 5241 | } |
| 5242 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5243 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5244 | { |
| 5245 | const char *msg; |
| 5246 | struct hlua_txn *htxn; |
| 5247 | |
| 5248 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5249 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5250 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5251 | |
| 5252 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5253 | return 0; |
| 5254 | } |
| 5255 | |
| 5256 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5257 | { |
| 5258 | int level; |
| 5259 | const char *msg; |
| 5260 | struct hlua_txn *htxn; |
| 5261 | |
| 5262 | MAY_LJMP(check_args(L, 3, "log")); |
| 5263 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5264 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5265 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5266 | |
| 5267 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5268 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5269 | |
| 5270 | hlua_sendlog(htxn->s->be, level, msg); |
| 5271 | return 0; |
| 5272 | } |
| 5273 | |
| 5274 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5275 | { |
| 5276 | const char *msg; |
| 5277 | struct hlua_txn *htxn; |
| 5278 | |
| 5279 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5280 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5281 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5282 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5283 | return 0; |
| 5284 | } |
| 5285 | |
| 5286 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5287 | { |
| 5288 | const char *msg; |
| 5289 | struct hlua_txn *htxn; |
| 5290 | |
| 5291 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5292 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5293 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5294 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5295 | return 0; |
| 5296 | } |
| 5297 | |
| 5298 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5299 | { |
| 5300 | const char *msg; |
| 5301 | struct hlua_txn *htxn; |
| 5302 | |
| 5303 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5304 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5305 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5306 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5307 | return 0; |
| 5308 | } |
| 5309 | |
| 5310 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5311 | { |
| 5312 | const char *msg; |
| 5313 | struct hlua_txn *htxn; |
| 5314 | |
| 5315 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5316 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5317 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5318 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5319 | return 0; |
| 5320 | } |
| 5321 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5322 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5323 | { |
| 5324 | struct hlua_txn *htxn; |
| 5325 | int ll; |
| 5326 | |
| 5327 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5328 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5329 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5330 | |
| 5331 | if (ll < 0 || ll > 7) |
| 5332 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5333 | |
| 5334 | htxn->s->logs.level = ll; |
| 5335 | return 0; |
| 5336 | } |
| 5337 | |
| 5338 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5339 | { |
| 5340 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5341 | int tos; |
| 5342 | |
| 5343 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5344 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5345 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5346 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5347 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5348 | return 0; |
| 5349 | } |
| 5350 | |
| 5351 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5352 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5353 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5354 | int mark; |
| 5355 | |
| 5356 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5357 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5358 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5359 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5360 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5361 | return 0; |
| 5362 | } |
| 5363 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5364 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5365 | { |
| 5366 | struct hlua_txn *htxn; |
| 5367 | |
| 5368 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5369 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5370 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5371 | return 0; |
| 5372 | } |
| 5373 | |
| 5374 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5375 | { |
| 5376 | struct hlua_txn *htxn; |
| 5377 | |
| 5378 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5379 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5380 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5381 | return 0; |
| 5382 | } |
| 5383 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5384 | /* This function is an Lua binding that send pending data |
| 5385 | * to the client, and close the stream interface. |
| 5386 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5387 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5388 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5389 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5390 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5391 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5392 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5393 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5394 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5395 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5396 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5397 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5398 | * session, so we just end the execution of the current |
| 5399 | * lua code. |
| 5400 | */ |
| 5401 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5402 | WILL_LJMP(hlua_done(L)); |
| 5403 | return 0; |
| 5404 | } |
| 5405 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5406 | ic = &htxn->s->req; |
| 5407 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5408 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5409 | if (IS_HTX_STRM(htxn->s)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5410 | http_reply_and_close(htxn->s, 0, NULL); |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5411 | else { |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5412 | channel_auto_read(ic); |
| 5413 | channel_abort(ic); |
| 5414 | channel_auto_close(ic); |
| 5415 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5416 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5417 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 5418 | channel_auto_read(oc); |
| 5419 | channel_auto_close(oc); |
| 5420 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5421 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5422 | ic->analysers = 0; |
| 5423 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5424 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5425 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5426 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5427 | return 0; |
| 5428 | } |
| 5429 | |
| 5430 | __LJMP static int hlua_log(lua_State *L) |
| 5431 | { |
| 5432 | int level; |
| 5433 | const char *msg; |
| 5434 | |
| 5435 | MAY_LJMP(check_args(L, 2, "log")); |
| 5436 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5437 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5438 | |
| 5439 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5440 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5441 | |
| 5442 | hlua_sendlog(NULL, level, msg); |
| 5443 | return 0; |
| 5444 | } |
| 5445 | |
| 5446 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5447 | { |
| 5448 | const char *msg; |
| 5449 | |
| 5450 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5451 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5452 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5453 | return 0; |
| 5454 | } |
| 5455 | |
| 5456 | __LJMP static int hlua_log_info(lua_State *L) |
| 5457 | { |
| 5458 | const char *msg; |
| 5459 | |
| 5460 | MAY_LJMP(check_args(L, 1, "info")); |
| 5461 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5462 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5463 | return 0; |
| 5464 | } |
| 5465 | |
| 5466 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5467 | { |
| 5468 | const char *msg; |
| 5469 | |
| 5470 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5471 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5472 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5473 | return 0; |
| 5474 | } |
| 5475 | |
| 5476 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5477 | { |
| 5478 | const char *msg; |
| 5479 | |
| 5480 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5481 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5482 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5483 | return 0; |
| 5484 | } |
| 5485 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5486 | __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] | 5487 | { |
| 5488 | int wakeup_ms = lua_tointeger(L, -1); |
| 5489 | if (now_ms < wakeup_ms) |
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 | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5491 | return 0; |
| 5492 | } |
| 5493 | |
| 5494 | __LJMP static int hlua_sleep(lua_State *L) |
| 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, "sleep")); |
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)) * 1000; |
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 | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5509 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5510 | { |
| 5511 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5512 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5513 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5514 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5515 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5516 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5517 | wakeup_ms = tick_add(now_ms, delay); |
| 5518 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5519 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5520 | 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] | 5521 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5522 | } |
| 5523 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5524 | /* This functionis an LUA binding. it permits to give back |
| 5525 | * the hand at the HAProxy scheduler. It is used when the |
| 5526 | * LUA processing consumes a lot of time. |
| 5527 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5528 | __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] | 5529 | { |
| 5530 | return 0; |
| 5531 | } |
| 5532 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5533 | __LJMP static int hlua_yield(lua_State *L) |
| 5534 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5535 | 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] | 5536 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5537 | } |
| 5538 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5539 | /* This function change the nice of the currently executed |
| 5540 | * task. It is used set low or high priority at the current |
| 5541 | * task. |
| 5542 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5543 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5544 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5545 | struct hlua *hlua; |
| 5546 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5547 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5548 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5549 | hlua = hlua_gethlua(L); |
| 5550 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5551 | |
| 5552 | /* If he task is not set, I'm in a start mode. */ |
| 5553 | if (!hlua || !hlua->task) |
| 5554 | return 0; |
| 5555 | |
| 5556 | if (nice < -1024) |
| 5557 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5558 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5559 | nice = 1024; |
| 5560 | |
| 5561 | hlua->task->nice = nice; |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5565 | /* 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] | 5566 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5567 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5568 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5569 | * |
| 5570 | * Task wrapper are longjmp safe because the only one Lua code |
| 5571 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5572 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5573 | static 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] | 5574 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5575 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5576 | enum hlua_exec status; |
| 5577 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5578 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5579 | task_set_affinity(task, tid_bit); |
| 5580 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5581 | /* If it is the first call to the task, we must initialize the |
| 5582 | * execution timeouts. |
| 5583 | */ |
| 5584 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5585 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5586 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5587 | /* Execute the Lua code. */ |
| 5588 | status = hlua_ctx_resume(hlua, 1); |
| 5589 | |
| 5590 | switch (status) { |
| 5591 | /* finished or yield */ |
| 5592 | case HLUA_E_OK: |
| 5593 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5594 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5595 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5596 | break; |
| 5597 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5598 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5599 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5600 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5601 | break; |
| 5602 | |
| 5603 | /* finished with error. */ |
| 5604 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5605 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5606 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5607 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5608 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5609 | break; |
| 5610 | |
| 5611 | case HLUA_E_ERR: |
| 5612 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5613 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5614 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5615 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5616 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5617 | break; |
| 5618 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5619 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5620 | } |
| 5621 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5622 | /* This function is an LUA binding that register LUA function to be |
| 5623 | * executed after the HAProxy configuration parsing and before the |
| 5624 | * HAProxy scheduler starts. This function expect only one LUA |
| 5625 | * argument that is a function. This function returns nothing, but |
| 5626 | * throws if an error is encountered. |
| 5627 | */ |
| 5628 | __LJMP static int hlua_register_init(lua_State *L) |
| 5629 | { |
| 5630 | struct hlua_init_function *init; |
| 5631 | int ref; |
| 5632 | |
| 5633 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5634 | |
| 5635 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5636 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5637 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5638 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5639 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5640 | |
| 5641 | init->function_ref = ref; |
| 5642 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5643 | return 0; |
| 5644 | } |
| 5645 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5646 | /* This functio is an LUA binding. It permits to register a task |
| 5647 | * executed in parallel of the main HAroxy activity. The task is |
| 5648 | * created and it is set in the HAProxy scheduler. It can be called |
| 5649 | * from the "init" section, "post init" or during the runtime. |
| 5650 | * |
| 5651 | * Lua prototype: |
| 5652 | * |
| 5653 | * <none> core.register_task(<function>) |
| 5654 | */ |
| 5655 | static int hlua_register_task(lua_State *L) |
| 5656 | { |
| 5657 | struct hlua *hlua; |
| 5658 | struct task *task; |
| 5659 | int ref; |
| 5660 | |
| 5661 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5662 | |
| 5663 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5664 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5665 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5666 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5667 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5668 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5669 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5670 | if (!task) |
| 5671 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5672 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5673 | task->context = hlua; |
| 5674 | task->process = hlua_process_task; |
| 5675 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5676 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5677 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5678 | |
| 5679 | /* Restore the function in the stack. */ |
| 5680 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5681 | hlua->nargs = 0; |
| 5682 | |
| 5683 | /* Schedule task. */ |
| 5684 | task_schedule(task, now_ms); |
| 5685 | |
| 5686 | return 0; |
| 5687 | } |
| 5688 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5689 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5690 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5691 | * resume converters. |
| 5692 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5693 | 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] | 5694 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5695 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5696 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5697 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5698 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5699 | if (!stream) |
| 5700 | return 0; |
| 5701 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5702 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5703 | * Lua context can be not initialized. This behavior |
| 5704 | * permits to save performances because a systematic |
| 5705 | * Lua initialization cause 5% performances loss. |
| 5706 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5707 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5708 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5709 | if (!stream->hlua) { |
| 5710 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5711 | return 0; |
| 5712 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5713 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5714 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5715 | return 0; |
| 5716 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5717 | } |
| 5718 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5719 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5720 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5721 | |
| 5722 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5723 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5724 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5725 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5726 | else |
| 5727 | error = "critical error"; |
| 5728 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5729 | return 0; |
| 5730 | } |
| 5731 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5732 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5733 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5734 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5735 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5736 | return 0; |
| 5737 | } |
| 5738 | |
| 5739 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5740 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5741 | |
| 5742 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5743 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5744 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5745 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5746 | return 0; |
| 5747 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5748 | hlua_smp2lua(stream->hlua->T, smp); |
| 5749 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5750 | |
| 5751 | /* push keywords in the stack. */ |
| 5752 | if (arg_p) { |
| 5753 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5754 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5755 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5756 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5757 | return 0; |
| 5758 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5759 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5760 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5761 | } |
| 5762 | } |
| 5763 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5764 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5765 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5766 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5767 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5768 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5769 | } |
| 5770 | |
| 5771 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5772 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5773 | /* finished. */ |
| 5774 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5775 | /* If the stack is empty, the function fails. */ |
| 5776 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5777 | return 0; |
| 5778 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5779 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5780 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5781 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5782 | return 1; |
| 5783 | |
| 5784 | /* yield. */ |
| 5785 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5786 | 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] | 5787 | return 0; |
| 5788 | |
| 5789 | /* finished with error. */ |
| 5790 | case HLUA_E_ERRMSG: |
| 5791 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5792 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5793 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5794 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5795 | return 0; |
| 5796 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5797 | case HLUA_E_ETMOUT: |
| 5798 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5799 | return 0; |
| 5800 | |
| 5801 | case HLUA_E_NOMEM: |
| 5802 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5803 | return 0; |
| 5804 | |
| 5805 | case HLUA_E_YIELD: |
| 5806 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5807 | return 0; |
| 5808 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5809 | case HLUA_E_ERR: |
| 5810 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5811 | 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] | 5812 | |
| 5813 | default: |
| 5814 | return 0; |
| 5815 | } |
| 5816 | } |
| 5817 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5818 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5819 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5820 | * resume sample-fetches. This function will be called by the sample |
| 5821 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5822 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5823 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5824 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5825 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5826 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5827 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5828 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5829 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5830 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5831 | if (!stream) |
| 5832 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5833 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5834 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5835 | * Lua context can be not initialized. This behavior |
| 5836 | * permits to save performances because a systematic |
| 5837 | * Lua initialization cause 5% performances loss. |
| 5838 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5839 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5840 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5841 | if (!stream->hlua) { |
| 5842 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5843 | return 0; |
| 5844 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5845 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5846 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5847 | return 0; |
| 5848 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5849 | } |
| 5850 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5851 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5852 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5853 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5854 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5855 | |
| 5856 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5857 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5858 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5859 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5860 | else |
| 5861 | error = "critical error"; |
| 5862 | 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] | 5863 | return 0; |
| 5864 | } |
| 5865 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5866 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5867 | if (!lua_checkstack(stream->hlua->T, 2)) { |
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 | } |
| 5872 | |
| 5873 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5874 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5875 | |
| 5876 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5877 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5878 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5879 | 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] | 5880 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5881 | return 0; |
| 5882 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5883 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5884 | |
| 5885 | /* push keywords in the stack. */ |
| 5886 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5887 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5888 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5889 | 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] | 5890 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5891 | return 0; |
| 5892 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5893 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5894 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5895 | } |
| 5896 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5897 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5898 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5899 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5900 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5901 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5902 | } |
| 5903 | |
| 5904 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5905 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5906 | /* finished. */ |
| 5907 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5908 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5909 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5910 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5911 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5912 | /* If the stack is empty, the function fails. */ |
| 5913 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5914 | return 0; |
| 5915 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5916 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5917 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5918 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5919 | |
| 5920 | /* Set the end of execution flag. */ |
| 5921 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5922 | return 1; |
| 5923 | |
| 5924 | /* yield. */ |
| 5925 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5926 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5927 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5928 | 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] | 5929 | return 0; |
| 5930 | |
| 5931 | /* finished with error. */ |
| 5932 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5933 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5934 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5935 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5936 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5937 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5938 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5939 | return 0; |
| 5940 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5941 | case HLUA_E_ETMOUT: |
| 5942 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5943 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5944 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5945 | return 0; |
| 5946 | |
| 5947 | case HLUA_E_NOMEM: |
| 5948 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5949 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5950 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5951 | return 0; |
| 5952 | |
| 5953 | case HLUA_E_YIELD: |
| 5954 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5955 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5956 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5957 | return 0; |
| 5958 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5959 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5960 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5961 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5962 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5963 | 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] | 5964 | |
| 5965 | default: |
| 5966 | return 0; |
| 5967 | } |
| 5968 | } |
| 5969 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5970 | /* This function is an LUA binding used for registering |
| 5971 | * "sample-conv" functions. It expects a converter name used |
| 5972 | * in the haproxy configuration file, and an LUA function. |
| 5973 | */ |
| 5974 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5975 | { |
| 5976 | struct sample_conv_kw_list *sck; |
| 5977 | const char *name; |
| 5978 | int ref; |
| 5979 | int len; |
| 5980 | struct hlua_function *fcn; |
| 5981 | |
| 5982 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5983 | |
| 5984 | /* First argument : converter name. */ |
| 5985 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5986 | |
| 5987 | /* Second argument : lua function. */ |
| 5988 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5989 | |
| 5990 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5991 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5992 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5993 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5994 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5995 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5996 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5997 | |
| 5998 | /* Fill fcn. */ |
| 5999 | fcn->name = strdup(name); |
| 6000 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6001 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6002 | fcn->function_ref = ref; |
| 6003 | |
| 6004 | /* List head */ |
| 6005 | sck->list.n = sck->list.p = NULL; |
| 6006 | |
| 6007 | /* converter keyword. */ |
| 6008 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6009 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6010 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6011 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6012 | |
| 6013 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6014 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6015 | 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] | 6016 | sck->kw[0].val_args = NULL; |
| 6017 | sck->kw[0].in_type = SMP_T_STR; |
| 6018 | sck->kw[0].out_type = SMP_T_STR; |
| 6019 | sck->kw[0].private = fcn; |
| 6020 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6021 | /* Register this new converter */ |
| 6022 | sample_register_convs(sck); |
| 6023 | |
| 6024 | return 0; |
| 6025 | } |
| 6026 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6027 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6028 | * "sample-fetch" functions. It expects a converter name used |
| 6029 | * in the haproxy configuration file, and an LUA function. |
| 6030 | */ |
| 6031 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6032 | { |
| 6033 | const char *name; |
| 6034 | int ref; |
| 6035 | int len; |
| 6036 | struct sample_fetch_kw_list *sfk; |
| 6037 | struct hlua_function *fcn; |
| 6038 | |
| 6039 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6040 | |
| 6041 | /* First argument : sample-fetch name. */ |
| 6042 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6043 | |
| 6044 | /* Second argument : lua function. */ |
| 6045 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6046 | |
| 6047 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6048 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6049 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6050 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6051 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6052 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6053 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6054 | |
| 6055 | /* Fill fcn. */ |
| 6056 | fcn->name = strdup(name); |
| 6057 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6058 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6059 | fcn->function_ref = ref; |
| 6060 | |
| 6061 | /* List head */ |
| 6062 | sfk->list.n = sfk->list.p = NULL; |
| 6063 | |
| 6064 | /* sample-fetch keyword. */ |
| 6065 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6066 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6067 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6068 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6069 | |
| 6070 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6071 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6072 | 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] | 6073 | sfk->kw[0].val_args = NULL; |
| 6074 | sfk->kw[0].out_type = SMP_T_STR; |
| 6075 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6076 | sfk->kw[0].val = 0; |
| 6077 | sfk->kw[0].private = fcn; |
| 6078 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6079 | /* Register this new fetch. */ |
| 6080 | sample_register_fetches(sfk); |
| 6081 | |
| 6082 | return 0; |
| 6083 | } |
| 6084 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6085 | /* This function is a wrapper to execute each LUA function declared |
| 6086 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6087 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6088 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6089 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6090 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6091 | 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] | 6092 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6093 | { |
| 6094 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6095 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6096 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6097 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6098 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6099 | |
| 6100 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6101 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6102 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6103 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6104 | case ACT_F_HTTP_RES: analyzer = AN_RES_HTTP_PROCESS_BE; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6105 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6106 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6107 | return ACT_RET_CONT; |
| 6108 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6109 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6110 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6111 | * Lua context can be not initialized. This behavior |
| 6112 | * permits to save performances because a systematic |
| 6113 | * Lua initialization cause 5% performances loss. |
| 6114 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6115 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6116 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6117 | if (!s->hlua) { |
| 6118 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6119 | rule->arg.hlua_rule->fcn.name); |
| 6120 | return ACT_RET_CONT; |
| 6121 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6122 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6123 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6124 | rule->arg.hlua_rule->fcn.name); |
| 6125 | return ACT_RET_CONT; |
| 6126 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6127 | } |
| 6128 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6129 | consistency_set(s, dir, &s->hlua->cons); |
| 6130 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6131 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6132 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6133 | |
| 6134 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6135 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6136 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6137 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6138 | else |
| 6139 | error = "critical error"; |
| 6140 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6141 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6142 | return ACT_RET_CONT; |
| 6143 | } |
| 6144 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6145 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6146 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6147 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6148 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6149 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6150 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6151 | } |
| 6152 | |
| 6153 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6154 | 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] | 6155 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6156 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6157 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6158 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6159 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6160 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6161 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6162 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6163 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6164 | |
| 6165 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6166 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6167 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6168 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6169 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6170 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6171 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6172 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6173 | lua_pushstring(s->hlua->T, *arg); |
| 6174 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6175 | } |
| 6176 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6177 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6178 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6179 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6180 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6181 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6182 | } |
| 6183 | |
| 6184 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6186 | /* finished. */ |
| 6187 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6189 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6190 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6191 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6192 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6193 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6194 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6195 | |
| 6196 | /* yield. */ |
| 6197 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6198 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6200 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6201 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6202 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6203 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6204 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6205 | /* Some actions can be wake up when a "write" event |
| 6206 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6207 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6208 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6210 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6211 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6212 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6213 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6214 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6215 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6216 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6217 | * because HAProxy is not able to manipulate data, it |
| 6218 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6219 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6220 | |
| 6221 | /* finished with error. */ |
| 6222 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6223 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6224 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6225 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6226 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6227 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6228 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6229 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6230 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6231 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6232 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6233 | case HLUA_E_ETMOUT: |
| 6234 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6235 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6236 | return ACT_RET_ERR; |
| 6237 | } |
| 6238 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6239 | return 0; |
| 6240 | |
| 6241 | case HLUA_E_NOMEM: |
| 6242 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6243 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6244 | return ACT_RET_ERR; |
| 6245 | } |
| 6246 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6247 | return 0; |
| 6248 | |
| 6249 | case HLUA_E_YIELD: |
| 6250 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6251 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6252 | return ACT_RET_ERR; |
| 6253 | } |
| 6254 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6255 | rule->arg.hlua_rule->fcn.name); |
| 6256 | return 0; |
| 6257 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6258 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6259 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6260 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6261 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6262 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6263 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6264 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6265 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6266 | |
| 6267 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6268 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6269 | } |
| 6270 | } |
| 6271 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6272 | 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] | 6273 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6274 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6275 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6276 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6277 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6278 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6279 | } |
| 6280 | |
| 6281 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6282 | { |
| 6283 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6284 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6285 | struct task *task; |
| 6286 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6287 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6288 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6289 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6290 | if (!hlua) { |
| 6291 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6292 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6293 | return 0; |
| 6294 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6295 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6296 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6297 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6298 | |
| 6299 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6300 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6301 | if (!task) { |
| 6302 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6303 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6304 | return 0; |
| 6305 | } |
| 6306 | task->nice = 0; |
| 6307 | task->context = ctx; |
| 6308 | task->process = hlua_applet_wakeup; |
| 6309 | ctx->ctx.hlua_apptcp.task = task; |
| 6310 | |
| 6311 | /* In the execution wrappers linked with a stream, the |
| 6312 | * Lua context can be not initialized. This behavior |
| 6313 | * permits to save performances because a systematic |
| 6314 | * Lua initialization cause 5% performances loss. |
| 6315 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6316 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6317 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6318 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6319 | return 0; |
| 6320 | } |
| 6321 | |
| 6322 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6323 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6324 | |
| 6325 | /* The following Lua calls can fail. */ |
| 6326 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6327 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6328 | error = lua_tostring(hlua->T, -1); |
| 6329 | else |
| 6330 | error = "critical error"; |
| 6331 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6332 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6333 | return 0; |
| 6334 | } |
| 6335 | |
| 6336 | /* Check stack available size. */ |
| 6337 | if (!lua_checkstack(hlua->T, 1)) { |
| 6338 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6339 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6340 | RESET_SAFE_LJMP(hlua->T); |
| 6341 | return 0; |
| 6342 | } |
| 6343 | |
| 6344 | /* Restore the function in the stack. */ |
| 6345 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6346 | |
| 6347 | /* Create and and push object stream in the stack. */ |
| 6348 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6349 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6350 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6351 | RESET_SAFE_LJMP(hlua->T); |
| 6352 | return 0; |
| 6353 | } |
| 6354 | hlua->nargs = 1; |
| 6355 | |
| 6356 | /* push keywords in the stack. */ |
| 6357 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6358 | if (!lua_checkstack(hlua->T, 1)) { |
| 6359 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6360 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6361 | RESET_SAFE_LJMP(hlua->T); |
| 6362 | return 0; |
| 6363 | } |
| 6364 | lua_pushstring(hlua->T, *arg); |
| 6365 | hlua->nargs++; |
| 6366 | } |
| 6367 | |
| 6368 | RESET_SAFE_LJMP(hlua->T); |
| 6369 | |
| 6370 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6371 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6372 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6373 | |
| 6374 | return 1; |
| 6375 | } |
| 6376 | |
| 6377 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6378 | { |
| 6379 | struct stream_interface *si = ctx->owner; |
| 6380 | struct stream *strm = si_strm(si); |
| 6381 | struct channel *res = si_ic(si); |
| 6382 | struct act_rule *rule = ctx->rule; |
| 6383 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6384 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6385 | |
| 6386 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6387 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6388 | /* eat the whole request */ |
| 6389 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6390 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6391 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6392 | |
| 6393 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6394 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6395 | return; |
| 6396 | |
| 6397 | /* Execute the function. */ |
| 6398 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6399 | /* finished. */ |
| 6400 | case HLUA_E_OK: |
| 6401 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6402 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6403 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6404 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6405 | res->flags |= CF_READ_NULL; |
| 6406 | si_shutr(si); |
| 6407 | return; |
| 6408 | |
| 6409 | /* yield. */ |
| 6410 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6411 | if (hlua->wake_time != TICK_ETERNITY) |
| 6412 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6413 | return; |
| 6414 | |
| 6415 | /* finished with error. */ |
| 6416 | case HLUA_E_ERRMSG: |
| 6417 | /* Display log. */ |
| 6418 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6419 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6420 | lua_pop(hlua->T, 1); |
| 6421 | goto error; |
| 6422 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6423 | case HLUA_E_ETMOUT: |
| 6424 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6425 | rule->arg.hlua_rule->fcn.name); |
| 6426 | goto error; |
| 6427 | |
| 6428 | case HLUA_E_NOMEM: |
| 6429 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6430 | rule->arg.hlua_rule->fcn.name); |
| 6431 | goto error; |
| 6432 | |
| 6433 | case HLUA_E_YIELD: /* unexpected */ |
| 6434 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6435 | rule->arg.hlua_rule->fcn.name); |
| 6436 | goto error; |
| 6437 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6438 | case HLUA_E_ERR: |
| 6439 | /* Display log. */ |
| 6440 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6441 | rule->arg.hlua_rule->fcn.name); |
| 6442 | goto error; |
| 6443 | |
| 6444 | default: |
| 6445 | goto error; |
| 6446 | } |
| 6447 | |
| 6448 | error: |
| 6449 | |
| 6450 | /* For all other cases, just close the stream. */ |
| 6451 | si_shutw(si); |
| 6452 | si_shutr(si); |
| 6453 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6454 | } |
| 6455 | |
| 6456 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6457 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6458 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6459 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6460 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6461 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6462 | } |
| 6463 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6464 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6465 | * an errors occurs and -1 if more data are required for initializing |
| 6466 | * the applet. |
| 6467 | */ |
| 6468 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6469 | { |
| 6470 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6471 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6472 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6473 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6474 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6475 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6476 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6477 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6478 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6479 | if (!hlua) { |
| 6480 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6481 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6482 | return 0; |
| 6483 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6484 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6485 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6486 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6487 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6488 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6489 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6490 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6491 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6492 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6493 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6494 | if (!task) { |
| 6495 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6496 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6497 | return 0; |
| 6498 | } |
| 6499 | task->nice = 0; |
| 6500 | task->context = ctx; |
| 6501 | task->process = hlua_applet_wakeup; |
| 6502 | ctx->ctx.hlua_apphttp.task = task; |
| 6503 | |
| 6504 | /* In the execution wrappers linked with a stream, the |
| 6505 | * Lua context can be not initialized. This behavior |
| 6506 | * permits to save performances because a systematic |
| 6507 | * Lua initialization cause 5% performances loss. |
| 6508 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6509 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6510 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6511 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6512 | return 0; |
| 6513 | } |
| 6514 | |
| 6515 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6516 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6517 | |
| 6518 | /* The following Lua calls can fail. */ |
| 6519 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6520 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6521 | error = lua_tostring(hlua->T, -1); |
| 6522 | else |
| 6523 | error = "critical error"; |
| 6524 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6525 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6526 | return 0; |
| 6527 | } |
| 6528 | |
| 6529 | /* Check stack available size. */ |
| 6530 | if (!lua_checkstack(hlua->T, 1)) { |
| 6531 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6532 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6533 | RESET_SAFE_LJMP(hlua->T); |
| 6534 | return 0; |
| 6535 | } |
| 6536 | |
| 6537 | /* Restore the function in the stack. */ |
| 6538 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6539 | |
| 6540 | /* Create and and push object stream in the stack. */ |
| 6541 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6542 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6543 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6544 | RESET_SAFE_LJMP(hlua->T); |
| 6545 | return 0; |
| 6546 | } |
| 6547 | hlua->nargs = 1; |
| 6548 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6549 | /* push keywords in the stack. */ |
| 6550 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6551 | if (!lua_checkstack(hlua->T, 1)) { |
| 6552 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6553 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6554 | RESET_SAFE_LJMP(hlua->T); |
| 6555 | return 0; |
| 6556 | } |
| 6557 | lua_pushstring(hlua->T, *arg); |
| 6558 | hlua->nargs++; |
| 6559 | } |
| 6560 | |
| 6561 | RESET_SAFE_LJMP(hlua->T); |
| 6562 | |
| 6563 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6564 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6565 | |
| 6566 | return 1; |
| 6567 | } |
| 6568 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6569 | static void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6570 | { |
| 6571 | struct stream_interface *si = ctx->owner; |
| 6572 | struct stream *strm = si_strm(si); |
| 6573 | struct channel *req = si_oc(si); |
| 6574 | struct channel *res = si_ic(si); |
| 6575 | struct act_rule *rule = ctx->rule; |
| 6576 | struct proxy *px = strm->be; |
| 6577 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 6578 | struct htx *req_htx, *res_htx; |
| 6579 | |
| 6580 | res_htx = htx_from_buf(&res->buf); |
| 6581 | |
| 6582 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6583 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6584 | goto out; |
| 6585 | |
| 6586 | /* Check if the input buffer is avalaible. */ |
| 6587 | if (!b_size(&res->buf)) { |
| 6588 | si_rx_room_blk(si); |
| 6589 | goto out; |
| 6590 | } |
| 6591 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6592 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6593 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6594 | |
| 6595 | /* Set the currently running flag. */ |
| 6596 | if (!HLUA_IS_RUNNING(hlua) && |
| 6597 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6598 | struct htx_blk *blk; |
| 6599 | size_t count = co_data(req); |
| 6600 | |
| 6601 | if (!count) { |
| 6602 | si_cant_get(si); |
| 6603 | goto out; |
| 6604 | } |
| 6605 | |
| 6606 | /* We need to flush the request header. This left the body for |
| 6607 | * the Lua. |
| 6608 | */ |
| 6609 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 6610 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6611 | while (count && blk) { |
| 6612 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6613 | uint32_t sz = htx_get_blksz(blk); |
| 6614 | |
| 6615 | if (sz > count) { |
| 6616 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6617 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6618 | goto out; |
| 6619 | } |
| 6620 | |
| 6621 | count -= sz; |
| 6622 | co_set_data(req, co_data(req) - sz); |
| 6623 | blk = htx_remove_blk(req_htx, blk); |
| 6624 | |
| 6625 | if (type == HTX_BLK_EOH) |
| 6626 | break; |
| 6627 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6628 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6629 | } |
| 6630 | |
| 6631 | /* Executes The applet if it is not done. */ |
| 6632 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6633 | |
| 6634 | /* Execute the function. */ |
| 6635 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6636 | /* finished. */ |
| 6637 | case HLUA_E_OK: |
| 6638 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6639 | break; |
| 6640 | |
| 6641 | /* yield. */ |
| 6642 | case HLUA_E_AGAIN: |
| 6643 | if (hlua->wake_time != TICK_ETERNITY) |
| 6644 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6645 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6646 | |
| 6647 | /* finished with error. */ |
| 6648 | case HLUA_E_ERRMSG: |
| 6649 | /* Display log. */ |
| 6650 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6651 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6652 | lua_pop(hlua->T, 1); |
| 6653 | goto error; |
| 6654 | |
| 6655 | case HLUA_E_ETMOUT: |
| 6656 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6657 | rule->arg.hlua_rule->fcn.name); |
| 6658 | goto error; |
| 6659 | |
| 6660 | case HLUA_E_NOMEM: |
| 6661 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6662 | rule->arg.hlua_rule->fcn.name); |
| 6663 | goto error; |
| 6664 | |
| 6665 | case HLUA_E_YIELD: /* unexpected */ |
| 6666 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6667 | rule->arg.hlua_rule->fcn.name); |
| 6668 | goto error; |
| 6669 | |
| 6670 | case HLUA_E_ERR: |
| 6671 | /* Display log. */ |
| 6672 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6673 | rule->arg.hlua_rule->fcn.name); |
| 6674 | goto error; |
| 6675 | |
| 6676 | default: |
| 6677 | goto error; |
| 6678 | } |
| 6679 | } |
| 6680 | |
| 6681 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6682 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 6683 | goto done; |
| 6684 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6685 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 6686 | goto error; |
| 6687 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 6688 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6689 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 6690 | si_rx_room_blk(si); |
| 6691 | goto out; |
| 6692 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6693 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6694 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6695 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6696 | } |
| 6697 | |
| 6698 | done: |
| 6699 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6700 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6701 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6702 | si_shutr(si); |
| 6703 | } |
| 6704 | |
| 6705 | /* eat the whole request */ |
| 6706 | if (co_data(req)) { |
| 6707 | req_htx = htx_from_buf(&req->buf); |
| 6708 | co_htx_skip(req, req_htx, co_data(req)); |
| 6709 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6710 | } |
| 6711 | } |
| 6712 | |
| 6713 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6714 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6715 | return; |
| 6716 | |
| 6717 | error: |
| 6718 | |
| 6719 | /* If we are in HTTP mode, and we are not send any |
| 6720 | * data, return a 500 server error in best effort: |
| 6721 | * if there is no room available in the buffer, |
| 6722 | * just close the connection. |
| 6723 | */ |
| 6724 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 6725 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6726 | |
| 6727 | channel_erase(res); |
| 6728 | res->buf.data = b_data(err); |
| 6729 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 6730 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6731 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6732 | } |
| 6733 | if (!(strm->flags & SF_ERR_MASK)) |
| 6734 | strm->flags |= SF_ERR_RESOURCE; |
| 6735 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6736 | goto done; |
| 6737 | } |
| 6738 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6739 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6740 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6741 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6742 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6743 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6744 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6745 | } |
| 6746 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6747 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6748 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6749 | * |
| 6750 | * This function can fail with an abort() due to an Lua critical error. |
| 6751 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6752 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6753 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6754 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6755 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6756 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6757 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6758 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6759 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6760 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6761 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6762 | if (!rule->arg.hlua_rule) { |
| 6763 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6764 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6765 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6766 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6767 | /* Memory for arguments. */ |
| 6768 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6769 | if (!rule->arg.hlua_rule->args) { |
| 6770 | memprintf(err, "out of memory error"); |
| 6771 | return ACT_RET_PRS_ERR; |
| 6772 | } |
| 6773 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6774 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6775 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6776 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6777 | /* Expect some arguments */ |
| 6778 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6779 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6780 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6781 | return ACT_RET_PRS_ERR; |
| 6782 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6783 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6784 | if (!rule->arg.hlua_rule->args[i]) { |
| 6785 | memprintf(err, "out of memory error"); |
| 6786 | return ACT_RET_PRS_ERR; |
| 6787 | } |
| 6788 | (*cur_arg)++; |
| 6789 | } |
| 6790 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6791 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6792 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6793 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6794 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6795 | } |
| 6796 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6797 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6798 | struct act_rule *rule, char **err) |
| 6799 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6800 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6801 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6802 | /* HTTP applets are forbidden in tcp-request rules. |
| 6803 | * HTTP applet request requires everything initilized by |
| 6804 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6805 | * The applet will be immediately initilized, but its before |
| 6806 | * the call of this analyzer. |
| 6807 | */ |
| 6808 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6809 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6810 | return ACT_RET_PRS_ERR; |
| 6811 | } |
| 6812 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6813 | /* Memory for the rule. */ |
| 6814 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6815 | if (!rule->arg.hlua_rule) { |
| 6816 | memprintf(err, "out of memory error"); |
| 6817 | return ACT_RET_PRS_ERR; |
| 6818 | } |
| 6819 | |
| 6820 | /* Reference the Lua function and store the reference. */ |
| 6821 | rule->arg.hlua_rule->fcn = *fcn; |
| 6822 | |
| 6823 | /* TODO: later accept arguments. */ |
| 6824 | rule->arg.hlua_rule->args = NULL; |
| 6825 | |
| 6826 | /* Add applet pointer in the rule. */ |
| 6827 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6828 | rule->applet.name = fcn->name; |
| 6829 | rule->applet.init = hlua_applet_http_init; |
| 6830 | rule->applet.fct = hlua_applet_http_fct; |
| 6831 | rule->applet.release = hlua_applet_http_release; |
| 6832 | rule->applet.timeout = hlua_timeout_applet; |
| 6833 | |
| 6834 | return ACT_RET_PRS_OK; |
| 6835 | } |
| 6836 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6837 | /* This function is an LUA binding used for registering |
| 6838 | * "sample-conv" functions. It expects a converter name used |
| 6839 | * in the haproxy configuration file, and an LUA function. |
| 6840 | */ |
| 6841 | __LJMP static int hlua_register_action(lua_State *L) |
| 6842 | { |
| 6843 | struct action_kw_list *akl; |
| 6844 | const char *name; |
| 6845 | int ref; |
| 6846 | int len; |
| 6847 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6848 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6849 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6850 | /* Initialise the number of expected arguments at 0. */ |
| 6851 | nargs = 0; |
| 6852 | |
| 6853 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6854 | 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] | 6855 | |
| 6856 | /* First argument : converter name. */ |
| 6857 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6858 | |
| 6859 | /* Second argument : environment. */ |
| 6860 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6861 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6862 | |
| 6863 | /* Third argument : lua function. */ |
| 6864 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6865 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6866 | /* 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] | 6867 | if (lua_gettop(L) >= 4) |
| 6868 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6869 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6870 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6871 | lua_pushnil(L); |
| 6872 | while (lua_next(L, 2) != 0) { |
| 6873 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6874 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6875 | |
| 6876 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6877 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6878 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6879 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6880 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6881 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6882 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6883 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6884 | |
| 6885 | /* Fill fcn. */ |
| 6886 | fcn->name = strdup(name); |
| 6887 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6888 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6889 | fcn->function_ref = ref; |
| 6890 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6891 | /* Set the expected number od arguments. */ |
| 6892 | fcn->nargs = nargs; |
| 6893 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6894 | /* List head */ |
| 6895 | akl->list.n = akl->list.p = NULL; |
| 6896 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6897 | /* action keyword. */ |
| 6898 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6899 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6900 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6901 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6902 | |
| 6903 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6904 | |
| 6905 | akl->kw[0].match_pfx = 0; |
| 6906 | akl->kw[0].private = fcn; |
| 6907 | akl->kw[0].parse = action_register_lua; |
| 6908 | |
| 6909 | /* select the action registering point. */ |
| 6910 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6911 | tcp_req_cont_keywords_register(akl); |
| 6912 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6913 | tcp_res_cont_keywords_register(akl); |
| 6914 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6915 | http_req_keywords_register(akl); |
| 6916 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6917 | http_res_keywords_register(akl); |
| 6918 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6919 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6920 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6921 | "are expected.", lua_tostring(L, -1))); |
| 6922 | |
| 6923 | /* pop the environment string. */ |
| 6924 | lua_pop(L, 1); |
| 6925 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6926 | return ACT_RET_PRS_OK; |
| 6927 | } |
| 6928 | |
| 6929 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6930 | struct act_rule *rule, char **err) |
| 6931 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6932 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6933 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 6934 | if (px->mode == PR_MODE_HTTP) { |
| 6935 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6936 | return ACT_RET_PRS_ERR; |
| 6937 | } |
| 6938 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6939 | /* Memory for the rule. */ |
| 6940 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6941 | if (!rule->arg.hlua_rule) { |
| 6942 | memprintf(err, "out of memory error"); |
| 6943 | return ACT_RET_PRS_ERR; |
| 6944 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6945 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6946 | /* Reference the Lua function and store the reference. */ |
| 6947 | rule->arg.hlua_rule->fcn = *fcn; |
| 6948 | |
| 6949 | /* TODO: later accept arguments. */ |
| 6950 | rule->arg.hlua_rule->args = NULL; |
| 6951 | |
| 6952 | /* Add applet pointer in the rule. */ |
| 6953 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6954 | rule->applet.name = fcn->name; |
| 6955 | rule->applet.init = hlua_applet_tcp_init; |
| 6956 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6957 | rule->applet.release = hlua_applet_tcp_release; |
| 6958 | rule->applet.timeout = hlua_timeout_applet; |
| 6959 | |
| 6960 | return 0; |
| 6961 | } |
| 6962 | |
| 6963 | /* This function is an LUA binding used for registering |
| 6964 | * "sample-conv" functions. It expects a converter name used |
| 6965 | * in the haproxy configuration file, and an LUA function. |
| 6966 | */ |
| 6967 | __LJMP static int hlua_register_service(lua_State *L) |
| 6968 | { |
| 6969 | struct action_kw_list *akl; |
| 6970 | const char *name; |
| 6971 | const char *env; |
| 6972 | int ref; |
| 6973 | int len; |
| 6974 | struct hlua_function *fcn; |
| 6975 | |
| 6976 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6977 | |
| 6978 | /* First argument : converter name. */ |
| 6979 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6980 | |
| 6981 | /* Second argument : environment. */ |
| 6982 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6983 | |
| 6984 | /* Third argument : lua function. */ |
| 6985 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6986 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6987 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6988 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6989 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6990 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6991 | fcn = calloc(1, sizeof(*fcn)); |
| 6992 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6993 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6994 | |
| 6995 | /* Fill fcn. */ |
| 6996 | len = strlen("<lua.>") + strlen(name) + 1; |
| 6997 | fcn->name = calloc(1, len); |
| 6998 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6999 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7000 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7001 | fcn->function_ref = ref; |
| 7002 | |
| 7003 | /* List head */ |
| 7004 | akl->list.n = akl->list.p = NULL; |
| 7005 | |
| 7006 | /* converter keyword. */ |
| 7007 | len = strlen("lua.") + strlen(name) + 1; |
| 7008 | akl->kw[0].kw = calloc(1, len); |
| 7009 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7010 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7011 | |
| 7012 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7013 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7014 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7015 | if (strcmp(env, "tcp") == 0) |
| 7016 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7017 | else if (strcmp(env, "http") == 0) |
| 7018 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7019 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7020 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7021 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7022 | |
| 7023 | akl->kw[0].match_pfx = 0; |
| 7024 | akl->kw[0].private = fcn; |
| 7025 | |
| 7026 | /* End of array. */ |
| 7027 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7028 | |
| 7029 | /* Register this new converter */ |
| 7030 | service_keywords_register(akl); |
| 7031 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7032 | return 0; |
| 7033 | } |
| 7034 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7035 | /* This function initialises Lua cli handler. It copies the |
| 7036 | * arguments in the Lua stack and create channel IO objects. |
| 7037 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7038 | 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] | 7039 | { |
| 7040 | struct hlua *hlua; |
| 7041 | struct hlua_function *fcn; |
| 7042 | int i; |
| 7043 | const char *error; |
| 7044 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7045 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7046 | appctx->ctx.hlua_cli.fcn = private; |
| 7047 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7048 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7049 | if (!hlua) { |
| 7050 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7051 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7052 | } |
| 7053 | HLUA_INIT(hlua); |
| 7054 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7055 | |
| 7056 | /* Create task used by signal to wakeup applets. |
| 7057 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7058 | * applet_http. It is absolutely compatible. |
| 7059 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7060 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7061 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7062 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7063 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7064 | } |
| 7065 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7066 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7067 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7068 | |
| 7069 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7070 | 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] | 7071 | 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] | 7072 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7073 | } |
| 7074 | |
| 7075 | /* The following Lua calls can fail. */ |
| 7076 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7077 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7078 | error = lua_tostring(hlua->T, -1); |
| 7079 | else |
| 7080 | error = "critical error"; |
| 7081 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7082 | goto error; |
| 7083 | } |
| 7084 | |
| 7085 | /* Check stack available size. */ |
| 7086 | if (!lua_checkstack(hlua->T, 2)) { |
| 7087 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7088 | goto error; |
| 7089 | } |
| 7090 | |
| 7091 | /* Restore the function in the stack. */ |
| 7092 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7093 | |
| 7094 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7095 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7096 | */ |
| 7097 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7098 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7099 | goto error; |
| 7100 | } |
| 7101 | hlua->nargs = 1; |
| 7102 | |
| 7103 | /* push keywords in the stack. */ |
| 7104 | for (i = 0; *args[i]; i++) { |
| 7105 | /* Check stack available size. */ |
| 7106 | if (!lua_checkstack(hlua->T, 1)) { |
| 7107 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7108 | goto error; |
| 7109 | } |
| 7110 | lua_pushstring(hlua->T, args[i]); |
| 7111 | hlua->nargs++; |
| 7112 | } |
| 7113 | |
| 7114 | /* We must initialize the execution timeouts. */ |
| 7115 | hlua->max_time = hlua_timeout_session; |
| 7116 | |
| 7117 | /* At this point the execution is safe. */ |
| 7118 | RESET_SAFE_LJMP(hlua->T); |
| 7119 | |
| 7120 | /* It's ok */ |
| 7121 | return 0; |
| 7122 | |
| 7123 | /* It's not ok. */ |
| 7124 | error: |
| 7125 | RESET_SAFE_LJMP(hlua->T); |
| 7126 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7127 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7128 | return 1; |
| 7129 | } |
| 7130 | |
| 7131 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7132 | { |
| 7133 | struct hlua *hlua; |
| 7134 | struct stream_interface *si; |
| 7135 | struct hlua_function *fcn; |
| 7136 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7137 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7138 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7139 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7140 | |
| 7141 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7142 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7143 | return 1; |
| 7144 | |
| 7145 | /* Execute the function. */ |
| 7146 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7147 | |
| 7148 | /* finished. */ |
| 7149 | case HLUA_E_OK: |
| 7150 | return 1; |
| 7151 | |
| 7152 | /* yield. */ |
| 7153 | case HLUA_E_AGAIN: |
| 7154 | /* We want write. */ |
| 7155 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7156 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7157 | /* Set the timeout. */ |
| 7158 | if (hlua->wake_time != TICK_ETERNITY) |
| 7159 | task_schedule(hlua->task, hlua->wake_time); |
| 7160 | return 0; |
| 7161 | |
| 7162 | /* finished with error. */ |
| 7163 | case HLUA_E_ERRMSG: |
| 7164 | /* Display log. */ |
| 7165 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7166 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7167 | lua_pop(hlua->T, 1); |
| 7168 | return 1; |
| 7169 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7170 | case HLUA_E_ETMOUT: |
| 7171 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7172 | fcn->name); |
| 7173 | return 1; |
| 7174 | |
| 7175 | case HLUA_E_NOMEM: |
| 7176 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7177 | fcn->name); |
| 7178 | return 1; |
| 7179 | |
| 7180 | case HLUA_E_YIELD: /* unexpected */ |
| 7181 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7182 | fcn->name); |
| 7183 | return 1; |
| 7184 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7185 | case HLUA_E_ERR: |
| 7186 | /* Display log. */ |
| 7187 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7188 | fcn->name); |
| 7189 | return 1; |
| 7190 | |
| 7191 | default: |
| 7192 | return 1; |
| 7193 | } |
| 7194 | |
| 7195 | return 1; |
| 7196 | } |
| 7197 | |
| 7198 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7199 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7200 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7201 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7202 | } |
| 7203 | |
| 7204 | /* This function is an LUA binding used for registering |
| 7205 | * new keywords in the cli. It expects a list of keywords |
| 7206 | * which are the "path". It is limited to 5 keywords. A |
| 7207 | * description of the command, a function to be executed |
| 7208 | * for the parsing and a function for io handlers. |
| 7209 | */ |
| 7210 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7211 | { |
| 7212 | struct cli_kw_list *cli_kws; |
| 7213 | const char *message; |
| 7214 | int ref_io; |
| 7215 | int len; |
| 7216 | struct hlua_function *fcn; |
| 7217 | int index; |
| 7218 | int i; |
| 7219 | |
| 7220 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7221 | |
| 7222 | /* First argument : an array of maximum 5 keywords. */ |
| 7223 | if (!lua_istable(L, 1)) |
| 7224 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7225 | |
| 7226 | /* Second argument : string with contextual message. */ |
| 7227 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7228 | |
| 7229 | /* Third and fourth argument : lua function. */ |
| 7230 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7231 | |
| 7232 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7233 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7234 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7235 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7236 | fcn = calloc(1, sizeof(*fcn)); |
| 7237 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7238 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7239 | |
| 7240 | /* Fill path. */ |
| 7241 | index = 0; |
| 7242 | lua_pushnil(L); |
| 7243 | while(lua_next(L, 1) != 0) { |
| 7244 | if (index >= 5) |
| 7245 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7246 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7247 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7248 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7249 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7250 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7251 | index++; |
| 7252 | lua_pop(L, 1); |
| 7253 | } |
| 7254 | |
| 7255 | /* Copy help message. */ |
| 7256 | cli_kws->kw[0].usage = strdup(message); |
| 7257 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7258 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7259 | |
| 7260 | /* Fill fcn io handler. */ |
| 7261 | len = strlen("<lua.cli>") + 1; |
| 7262 | for (i = 0; i < index; i++) |
| 7263 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7264 | fcn->name = calloc(1, len); |
| 7265 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7266 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7267 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7268 | for (i = 0; i < index; i++) { |
| 7269 | strncat((char *)fcn->name, ".", len); |
| 7270 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7271 | } |
| 7272 | strncat((char *)fcn->name, ">", len); |
| 7273 | fcn->function_ref = ref_io; |
| 7274 | |
| 7275 | /* Fill last entries. */ |
| 7276 | cli_kws->kw[0].private = fcn; |
| 7277 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7278 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7279 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7280 | |
| 7281 | /* Register this new converter */ |
| 7282 | cli_register_kw(cli_kws); |
| 7283 | |
| 7284 | return 0; |
| 7285 | } |
| 7286 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7287 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7288 | struct proxy *defpx, const char *file, int line, |
| 7289 | char **err, unsigned int *timeout) |
| 7290 | { |
| 7291 | const char *error; |
| 7292 | |
| 7293 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7294 | if (error == PARSE_TIME_OVER) { |
| 7295 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7296 | args[1], args[0]); |
| 7297 | return -1; |
| 7298 | } |
| 7299 | else if (error == PARSE_TIME_UNDER) { |
| 7300 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7301 | args[1], args[0]); |
| 7302 | return -1; |
| 7303 | } |
| 7304 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7305 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7306 | return -1; |
| 7307 | } |
| 7308 | return 0; |
| 7309 | } |
| 7310 | |
| 7311 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7312 | struct proxy *defpx, const char *file, int line, |
| 7313 | char **err) |
| 7314 | { |
| 7315 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7316 | file, line, err, &hlua_timeout_session); |
| 7317 | } |
| 7318 | |
| 7319 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7320 | struct proxy *defpx, const char *file, int line, |
| 7321 | char **err) |
| 7322 | { |
| 7323 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7324 | file, line, err, &hlua_timeout_task); |
| 7325 | } |
| 7326 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7327 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7328 | struct proxy *defpx, const char *file, int line, |
| 7329 | char **err) |
| 7330 | { |
| 7331 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7332 | file, line, err, &hlua_timeout_applet); |
| 7333 | } |
| 7334 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7335 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7336 | struct proxy *defpx, const char *file, int line, |
| 7337 | char **err) |
| 7338 | { |
| 7339 | char *error; |
| 7340 | |
| 7341 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7342 | if (*error != '\0') { |
| 7343 | memprintf(err, "%s: invalid number", args[0]); |
| 7344 | return -1; |
| 7345 | } |
| 7346 | return 0; |
| 7347 | } |
| 7348 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7349 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7350 | struct proxy *defpx, const char *file, int line, |
| 7351 | char **err) |
| 7352 | { |
| 7353 | char *error; |
| 7354 | |
| 7355 | if (*(args[1]) == 0) { |
| 7356 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7357 | return -1; |
| 7358 | } |
| 7359 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7360 | if (*error != '\0') { |
| 7361 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7362 | return -1; |
| 7363 | } |
| 7364 | return 0; |
| 7365 | } |
| 7366 | |
| 7367 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7368 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7369 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7370 | * the main lua entry point. |
| 7371 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7372 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7373 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7374 | * |
| 7375 | * In some error case, LUA set an error message in top of the stack. This function |
| 7376 | * 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] | 7377 | * |
| 7378 | * This function can fail with an abort() due to an Lua critical error. |
| 7379 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7380 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7381 | */ |
| 7382 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7383 | struct proxy *defpx, const char *file, int line, |
| 7384 | char **err) |
| 7385 | { |
| 7386 | int error; |
| 7387 | |
| 7388 | /* Just load and compile the file. */ |
| 7389 | error = luaL_loadfile(gL.T, args[1]); |
| 7390 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7391 | 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] | 7392 | lua_pop(gL.T, 1); |
| 7393 | return -1; |
| 7394 | } |
| 7395 | |
| 7396 | /* If no syntax error where detected, execute the code. */ |
| 7397 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7398 | switch (error) { |
| 7399 | case LUA_OK: |
| 7400 | break; |
| 7401 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7402 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7403 | lua_pop(gL.T, 1); |
| 7404 | return -1; |
| 7405 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7406 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7407 | return -1; |
| 7408 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7409 | 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] | 7410 | lua_pop(gL.T, 1); |
| 7411 | return -1; |
| 7412 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7413 | 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] | 7414 | lua_pop(gL.T, 1); |
| 7415 | return -1; |
| 7416 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7417 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7418 | lua_pop(gL.T, 1); |
| 7419 | return -1; |
| 7420 | } |
| 7421 | |
| 7422 | return 0; |
| 7423 | } |
| 7424 | |
| 7425 | /* configuration keywords declaration */ |
| 7426 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7427 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7428 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7429 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7430 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7431 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7432 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7433 | { 0, NULL, NULL }, |
| 7434 | }}; |
| 7435 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7436 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7437 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7438 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7439 | /* This function can fail with an abort() due to an Lua critical error. |
| 7440 | * We are in the initialisation process of HAProxy, this abort() is |
| 7441 | * tolerated. |
| 7442 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7443 | int hlua_post_init() |
| 7444 | { |
| 7445 | struct hlua_init_function *init; |
| 7446 | const char *msg; |
| 7447 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7448 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7449 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7450 | /* Call post initialisation function in safe environement. */ |
| 7451 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7452 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7453 | error = lua_tostring(gL.T, -1); |
| 7454 | else |
| 7455 | error = "critical error"; |
| 7456 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7457 | exit(1); |
| 7458 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7459 | |
| 7460 | #if USE_OPENSSL |
| 7461 | /* Initialize SSL server. */ |
| 7462 | if (socket_ssl.xprt->prepare_srv) { |
| 7463 | int saved_used_backed = global.ssl_used_backend; |
| 7464 | // don't affect maxconn automatic computation |
| 7465 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7466 | global.ssl_used_backend = saved_used_backed; |
| 7467 | } |
| 7468 | #endif |
| 7469 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7470 | hlua_fcn_post_init(gL.T); |
| 7471 | RESET_SAFE_LJMP(gL.T); |
| 7472 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7473 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7474 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7475 | ret = hlua_ctx_resume(&gL, 0); |
| 7476 | switch (ret) { |
| 7477 | case HLUA_E_OK: |
| 7478 | lua_pop(gL.T, -1); |
| 7479 | return 1; |
| 7480 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7481 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7482 | return 0; |
| 7483 | case HLUA_E_ERRMSG: |
| 7484 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7485 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7486 | return 0; |
| 7487 | case HLUA_E_ERR: |
| 7488 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7489 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7490 | return 0; |
| 7491 | } |
| 7492 | } |
| 7493 | return 1; |
| 7494 | } |
| 7495 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7496 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7497 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7498 | * is the previously allocated size or the kind of object in case of a new |
| 7499 | * allocation. <nsize> is the requested new size. |
| 7500 | */ |
| 7501 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7502 | { |
| 7503 | struct hlua_mem_allocator *zone = ud; |
| 7504 | |
| 7505 | if (nsize == 0) { |
| 7506 | /* it's a free */ |
| 7507 | if (ptr) |
| 7508 | zone->allocated -= osize; |
| 7509 | free(ptr); |
| 7510 | return NULL; |
| 7511 | } |
| 7512 | |
| 7513 | if (!ptr) { |
| 7514 | /* it's a new allocation */ |
| 7515 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7516 | return NULL; |
| 7517 | |
| 7518 | ptr = malloc(nsize); |
| 7519 | if (ptr) |
| 7520 | zone->allocated += nsize; |
| 7521 | return ptr; |
| 7522 | } |
| 7523 | |
| 7524 | /* it's a realloc */ |
| 7525 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7526 | return NULL; |
| 7527 | |
| 7528 | ptr = realloc(ptr, nsize); |
| 7529 | if (ptr) |
| 7530 | zone->allocated += nsize - osize; |
| 7531 | return ptr; |
| 7532 | } |
| 7533 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7534 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7535 | * We are in the initialisation process of HAProxy, this abort() is |
| 7536 | * tolerated. |
| 7537 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7538 | void hlua_init(void) |
| 7539 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7540 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7541 | int idx; |
| 7542 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7543 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7544 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7545 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7546 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7547 | struct srv_kw *kw; |
| 7548 | int tmp_error; |
| 7549 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7550 | char *args[] = { /* SSL client configuration. */ |
| 7551 | "ssl", |
| 7552 | "verify", |
| 7553 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7554 | NULL |
| 7555 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7556 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7557 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7558 | /* Init main lua stack. */ |
| 7559 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7560 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7561 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7562 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7563 | hlua_sethlua(&gL); |
| 7564 | gL.Tref = LUA_REFNIL; |
| 7565 | gL.task = NULL; |
| 7566 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7567 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7568 | * the Lua function can fail with an abort. We are in the initialisation |
| 7569 | * process of HAProxy, this abort() is tolerated. |
| 7570 | */ |
| 7571 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7572 | /* Initialise lua. */ |
| 7573 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7574 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7575 | /* Set safe environment for the initialisation. */ |
| 7576 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7577 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7578 | error_msg = lua_tostring(gL.T, -1); |
| 7579 | else |
| 7580 | error_msg = "critical error"; |
| 7581 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7582 | exit(1); |
| 7583 | } |
| 7584 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7585 | /* |
| 7586 | * |
| 7587 | * Create "core" object. |
| 7588 | * |
| 7589 | */ |
| 7590 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7591 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7592 | lua_newtable(gL.T); |
| 7593 | |
| 7594 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7595 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7596 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7597 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7598 | /* Register special functions. */ |
| 7599 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7600 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7601 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7602 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7603 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7604 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7605 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7606 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7607 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7608 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7609 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7610 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7611 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7612 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7613 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7614 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7615 | hlua_class_function(gL.T, "log", hlua_log); |
| 7616 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7617 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7618 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7619 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7620 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7621 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7622 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7623 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7624 | |
| 7625 | /* |
| 7626 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7627 | * Register class Map |
| 7628 | * |
| 7629 | */ |
| 7630 | |
| 7631 | /* This table entry is the object "Map" base. */ |
| 7632 | lua_newtable(gL.T); |
| 7633 | |
| 7634 | /* register pattern types. */ |
| 7635 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7636 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7637 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7638 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7639 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7640 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7641 | |
| 7642 | /* register constructor. */ |
| 7643 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7644 | |
| 7645 | /* Create and fill the metatable. */ |
| 7646 | lua_newtable(gL.T); |
| 7647 | |
| 7648 | /* Create and fille the __index entry. */ |
| 7649 | lua_pushstring(gL.T, "__index"); |
| 7650 | lua_newtable(gL.T); |
| 7651 | |
| 7652 | /* Register . */ |
| 7653 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7654 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7655 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7656 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7657 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7658 | /* Register previous table in the registry with reference and named entry. |
| 7659 | * The function hlua_register_metatable() pops the stack, so we |
| 7660 | * previously create a copy of the table. |
| 7661 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7662 | 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] | 7663 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7664 | |
| 7665 | /* Assign the metatable to the mai Map object. */ |
| 7666 | lua_setmetatable(gL.T, -2); |
| 7667 | |
| 7668 | /* Set a name to the table. */ |
| 7669 | lua_setglobal(gL.T, "Map"); |
| 7670 | |
| 7671 | /* |
| 7672 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7673 | * Register class Channel |
| 7674 | * |
| 7675 | */ |
| 7676 | |
| 7677 | /* Create and fill the metatable. */ |
| 7678 | lua_newtable(gL.T); |
| 7679 | |
| 7680 | /* Create and fille the __index entry. */ |
| 7681 | lua_pushstring(gL.T, "__index"); |
| 7682 | lua_newtable(gL.T); |
| 7683 | |
| 7684 | /* Register . */ |
| 7685 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7686 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7687 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7688 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7689 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7690 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7691 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7692 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7693 | 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] | 7694 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7695 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7696 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7697 | |
| 7698 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7699 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7700 | |
| 7701 | /* |
| 7702 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7703 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7704 | * |
| 7705 | */ |
| 7706 | |
| 7707 | /* Create and fill the metatable. */ |
| 7708 | lua_newtable(gL.T); |
| 7709 | |
| 7710 | /* Create and fille the __index entry. */ |
| 7711 | lua_pushstring(gL.T, "__index"); |
| 7712 | lua_newtable(gL.T); |
| 7713 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7714 | /* Browse existing fetches and create the associated |
| 7715 | * object method. |
| 7716 | */ |
| 7717 | sf = NULL; |
| 7718 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7719 | |
| 7720 | /* Dont register the keywork if the arguments check function are |
| 7721 | * not safe during the runtime. |
| 7722 | */ |
| 7723 | if ((sf->val_args != NULL) && |
| 7724 | (sf->val_args != val_payload_lv) && |
| 7725 | (sf->val_args != val_hdr)) |
| 7726 | continue; |
| 7727 | |
| 7728 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7729 | * by an underscore. |
| 7730 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7731 | strncpy(trash.area, sf->kw, trash.size); |
| 7732 | trash.area[trash.size - 1] = '\0'; |
| 7733 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7734 | if (*p == '.' || *p == '-' || *p == '+') |
| 7735 | *p = '_'; |
| 7736 | |
| 7737 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7738 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7739 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7740 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7741 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7742 | } |
| 7743 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7744 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7745 | |
| 7746 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7747 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7748 | |
| 7749 | /* |
| 7750 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7751 | * Register class Converters |
| 7752 | * |
| 7753 | */ |
| 7754 | |
| 7755 | /* Create and fill the metatable. */ |
| 7756 | lua_newtable(gL.T); |
| 7757 | |
| 7758 | /* Create and fill the __index entry. */ |
| 7759 | lua_pushstring(gL.T, "__index"); |
| 7760 | lua_newtable(gL.T); |
| 7761 | |
| 7762 | /* Browse existing converters and create the associated |
| 7763 | * object method. |
| 7764 | */ |
| 7765 | sc = NULL; |
| 7766 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7767 | /* Dont register the keywork if the arguments check function are |
| 7768 | * not safe during the runtime. |
| 7769 | */ |
| 7770 | if (sc->val_args != NULL) |
| 7771 | continue; |
| 7772 | |
| 7773 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7774 | * by an underscore. |
| 7775 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7776 | strncpy(trash.area, sc->kw, trash.size); |
| 7777 | trash.area[trash.size - 1] = '\0'; |
| 7778 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7779 | if (*p == '.' || *p == '-' || *p == '+') |
| 7780 | *p = '_'; |
| 7781 | |
| 7782 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7783 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7784 | lua_pushlightuserdata(gL.T, sc); |
| 7785 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7786 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7787 | } |
| 7788 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7789 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7790 | |
| 7791 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7792 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7793 | |
| 7794 | /* |
| 7795 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7796 | * Register class HTTP |
| 7797 | * |
| 7798 | */ |
| 7799 | |
| 7800 | /* Create and fill the metatable. */ |
| 7801 | lua_newtable(gL.T); |
| 7802 | |
| 7803 | /* Create and fille the __index entry. */ |
| 7804 | lua_pushstring(gL.T, "__index"); |
| 7805 | lua_newtable(gL.T); |
| 7806 | |
| 7807 | /* Register Lua functions. */ |
| 7808 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7809 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7810 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7811 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7812 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7813 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7814 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7815 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7816 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7817 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7818 | |
| 7819 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7820 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7821 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7822 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7823 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7824 | 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] | 7825 | 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] | 7826 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7827 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7828 | |
| 7829 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7830 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7831 | |
| 7832 | /* |
| 7833 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7834 | * Register class AppletTCP |
| 7835 | * |
| 7836 | */ |
| 7837 | |
| 7838 | /* Create and fill the metatable. */ |
| 7839 | lua_newtable(gL.T); |
| 7840 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7841 | /* Create and fille the __index entry. */ |
| 7842 | lua_pushstring(gL.T, "__index"); |
| 7843 | lua_newtable(gL.T); |
| 7844 | |
| 7845 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7846 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7847 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7848 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7849 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7850 | 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] | 7851 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7852 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7853 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7854 | |
| 7855 | lua_settable(gL.T, -3); |
| 7856 | |
| 7857 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7858 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7859 | |
| 7860 | /* |
| 7861 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7862 | * Register class AppletHTTP |
| 7863 | * |
| 7864 | */ |
| 7865 | |
| 7866 | /* Create and fill the metatable. */ |
| 7867 | lua_newtable(gL.T); |
| 7868 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7869 | /* Create and fille the __index entry. */ |
| 7870 | lua_pushstring(gL.T, "__index"); |
| 7871 | lua_newtable(gL.T); |
| 7872 | |
| 7873 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7874 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7875 | 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] | 7876 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7877 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7878 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7879 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7880 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7881 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7882 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7883 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7884 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7885 | |
| 7886 | lua_settable(gL.T, -3); |
| 7887 | |
| 7888 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7889 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7890 | |
| 7891 | /* |
| 7892 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7893 | * Register class TXN |
| 7894 | * |
| 7895 | */ |
| 7896 | |
| 7897 | /* Create and fill the metatable. */ |
| 7898 | lua_newtable(gL.T); |
| 7899 | |
| 7900 | /* Create and fille the __index entry. */ |
| 7901 | lua_pushstring(gL.T, "__index"); |
| 7902 | lua_newtable(gL.T); |
| 7903 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7904 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7905 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7906 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 7907 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 7908 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 7909 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 7910 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 7911 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 7912 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7913 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 7914 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 7915 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 7916 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7917 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7918 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7919 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7920 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7921 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7922 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7923 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7924 | |
| 7925 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7926 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7927 | |
| 7928 | /* |
| 7929 | * |
| 7930 | * Register class Socket |
| 7931 | * |
| 7932 | */ |
| 7933 | |
| 7934 | /* Create and fill the metatable. */ |
| 7935 | lua_newtable(gL.T); |
| 7936 | |
| 7937 | /* Create and fille the __index entry. */ |
| 7938 | lua_pushstring(gL.T, "__index"); |
| 7939 | lua_newtable(gL.T); |
| 7940 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7941 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7942 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7943 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7944 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7945 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7946 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7947 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7948 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7949 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7950 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7951 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7952 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7953 | 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] | 7954 | |
| 7955 | /* Register the garbage collector entry. */ |
| 7956 | lua_pushstring(gL.T, "__gc"); |
| 7957 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7958 | 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] | 7959 | |
| 7960 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7961 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7962 | |
| 7963 | /* Proxy and server configuration initialisation. */ |
| 7964 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7965 | init_new_proxy(&socket_proxy); |
| 7966 | socket_proxy.parent = NULL; |
| 7967 | socket_proxy.last_change = now.tv_sec; |
| 7968 | socket_proxy.id = "LUA-SOCKET"; |
| 7969 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7970 | socket_proxy.maxconn = 0; |
| 7971 | socket_proxy.accept = NULL; |
| 7972 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7973 | socket_proxy.srv = NULL; |
| 7974 | socket_proxy.conn_retries = 0; |
| 7975 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7976 | |
| 7977 | /* Init TCP server: unchanged parameters */ |
| 7978 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7979 | socket_tcp.next = NULL; |
| 7980 | socket_tcp.proxy = &socket_proxy; |
| 7981 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7982 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7983 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7984 | socket_tcp.priv_conns = NULL; |
| 7985 | socket_tcp.idle_conns = NULL; |
| 7986 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7987 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7988 | socket_tcp.last_change = 0; |
| 7989 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7990 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7991 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7992 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7993 | |
| 7994 | /* XXX: Copy default parameter from default server, |
| 7995 | * but the default server is not initialized. |
| 7996 | */ |
| 7997 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 7998 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 7999 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8000 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8001 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8002 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8003 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8004 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8005 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8006 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8007 | |
| 8008 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8009 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8010 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8011 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8012 | socket_tcp.check.server = &socket_tcp; |
| 8013 | |
| 8014 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8015 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8016 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8017 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8018 | socket_tcp.agent.server = &socket_tcp; |
| 8019 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8020 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8021 | |
| 8022 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8023 | /* Init TCP server: unchanged parameters */ |
| 8024 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8025 | socket_ssl.next = NULL; |
| 8026 | socket_ssl.proxy = &socket_proxy; |
| 8027 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8028 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8029 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8030 | socket_ssl.priv_conns = NULL; |
| 8031 | socket_ssl.idle_conns = NULL; |
| 8032 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8033 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8034 | socket_ssl.last_change = 0; |
| 8035 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8036 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8037 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8038 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8039 | |
| 8040 | /* XXX: Copy default parameter from default server, |
| 8041 | * but the default server is not initialized. |
| 8042 | */ |
| 8043 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8044 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8045 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8046 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8047 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8048 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8049 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8050 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8051 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8052 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8053 | |
| 8054 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8055 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8056 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8057 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8058 | socket_ssl.check.server = &socket_ssl; |
| 8059 | |
| 8060 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8061 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8062 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8063 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8064 | socket_ssl.agent.server = &socket_ssl; |
| 8065 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8066 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8067 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8068 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8069 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8070 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8071 | /* |
| 8072 | * |
| 8073 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8074 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8075 | * features like client certificates and ssl_verify. |
| 8076 | * |
| 8077 | */ |
| 8078 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8079 | if (tmp_error != 0) { |
| 8080 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8081 | abort(); /* This must be never arrives because the command line |
| 8082 | not editable by the user. */ |
| 8083 | } |
| 8084 | idx += kw->skip; |
| 8085 | } |
| 8086 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8087 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8088 | |
| 8089 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8090 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8091 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8092 | static void hlua_register_build_options(void) |
| 8093 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8094 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8095 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8096 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8097 | hap_register_build_opts(ptr, 1); |
| 8098 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8099 | |
| 8100 | INITCALL0(STG_REGISTER, hlua_register_build_options); |