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 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2267 | ret = MAY_LJMP(hlua_socket_info(L, conn->dst)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 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 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2316 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 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 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2463 | |
Willy Tarreau | ca79f59 | 2019-07-17 19:04:47 +0200 | [diff] [blame] | 2464 | if (!sockaddr_alloc(&conn->dst)) { |
| 2465 | xref_unlock(&socket->xref, peer); |
| 2466 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2467 | } |
| 2468 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2469 | memcpy(conn->dst, addr, sizeof(struct sockaddr_storage)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2470 | |
| 2471 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2472 | if (low == 0) { |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2473 | if (conn->dst->ss_family == AF_INET) { |
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 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2478 | ((struct sockaddr_in *)conn->dst)->sin_port = htons(port); |
| 2479 | } else if (conn->dst->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2480 | if (port == -1) { |
| 2481 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2482 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2483 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2484 | ((struct sockaddr_in6 *)conn->dst)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2485 | } |
| 2486 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2487 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2488 | hlua = hlua_gethlua(L); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2489 | appctx = __objt_appctx(s->si[0].end); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2490 | |
| 2491 | /* inform the stream that we want to be notified whenever the |
| 2492 | * connection completes. |
| 2493 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2494 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2495 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2496 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2497 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2498 | hlua->flags |= HLUA_MUST_GC; |
| 2499 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2500 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2501 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2502 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2503 | } |
| 2504 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2505 | |
| 2506 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2507 | /* Return yield waiting for connection. */ |
| 2508 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2509 | 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] | 2510 | |
| 2511 | return 0; |
| 2512 | } |
| 2513 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2514 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2515 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2516 | { |
| 2517 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2518 | struct xref *peer; |
| 2519 | struct appctx *appctx; |
| 2520 | struct stream_interface *si; |
| 2521 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2522 | |
| 2523 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2524 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2525 | |
| 2526 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2527 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2528 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2529 | lua_pushnil(L); |
| 2530 | return 1; |
| 2531 | } |
| 2532 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2533 | si = appctx->owner; |
| 2534 | s = si_strm(si); |
| 2535 | |
| 2536 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2537 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2538 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2539 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2540 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2541 | |
| 2542 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2543 | { |
| 2544 | return 0; |
| 2545 | } |
| 2546 | |
| 2547 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2548 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2549 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2550 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2551 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2552 | struct xref *peer; |
| 2553 | struct appctx *appctx; |
| 2554 | struct stream_interface *si; |
| 2555 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2556 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2557 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2558 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2559 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2560 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2561 | /* convert the timeout to millis */ |
| 2562 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2563 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2564 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2565 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2566 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2567 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2568 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2569 | 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] | 2570 | |
| 2571 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2572 | if (tmout == 0) |
| 2573 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2574 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2575 | /* Check if we run on the same thread than the xreator thread. |
| 2576 | * We cannot access to the socket if the thread is different. |
| 2577 | */ |
| 2578 | if (socket->tid != tid) |
| 2579 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2580 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2581 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2582 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2583 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2584 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2585 | WILL_LJMP(lua_error(L)); |
| 2586 | return 0; |
| 2587 | } |
| 2588 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2589 | si = appctx->owner; |
| 2590 | s = si_strm(si); |
| 2591 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2592 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2593 | s->req.rto = tmout; |
| 2594 | s->req.wto = tmout; |
| 2595 | s->res.rto = tmout; |
| 2596 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2597 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2598 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2599 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2600 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2601 | |
| 2602 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2603 | task_queue(s->task); |
| 2604 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2605 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2607 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2608 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2612 | { |
| 2613 | struct hlua_socket *socket; |
| 2614 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2615 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2616 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2617 | |
| 2618 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2619 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2620 | hlua_pusherror(L, "socket: full stack"); |
| 2621 | goto out_fail_conf; |
| 2622 | } |
| 2623 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2624 | /* Create the object: obj[0] = userdata. */ |
| 2625 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2627 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2628 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2629 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2631 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2632 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2633 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2634 | goto out_fail_conf; |
| 2635 | } |
| 2636 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2637 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2638 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2639 | lua_setmetatable(L, -2); |
| 2640 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2641 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2642 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2643 | if (!appctx) { |
| 2644 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2645 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2646 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2648 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2649 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2650 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2651 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2652 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2653 | /* Now create a session, task and stream for this applet */ |
| 2654 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2655 | if (!sess) { |
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_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2658 | } |
| 2659 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2660 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2661 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2662 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2663 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | } |
| 2665 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2666 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2667 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2668 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2669 | /* Configure "right" stream interface. this "si" is used to connect |
| 2670 | * and retrieve data from the server. The connection is initialized |
| 2671 | * with the "struct server". |
| 2672 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2673 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2674 | |
| 2675 | /* Force destination server. */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2676 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_ADDR_SET | SF_BE_ASSIGNED; |
| 2677 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2678 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2679 | return 1; |
| 2680 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2681 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2682 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2683 | out_fail_sess: |
| 2684 | appctx_free(appctx); |
| 2685 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2686 | WILL_LJMP(lua_error(L)); |
| 2687 | return 0; |
| 2688 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2689 | |
| 2690 | /* |
| 2691 | * |
| 2692 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2693 | * Class Channel |
| 2694 | * |
| 2695 | * |
| 2696 | */ |
| 2697 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2698 | /* This function is called before the Lua execution. It stores |
| 2699 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2700 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2701 | 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] | 2702 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2703 | c->mode = stream->be->mode; |
| 2704 | switch (c->mode) { |
| 2705 | case PR_MODE_HTTP: |
| 2706 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2707 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2708 | c->data.http.state = stream->txn->req.msg_state; |
| 2709 | else |
| 2710 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2711 | break; |
| 2712 | default: |
| 2713 | break; |
| 2714 | } |
| 2715 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2716 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2717 | /* This function is called after the Lua execution. it |
| 2718 | * returns true if the parser state is consistent, otherwise, |
| 2719 | * it return false. |
| 2720 | * |
| 2721 | * In HTTP mode, the parser state must be in the same state |
| 2722 | * or greater when we exit the function. Even if we do a |
| 2723 | * control yield. This prevent to break the HTTP message |
| 2724 | * from the Lua code. |
| 2725 | */ |
| 2726 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2727 | { |
| 2728 | if (c->mode != stream->be->mode) |
| 2729 | return 0; |
| 2730 | |
| 2731 | switch (c->mode) { |
| 2732 | case PR_MODE_HTTP: |
| 2733 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2734 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2735 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2736 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2737 | else |
| 2738 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2739 | default: |
| 2740 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2741 | } |
| 2742 | return 1; |
| 2743 | } |
| 2744 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2745 | /* Returns the struct hlua_channel join to the class channel in the |
| 2746 | * stack entry "ud" or throws an argument error. |
| 2747 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2748 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2749 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2750 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2751 | } |
| 2752 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2753 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2754 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2755 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2756 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2757 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2758 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2759 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2760 | return 0; |
| 2761 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2762 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2763 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2764 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2765 | |
| 2766 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2767 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2768 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2769 | return 1; |
| 2770 | } |
| 2771 | |
| 2772 | /* Duplicate all the data present in the input channel and put it |
| 2773 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2774 | * the stack if the channel is closed and all the data are consumed, |
| 2775 | * returns 0 if no data are available, otherwise it returns the length |
| 2776 | * of the builded string. |
| 2777 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2778 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | { |
| 2780 | char *blk1; |
| 2781 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2782 | size_t len1; |
| 2783 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | int ret; |
| 2785 | luaL_Buffer b; |
| 2786 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2787 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2788 | if (unlikely(ret == 0)) |
| 2789 | return 0; |
| 2790 | |
| 2791 | if (unlikely(ret < 0)) { |
| 2792 | lua_pushnil(L); |
| 2793 | return -1; |
| 2794 | } |
| 2795 | |
| 2796 | luaL_buffinit(L, &b); |
| 2797 | luaL_addlstring(&b, blk1, len1); |
| 2798 | if (unlikely(ret == 2)) |
| 2799 | luaL_addlstring(&b, blk2, len2); |
| 2800 | luaL_pushresult(&b); |
| 2801 | |
| 2802 | if (unlikely(ret == 2)) |
| 2803 | return len1 + len2; |
| 2804 | return len1; |
| 2805 | } |
| 2806 | |
| 2807 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2808 | * a yield. This function keep the data in the buffer. |
| 2809 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2810 | __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] | 2811 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2812 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2813 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2814 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2815 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2816 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2817 | WILL_LJMP(lua_error(L)); |
| 2818 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2820 | 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] | 2821 | return 1; |
| 2822 | } |
| 2823 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2824 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2825 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2826 | { |
| 2827 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2828 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2829 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2830 | } |
| 2831 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2832 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2833 | * a yield. This function consumes the data in the buffer. It returns |
| 2834 | * a string containing the data or a nil pointer if no data are available |
| 2835 | * and the channel is closed. |
| 2836 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2837 | __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] | 2838 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2839 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2840 | int ret; |
| 2841 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2842 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2843 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2844 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2845 | WILL_LJMP(lua_error(L)); |
| 2846 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2847 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2848 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2849 | 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] | 2850 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2851 | if (unlikely(ret == -1)) |
| 2852 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2854 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2855 | return 1; |
| 2856 | } |
| 2857 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2858 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2859 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2860 | { |
| 2861 | MAY_LJMP(check_args(L, 1, "get")); |
| 2862 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2863 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2864 | } |
| 2865 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2866 | /* This functions consumes and returns one line. If the channel is closed, |
| 2867 | * 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] | 2868 | * 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] | 2869 | * value. |
| 2870 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2871 | __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] | 2872 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2873 | char *blk1; |
| 2874 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2875 | size_t len1; |
| 2876 | size_t len2; |
| 2877 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2878 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | int ret; |
| 2880 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2881 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2882 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2883 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2884 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2885 | WILL_LJMP(lua_error(L)); |
| 2886 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2887 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2888 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2889 | 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] | 2890 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2891 | if (ret == -1) { |
| 2892 | lua_pushnil(L); |
| 2893 | return 1; |
| 2894 | } |
| 2895 | |
| 2896 | luaL_buffinit(L, &b); |
| 2897 | luaL_addlstring(&b, blk1, len1); |
| 2898 | len = len1; |
| 2899 | if (unlikely(ret == 2)) { |
| 2900 | luaL_addlstring(&b, blk2, len2); |
| 2901 | len += len2; |
| 2902 | } |
| 2903 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2904 | 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] | 2905 | return 1; |
| 2906 | } |
| 2907 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2908 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2909 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2910 | { |
| 2911 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2912 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2913 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2914 | } |
| 2915 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | /* This function takes a string as input, and append it at the |
| 2917 | * input side of channel. If the data is too big, but a space |
| 2918 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2919 | * yields. If the data is bigger than the buffer, or if the |
| 2920 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2921 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2923 | __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] | 2924 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2925 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2926 | size_t len; |
| 2927 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2928 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2929 | int ret; |
| 2930 | int max; |
| 2931 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2932 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2933 | WILL_LJMP(lua_error(L)); |
| 2934 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2935 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2936 | * the request buffer if its not required. |
| 2937 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2938 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2939 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2940 | 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] | 2941 | } |
| 2942 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2943 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2944 | if (max > len - l) |
| 2945 | max = len - l; |
| 2946 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2947 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2948 | if (ret == -2 || ret == -3) { |
| 2949 | lua_pushinteger(L, -1); |
| 2950 | return 1; |
| 2951 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2952 | if (ret == -1) { |
| 2953 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2954 | 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] | 2955 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | l += ret; |
| 2957 | lua_pop(L, 1); |
| 2958 | lua_pushinteger(L, l); |
| 2959 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2960 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2961 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2962 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2963 | * in this case, we cannot add more data, so we cannot yield, |
| 2964 | * we return the amount of copyied data. |
| 2965 | */ |
| 2966 | return 1; |
| 2967 | } |
| 2968 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2969 | 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] | 2970 | return 1; |
| 2971 | } |
| 2972 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2973 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2974 | * 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] | 2975 | * buffer size is too little for the data. |
| 2976 | */ |
| 2977 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2978 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2979 | size_t len; |
| 2980 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2981 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2982 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2983 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2984 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2985 | lua_pushinteger(L, 0); |
| 2986 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2987 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2988 | } |
| 2989 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2990 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2991 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2992 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | * or -1 if the channel is closed or if the buffer size is too |
| 2994 | * little for the data. |
| 2995 | */ |
| 2996 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2997 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2998 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2999 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3001 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3002 | lua_pushinteger(L, 0); |
| 3003 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3004 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3005 | WILL_LJMP(lua_error(L)); |
| 3006 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3007 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3008 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3009 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3010 | } |
| 3011 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3012 | /* Append data in the output side of the buffer. This data is immediately |
| 3013 | * sent. The function returns the amount of data written. If the buffer |
| 3014 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | * if the channel is closed. |
| 3016 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3017 | __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] | 3018 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3019 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3020 | size_t len; |
| 3021 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3022 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3023 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3024 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3026 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3027 | WILL_LJMP(lua_error(L)); |
| 3028 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3029 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3030 | lua_pushinteger(L, -1); |
| 3031 | return 1; |
| 3032 | } |
| 3033 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3034 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3035 | * the request buffer if its not required. |
| 3036 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3037 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3038 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3039 | 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] | 3040 | } |
| 3041 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3042 | /* The written data will be immediately sent, so we can check |
| 3043 | * the available space without taking in account the reserve. |
| 3044 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3045 | * data, because the buffer will be flushed. |
| 3046 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3047 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3048 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3049 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3050 | * in this case, we cannot add more data, so we cannot yield, |
| 3051 | * we return the amount of copyied data. |
| 3052 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3053 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3054 | return 1; |
| 3055 | |
| 3056 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3057 | if (max > len - l) |
| 3058 | max = len - l; |
| 3059 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3060 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3061 | * detects a non contiguous buffer and realign it. |
| 3062 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3063 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3064 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3065 | |
| 3066 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3067 | 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] | 3068 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3069 | /* buffer replace considers that the input part is filled. |
| 3070 | * so, I must forward these new data in the output part. |
| 3071 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3072 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3073 | |
| 3074 | l += max; |
| 3075 | lua_pop(L, 1); |
| 3076 | lua_pushinteger(L, l); |
| 3077 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3078 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3079 | * in this case, we cannot add more data, so we cannot yield, |
| 3080 | * we return the amount of copyied data. |
| 3081 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3082 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3083 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3084 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3085 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3086 | if (l < len) { |
| 3087 | /* If we are waiting for space in the response buffer, we |
| 3088 | * must set the flag WAKERESWR. This flag required the task |
| 3089 | * wake up if any activity is detected on the response buffer. |
| 3090 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3091 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3092 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3093 | else |
| 3094 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3095 | 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] | 3096 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3097 | |
| 3098 | return 1; |
| 3099 | } |
| 3100 | |
| 3101 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3102 | * yield the LUA process, and resume it without checking the |
| 3103 | * input arguments. |
| 3104 | */ |
| 3105 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3106 | { |
| 3107 | MAY_LJMP(check_args(L, 2, "send")); |
| 3108 | lua_pushinteger(L, 0); |
| 3109 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3110 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /* This function forward and amount of butes. The data pass from |
| 3114 | * the input side of the buffer to the output side, and can be |
| 3115 | * forwarded. This function never fails. |
| 3116 | * |
| 3117 | * The Lua function takes an amount of bytes to be forwarded in |
| 3118 | * imput. It returns the number of bytes forwarded. |
| 3119 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3120 | __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] | 3121 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3122 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3123 | int len; |
| 3124 | int l; |
| 3125 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3126 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3127 | |
| 3128 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3129 | |
| 3130 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3131 | WILL_LJMP(lua_error(L)); |
| 3132 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3133 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3134 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3135 | |
| 3136 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3137 | if (max > ci_data(chn)) |
| 3138 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3139 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3140 | l += max; |
| 3141 | |
| 3142 | lua_pop(L, 1); |
| 3143 | lua_pushinteger(L, l); |
| 3144 | |
| 3145 | /* Check if it miss bytes to forward. */ |
| 3146 | if (l < len) { |
| 3147 | /* The the input channel or the output channel are closed, we |
| 3148 | * must return the amount of data forwarded. |
| 3149 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3150 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3151 | return 1; |
| 3152 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3153 | /* If we are waiting for space data in the response buffer, we |
| 3154 | * must set the flag WAKERESWR. This flag required the task |
| 3155 | * wake up if any activity is detected on the response buffer. |
| 3156 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3157 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3158 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3159 | else |
| 3160 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3161 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3162 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3163 | 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] | 3164 | } |
| 3165 | |
| 3166 | return 1; |
| 3167 | } |
| 3168 | |
| 3169 | /* Just check the input and prepare the stack for the previous |
| 3170 | * function "hlua_channel_forward_yield" |
| 3171 | */ |
| 3172 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3173 | { |
| 3174 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3175 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3176 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3177 | |
| 3178 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3179 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3180 | } |
| 3181 | |
| 3182 | /* Just returns the number of bytes available in the input |
| 3183 | * side of the buffer. This function never fails. |
| 3184 | */ |
| 3185 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3186 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3187 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3188 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3189 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3190 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3191 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3192 | struct htx *htx = htxbuf(&chn->buf); |
| 3193 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3194 | } |
| 3195 | else |
| 3196 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3197 | return 1; |
| 3198 | } |
| 3199 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3200 | /* Returns true if the channel is full. */ |
| 3201 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3202 | { |
| 3203 | struct channel *chn; |
| 3204 | int rem; |
| 3205 | |
| 3206 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3207 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3208 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3209 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3210 | struct htx *htx = htxbuf(&chn->buf); |
| 3211 | |
| 3212 | rem = htx_free_data_space(htx); |
| 3213 | } |
| 3214 | else |
| 3215 | rem = b_room(&chn->buf); |
| 3216 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3217 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3218 | |
| 3219 | lua_pushboolean(L, rem <= 0); |
| 3220 | return 1; |
| 3221 | } |
| 3222 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3223 | /* Just returns the number of bytes available in the output |
| 3224 | * side of the buffer. This function never fails. |
| 3225 | */ |
| 3226 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3227 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3228 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3229 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3230 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3231 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3232 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3233 | return 1; |
| 3234 | } |
| 3235 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3236 | /* |
| 3237 | * |
| 3238 | * |
| 3239 | * Class Fetches |
| 3240 | * |
| 3241 | * |
| 3242 | */ |
| 3243 | |
| 3244 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3245 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3246 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3247 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3248 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3249 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3250 | } |
| 3251 | |
| 3252 | /* This function creates and push in the stack a fetch object according |
| 3253 | * with a current TXN. |
| 3254 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3255 | 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] | 3256 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3257 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3258 | |
| 3259 | /* Check stack size. */ |
| 3260 | if (!lua_checkstack(L, 3)) |
| 3261 | return 0; |
| 3262 | |
| 3263 | /* Create the object: obj[0] = userdata. |
| 3264 | * Note that the base of the Fetches object is the |
| 3265 | * transaction object. |
| 3266 | */ |
| 3267 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3268 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3269 | lua_rawseti(L, -2, 0); |
| 3270 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3271 | hsmp->s = txn->s; |
| 3272 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3273 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3274 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3275 | |
| 3276 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3277 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3278 | lua_setmetatable(L, -2); |
| 3279 | |
| 3280 | return 1; |
| 3281 | } |
| 3282 | |
| 3283 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3284 | * It uses closure argument to store the associated sample-fetch. It |
| 3285 | * returns only one argument or throws an error. An error is thrown |
| 3286 | * only if an error is encountered during the argument parsing. If |
| 3287 | * the "sample-fetch" function fails, nil is returned. |
| 3288 | */ |
| 3289 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3290 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3291 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3292 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3293 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3294 | int i; |
| 3295 | struct sample smp; |
| 3296 | |
| 3297 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3298 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3299 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3300 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3301 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3302 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3303 | /* Check execution authorization. */ |
| 3304 | if (f->use & SMP_USE_HTTP_ANY && |
| 3305 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3306 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3307 | "is not available in Lua services", f->kw); |
| 3308 | WILL_LJMP(lua_error(L)); |
| 3309 | } |
| 3310 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3311 | /* Get extra arguments. */ |
| 3312 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3313 | if (i >= ARGM_NBARGS) |
| 3314 | break; |
| 3315 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3316 | } |
| 3317 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3318 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3319 | |
| 3320 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3321 | 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] | 3322 | |
| 3323 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3324 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3325 | lua_pushfstring(L, "error in arguments"); |
| 3326 | WILL_LJMP(lua_error(L)); |
| 3327 | } |
| 3328 | |
| 3329 | /* Initialise the sample. */ |
| 3330 | memset(&smp, 0, sizeof(smp)); |
| 3331 | |
| 3332 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3333 | 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] | 3334 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3335 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3336 | lua_pushstring(L, ""); |
| 3337 | else |
| 3338 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3339 | return 1; |
| 3340 | } |
| 3341 | |
| 3342 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3343 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3344 | hlua_smp2lua_str(L, &smp); |
| 3345 | else |
| 3346 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3347 | return 1; |
| 3348 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3349 | |
| 3350 | /* |
| 3351 | * |
| 3352 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3353 | * Class Converters |
| 3354 | * |
| 3355 | * |
| 3356 | */ |
| 3357 | |
| 3358 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3359 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3360 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3361 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3362 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3363 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3364 | } |
| 3365 | |
| 3366 | /* This function creates and push in the stack a Converters object |
| 3367 | * according with a current TXN. |
| 3368 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3369 | 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] | 3370 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3371 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3372 | |
| 3373 | /* Check stack size. */ |
| 3374 | if (!lua_checkstack(L, 3)) |
| 3375 | return 0; |
| 3376 | |
| 3377 | /* Create the object: obj[0] = userdata. |
| 3378 | * Note that the base of the Converters object is the |
| 3379 | * same than the TXN object. |
| 3380 | */ |
| 3381 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3382 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3383 | lua_rawseti(L, -2, 0); |
| 3384 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3385 | hsmp->s = txn->s; |
| 3386 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3387 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3388 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3389 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3390 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3391 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3392 | lua_setmetatable(L, -2); |
| 3393 | |
| 3394 | return 1; |
| 3395 | } |
| 3396 | |
| 3397 | /* This function is an LUA binding. It is called with each converter. |
| 3398 | * It uses closure argument to store the associated converter. It |
| 3399 | * returns only one argument or throws an error. An error is thrown |
| 3400 | * only if an error is encountered during the argument parsing. If |
| 3401 | * the converter function function fails, nil is returned. |
| 3402 | */ |
| 3403 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3404 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3405 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3406 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3407 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3408 | int i; |
| 3409 | struct sample smp; |
| 3410 | |
| 3411 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3412 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3413 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3414 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3415 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3416 | |
| 3417 | /* Get extra arguments. */ |
| 3418 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3419 | if (i >= ARGM_NBARGS) |
| 3420 | break; |
| 3421 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3422 | } |
| 3423 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3424 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3425 | |
| 3426 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3427 | 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] | 3428 | |
| 3429 | /* Run the special args checker. */ |
| 3430 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3431 | hlua_pusherror(L, "error in arguments"); |
| 3432 | WILL_LJMP(lua_error(L)); |
| 3433 | } |
| 3434 | |
| 3435 | /* Initialise the sample. */ |
| 3436 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3437 | hlua_pusherror(L, "error in the input argument"); |
| 3438 | WILL_LJMP(lua_error(L)); |
| 3439 | } |
| 3440 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3441 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3442 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3443 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3444 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3445 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3446 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3447 | WILL_LJMP(lua_error(L)); |
| 3448 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3449 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3450 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3451 | hlua_pusherror(L, "error during the input argument casting"); |
| 3452 | WILL_LJMP(lua_error(L)); |
| 3453 | } |
| 3454 | |
| 3455 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3456 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3457 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3458 | lua_pushstring(L, ""); |
| 3459 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3460 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3461 | return 1; |
| 3462 | } |
| 3463 | |
| 3464 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3465 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3466 | hlua_smp2lua_str(L, &smp); |
| 3467 | else |
| 3468 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3469 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3470 | } |
| 3471 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3472 | /* |
| 3473 | * |
| 3474 | * |
| 3475 | * Class AppletTCP |
| 3476 | * |
| 3477 | * |
| 3478 | */ |
| 3479 | |
| 3480 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3481 | * a class stream, otherwise it throws an error. |
| 3482 | */ |
| 3483 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3484 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3485 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3486 | } |
| 3487 | |
| 3488 | /* This function creates and push in the stack an Applet object |
| 3489 | * according with a current TXN. |
| 3490 | */ |
| 3491 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3492 | { |
| 3493 | struct hlua_appctx *appctx; |
| 3494 | struct stream_interface *si = ctx->owner; |
| 3495 | struct stream *s = si_strm(si); |
| 3496 | struct proxy *p = s->be; |
| 3497 | |
| 3498 | /* Check stack size. */ |
| 3499 | if (!lua_checkstack(L, 3)) |
| 3500 | return 0; |
| 3501 | |
| 3502 | /* Create the object: obj[0] = userdata. |
| 3503 | * Note that the base of the Converters object is the |
| 3504 | * same than the TXN object. |
| 3505 | */ |
| 3506 | lua_newtable(L); |
| 3507 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3508 | lua_rawseti(L, -2, 0); |
| 3509 | appctx->appctx = ctx; |
| 3510 | appctx->htxn.s = s; |
| 3511 | appctx->htxn.p = p; |
| 3512 | |
| 3513 | /* Create the "f" field that contains a list of fetches. */ |
| 3514 | lua_pushstring(L, "f"); |
| 3515 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3516 | return 0; |
| 3517 | lua_settable(L, -3); |
| 3518 | |
| 3519 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3520 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3521 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3522 | return 0; |
| 3523 | lua_settable(L, -3); |
| 3524 | |
| 3525 | /* Create the "c" field that contains a list of converters. */ |
| 3526 | lua_pushstring(L, "c"); |
| 3527 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3528 | return 0; |
| 3529 | lua_settable(L, -3); |
| 3530 | |
| 3531 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3532 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3533 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3534 | return 0; |
| 3535 | lua_settable(L, -3); |
| 3536 | |
| 3537 | /* Pop a class stream metatable and affect it to the table. */ |
| 3538 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3539 | lua_setmetatable(L, -2); |
| 3540 | |
| 3541 | return 1; |
| 3542 | } |
| 3543 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3544 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3545 | { |
| 3546 | struct hlua_appctx *appctx; |
| 3547 | struct stream *s; |
| 3548 | const char *name; |
| 3549 | size_t len; |
| 3550 | struct sample smp; |
| 3551 | |
| 3552 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3553 | |
| 3554 | /* It is useles to retrieve the stream, but this function |
| 3555 | * runs only in a stream context. |
| 3556 | */ |
| 3557 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3558 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3559 | s = appctx->htxn.s; |
| 3560 | |
| 3561 | /* Converts the third argument in a sample. */ |
| 3562 | hlua_lua2smp(L, 3, &smp); |
| 3563 | |
| 3564 | /* Store the sample in a variable. */ |
| 3565 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3566 | vars_set_by_name(name, len, &smp); |
| 3567 | return 0; |
| 3568 | } |
| 3569 | |
| 3570 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3571 | { |
| 3572 | struct hlua_appctx *appctx; |
| 3573 | struct stream *s; |
| 3574 | const char *name; |
| 3575 | size_t len; |
| 3576 | struct sample smp; |
| 3577 | |
| 3578 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3579 | |
| 3580 | /* It is useles to retrieve the stream, but this function |
| 3581 | * runs only in a stream context. |
| 3582 | */ |
| 3583 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3584 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3585 | s = appctx->htxn.s; |
| 3586 | |
| 3587 | /* Unset the variable. */ |
| 3588 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3589 | vars_unset_by_name(name, len, &smp); |
| 3590 | return 0; |
| 3591 | } |
| 3592 | |
| 3593 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3594 | { |
| 3595 | struct hlua_appctx *appctx; |
| 3596 | struct stream *s; |
| 3597 | const char *name; |
| 3598 | size_t len; |
| 3599 | struct sample smp; |
| 3600 | |
| 3601 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3602 | |
| 3603 | /* It is useles to retrieve the stream, but this function |
| 3604 | * runs only in a stream context. |
| 3605 | */ |
| 3606 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3607 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3608 | s = appctx->htxn.s; |
| 3609 | |
| 3610 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3611 | if (!vars_get_by_name(name, len, &smp)) { |
| 3612 | lua_pushnil(L); |
| 3613 | return 1; |
| 3614 | } |
| 3615 | |
| 3616 | return hlua_smp2lua(L, &smp); |
| 3617 | } |
| 3618 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3619 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3620 | { |
| 3621 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3622 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3623 | struct hlua *hlua; |
| 3624 | |
| 3625 | /* 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] | 3626 | if (!s->hlua) |
| 3627 | return 0; |
| 3628 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3629 | |
| 3630 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3631 | |
| 3632 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3633 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3634 | |
| 3635 | /* Get and store new value. */ |
| 3636 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3637 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3638 | |
| 3639 | return 0; |
| 3640 | } |
| 3641 | |
| 3642 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3643 | { |
| 3644 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3645 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3646 | struct hlua *hlua; |
| 3647 | |
| 3648 | /* 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] | 3649 | if (!s->hlua) { |
| 3650 | lua_pushnil(L); |
| 3651 | return 1; |
| 3652 | } |
| 3653 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3654 | |
| 3655 | /* Push configuration index in the stack. */ |
| 3656 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3657 | |
| 3658 | return 1; |
| 3659 | } |
| 3660 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3661 | /* If expected data not yet available, it returns a yield. This function |
| 3662 | * consumes the data in the buffer. It returns a string containing the |
| 3663 | * data. This string can be empty. |
| 3664 | */ |
| 3665 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3666 | { |
| 3667 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3668 | struct stream_interface *si = appctx->appctx->owner; |
| 3669 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3670 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3671 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3672 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3673 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3674 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3675 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3676 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3677 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3678 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3679 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3680 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3681 | 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] | 3682 | } |
| 3683 | |
| 3684 | /* End of data: commit the total strings and return. */ |
| 3685 | if (ret < 0) { |
| 3686 | luaL_pushresult(&appctx->b); |
| 3687 | return 1; |
| 3688 | } |
| 3689 | |
| 3690 | /* Ensure that the block 2 length is usable. */ |
| 3691 | if (ret == 1) |
| 3692 | len2 = 0; |
| 3693 | |
| 3694 | /* dont check the max length read and dont check. */ |
| 3695 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3696 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3697 | |
| 3698 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3699 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | luaL_pushresult(&appctx->b); |
| 3701 | return 1; |
| 3702 | } |
| 3703 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3704 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3705 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3706 | { |
| 3707 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3708 | |
| 3709 | /* Initialise the string catenation. */ |
| 3710 | luaL_buffinit(L, &appctx->b); |
| 3711 | |
| 3712 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3713 | } |
| 3714 | |
| 3715 | /* If expected data not yet available, it returns a yield. This function |
| 3716 | * consumes the data in the buffer. It returns a string containing the |
| 3717 | * data. This string can be empty. |
| 3718 | */ |
| 3719 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3720 | { |
| 3721 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3722 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3723 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3724 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3725 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3726 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3727 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3728 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3729 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3730 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3731 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3732 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3733 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3734 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3735 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3736 | 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] | 3737 | } |
| 3738 | |
| 3739 | /* End of data: commit the total strings and return. */ |
| 3740 | if (ret < 0) { |
| 3741 | luaL_pushresult(&appctx->b); |
| 3742 | return 1; |
| 3743 | } |
| 3744 | |
| 3745 | /* Ensure that the block 2 length is usable. */ |
| 3746 | if (ret == 1) |
| 3747 | len2 = 0; |
| 3748 | |
| 3749 | if (len == -1) { |
| 3750 | |
| 3751 | /* If len == -1, catenate all the data avalaile and |
| 3752 | * yield because we want to get all the data until |
| 3753 | * the end of data stream. |
| 3754 | */ |
| 3755 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3756 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3757 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3758 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3759 | 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] | 3760 | |
| 3761 | } else { |
| 3762 | |
| 3763 | /* Copy the fisrt block caping to the length required. */ |
| 3764 | if (len1 > len) |
| 3765 | len1 = len; |
| 3766 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3767 | len -= len1; |
| 3768 | |
| 3769 | /* Copy the second block. */ |
| 3770 | if (len2 > len) |
| 3771 | len2 = len; |
| 3772 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3773 | len -= len2; |
| 3774 | |
| 3775 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3776 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3777 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3778 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3779 | if (len > 0) { |
| 3780 | lua_pushinteger(L, len); |
| 3781 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3782 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3783 | 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] | 3784 | } |
| 3785 | |
| 3786 | /* return the result. */ |
| 3787 | luaL_pushresult(&appctx->b); |
| 3788 | return 1; |
| 3789 | } |
| 3790 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3791 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3792 | hlua_pusherror(L, "Lua: internal error"); |
| 3793 | WILL_LJMP(lua_error(L)); |
| 3794 | return 0; |
| 3795 | } |
| 3796 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3797 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3798 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3799 | { |
| 3800 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3801 | int len = -1; |
| 3802 | |
| 3803 | if (lua_gettop(L) > 2) |
| 3804 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3805 | if (lua_gettop(L) >= 2) { |
| 3806 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3807 | lua_pop(L, 1); |
| 3808 | } |
| 3809 | |
| 3810 | /* Confirm or set the required length */ |
| 3811 | lua_pushinteger(L, len); |
| 3812 | |
| 3813 | /* Initialise the string catenation. */ |
| 3814 | luaL_buffinit(L, &appctx->b); |
| 3815 | |
| 3816 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3817 | } |
| 3818 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3819 | /* Append data in the output side of the buffer. This data is immediately |
| 3820 | * sent. The function returns the amount of data written. If the buffer |
| 3821 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3822 | * if the channel is closed. |
| 3823 | */ |
| 3824 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3825 | { |
| 3826 | size_t len; |
| 3827 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3828 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3829 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3830 | struct stream_interface *si = appctx->appctx->owner; |
| 3831 | struct channel *chn = si_ic(si); |
| 3832 | int max; |
| 3833 | |
| 3834 | /* Get the max amount of data which can write as input in the channel. */ |
| 3835 | max = channel_recv_max(chn); |
| 3836 | if (max > (len - l)) |
| 3837 | max = len - l; |
| 3838 | |
| 3839 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3840 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3841 | |
| 3842 | /* update counters. */ |
| 3843 | l += max; |
| 3844 | lua_pop(L, 1); |
| 3845 | lua_pushinteger(L, l); |
| 3846 | |
| 3847 | /* If some data is not send, declares the situation to the |
| 3848 | * applet, and returns a yield. |
| 3849 | */ |
| 3850 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3851 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3852 | 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] | 3853 | } |
| 3854 | |
| 3855 | return 1; |
| 3856 | } |
| 3857 | |
| 3858 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3859 | * yield the LUA process, and resume it without checking the |
| 3860 | * input arguments. |
| 3861 | */ |
| 3862 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3863 | { |
| 3864 | MAY_LJMP(check_args(L, 2, "send")); |
| 3865 | lua_pushinteger(L, 0); |
| 3866 | |
| 3867 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3868 | } |
| 3869 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3870 | /* |
| 3871 | * |
| 3872 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3873 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3874 | * |
| 3875 | * |
| 3876 | */ |
| 3877 | |
| 3878 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3879 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3880 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3881 | __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] | 3882 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3883 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3884 | } |
| 3885 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3886 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3887 | * according with a current TXN. |
| 3888 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3889 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3890 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3891 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3892 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3893 | struct stream_interface *si = ctx->owner; |
| 3894 | struct stream *s = si_strm(si); |
| 3895 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3896 | struct htx *htx; |
| 3897 | struct htx_blk *blk; |
| 3898 | struct htx_sl *sl; |
| 3899 | struct ist path; |
| 3900 | unsigned long long len = 0; |
| 3901 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3902 | |
| 3903 | /* Check stack size. */ |
| 3904 | if (!lua_checkstack(L, 3)) |
| 3905 | return 0; |
| 3906 | |
| 3907 | /* Create the object: obj[0] = userdata. |
| 3908 | * Note that the base of the Converters object is the |
| 3909 | * same than the TXN object. |
| 3910 | */ |
| 3911 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3912 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3913 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3914 | appctx->appctx = ctx; |
| 3915 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3916 | 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] | 3917 | appctx->htxn.s = s; |
| 3918 | appctx->htxn.p = px; |
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 "f" field that contains a list of fetches. */ |
| 3921 | lua_pushstring(L, "f"); |
| 3922 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 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 "sf" field that contains a list of stringsafe fetches. */ |
| 3927 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3928 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 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 "c" field that contains a list of converters. */ |
| 3933 | lua_pushstring(L, "c"); |
| 3934 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3935 | return 0; |
| 3936 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3937 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3938 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3939 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3940 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3941 | return 0; |
| 3942 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3943 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3944 | htx = htxbuf(&s->req.buf); |
| 3945 | blk = htx_get_first_blk(htx); |
| 3946 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 3947 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3948 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3949 | /* Stores the request method. */ |
| 3950 | lua_pushstring(L, "method"); |
| 3951 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3952 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3953 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3954 | /* Stores the http version. */ |
| 3955 | lua_pushstring(L, "version"); |
| 3956 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3957 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3958 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3959 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3960 | * the array on the top of the stack. |
| 3961 | */ |
| 3962 | lua_pushstring(L, "headers"); |
| 3963 | htxn.s = s; |
| 3964 | htxn.p = px; |
| 3965 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3966 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3967 | return 0; |
| 3968 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3969 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3970 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3971 | if (path.ptr) { |
| 3972 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3973 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3974 | p = path.ptr; |
| 3975 | end = path.ptr + path.len; |
| 3976 | q = p; |
| 3977 | while (q < end && *q != '?') |
| 3978 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3979 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3980 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3981 | lua_pushstring(L, "path"); |
| 3982 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3983 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3984 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3985 | /* Stores the query string. */ |
| 3986 | lua_pushstring(L, "qs"); |
| 3987 | if (*q == '?') |
| 3988 | q++; |
| 3989 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3990 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3991 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3992 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3993 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3994 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3995 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3996 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3997 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3998 | break; |
| 3999 | if (type == HTX_BLK_DATA) |
| 4000 | len += htx_get_blksz(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4001 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4002 | if (htx->extra != ULLONG_MAX) |
| 4003 | len += htx->extra; |
| 4004 | |
| 4005 | /* Stores the request path. */ |
| 4006 | lua_pushstring(L, "length"); |
| 4007 | lua_pushinteger(L, len); |
| 4008 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4009 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4010 | /* Create an empty array of HTTP request headers. */ |
| 4011 | lua_pushstring(L, "response"); |
| 4012 | lua_newtable(L); |
| 4013 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 4014 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4015 | /* Pop a class stream metatable and affect it to the table. */ |
| 4016 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 4017 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4018 | |
| 4019 | return 1; |
| 4020 | } |
| 4021 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4022 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 4023 | { |
| 4024 | struct hlua_appctx *appctx; |
| 4025 | struct stream *s; |
| 4026 | const char *name; |
| 4027 | size_t len; |
| 4028 | struct sample smp; |
| 4029 | |
| 4030 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 4031 | |
| 4032 | /* It is useles to retrieve the stream, but this function |
| 4033 | * runs only in a stream context. |
| 4034 | */ |
| 4035 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4036 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4037 | s = appctx->htxn.s; |
| 4038 | |
| 4039 | /* Converts the third argument in a sample. */ |
| 4040 | hlua_lua2smp(L, 3, &smp); |
| 4041 | |
| 4042 | /* Store the sample in a variable. */ |
| 4043 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4044 | vars_set_by_name(name, len, &smp); |
| 4045 | return 0; |
| 4046 | } |
| 4047 | |
| 4048 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4049 | { |
| 4050 | struct hlua_appctx *appctx; |
| 4051 | struct stream *s; |
| 4052 | const char *name; |
| 4053 | size_t len; |
| 4054 | struct sample smp; |
| 4055 | |
| 4056 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4057 | |
| 4058 | /* It is useles to retrieve the stream, but this function |
| 4059 | * runs only in a stream context. |
| 4060 | */ |
| 4061 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4062 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4063 | s = appctx->htxn.s; |
| 4064 | |
| 4065 | /* Unset the variable. */ |
| 4066 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4067 | vars_unset_by_name(name, len, &smp); |
| 4068 | return 0; |
| 4069 | } |
| 4070 | |
| 4071 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4072 | { |
| 4073 | struct hlua_appctx *appctx; |
| 4074 | struct stream *s; |
| 4075 | const char *name; |
| 4076 | size_t len; |
| 4077 | struct sample smp; |
| 4078 | |
| 4079 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4080 | |
| 4081 | /* It is useles to retrieve the stream, but this function |
| 4082 | * runs only in a stream context. |
| 4083 | */ |
| 4084 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4085 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4086 | s = appctx->htxn.s; |
| 4087 | |
| 4088 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4089 | if (!vars_get_by_name(name, len, &smp)) { |
| 4090 | lua_pushnil(L); |
| 4091 | return 1; |
| 4092 | } |
| 4093 | |
| 4094 | return hlua_smp2lua(L, &smp); |
| 4095 | } |
| 4096 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4097 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4098 | { |
| 4099 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4100 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4101 | struct hlua *hlua; |
| 4102 | |
| 4103 | /* 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] | 4104 | if (!s->hlua) |
| 4105 | return 0; |
| 4106 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4107 | |
| 4108 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4109 | |
| 4110 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4111 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4112 | |
| 4113 | /* Get and store new value. */ |
| 4114 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4115 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4116 | |
| 4117 | return 0; |
| 4118 | } |
| 4119 | |
| 4120 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4121 | { |
| 4122 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4123 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4124 | struct hlua *hlua; |
| 4125 | |
| 4126 | /* 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] | 4127 | if (!s->hlua) { |
| 4128 | lua_pushnil(L); |
| 4129 | return 1; |
| 4130 | } |
| 4131 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4132 | |
| 4133 | /* Push configuration index in the stack. */ |
| 4134 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4135 | |
| 4136 | return 1; |
| 4137 | } |
| 4138 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4139 | /* If expected data not yet available, it returns a yield. This function |
| 4140 | * consumes the data in the buffer. It returns a string containing the |
| 4141 | * data. This string can be empty. |
| 4142 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4143 | __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] | 4144 | { |
| 4145 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4146 | struct stream_interface *si = appctx->appctx->owner; |
| 4147 | struct channel *req = si_oc(si); |
| 4148 | struct htx *htx; |
| 4149 | struct htx_blk *blk; |
| 4150 | size_t count; |
| 4151 | int stop = 0; |
| 4152 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4153 | htx = htx_from_buf(&req->buf); |
| 4154 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4155 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4156 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4157 | while (count && !stop && blk) { |
| 4158 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4159 | uint32_t sz = htx_get_blksz(blk); |
| 4160 | struct ist v; |
| 4161 | uint32_t vlen; |
| 4162 | char *nl; |
| 4163 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4164 | if (type == HTX_BLK_EOM) { |
| 4165 | stop = 1; |
| 4166 | break; |
| 4167 | } |
| 4168 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4169 | vlen = sz; |
| 4170 | if (vlen > count) { |
| 4171 | if (type != HTX_BLK_DATA) |
| 4172 | break; |
| 4173 | vlen = count; |
| 4174 | } |
| 4175 | |
| 4176 | switch (type) { |
| 4177 | case HTX_BLK_UNUSED: |
| 4178 | break; |
| 4179 | |
| 4180 | case HTX_BLK_DATA: |
| 4181 | v = htx_get_blk_value(htx, blk); |
| 4182 | v.len = vlen; |
| 4183 | nl = istchr(v, '\n'); |
| 4184 | if (nl != NULL) { |
| 4185 | stop = 1; |
| 4186 | vlen = nl - v.ptr + 1; |
| 4187 | } |
| 4188 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4189 | break; |
| 4190 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4191 | case HTX_BLK_TLR: |
| 4192 | case HTX_BLK_EOM: |
| 4193 | stop = 1; |
| 4194 | break; |
| 4195 | |
| 4196 | default: |
| 4197 | break; |
| 4198 | } |
| 4199 | |
| 4200 | co_set_data(req, co_data(req) - vlen); |
| 4201 | count -= vlen; |
| 4202 | if (sz == vlen) |
| 4203 | blk = htx_remove_blk(htx, blk); |
| 4204 | else { |
| 4205 | htx_cut_data_blk(htx, blk, vlen); |
| 4206 | break; |
| 4207 | } |
| 4208 | } |
| 4209 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4210 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4211 | if (!stop) { |
| 4212 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4213 | 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] | 4214 | } |
| 4215 | |
| 4216 | /* return the result. */ |
| 4217 | luaL_pushresult(&appctx->b); |
| 4218 | return 1; |
| 4219 | } |
| 4220 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4221 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4222 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4223 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4224 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4225 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4226 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4227 | /* Initialise the string catenation. */ |
| 4228 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4229 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4230 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4231 | } |
| 4232 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4233 | /* If expected data not yet available, it returns a yield. This function |
| 4234 | * consumes the data in the buffer. It returns a string containing the |
| 4235 | * data. This string can be empty. |
| 4236 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4237 | __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] | 4238 | { |
| 4239 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4240 | struct stream_interface *si = appctx->appctx->owner; |
| 4241 | struct channel *req = si_oc(si); |
| 4242 | struct htx *htx; |
| 4243 | struct htx_blk *blk; |
| 4244 | size_t count; |
| 4245 | int len; |
| 4246 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4247 | htx = htx_from_buf(&req->buf); |
| 4248 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4249 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4250 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4251 | while (count && len && blk) { |
| 4252 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4253 | uint32_t sz = htx_get_blksz(blk); |
| 4254 | struct ist v; |
| 4255 | uint32_t vlen; |
| 4256 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4257 | if (type == HTX_BLK_EOM) { |
| 4258 | len = 0; |
| 4259 | break; |
| 4260 | } |
| 4261 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4262 | vlen = sz; |
| 4263 | if (len > 0 && vlen > len) |
| 4264 | vlen = len; |
| 4265 | if (vlen > count) { |
| 4266 | if (type != HTX_BLK_DATA) |
| 4267 | break; |
| 4268 | vlen = count; |
| 4269 | } |
| 4270 | |
| 4271 | switch (type) { |
| 4272 | case HTX_BLK_UNUSED: |
| 4273 | break; |
| 4274 | |
| 4275 | case HTX_BLK_DATA: |
| 4276 | v = htx_get_blk_value(htx, blk); |
| 4277 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4278 | break; |
| 4279 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4280 | case HTX_BLK_TLR: |
| 4281 | case HTX_BLK_EOM: |
| 4282 | len = 0; |
| 4283 | break; |
| 4284 | |
| 4285 | default: |
| 4286 | break; |
| 4287 | } |
| 4288 | |
| 4289 | co_set_data(req, co_data(req) - vlen); |
| 4290 | count -= vlen; |
| 4291 | if (len > 0) |
| 4292 | len -= vlen; |
| 4293 | if (sz == vlen) |
| 4294 | blk = htx_remove_blk(htx, blk); |
| 4295 | else { |
| 4296 | htx_cut_data_blk(htx, blk, vlen); |
| 4297 | break; |
| 4298 | } |
| 4299 | } |
| 4300 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4301 | htx_to_buf(htx, &req->buf); |
| 4302 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4303 | /* If we are no other data available, yield waiting for new data. */ |
| 4304 | if (len) { |
| 4305 | if (len > 0) { |
| 4306 | lua_pushinteger(L, len); |
| 4307 | lua_replace(L, 2); |
| 4308 | } |
| 4309 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4310 | 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] | 4311 | } |
| 4312 | |
| 4313 | /* return the result. */ |
| 4314 | luaL_pushresult(&appctx->b); |
| 4315 | return 1; |
| 4316 | } |
| 4317 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4318 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4319 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4320 | { |
| 4321 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4322 | int len = -1; |
| 4323 | |
| 4324 | /* Check arguments. */ |
| 4325 | if (lua_gettop(L) > 2) |
| 4326 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4327 | if (lua_gettop(L) >= 2) { |
| 4328 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4329 | lua_pop(L, 1); |
| 4330 | } |
| 4331 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4332 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4333 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4334 | /* Initialise the string catenation. */ |
| 4335 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4336 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4337 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4338 | } |
| 4339 | |
| 4340 | /* Append data in the output side of the buffer. This data is immediately |
| 4341 | * sent. The function returns the amount of data written. If the buffer |
| 4342 | * cannot contain the data, the function yields. The function returns -1 |
| 4343 | * if the channel is closed. |
| 4344 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4345 | __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] | 4346 | { |
| 4347 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4348 | struct stream_interface *si = appctx->appctx->owner; |
| 4349 | struct channel *res = si_ic(si); |
| 4350 | struct htx *htx = htx_from_buf(&res->buf); |
| 4351 | const char *data; |
| 4352 | size_t len; |
| 4353 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4354 | int max; |
| 4355 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4356 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4357 | if (!max) |
| 4358 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4359 | |
| 4360 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4361 | |
| 4362 | /* Get the max amount of data which can write as input in the channel. */ |
| 4363 | if (max > (len - l)) |
| 4364 | max = len - l; |
| 4365 | |
| 4366 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4367 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4368 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4369 | |
| 4370 | /* update counters. */ |
| 4371 | l += max; |
| 4372 | lua_pop(L, 1); |
| 4373 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4374 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4375 | /* If some data is not send, declares the situation to the |
| 4376 | * applet, and returns a yield. |
| 4377 | */ |
| 4378 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4379 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4380 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4381 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4382 | 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] | 4383 | } |
| 4384 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4385 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4386 | return 1; |
| 4387 | } |
| 4388 | |
| 4389 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4390 | * yield the LUA process, and resume it without checking the |
| 4391 | * input arguments. |
| 4392 | */ |
| 4393 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4394 | { |
| 4395 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4396 | |
| 4397 | /* We want to send some data. Headers must be sent. */ |
| 4398 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4399 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4400 | WILL_LJMP(lua_error(L)); |
| 4401 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4402 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4403 | /* This interger is used for followinf the amount of data sent. */ |
| 4404 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4405 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4406 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4407 | } |
| 4408 | |
| 4409 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4410 | { |
| 4411 | const char *name; |
| 4412 | int ret; |
| 4413 | |
| 4414 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4415 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4416 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4417 | |
| 4418 | /* Push in the stack the "response" entry. */ |
| 4419 | ret = lua_getfield(L, 1, "response"); |
| 4420 | if (ret != LUA_TTABLE) { |
| 4421 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4422 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4423 | WILL_LJMP(lua_error(L)); |
| 4424 | } |
| 4425 | |
| 4426 | /* check if the header is already registered if it is not |
| 4427 | * the case, register it. |
| 4428 | */ |
| 4429 | ret = lua_getfield(L, -1, name); |
| 4430 | if (ret == LUA_TNIL) { |
| 4431 | |
| 4432 | /* Entry not found. */ |
| 4433 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4434 | |
| 4435 | /* Insert the new header name in the array in the top of the stack. |
| 4436 | * It left the new array in the top of the stack. |
| 4437 | */ |
| 4438 | lua_newtable(L); |
| 4439 | lua_pushvalue(L, 2); |
| 4440 | lua_pushvalue(L, -2); |
| 4441 | lua_settable(L, -4); |
| 4442 | |
| 4443 | } else if (ret != LUA_TTABLE) { |
| 4444 | |
| 4445 | /* corruption error. */ |
| 4446 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4447 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4448 | WILL_LJMP(lua_error(L)); |
| 4449 | } |
| 4450 | |
| 4451 | /* Now the top od thestack is an array of values. We push |
| 4452 | * the header value as new entry. |
| 4453 | */ |
| 4454 | lua_pushvalue(L, 3); |
| 4455 | ret = lua_rawlen(L, -2); |
| 4456 | lua_rawseti(L, -2, ret + 1); |
| 4457 | lua_pushboolean(L, 1); |
| 4458 | return 1; |
| 4459 | } |
| 4460 | |
| 4461 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4462 | { |
| 4463 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4464 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4465 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4466 | |
| 4467 | if (status < 100 || status > 599) { |
| 4468 | lua_pushboolean(L, 0); |
| 4469 | return 1; |
| 4470 | } |
| 4471 | |
| 4472 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4473 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4474 | lua_pushboolean(L, 1); |
| 4475 | return 1; |
| 4476 | } |
| 4477 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4478 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4479 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4480 | { |
| 4481 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4482 | struct stream_interface *si = appctx->appctx->owner; |
| 4483 | struct channel *res = si_ic(si); |
| 4484 | struct htx *htx; |
| 4485 | struct htx_sl *sl; |
| 4486 | struct h1m h1m; |
| 4487 | const char *status, *reason; |
| 4488 | const char *name, *value; |
| 4489 | size_t nlen, vlen; |
| 4490 | unsigned int flags; |
| 4491 | |
| 4492 | /* Send the message at once. */ |
| 4493 | htx = htx_from_buf(&res->buf); |
| 4494 | h1m_init_res(&h1m); |
| 4495 | |
| 4496 | /* Use the same http version than the request. */ |
| 4497 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4498 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4499 | if (reason == NULL) |
| 4500 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4501 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4502 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4503 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4504 | } |
| 4505 | else { |
| 4506 | flags = HTX_SL_F_IS_RESP; |
| 4507 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4508 | } |
| 4509 | if (!sl) { |
| 4510 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4511 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4512 | WILL_LJMP(lua_error(L)); |
| 4513 | } |
| 4514 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4515 | |
| 4516 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4517 | lua_pushvalue(L, 0); |
| 4518 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4519 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4520 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4521 | WILL_LJMP(lua_error(L)); |
| 4522 | } |
| 4523 | |
| 4524 | /* Browse the list of headers. */ |
| 4525 | lua_pushnil(L); |
| 4526 | while(lua_next(L, -2) != 0) { |
| 4527 | /* We expect a string as -2. */ |
| 4528 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4529 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4530 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4531 | lua_typename(L, lua_type(L, -2))); |
| 4532 | WILL_LJMP(lua_error(L)); |
| 4533 | } |
| 4534 | name = lua_tolstring(L, -2, &nlen); |
| 4535 | |
| 4536 | /* We expect an array as -1. */ |
| 4537 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4538 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4539 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4540 | name, |
| 4541 | lua_typename(L, lua_type(L, -1))); |
| 4542 | WILL_LJMP(lua_error(L)); |
| 4543 | } |
| 4544 | |
| 4545 | /* Browse the table who is on the top of the stack. */ |
| 4546 | lua_pushnil(L); |
| 4547 | while(lua_next(L, -2) != 0) { |
| 4548 | int id; |
| 4549 | |
| 4550 | /* We expect a number as -2. */ |
| 4551 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4552 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4553 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4554 | name, |
| 4555 | lua_typename(L, lua_type(L, -2))); |
| 4556 | WILL_LJMP(lua_error(L)); |
| 4557 | } |
| 4558 | id = lua_tointeger(L, -2); |
| 4559 | |
| 4560 | /* We expect a string as -2. */ |
| 4561 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4562 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4563 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4564 | name, id, |
| 4565 | lua_typename(L, lua_type(L, -1))); |
| 4566 | WILL_LJMP(lua_error(L)); |
| 4567 | } |
| 4568 | value = lua_tolstring(L, -1, &vlen); |
| 4569 | |
| 4570 | /* Simple Protocol checks. */ |
| 4571 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4572 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4573 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4574 | struct ist v = ist2(value, vlen); |
| 4575 | int ret; |
| 4576 | |
| 4577 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4578 | if (ret < 0) { |
| 4579 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4580 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4581 | name); |
| 4582 | WILL_LJMP(lua_error(L)); |
| 4583 | } |
| 4584 | else if (ret == 0) |
| 4585 | goto next; /* Skip it */ |
| 4586 | } |
| 4587 | |
| 4588 | /* Add a new header */ |
| 4589 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4590 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4591 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4592 | name); |
| 4593 | WILL_LJMP(lua_error(L)); |
| 4594 | } |
| 4595 | next: |
| 4596 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4597 | lua_pop(L, 1); |
| 4598 | } |
| 4599 | |
| 4600 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4601 | lua_pop(L, 1); |
| 4602 | } |
| 4603 | |
| 4604 | if (h1m.flags & H1_MF_CHNK) |
| 4605 | h1m.flags &= ~H1_MF_CLEN; |
| 4606 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4607 | h1m.flags |= H1_MF_XFER_LEN; |
| 4608 | |
| 4609 | /* Uset HTX start-line flags */ |
| 4610 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4611 | flags |= HTX_SL_F_XFER_ENC; |
| 4612 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4613 | flags |= HTX_SL_F_XFER_LEN; |
| 4614 | if (h1m.flags & H1_MF_CHNK) |
| 4615 | flags |= HTX_SL_F_CHNK; |
| 4616 | else if (h1m.flags & H1_MF_CLEN) |
| 4617 | flags |= HTX_SL_F_CLEN; |
| 4618 | if (h1m.body_len == 0) |
| 4619 | flags |= HTX_SL_F_BODYLESS; |
| 4620 | } |
| 4621 | sl->flags |= flags; |
| 4622 | |
| 4623 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4624 | * and the status code implies the presence of a message body, we must |
| 4625 | * announce a transfer encoding chunked. This is required by haproxy |
| 4626 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4627 | * chunked itslef, don't do anything. |
| 4628 | */ |
| 4629 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4630 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4631 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4632 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4633 | /* Add a new header */ |
| 4634 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4635 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4636 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4637 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4638 | WILL_LJMP(lua_error(L)); |
| 4639 | } |
| 4640 | } |
| 4641 | |
| 4642 | /* Finalize headers. */ |
| 4643 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4644 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4645 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4646 | WILL_LJMP(lua_error(L)); |
| 4647 | } |
| 4648 | |
| 4649 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4650 | b_reset(&res->buf); |
| 4651 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4652 | WILL_LJMP(lua_error(L)); |
| 4653 | } |
| 4654 | |
| 4655 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4656 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4657 | |
| 4658 | /* Headers sent, set the flag. */ |
| 4659 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4660 | return 0; |
| 4661 | |
| 4662 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4663 | /* We will build the status line and the headers of the HTTP response. |
| 4664 | * We will try send at once if its not possible, we give back the hand |
| 4665 | * waiting for more room. |
| 4666 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4667 | __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] | 4668 | { |
| 4669 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4670 | struct stream_interface *si = appctx->appctx->owner; |
| 4671 | struct channel *res = si_ic(si); |
| 4672 | |
| 4673 | if (co_data(res)) { |
| 4674 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4675 | 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] | 4676 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4678 | } |
| 4679 | |
| 4680 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4681 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4682 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4683 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4684 | } |
| 4685 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4686 | /* |
| 4687 | * |
| 4688 | * |
| 4689 | * Class HTTP |
| 4690 | * |
| 4691 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4692 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4693 | |
| 4694 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4695 | * a class stream, otherwise it throws an error. |
| 4696 | */ |
| 4697 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4698 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4699 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4700 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4701 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4702 | /* This function creates and push in the stack a HTTP object |
| 4703 | * according with a current TXN. |
| 4704 | */ |
| 4705 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4706 | { |
| 4707 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4708 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4709 | /* Check stack size. */ |
| 4710 | if (!lua_checkstack(L, 3)) |
| 4711 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4712 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4713 | /* Create the object: obj[0] = userdata. |
| 4714 | * Note that the base of the Converters object is the |
| 4715 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4716 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4717 | lua_newtable(L); |
| 4718 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4719 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4720 | |
| 4721 | htxn->s = txn->s; |
| 4722 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4723 | htxn->dir = txn->dir; |
| 4724 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4725 | |
| 4726 | /* Pop a class stream metatable and affect it to the table. */ |
| 4727 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4728 | lua_setmetatable(L, -2); |
| 4729 | |
| 4730 | return 1; |
| 4731 | } |
| 4732 | |
| 4733 | /* This function creates ans returns an array of HTTP headers. |
| 4734 | * This function does not fails. It is used as wrapper with the |
| 4735 | * 2 following functions. |
| 4736 | */ |
| 4737 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4738 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4739 | struct htx *htx; |
| 4740 | int32_t pos; |
| 4741 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4742 | /* Create the table. */ |
| 4743 | lua_newtable(L); |
| 4744 | |
| 4745 | if (!htxn->s->txn) |
| 4746 | return 1; |
| 4747 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4748 | htx = htxbuf(&msg->chn->buf); |
| 4749 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4750 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4751 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4752 | struct ist n, v; |
| 4753 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4754 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4755 | if (type == HTX_BLK_HDR) { |
| 4756 | n = htx_get_blk_name(htx,blk); |
| 4757 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4758 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4759 | else if (type == HTX_BLK_EOH) |
| 4760 | break; |
| 4761 | else |
| 4762 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4763 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4764 | /* Check for existing entry: |
| 4765 | * assume that the table is on the top of the stack, and |
| 4766 | * push the key in the stack, the function lua_gettable() |
| 4767 | * perform the lookup. |
| 4768 | */ |
| 4769 | lua_pushlstring(L, n.ptr, n.len); |
| 4770 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4771 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4772 | switch (lua_type(L, -1)) { |
| 4773 | case LUA_TNIL: |
| 4774 | /* Table not found, create it. */ |
| 4775 | lua_pop(L, 1); /* remove the nil value. */ |
| 4776 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4777 | lua_newtable(L); /* create and push empty table. */ |
| 4778 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4779 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4780 | 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] | 4781 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4782 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4783 | case LUA_TTABLE: |
| 4784 | /* Entry found: push the value in the table. */ |
| 4785 | len = lua_rawlen(L, -1); |
| 4786 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4787 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4788 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4789 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4790 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4791 | default: |
| 4792 | /* Other cases are errors. */ |
| 4793 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4794 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4795 | } |
| 4796 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4797 | return 1; |
| 4798 | } |
| 4799 | |
| 4800 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4801 | { |
| 4802 | struct hlua_txn *htxn; |
| 4803 | |
| 4804 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4805 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4806 | |
| 4807 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4808 | } |
| 4809 | |
| 4810 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4811 | { |
| 4812 | struct hlua_txn *htxn; |
| 4813 | |
| 4814 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4815 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4816 | |
| 4817 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4818 | } |
| 4819 | |
| 4820 | /* This function replace full header, or just a value in |
| 4821 | * the request or in the response. It is a wrapper fir the |
| 4822 | * 4 following functions. |
| 4823 | */ |
| 4824 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4825 | struct http_msg *msg, int action) |
| 4826 | { |
| 4827 | size_t name_len; |
| 4828 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4829 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4830 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4831 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4832 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4833 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4834 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4835 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4836 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4837 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4838 | 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] | 4839 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4840 | return 0; |
| 4841 | } |
| 4842 | |
| 4843 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4844 | { |
| 4845 | struct hlua_txn *htxn; |
| 4846 | |
| 4847 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4848 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4849 | |
| 4850 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4851 | } |
| 4852 | |
| 4853 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4854 | { |
| 4855 | struct hlua_txn *htxn; |
| 4856 | |
| 4857 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4858 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4859 | |
| 4860 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4861 | } |
| 4862 | |
| 4863 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4864 | { |
| 4865 | struct hlua_txn *htxn; |
| 4866 | |
| 4867 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4868 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4869 | |
| 4870 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4871 | } |
| 4872 | |
| 4873 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4874 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4875 | struct hlua_txn *htxn; |
| 4876 | |
| 4877 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4878 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4879 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4880 | 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] | 4881 | } |
| 4882 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4883 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4884 | * It is a wrapper for the 2 following functions. |
| 4885 | */ |
| 4886 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4887 | { |
| 4888 | size_t len; |
| 4889 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4890 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4891 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4892 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4893 | ctx.blk = NULL; |
| 4894 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4895 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4896 | return 0; |
| 4897 | } |
| 4898 | |
| 4899 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4900 | { |
| 4901 | struct hlua_txn *htxn; |
| 4902 | |
| 4903 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4904 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4905 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4906 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4907 | } |
| 4908 | |
| 4909 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4910 | { |
| 4911 | struct hlua_txn *htxn; |
| 4912 | |
| 4913 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4914 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4915 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4916 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4917 | } |
| 4918 | |
| 4919 | /* This function adds an header. It is a wrapper used by |
| 4920 | * the 2 following functions. |
| 4921 | */ |
| 4922 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4923 | { |
| 4924 | size_t name_len; |
| 4925 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4926 | size_t value_len; |
| 4927 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4928 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4929 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4930 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4931 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4932 | return 0; |
| 4933 | } |
| 4934 | |
| 4935 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4936 | { |
| 4937 | struct hlua_txn *htxn; |
| 4938 | |
| 4939 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4940 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4941 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4942 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4943 | } |
| 4944 | |
| 4945 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4946 | { |
| 4947 | struct hlua_txn *htxn; |
| 4948 | |
| 4949 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4950 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4951 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4952 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4953 | } |
| 4954 | |
| 4955 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4956 | { |
| 4957 | struct hlua_txn *htxn; |
| 4958 | |
| 4959 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4960 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4961 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4962 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4963 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4964 | } |
| 4965 | |
| 4966 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4967 | { |
| 4968 | struct hlua_txn *htxn; |
| 4969 | |
| 4970 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4971 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4972 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4973 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4974 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4975 | } |
| 4976 | |
| 4977 | /* This function set the method. */ |
| 4978 | static int hlua_http_req_set_meth(lua_State *L) |
| 4979 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4980 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4981 | size_t name_len; |
| 4982 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4983 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4984 | 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] | 4985 | return 1; |
| 4986 | } |
| 4987 | |
| 4988 | /* This function set the method. */ |
| 4989 | static int hlua_http_req_set_path(lua_State *L) |
| 4990 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4991 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4992 | size_t name_len; |
| 4993 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER / OZON.IO | b84ae92 | 2016-08-02 23:44:58 +0200 | [diff] [blame] | 4994 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4995 | 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] | 4996 | return 1; |
| 4997 | } |
| 4998 | |
| 4999 | /* This function set the query-string. */ |
| 5000 | static int hlua_http_req_set_query(lua_State *L) |
| 5001 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5002 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5003 | size_t name_len; |
| 5004 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5005 | |
| 5006 | /* Check length. */ |
| 5007 | if (name_len > trash.size - 1) { |
| 5008 | lua_pushboolean(L, 0); |
| 5009 | return 1; |
| 5010 | } |
| 5011 | |
| 5012 | /* Add the mark question as prefix. */ |
| 5013 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5014 | trash.area[trash.data++] = '?'; |
| 5015 | memcpy(trash.area + trash.data, name, name_len); |
| 5016 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5018 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5019 | 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] | 5020 | return 1; |
| 5021 | } |
| 5022 | |
| 5023 | /* This function set the uri. */ |
| 5024 | static int hlua_http_req_set_uri(lua_State *L) |
| 5025 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5026 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5027 | size_t name_len; |
| 5028 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5029 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5030 | 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] | 5031 | return 1; |
| 5032 | } |
| 5033 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5034 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5035 | static int hlua_http_res_set_status(lua_State *L) |
| 5036 | { |
| 5037 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5038 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5039 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5040 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5041 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5042 | return 0; |
| 5043 | } |
| 5044 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5045 | /* |
| 5046 | * |
| 5047 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5048 | * Class TXN |
| 5049 | * |
| 5050 | * |
| 5051 | */ |
| 5052 | |
| 5053 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5054 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5055 | */ |
| 5056 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5057 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5058 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5059 | } |
| 5060 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5061 | __LJMP static int hlua_set_var(lua_State *L) |
| 5062 | { |
| 5063 | struct hlua_txn *htxn; |
| 5064 | const char *name; |
| 5065 | size_t len; |
| 5066 | struct sample smp; |
| 5067 | |
| 5068 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5069 | |
| 5070 | /* It is useles to retrieve the stream, but this function |
| 5071 | * runs only in a stream context. |
| 5072 | */ |
| 5073 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5074 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5075 | |
| 5076 | /* Converts the third argument in a sample. */ |
| 5077 | hlua_lua2smp(L, 3, &smp); |
| 5078 | |
| 5079 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5080 | 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] | 5081 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5082 | return 0; |
| 5083 | } |
| 5084 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5085 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5086 | { |
| 5087 | struct hlua_txn *htxn; |
| 5088 | const char *name; |
| 5089 | size_t len; |
| 5090 | struct sample smp; |
| 5091 | |
| 5092 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5093 | |
| 5094 | /* It is useles to retrieve the stream, but this function |
| 5095 | * runs only in a stream context. |
| 5096 | */ |
| 5097 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5098 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5099 | |
| 5100 | /* Unset the variable. */ |
| 5101 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5102 | vars_unset_by_name(name, len, &smp); |
| 5103 | return 0; |
| 5104 | } |
| 5105 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5106 | __LJMP static int hlua_get_var(lua_State *L) |
| 5107 | { |
| 5108 | struct hlua_txn *htxn; |
| 5109 | const char *name; |
| 5110 | size_t len; |
| 5111 | struct sample smp; |
| 5112 | |
| 5113 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5114 | |
| 5115 | /* It is useles to retrieve the stream, but this function |
| 5116 | * runs only in a stream context. |
| 5117 | */ |
| 5118 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5119 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5120 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5121 | 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] | 5122 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5123 | lua_pushnil(L); |
| 5124 | return 1; |
| 5125 | } |
| 5126 | |
| 5127 | return hlua_smp2lua(L, &smp); |
| 5128 | } |
| 5129 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5130 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5131 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5132 | struct hlua *hlua; |
| 5133 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5134 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5135 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5136 | /* It is useles to retrieve the stream, but this function |
| 5137 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5138 | */ |
| 5139 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5140 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5141 | |
| 5142 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5143 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5144 | |
| 5145 | /* Get and store new value. */ |
| 5146 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5147 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5148 | |
| 5149 | return 0; |
| 5150 | } |
| 5151 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5152 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5153 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5154 | struct hlua *hlua; |
| 5155 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5156 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5157 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5158 | /* It is useles to retrieve the stream, but this function |
| 5159 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | */ |
| 5161 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5162 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5163 | |
| 5164 | /* Push configuration index in the stack. */ |
| 5165 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5166 | |
| 5167 | return 1; |
| 5168 | } |
| 5169 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5170 | /* Create stack entry containing a class TXN. This function |
| 5171 | * return 0 if the stack does not contains free slots, |
| 5172 | * otherwise it returns 1. |
| 5173 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5174 | 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] | 5175 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5176 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5177 | |
| 5178 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5179 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5180 | return 0; |
| 5181 | |
| 5182 | /* NOTE: The allocation never fails. The failure |
| 5183 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5184 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5185 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5186 | /* Create the object: obj[0] = userdata. */ |
| 5187 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5188 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5189 | lua_rawseti(L, -2, 0); |
| 5190 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5191 | htxn->s = s; |
| 5192 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5193 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5194 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5195 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5196 | /* Create the "f" field that contains a list of fetches. */ |
| 5197 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5198 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5201 | |
| 5202 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5203 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5204 | 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] | 5205 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5206 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5207 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5208 | /* Create the "c" field that contains a list of converters. */ |
| 5209 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5210 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +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 | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5213 | |
| 5214 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5215 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5216 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5219 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5220 | /* Create the "req" field that contains the request channel object. */ |
| 5221 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5222 | if (!hlua_channel_new(L, &s->req)) |
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 | |
| 5226 | /* Create the "res" field that contains the response channel object. */ |
| 5227 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5228 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5229 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5230 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5231 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5232 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5233 | lua_pushstring(L, "http"); |
| 5234 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5235 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5236 | return 0; |
| 5237 | } |
| 5238 | else |
| 5239 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5240 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5241 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5242 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5243 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5244 | lua_setmetatable(L, -2); |
| 5245 | |
| 5246 | return 1; |
| 5247 | } |
| 5248 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5249 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5250 | { |
| 5251 | const char *msg; |
| 5252 | struct hlua_txn *htxn; |
| 5253 | |
| 5254 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5255 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5256 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5257 | |
| 5258 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5259 | return 0; |
| 5260 | } |
| 5261 | |
| 5262 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5263 | { |
| 5264 | int level; |
| 5265 | const char *msg; |
| 5266 | struct hlua_txn *htxn; |
| 5267 | |
| 5268 | MAY_LJMP(check_args(L, 3, "log")); |
| 5269 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5270 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5271 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5272 | |
| 5273 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5274 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5275 | |
| 5276 | hlua_sendlog(htxn->s->be, level, msg); |
| 5277 | return 0; |
| 5278 | } |
| 5279 | |
| 5280 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5281 | { |
| 5282 | const char *msg; |
| 5283 | struct hlua_txn *htxn; |
| 5284 | |
| 5285 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5286 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5287 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5288 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5289 | return 0; |
| 5290 | } |
| 5291 | |
| 5292 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5293 | { |
| 5294 | const char *msg; |
| 5295 | struct hlua_txn *htxn; |
| 5296 | |
| 5297 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5298 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5299 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5300 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5301 | return 0; |
| 5302 | } |
| 5303 | |
| 5304 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5305 | { |
| 5306 | const char *msg; |
| 5307 | struct hlua_txn *htxn; |
| 5308 | |
| 5309 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5310 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5311 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5312 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5313 | return 0; |
| 5314 | } |
| 5315 | |
| 5316 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5317 | { |
| 5318 | const char *msg; |
| 5319 | struct hlua_txn *htxn; |
| 5320 | |
| 5321 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5322 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5323 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5324 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5325 | return 0; |
| 5326 | } |
| 5327 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5328 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5329 | { |
| 5330 | struct hlua_txn *htxn; |
| 5331 | int ll; |
| 5332 | |
| 5333 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5334 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5335 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5336 | |
| 5337 | if (ll < 0 || ll > 7) |
| 5338 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5339 | |
| 5340 | htxn->s->logs.level = ll; |
| 5341 | return 0; |
| 5342 | } |
| 5343 | |
| 5344 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5345 | { |
| 5346 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5347 | int tos; |
| 5348 | |
| 5349 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5350 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5351 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5352 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5353 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5354 | return 0; |
| 5355 | } |
| 5356 | |
| 5357 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5358 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5359 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5360 | int mark; |
| 5361 | |
| 5362 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5363 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5364 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5365 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5366 | conn_set_tos(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5367 | return 0; |
| 5368 | } |
| 5369 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5370 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5371 | { |
| 5372 | struct hlua_txn *htxn; |
| 5373 | |
| 5374 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5375 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5376 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5377 | return 0; |
| 5378 | } |
| 5379 | |
| 5380 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5381 | { |
| 5382 | struct hlua_txn *htxn; |
| 5383 | |
| 5384 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5385 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5386 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5387 | return 0; |
| 5388 | } |
| 5389 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5390 | /* This function is an Lua binding that send pending data |
| 5391 | * to the client, and close the stream interface. |
| 5392 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5393 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5394 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5395 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5396 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5397 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5398 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5399 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5400 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5401 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5402 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5403 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5404 | * session, so we just end the execution of the current |
| 5405 | * lua code. |
| 5406 | */ |
| 5407 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5408 | WILL_LJMP(hlua_done(L)); |
| 5409 | return 0; |
| 5410 | } |
| 5411 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5412 | ic = &htxn->s->req; |
| 5413 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5414 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5415 | if (IS_HTX_STRM(htxn->s)) |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5416 | http_reply_and_close(htxn->s, 0, NULL); |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5417 | else { |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5418 | channel_auto_read(ic); |
| 5419 | channel_abort(ic); |
| 5420 | channel_auto_close(ic); |
| 5421 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5422 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5423 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 5424 | channel_auto_read(oc); |
| 5425 | channel_auto_close(oc); |
| 5426 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5427 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5428 | ic->analysers = 0; |
| 5429 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5430 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5431 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5432 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5433 | return 0; |
| 5434 | } |
| 5435 | |
| 5436 | __LJMP static int hlua_log(lua_State *L) |
| 5437 | { |
| 5438 | int level; |
| 5439 | const char *msg; |
| 5440 | |
| 5441 | MAY_LJMP(check_args(L, 2, "log")); |
| 5442 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5443 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5444 | |
| 5445 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5446 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5447 | |
| 5448 | hlua_sendlog(NULL, level, msg); |
| 5449 | return 0; |
| 5450 | } |
| 5451 | |
| 5452 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5453 | { |
| 5454 | const char *msg; |
| 5455 | |
| 5456 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5457 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5458 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5459 | return 0; |
| 5460 | } |
| 5461 | |
| 5462 | __LJMP static int hlua_log_info(lua_State *L) |
| 5463 | { |
| 5464 | const char *msg; |
| 5465 | |
| 5466 | MAY_LJMP(check_args(L, 1, "info")); |
| 5467 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5468 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5469 | return 0; |
| 5470 | } |
| 5471 | |
| 5472 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5473 | { |
| 5474 | const char *msg; |
| 5475 | |
| 5476 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5477 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5478 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5479 | return 0; |
| 5480 | } |
| 5481 | |
| 5482 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5483 | { |
| 5484 | const char *msg; |
| 5485 | |
| 5486 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5487 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5488 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5489 | return 0; |
| 5490 | } |
| 5491 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5492 | __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] | 5493 | { |
| 5494 | int wakeup_ms = lua_tointeger(L, -1); |
| 5495 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5496 | 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] | 5497 | return 0; |
| 5498 | } |
| 5499 | |
| 5500 | __LJMP static int hlua_sleep(lua_State *L) |
| 5501 | { |
| 5502 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5503 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5504 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5505 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5506 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5507 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5508 | wakeup_ms = tick_add(now_ms, delay); |
| 5509 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5510 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5511 | 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] | 5512 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5513 | } |
| 5514 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5515 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5516 | { |
| 5517 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5518 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5519 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5520 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5521 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5522 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5523 | wakeup_ms = tick_add(now_ms, delay); |
| 5524 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5525 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5526 | 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] | 5527 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5528 | } |
| 5529 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5530 | /* This functionis an LUA binding. it permits to give back |
| 5531 | * the hand at the HAProxy scheduler. It is used when the |
| 5532 | * LUA processing consumes a lot of time. |
| 5533 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5534 | __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] | 5535 | { |
| 5536 | return 0; |
| 5537 | } |
| 5538 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5539 | __LJMP static int hlua_yield(lua_State *L) |
| 5540 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5541 | 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] | 5542 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5543 | } |
| 5544 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5545 | /* This function change the nice of the currently executed |
| 5546 | * task. It is used set low or high priority at the current |
| 5547 | * task. |
| 5548 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5549 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5550 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5551 | struct hlua *hlua; |
| 5552 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5553 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5554 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5555 | hlua = hlua_gethlua(L); |
| 5556 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5557 | |
| 5558 | /* If he task is not set, I'm in a start mode. */ |
| 5559 | if (!hlua || !hlua->task) |
| 5560 | return 0; |
| 5561 | |
| 5562 | if (nice < -1024) |
| 5563 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5564 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5565 | nice = 1024; |
| 5566 | |
| 5567 | hlua->task->nice = nice; |
| 5568 | return 0; |
| 5569 | } |
| 5570 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5571 | /* 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] | 5572 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5573 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5574 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5575 | * |
| 5576 | * Task wrapper are longjmp safe because the only one Lua code |
| 5577 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5578 | */ |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5579 | 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] | 5580 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5581 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5582 | enum hlua_exec status; |
| 5583 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5584 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5585 | task_set_affinity(task, tid_bit); |
| 5586 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5587 | /* If it is the first call to the task, we must initialize the |
| 5588 | * execution timeouts. |
| 5589 | */ |
| 5590 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5591 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5592 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5593 | /* Execute the Lua code. */ |
| 5594 | status = hlua_ctx_resume(hlua, 1); |
| 5595 | |
| 5596 | switch (status) { |
| 5597 | /* finished or yield */ |
| 5598 | case HLUA_E_OK: |
| 5599 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5600 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5601 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5602 | break; |
| 5603 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5604 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5605 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5606 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5607 | break; |
| 5608 | |
| 5609 | /* finished with error. */ |
| 5610 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5611 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5612 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5613 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5614 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5615 | break; |
| 5616 | |
| 5617 | case HLUA_E_ERR: |
| 5618 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5619 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5620 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5621 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5622 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5623 | break; |
| 5624 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5625 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5626 | } |
| 5627 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5628 | /* This function is an LUA binding that register LUA function to be |
| 5629 | * executed after the HAProxy configuration parsing and before the |
| 5630 | * HAProxy scheduler starts. This function expect only one LUA |
| 5631 | * argument that is a function. This function returns nothing, but |
| 5632 | * throws if an error is encountered. |
| 5633 | */ |
| 5634 | __LJMP static int hlua_register_init(lua_State *L) |
| 5635 | { |
| 5636 | struct hlua_init_function *init; |
| 5637 | int ref; |
| 5638 | |
| 5639 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5640 | |
| 5641 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5642 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5643 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5644 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5645 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5646 | |
| 5647 | init->function_ref = ref; |
| 5648 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5649 | return 0; |
| 5650 | } |
| 5651 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5652 | /* This functio is an LUA binding. It permits to register a task |
| 5653 | * executed in parallel of the main HAroxy activity. The task is |
| 5654 | * created and it is set in the HAProxy scheduler. It can be called |
| 5655 | * from the "init" section, "post init" or during the runtime. |
| 5656 | * |
| 5657 | * Lua prototype: |
| 5658 | * |
| 5659 | * <none> core.register_task(<function>) |
| 5660 | */ |
| 5661 | static int hlua_register_task(lua_State *L) |
| 5662 | { |
| 5663 | struct hlua *hlua; |
| 5664 | struct task *task; |
| 5665 | int ref; |
| 5666 | |
| 5667 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5668 | |
| 5669 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5670 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5671 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5672 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5673 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5674 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5675 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5676 | if (!task) |
| 5677 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5678 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5679 | task->context = hlua; |
| 5680 | task->process = hlua_process_task; |
| 5681 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5682 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5683 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5684 | |
| 5685 | /* Restore the function in the stack. */ |
| 5686 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5687 | hlua->nargs = 0; |
| 5688 | |
| 5689 | /* Schedule task. */ |
| 5690 | task_schedule(task, now_ms); |
| 5691 | |
| 5692 | return 0; |
| 5693 | } |
| 5694 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5695 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5696 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5697 | * resume converters. |
| 5698 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5699 | 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] | 5700 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5701 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5702 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5703 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5704 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5705 | if (!stream) |
| 5706 | return 0; |
| 5707 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5708 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5709 | * Lua context can be not initialized. This behavior |
| 5710 | * permits to save performances because a systematic |
| 5711 | * Lua initialization cause 5% performances loss. |
| 5712 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5713 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5714 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5715 | if (!stream->hlua) { |
| 5716 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5717 | return 0; |
| 5718 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5719 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5720 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5721 | return 0; |
| 5722 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5723 | } |
| 5724 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5725 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5726 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5727 | |
| 5728 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5729 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5730 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5731 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5732 | else |
| 5733 | error = "critical error"; |
| 5734 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5735 | return 0; |
| 5736 | } |
| 5737 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5738 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5739 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5740 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5741 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5742 | return 0; |
| 5743 | } |
| 5744 | |
| 5745 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5746 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5747 | |
| 5748 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5749 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5750 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5751 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5752 | return 0; |
| 5753 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5754 | hlua_smp2lua(stream->hlua->T, smp); |
| 5755 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5756 | |
| 5757 | /* push keywords in the stack. */ |
| 5758 | if (arg_p) { |
| 5759 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5760 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5761 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5762 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5763 | return 0; |
| 5764 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5765 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5766 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5767 | } |
| 5768 | } |
| 5769 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5770 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5771 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5772 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5773 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5774 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5775 | } |
| 5776 | |
| 5777 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5778 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5779 | /* finished. */ |
| 5780 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5781 | /* If the stack is empty, the function fails. */ |
| 5782 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5783 | return 0; |
| 5784 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5785 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5787 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5788 | return 1; |
| 5789 | |
| 5790 | /* yield. */ |
| 5791 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5792 | 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] | 5793 | return 0; |
| 5794 | |
| 5795 | /* finished with error. */ |
| 5796 | case HLUA_E_ERRMSG: |
| 5797 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5798 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5799 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5800 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5801 | return 0; |
| 5802 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5803 | case HLUA_E_ETMOUT: |
| 5804 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5805 | return 0; |
| 5806 | |
| 5807 | case HLUA_E_NOMEM: |
| 5808 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5809 | return 0; |
| 5810 | |
| 5811 | case HLUA_E_YIELD: |
| 5812 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5813 | return 0; |
| 5814 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5815 | case HLUA_E_ERR: |
| 5816 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5817 | 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] | 5818 | |
| 5819 | default: |
| 5820 | return 0; |
| 5821 | } |
| 5822 | } |
| 5823 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5824 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5825 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5826 | * resume sample-fetches. This function will be called by the sample |
| 5827 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5828 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5829 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5830 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5831 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5832 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5833 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5834 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5835 | const struct buffer msg = { }; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5836 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5837 | if (!stream) |
| 5838 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5839 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5840 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5841 | * Lua context can be not initialized. This behavior |
| 5842 | * permits to save performances because a systematic |
| 5843 | * Lua initialization cause 5% performances loss. |
| 5844 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5845 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5846 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5847 | if (!stream->hlua) { |
| 5848 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5849 | return 0; |
| 5850 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5851 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5852 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5853 | return 0; |
| 5854 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5855 | } |
| 5856 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5857 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5858 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5859 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5860 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5861 | |
| 5862 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5863 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5864 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5865 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5866 | else |
| 5867 | error = "critical error"; |
| 5868 | 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] | 5869 | return 0; |
| 5870 | } |
| 5871 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5872 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5873 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5874 | 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] | 5875 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5876 | return 0; |
| 5877 | } |
| 5878 | |
| 5879 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5880 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5881 | |
| 5882 | /* push arguments in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5883 | 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] | 5884 | HLUA_TXN_NOTERM)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5885 | 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] | 5886 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5887 | return 0; |
| 5888 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5889 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5890 | |
| 5891 | /* push keywords in the stack. */ |
| 5892 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5893 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5894 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5895 | 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] | 5896 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5897 | return 0; |
| 5898 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5899 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5900 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5901 | } |
| 5902 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5903 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5904 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5905 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5906 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5907 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5908 | } |
| 5909 | |
| 5910 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5911 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5912 | /* finished. */ |
| 5913 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5915 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5916 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5917 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5918 | /* If the stack is empty, the function fails. */ |
| 5919 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5920 | return 0; |
| 5921 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5922 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5923 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5924 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5925 | |
| 5926 | /* Set the end of execution flag. */ |
| 5927 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5928 | return 1; |
| 5929 | |
| 5930 | /* yield. */ |
| 5931 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5932 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5933 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5934 | 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] | 5935 | return 0; |
| 5936 | |
| 5937 | /* finished with error. */ |
| 5938 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5939 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5940 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5941 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5942 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5943 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5944 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5945 | return 0; |
| 5946 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5947 | case HLUA_E_ETMOUT: |
| 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': execution timeout.\n", fcn->name); |
| 5951 | return 0; |
| 5952 | |
| 5953 | case HLUA_E_NOMEM: |
| 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': out of memory error.\n", fcn->name); |
| 5957 | return 0; |
| 5958 | |
| 5959 | case HLUA_E_YIELD: |
| 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 | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5962 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5963 | return 0; |
| 5964 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5965 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5966 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5967 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5968 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5969 | 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] | 5970 | |
| 5971 | default: |
| 5972 | return 0; |
| 5973 | } |
| 5974 | } |
| 5975 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5976 | /* This function is an LUA binding used for registering |
| 5977 | * "sample-conv" functions. It expects a converter name used |
| 5978 | * in the haproxy configuration file, and an LUA function. |
| 5979 | */ |
| 5980 | __LJMP static int hlua_register_converters(lua_State *L) |
| 5981 | { |
| 5982 | struct sample_conv_kw_list *sck; |
| 5983 | const char *name; |
| 5984 | int ref; |
| 5985 | int len; |
| 5986 | struct hlua_function *fcn; |
| 5987 | |
| 5988 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 5989 | |
| 5990 | /* First argument : converter name. */ |
| 5991 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5992 | |
| 5993 | /* Second argument : lua function. */ |
| 5994 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 5995 | |
| 5996 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5997 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5998 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5999 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6000 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6001 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6002 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6003 | |
| 6004 | /* Fill fcn. */ |
| 6005 | fcn->name = strdup(name); |
| 6006 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6007 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6008 | fcn->function_ref = ref; |
| 6009 | |
| 6010 | /* List head */ |
| 6011 | sck->list.n = sck->list.p = NULL; |
| 6012 | |
| 6013 | /* converter keyword. */ |
| 6014 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6015 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6016 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6017 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6018 | |
| 6019 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6020 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6021 | 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] | 6022 | sck->kw[0].val_args = NULL; |
| 6023 | sck->kw[0].in_type = SMP_T_STR; |
| 6024 | sck->kw[0].out_type = SMP_T_STR; |
| 6025 | sck->kw[0].private = fcn; |
| 6026 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6027 | /* Register this new converter */ |
| 6028 | sample_register_convs(sck); |
| 6029 | |
| 6030 | return 0; |
| 6031 | } |
| 6032 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6033 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6034 | * "sample-fetch" functions. It expects a converter name used |
| 6035 | * in the haproxy configuration file, and an LUA function. |
| 6036 | */ |
| 6037 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6038 | { |
| 6039 | const char *name; |
| 6040 | int ref; |
| 6041 | int len; |
| 6042 | struct sample_fetch_kw_list *sfk; |
| 6043 | struct hlua_function *fcn; |
| 6044 | |
| 6045 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6046 | |
| 6047 | /* First argument : sample-fetch name. */ |
| 6048 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6049 | |
| 6050 | /* Second argument : lua function. */ |
| 6051 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6052 | |
| 6053 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6054 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6055 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6056 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6057 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6058 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6059 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6060 | |
| 6061 | /* Fill fcn. */ |
| 6062 | fcn->name = strdup(name); |
| 6063 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6064 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6065 | fcn->function_ref = ref; |
| 6066 | |
| 6067 | /* List head */ |
| 6068 | sfk->list.n = sfk->list.p = NULL; |
| 6069 | |
| 6070 | /* sample-fetch keyword. */ |
| 6071 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6072 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6073 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6074 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6075 | |
| 6076 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6077 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6078 | 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] | 6079 | sfk->kw[0].val_args = NULL; |
| 6080 | sfk->kw[0].out_type = SMP_T_STR; |
| 6081 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6082 | sfk->kw[0].val = 0; |
| 6083 | sfk->kw[0].private = fcn; |
| 6084 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6085 | /* Register this new fetch. */ |
| 6086 | sample_register_fetches(sfk); |
| 6087 | |
| 6088 | return 0; |
| 6089 | } |
| 6090 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6091 | /* This function is a wrapper to execute each LUA function declared |
| 6092 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6093 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6094 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6095 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6096 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6097 | 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] | 6098 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6099 | { |
| 6100 | char **arg; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6101 | unsigned int analyzer; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6102 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6103 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6104 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6105 | |
| 6106 | switch (rule->from) { |
Thierry FOURNIER | 6e01f38 | 2015-11-02 09:52:54 +0100 | [diff] [blame] | 6107 | case ACT_F_TCP_REQ_CNT: analyzer = AN_REQ_INSPECT_FE ; dir = SMP_OPT_DIR_REQ; break; |
| 6108 | case ACT_F_TCP_RES_CNT: analyzer = AN_RES_INSPECT ; dir = SMP_OPT_DIR_RES; break; |
| 6109 | case ACT_F_HTTP_REQ: analyzer = AN_REQ_HTTP_PROCESS_FE; dir = SMP_OPT_DIR_REQ; break; |
| 6110 | 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] | 6111 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6112 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6113 | return ACT_RET_CONT; |
| 6114 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6115 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6116 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6117 | * Lua context can be not initialized. This behavior |
| 6118 | * permits to save performances because a systematic |
| 6119 | * Lua initialization cause 5% performances loss. |
| 6120 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6121 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6122 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6123 | if (!s->hlua) { |
| 6124 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6125 | rule->arg.hlua_rule->fcn.name); |
| 6126 | return ACT_RET_CONT; |
| 6127 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6128 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6129 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6130 | rule->arg.hlua_rule->fcn.name); |
| 6131 | return ACT_RET_CONT; |
| 6132 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6133 | } |
| 6134 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6135 | consistency_set(s, dir, &s->hlua->cons); |
| 6136 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6137 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6138 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6139 | |
| 6140 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6141 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6142 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6143 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6144 | else |
| 6145 | error = "critical error"; |
| 6146 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6147 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6148 | return ACT_RET_CONT; |
| 6149 | } |
| 6150 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6151 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6152 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6153 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6154 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6155 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6156 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6157 | } |
| 6158 | |
| 6159 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6160 | 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] | 6161 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6162 | /* Create and and push object stream in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6163 | if (!hlua_txn_new(s->hlua->T, s, px, dir, 0)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6164 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6165 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6166 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6167 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6168 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6169 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6170 | |
| 6171 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6172 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6173 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6174 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6175 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6176 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6177 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6178 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6179 | lua_pushstring(s->hlua->T, *arg); |
| 6180 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6181 | } |
| 6182 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6183 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6184 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6185 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6186 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6187 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6188 | } |
| 6189 | |
| 6190 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6191 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6192 | /* finished. */ |
| 6193 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6194 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6195 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6196 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6197 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6198 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6199 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6200 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6201 | |
| 6202 | /* yield. */ |
| 6203 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6204 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6205 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6206 | if (analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6207 | s->req.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6208 | else if (analyzer & (AN_RES_INSPECT|AN_RES_HTTP_PROCESS_BE)) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6210 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6211 | /* Some actions can be wake up when a "write" event |
| 6212 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6213 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6214 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6215 | if (HLUA_IS_WAKERESWR(s->hlua)) { |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6216 | s->res.flags |= CF_WAKE_WRITE; |
Willy Tarreau | 76bd97f | 2015-03-10 17:16:10 +0100 | [diff] [blame] | 6217 | if ((analyzer & (AN_REQ_INSPECT_FE|AN_REQ_HTTP_PROCESS_FE))) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6218 | s->res.analysers |= analyzer; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6219 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6220 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6221 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6222 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6223 | * because HAProxy is not able to manipulate data, it |
| 6224 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6225 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6226 | |
| 6227 | /* finished with error. */ |
| 6228 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6229 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6230 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6231 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6232 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6233 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6234 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6235 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6236 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6237 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6238 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6239 | case HLUA_E_ETMOUT: |
| 6240 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6241 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6242 | return ACT_RET_ERR; |
| 6243 | } |
| 6244 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6245 | return 0; |
| 6246 | |
| 6247 | case HLUA_E_NOMEM: |
| 6248 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6249 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6250 | return ACT_RET_ERR; |
| 6251 | } |
| 6252 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6253 | return 0; |
| 6254 | |
| 6255 | case HLUA_E_YIELD: |
| 6256 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6257 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6258 | return ACT_RET_ERR; |
| 6259 | } |
| 6260 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6261 | rule->arg.hlua_rule->fcn.name); |
| 6262 | return 0; |
| 6263 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6264 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6265 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6266 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6267 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6268 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6269 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6270 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6271 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6272 | |
| 6273 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6274 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6275 | } |
| 6276 | } |
| 6277 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6278 | 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] | 6279 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6280 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6281 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6282 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6283 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6284 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6285 | } |
| 6286 | |
| 6287 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6288 | { |
| 6289 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6290 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6291 | struct task *task; |
| 6292 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6293 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6294 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6295 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6296 | if (!hlua) { |
| 6297 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6298 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6299 | return 0; |
| 6300 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6301 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6302 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6303 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6304 | |
| 6305 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6306 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6307 | if (!task) { |
| 6308 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6309 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6310 | return 0; |
| 6311 | } |
| 6312 | task->nice = 0; |
| 6313 | task->context = ctx; |
| 6314 | task->process = hlua_applet_wakeup; |
| 6315 | ctx->ctx.hlua_apptcp.task = task; |
| 6316 | |
| 6317 | /* In the execution wrappers linked with a stream, the |
| 6318 | * Lua context can be not initialized. This behavior |
| 6319 | * permits to save performances because a systematic |
| 6320 | * Lua initialization cause 5% performances loss. |
| 6321 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6322 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6323 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6324 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6325 | return 0; |
| 6326 | } |
| 6327 | |
| 6328 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6329 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6330 | |
| 6331 | /* The following Lua calls can fail. */ |
| 6332 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6333 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6334 | error = lua_tostring(hlua->T, -1); |
| 6335 | else |
| 6336 | error = "critical error"; |
| 6337 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6338 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6339 | return 0; |
| 6340 | } |
| 6341 | |
| 6342 | /* Check stack available size. */ |
| 6343 | if (!lua_checkstack(hlua->T, 1)) { |
| 6344 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6345 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6346 | RESET_SAFE_LJMP(hlua->T); |
| 6347 | return 0; |
| 6348 | } |
| 6349 | |
| 6350 | /* Restore the function in the stack. */ |
| 6351 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6352 | |
| 6353 | /* Create and and push object stream in the stack. */ |
| 6354 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6355 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6356 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6357 | RESET_SAFE_LJMP(hlua->T); |
| 6358 | return 0; |
| 6359 | } |
| 6360 | hlua->nargs = 1; |
| 6361 | |
| 6362 | /* push keywords in the stack. */ |
| 6363 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6364 | if (!lua_checkstack(hlua->T, 1)) { |
| 6365 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6366 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6367 | RESET_SAFE_LJMP(hlua->T); |
| 6368 | return 0; |
| 6369 | } |
| 6370 | lua_pushstring(hlua->T, *arg); |
| 6371 | hlua->nargs++; |
| 6372 | } |
| 6373 | |
| 6374 | RESET_SAFE_LJMP(hlua->T); |
| 6375 | |
| 6376 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6377 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6378 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6379 | |
| 6380 | return 1; |
| 6381 | } |
| 6382 | |
| 6383 | static void hlua_applet_tcp_fct(struct appctx *ctx) |
| 6384 | { |
| 6385 | struct stream_interface *si = ctx->owner; |
| 6386 | struct stream *strm = si_strm(si); |
| 6387 | struct channel *res = si_ic(si); |
| 6388 | struct act_rule *rule = ctx->rule; |
| 6389 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6390 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6391 | |
| 6392 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6393 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6394 | /* eat the whole request */ |
| 6395 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6396 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6397 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6398 | |
| 6399 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6400 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6401 | return; |
| 6402 | |
| 6403 | /* Execute the function. */ |
| 6404 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6405 | /* finished. */ |
| 6406 | case HLUA_E_OK: |
| 6407 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6408 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6409 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6410 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6411 | res->flags |= CF_READ_NULL; |
| 6412 | si_shutr(si); |
| 6413 | return; |
| 6414 | |
| 6415 | /* yield. */ |
| 6416 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6417 | if (hlua->wake_time != TICK_ETERNITY) |
| 6418 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6419 | return; |
| 6420 | |
| 6421 | /* finished with error. */ |
| 6422 | case HLUA_E_ERRMSG: |
| 6423 | /* Display log. */ |
| 6424 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6425 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6426 | lua_pop(hlua->T, 1); |
| 6427 | goto error; |
| 6428 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6429 | case HLUA_E_ETMOUT: |
| 6430 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6431 | rule->arg.hlua_rule->fcn.name); |
| 6432 | goto error; |
| 6433 | |
| 6434 | case HLUA_E_NOMEM: |
| 6435 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6436 | rule->arg.hlua_rule->fcn.name); |
| 6437 | goto error; |
| 6438 | |
| 6439 | case HLUA_E_YIELD: /* unexpected */ |
| 6440 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6441 | rule->arg.hlua_rule->fcn.name); |
| 6442 | goto error; |
| 6443 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6444 | case HLUA_E_ERR: |
| 6445 | /* Display log. */ |
| 6446 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6447 | rule->arg.hlua_rule->fcn.name); |
| 6448 | goto error; |
| 6449 | |
| 6450 | default: |
| 6451 | goto error; |
| 6452 | } |
| 6453 | |
| 6454 | error: |
| 6455 | |
| 6456 | /* For all other cases, just close the stream. */ |
| 6457 | si_shutw(si); |
| 6458 | si_shutr(si); |
| 6459 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6460 | } |
| 6461 | |
| 6462 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6463 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6464 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6465 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6466 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6467 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6468 | } |
| 6469 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6470 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6471 | * an errors occurs and -1 if more data are required for initializing |
| 6472 | * the applet. |
| 6473 | */ |
| 6474 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6475 | { |
| 6476 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6477 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6478 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6479 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6480 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6481 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6482 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6483 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6484 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6485 | if (!hlua) { |
| 6486 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6487 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6488 | return 0; |
| 6489 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6490 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6491 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6492 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6493 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6494 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6495 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6496 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6497 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6498 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6499 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6500 | if (!task) { |
| 6501 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6502 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6503 | return 0; |
| 6504 | } |
| 6505 | task->nice = 0; |
| 6506 | task->context = ctx; |
| 6507 | task->process = hlua_applet_wakeup; |
| 6508 | ctx->ctx.hlua_apphttp.task = task; |
| 6509 | |
| 6510 | /* In the execution wrappers linked with a stream, the |
| 6511 | * Lua context can be not initialized. This behavior |
| 6512 | * permits to save performances because a systematic |
| 6513 | * Lua initialization cause 5% performances loss. |
| 6514 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6515 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6516 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6517 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6518 | return 0; |
| 6519 | } |
| 6520 | |
| 6521 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6522 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6523 | |
| 6524 | /* The following Lua calls can fail. */ |
| 6525 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6526 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6527 | error = lua_tostring(hlua->T, -1); |
| 6528 | else |
| 6529 | error = "critical error"; |
| 6530 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6531 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6532 | return 0; |
| 6533 | } |
| 6534 | |
| 6535 | /* Check stack available size. */ |
| 6536 | if (!lua_checkstack(hlua->T, 1)) { |
| 6537 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6538 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6539 | RESET_SAFE_LJMP(hlua->T); |
| 6540 | return 0; |
| 6541 | } |
| 6542 | |
| 6543 | /* Restore the function in the stack. */ |
| 6544 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6545 | |
| 6546 | /* Create and and push object stream in the stack. */ |
| 6547 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6548 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6549 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6550 | RESET_SAFE_LJMP(hlua->T); |
| 6551 | return 0; |
| 6552 | } |
| 6553 | hlua->nargs = 1; |
| 6554 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6555 | /* push keywords in the stack. */ |
| 6556 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6557 | if (!lua_checkstack(hlua->T, 1)) { |
| 6558 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6559 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6560 | RESET_SAFE_LJMP(hlua->T); |
| 6561 | return 0; |
| 6562 | } |
| 6563 | lua_pushstring(hlua->T, *arg); |
| 6564 | hlua->nargs++; |
| 6565 | } |
| 6566 | |
| 6567 | RESET_SAFE_LJMP(hlua->T); |
| 6568 | |
| 6569 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6570 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6571 | |
| 6572 | return 1; |
| 6573 | } |
| 6574 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 6575 | static void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6576 | { |
| 6577 | struct stream_interface *si = ctx->owner; |
| 6578 | struct stream *strm = si_strm(si); |
| 6579 | struct channel *req = si_oc(si); |
| 6580 | struct channel *res = si_ic(si); |
| 6581 | struct act_rule *rule = ctx->rule; |
| 6582 | struct proxy *px = strm->be; |
| 6583 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 6584 | struct htx *req_htx, *res_htx; |
| 6585 | |
| 6586 | res_htx = htx_from_buf(&res->buf); |
| 6587 | |
| 6588 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6589 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6590 | goto out; |
| 6591 | |
| 6592 | /* Check if the input buffer is avalaible. */ |
| 6593 | if (!b_size(&res->buf)) { |
| 6594 | si_rx_room_blk(si); |
| 6595 | goto out; |
| 6596 | } |
| 6597 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6598 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6599 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6600 | |
| 6601 | /* Set the currently running flag. */ |
| 6602 | if (!HLUA_IS_RUNNING(hlua) && |
| 6603 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6604 | struct htx_blk *blk; |
| 6605 | size_t count = co_data(req); |
| 6606 | |
| 6607 | if (!count) { |
| 6608 | si_cant_get(si); |
| 6609 | goto out; |
| 6610 | } |
| 6611 | |
| 6612 | /* We need to flush the request header. This left the body for |
| 6613 | * the Lua. |
| 6614 | */ |
| 6615 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 6616 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6617 | while (count && blk) { |
| 6618 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6619 | uint32_t sz = htx_get_blksz(blk); |
| 6620 | |
| 6621 | if (sz > count) { |
| 6622 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6623 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6624 | goto out; |
| 6625 | } |
| 6626 | |
| 6627 | count -= sz; |
| 6628 | co_set_data(req, co_data(req) - sz); |
| 6629 | blk = htx_remove_blk(req_htx, blk); |
| 6630 | |
| 6631 | if (type == HTX_BLK_EOH) |
| 6632 | break; |
| 6633 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6634 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6635 | } |
| 6636 | |
| 6637 | /* Executes The applet if it is not done. */ |
| 6638 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6639 | |
| 6640 | /* Execute the function. */ |
| 6641 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6642 | /* finished. */ |
| 6643 | case HLUA_E_OK: |
| 6644 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6645 | break; |
| 6646 | |
| 6647 | /* yield. */ |
| 6648 | case HLUA_E_AGAIN: |
| 6649 | if (hlua->wake_time != TICK_ETERNITY) |
| 6650 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6651 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6652 | |
| 6653 | /* finished with error. */ |
| 6654 | case HLUA_E_ERRMSG: |
| 6655 | /* Display log. */ |
| 6656 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6657 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6658 | lua_pop(hlua->T, 1); |
| 6659 | goto error; |
| 6660 | |
| 6661 | case HLUA_E_ETMOUT: |
| 6662 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6663 | rule->arg.hlua_rule->fcn.name); |
| 6664 | goto error; |
| 6665 | |
| 6666 | case HLUA_E_NOMEM: |
| 6667 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6668 | rule->arg.hlua_rule->fcn.name); |
| 6669 | goto error; |
| 6670 | |
| 6671 | case HLUA_E_YIELD: /* unexpected */ |
| 6672 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6673 | rule->arg.hlua_rule->fcn.name); |
| 6674 | goto error; |
| 6675 | |
| 6676 | case HLUA_E_ERR: |
| 6677 | /* Display log. */ |
| 6678 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6679 | rule->arg.hlua_rule->fcn.name); |
| 6680 | goto error; |
| 6681 | |
| 6682 | default: |
| 6683 | goto error; |
| 6684 | } |
| 6685 | } |
| 6686 | |
| 6687 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6688 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 6689 | goto done; |
| 6690 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6691 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 6692 | goto error; |
| 6693 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 6694 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6695 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 6696 | si_rx_room_blk(si); |
| 6697 | goto out; |
| 6698 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6699 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6700 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6701 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6702 | } |
| 6703 | |
| 6704 | done: |
| 6705 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6706 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6707 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6708 | si_shutr(si); |
| 6709 | } |
| 6710 | |
| 6711 | /* eat the whole request */ |
| 6712 | if (co_data(req)) { |
| 6713 | req_htx = htx_from_buf(&req->buf); |
| 6714 | co_htx_skip(req, req_htx, co_data(req)); |
| 6715 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6716 | } |
| 6717 | } |
| 6718 | |
| 6719 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6720 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6721 | return; |
| 6722 | |
| 6723 | error: |
| 6724 | |
| 6725 | /* If we are in HTTP mode, and we are not send any |
| 6726 | * data, return a 500 server error in best effort: |
| 6727 | * if there is no room available in the buffer, |
| 6728 | * just close the connection. |
| 6729 | */ |
| 6730 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 6731 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6732 | |
| 6733 | channel_erase(res); |
| 6734 | res->buf.data = b_data(err); |
| 6735 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 6736 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6737 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6738 | } |
| 6739 | if (!(strm->flags & SF_ERR_MASK)) |
| 6740 | strm->flags |= SF_ERR_RESOURCE; |
| 6741 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6742 | goto done; |
| 6743 | } |
| 6744 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6745 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6746 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6747 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6748 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6749 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6750 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6751 | } |
| 6752 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6753 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6754 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6755 | * |
| 6756 | * This function can fail with an abort() due to an Lua critical error. |
| 6757 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6758 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6759 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6760 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6761 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6762 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6763 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6764 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6765 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6766 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6767 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6768 | if (!rule->arg.hlua_rule) { |
| 6769 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6770 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6771 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6772 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6773 | /* Memory for arguments. */ |
| 6774 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6775 | if (!rule->arg.hlua_rule->args) { |
| 6776 | memprintf(err, "out of memory error"); |
| 6777 | return ACT_RET_PRS_ERR; |
| 6778 | } |
| 6779 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6780 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6781 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6782 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6783 | /* Expect some arguments */ |
| 6784 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6785 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6786 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6787 | return ACT_RET_PRS_ERR; |
| 6788 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6789 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6790 | if (!rule->arg.hlua_rule->args[i]) { |
| 6791 | memprintf(err, "out of memory error"); |
| 6792 | return ACT_RET_PRS_ERR; |
| 6793 | } |
| 6794 | (*cur_arg)++; |
| 6795 | } |
| 6796 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6797 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6798 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6799 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6800 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6801 | } |
| 6802 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6803 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6804 | struct act_rule *rule, char **err) |
| 6805 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6806 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6807 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6808 | /* HTTP applets are forbidden in tcp-request rules. |
| 6809 | * HTTP applet request requires everything initilized by |
| 6810 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6811 | * The applet will be immediately initilized, but its before |
| 6812 | * the call of this analyzer. |
| 6813 | */ |
| 6814 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6815 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6816 | return ACT_RET_PRS_ERR; |
| 6817 | } |
| 6818 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6819 | /* Memory for the rule. */ |
| 6820 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6821 | if (!rule->arg.hlua_rule) { |
| 6822 | memprintf(err, "out of memory error"); |
| 6823 | return ACT_RET_PRS_ERR; |
| 6824 | } |
| 6825 | |
| 6826 | /* Reference the Lua function and store the reference. */ |
| 6827 | rule->arg.hlua_rule->fcn = *fcn; |
| 6828 | |
| 6829 | /* TODO: later accept arguments. */ |
| 6830 | rule->arg.hlua_rule->args = NULL; |
| 6831 | |
| 6832 | /* Add applet pointer in the rule. */ |
| 6833 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6834 | rule->applet.name = fcn->name; |
| 6835 | rule->applet.init = hlua_applet_http_init; |
| 6836 | rule->applet.fct = hlua_applet_http_fct; |
| 6837 | rule->applet.release = hlua_applet_http_release; |
| 6838 | rule->applet.timeout = hlua_timeout_applet; |
| 6839 | |
| 6840 | return ACT_RET_PRS_OK; |
| 6841 | } |
| 6842 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6843 | /* This function is an LUA binding used for registering |
| 6844 | * "sample-conv" functions. It expects a converter name used |
| 6845 | * in the haproxy configuration file, and an LUA function. |
| 6846 | */ |
| 6847 | __LJMP static int hlua_register_action(lua_State *L) |
| 6848 | { |
| 6849 | struct action_kw_list *akl; |
| 6850 | const char *name; |
| 6851 | int ref; |
| 6852 | int len; |
| 6853 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6854 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6855 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6856 | /* Initialise the number of expected arguments at 0. */ |
| 6857 | nargs = 0; |
| 6858 | |
| 6859 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6860 | 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] | 6861 | |
| 6862 | /* First argument : converter name. */ |
| 6863 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6864 | |
| 6865 | /* Second argument : environment. */ |
| 6866 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6867 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6868 | |
| 6869 | /* Third argument : lua function. */ |
| 6870 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6871 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6872 | /* 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] | 6873 | if (lua_gettop(L) >= 4) |
| 6874 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6875 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6876 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6877 | lua_pushnil(L); |
| 6878 | while (lua_next(L, 2) != 0) { |
| 6879 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6880 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6881 | |
| 6882 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6883 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6884 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6885 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6886 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6887 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6888 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6889 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6890 | |
| 6891 | /* Fill fcn. */ |
| 6892 | fcn->name = strdup(name); |
| 6893 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6894 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6895 | fcn->function_ref = ref; |
| 6896 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6897 | /* Set the expected number od arguments. */ |
| 6898 | fcn->nargs = nargs; |
| 6899 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6900 | /* List head */ |
| 6901 | akl->list.n = akl->list.p = NULL; |
| 6902 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6903 | /* action keyword. */ |
| 6904 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6905 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6906 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6907 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6908 | |
| 6909 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6910 | |
| 6911 | akl->kw[0].match_pfx = 0; |
| 6912 | akl->kw[0].private = fcn; |
| 6913 | akl->kw[0].parse = action_register_lua; |
| 6914 | |
| 6915 | /* select the action registering point. */ |
| 6916 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6917 | tcp_req_cont_keywords_register(akl); |
| 6918 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6919 | tcp_res_cont_keywords_register(akl); |
| 6920 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6921 | http_req_keywords_register(akl); |
| 6922 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6923 | http_res_keywords_register(akl); |
| 6924 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6925 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6926 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6927 | "are expected.", lua_tostring(L, -1))); |
| 6928 | |
| 6929 | /* pop the environment string. */ |
| 6930 | lua_pop(L, 1); |
| 6931 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6932 | return ACT_RET_PRS_OK; |
| 6933 | } |
| 6934 | |
| 6935 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6936 | struct act_rule *rule, char **err) |
| 6937 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6938 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6939 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 6940 | if (px->mode == PR_MODE_HTTP) { |
| 6941 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6942 | return ACT_RET_PRS_ERR; |
| 6943 | } |
| 6944 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6945 | /* Memory for the rule. */ |
| 6946 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6947 | if (!rule->arg.hlua_rule) { |
| 6948 | memprintf(err, "out of memory error"); |
| 6949 | return ACT_RET_PRS_ERR; |
| 6950 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6951 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6952 | /* Reference the Lua function and store the reference. */ |
| 6953 | rule->arg.hlua_rule->fcn = *fcn; |
| 6954 | |
| 6955 | /* TODO: later accept arguments. */ |
| 6956 | rule->arg.hlua_rule->args = NULL; |
| 6957 | |
| 6958 | /* Add applet pointer in the rule. */ |
| 6959 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6960 | rule->applet.name = fcn->name; |
| 6961 | rule->applet.init = hlua_applet_tcp_init; |
| 6962 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6963 | rule->applet.release = hlua_applet_tcp_release; |
| 6964 | rule->applet.timeout = hlua_timeout_applet; |
| 6965 | |
| 6966 | return 0; |
| 6967 | } |
| 6968 | |
| 6969 | /* This function is an LUA binding used for registering |
| 6970 | * "sample-conv" functions. It expects a converter name used |
| 6971 | * in the haproxy configuration file, and an LUA function. |
| 6972 | */ |
| 6973 | __LJMP static int hlua_register_service(lua_State *L) |
| 6974 | { |
| 6975 | struct action_kw_list *akl; |
| 6976 | const char *name; |
| 6977 | const char *env; |
| 6978 | int ref; |
| 6979 | int len; |
| 6980 | struct hlua_function *fcn; |
| 6981 | |
| 6982 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 6983 | |
| 6984 | /* First argument : converter name. */ |
| 6985 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6986 | |
| 6987 | /* Second argument : environment. */ |
| 6988 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 6989 | |
| 6990 | /* Third argument : lua function. */ |
| 6991 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6992 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6993 | /* Allocate and fill the sample fetch keyword struct. */ |
| 6994 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 6995 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6996 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6997 | fcn = calloc(1, sizeof(*fcn)); |
| 6998 | if (!fcn) |
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 | |
| 7001 | /* Fill fcn. */ |
| 7002 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7003 | fcn->name = calloc(1, len); |
| 7004 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7005 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7006 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7007 | fcn->function_ref = ref; |
| 7008 | |
| 7009 | /* List head */ |
| 7010 | akl->list.n = akl->list.p = NULL; |
| 7011 | |
| 7012 | /* converter keyword. */ |
| 7013 | len = strlen("lua.") + strlen(name) + 1; |
| 7014 | akl->kw[0].kw = calloc(1, len); |
| 7015 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7016 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7017 | |
| 7018 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7019 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7020 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7021 | if (strcmp(env, "tcp") == 0) |
| 7022 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7023 | else if (strcmp(env, "http") == 0) |
| 7024 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7025 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7026 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7027 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7028 | |
| 7029 | akl->kw[0].match_pfx = 0; |
| 7030 | akl->kw[0].private = fcn; |
| 7031 | |
| 7032 | /* End of array. */ |
| 7033 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7034 | |
| 7035 | /* Register this new converter */ |
| 7036 | service_keywords_register(akl); |
| 7037 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7038 | return 0; |
| 7039 | } |
| 7040 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7041 | /* This function initialises Lua cli handler. It copies the |
| 7042 | * arguments in the Lua stack and create channel IO objects. |
| 7043 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7044 | 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] | 7045 | { |
| 7046 | struct hlua *hlua; |
| 7047 | struct hlua_function *fcn; |
| 7048 | int i; |
| 7049 | const char *error; |
| 7050 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7051 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7052 | appctx->ctx.hlua_cli.fcn = private; |
| 7053 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7054 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7055 | if (!hlua) { |
| 7056 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7057 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7058 | } |
| 7059 | HLUA_INIT(hlua); |
| 7060 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7061 | |
| 7062 | /* Create task used by signal to wakeup applets. |
| 7063 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7064 | * applet_http. It is absolutely compatible. |
| 7065 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7066 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7067 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7068 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7069 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7070 | } |
| 7071 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7072 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7073 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7074 | |
| 7075 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7076 | 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] | 7077 | 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] | 7078 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7079 | } |
| 7080 | |
| 7081 | /* The following Lua calls can fail. */ |
| 7082 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7083 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7084 | error = lua_tostring(hlua->T, -1); |
| 7085 | else |
| 7086 | error = "critical error"; |
| 7087 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7088 | goto error; |
| 7089 | } |
| 7090 | |
| 7091 | /* Check stack available size. */ |
| 7092 | if (!lua_checkstack(hlua->T, 2)) { |
| 7093 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7094 | goto error; |
| 7095 | } |
| 7096 | |
| 7097 | /* Restore the function in the stack. */ |
| 7098 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7099 | |
| 7100 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7101 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7102 | */ |
| 7103 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7104 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7105 | goto error; |
| 7106 | } |
| 7107 | hlua->nargs = 1; |
| 7108 | |
| 7109 | /* push keywords in the stack. */ |
| 7110 | for (i = 0; *args[i]; i++) { |
| 7111 | /* Check stack available size. */ |
| 7112 | if (!lua_checkstack(hlua->T, 1)) { |
| 7113 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7114 | goto error; |
| 7115 | } |
| 7116 | lua_pushstring(hlua->T, args[i]); |
| 7117 | hlua->nargs++; |
| 7118 | } |
| 7119 | |
| 7120 | /* We must initialize the execution timeouts. */ |
| 7121 | hlua->max_time = hlua_timeout_session; |
| 7122 | |
| 7123 | /* At this point the execution is safe. */ |
| 7124 | RESET_SAFE_LJMP(hlua->T); |
| 7125 | |
| 7126 | /* It's ok */ |
| 7127 | return 0; |
| 7128 | |
| 7129 | /* It's not ok. */ |
| 7130 | error: |
| 7131 | RESET_SAFE_LJMP(hlua->T); |
| 7132 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7133 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7134 | return 1; |
| 7135 | } |
| 7136 | |
| 7137 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7138 | { |
| 7139 | struct hlua *hlua; |
| 7140 | struct stream_interface *si; |
| 7141 | struct hlua_function *fcn; |
| 7142 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7143 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7144 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7145 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7146 | |
| 7147 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7148 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7149 | return 1; |
| 7150 | |
| 7151 | /* Execute the function. */ |
| 7152 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7153 | |
| 7154 | /* finished. */ |
| 7155 | case HLUA_E_OK: |
| 7156 | return 1; |
| 7157 | |
| 7158 | /* yield. */ |
| 7159 | case HLUA_E_AGAIN: |
| 7160 | /* We want write. */ |
| 7161 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7162 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7163 | /* Set the timeout. */ |
| 7164 | if (hlua->wake_time != TICK_ETERNITY) |
| 7165 | task_schedule(hlua->task, hlua->wake_time); |
| 7166 | return 0; |
| 7167 | |
| 7168 | /* finished with error. */ |
| 7169 | case HLUA_E_ERRMSG: |
| 7170 | /* Display log. */ |
| 7171 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7172 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7173 | lua_pop(hlua->T, 1); |
| 7174 | return 1; |
| 7175 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7176 | case HLUA_E_ETMOUT: |
| 7177 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7178 | fcn->name); |
| 7179 | return 1; |
| 7180 | |
| 7181 | case HLUA_E_NOMEM: |
| 7182 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7183 | fcn->name); |
| 7184 | return 1; |
| 7185 | |
| 7186 | case HLUA_E_YIELD: /* unexpected */ |
| 7187 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7188 | fcn->name); |
| 7189 | return 1; |
| 7190 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7191 | case HLUA_E_ERR: |
| 7192 | /* Display log. */ |
| 7193 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7194 | fcn->name); |
| 7195 | return 1; |
| 7196 | |
| 7197 | default: |
| 7198 | return 1; |
| 7199 | } |
| 7200 | |
| 7201 | return 1; |
| 7202 | } |
| 7203 | |
| 7204 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7205 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7206 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7207 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7208 | } |
| 7209 | |
| 7210 | /* This function is an LUA binding used for registering |
| 7211 | * new keywords in the cli. It expects a list of keywords |
| 7212 | * which are the "path". It is limited to 5 keywords. A |
| 7213 | * description of the command, a function to be executed |
| 7214 | * for the parsing and a function for io handlers. |
| 7215 | */ |
| 7216 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7217 | { |
| 7218 | struct cli_kw_list *cli_kws; |
| 7219 | const char *message; |
| 7220 | int ref_io; |
| 7221 | int len; |
| 7222 | struct hlua_function *fcn; |
| 7223 | int index; |
| 7224 | int i; |
| 7225 | |
| 7226 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7227 | |
| 7228 | /* First argument : an array of maximum 5 keywords. */ |
| 7229 | if (!lua_istable(L, 1)) |
| 7230 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7231 | |
| 7232 | /* Second argument : string with contextual message. */ |
| 7233 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7234 | |
| 7235 | /* Third and fourth argument : lua function. */ |
| 7236 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7237 | |
| 7238 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7239 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7240 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7241 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7242 | fcn = calloc(1, sizeof(*fcn)); |
| 7243 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7244 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7245 | |
| 7246 | /* Fill path. */ |
| 7247 | index = 0; |
| 7248 | lua_pushnil(L); |
| 7249 | while(lua_next(L, 1) != 0) { |
| 7250 | if (index >= 5) |
| 7251 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7252 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7253 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7254 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7255 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7256 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7257 | index++; |
| 7258 | lua_pop(L, 1); |
| 7259 | } |
| 7260 | |
| 7261 | /* Copy help message. */ |
| 7262 | cli_kws->kw[0].usage = strdup(message); |
| 7263 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7264 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7265 | |
| 7266 | /* Fill fcn io handler. */ |
| 7267 | len = strlen("<lua.cli>") + 1; |
| 7268 | for (i = 0; i < index; i++) |
| 7269 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7270 | fcn->name = calloc(1, len); |
| 7271 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7272 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7273 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7274 | for (i = 0; i < index; i++) { |
| 7275 | strncat((char *)fcn->name, ".", len); |
| 7276 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7277 | } |
| 7278 | strncat((char *)fcn->name, ">", len); |
| 7279 | fcn->function_ref = ref_io; |
| 7280 | |
| 7281 | /* Fill last entries. */ |
| 7282 | cli_kws->kw[0].private = fcn; |
| 7283 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7284 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7285 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7286 | |
| 7287 | /* Register this new converter */ |
| 7288 | cli_register_kw(cli_kws); |
| 7289 | |
| 7290 | return 0; |
| 7291 | } |
| 7292 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7293 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7294 | struct proxy *defpx, const char *file, int line, |
| 7295 | char **err, unsigned int *timeout) |
| 7296 | { |
| 7297 | const char *error; |
| 7298 | |
| 7299 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7300 | if (error == PARSE_TIME_OVER) { |
| 7301 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7302 | args[1], args[0]); |
| 7303 | return -1; |
| 7304 | } |
| 7305 | else if (error == PARSE_TIME_UNDER) { |
| 7306 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7307 | args[1], args[0]); |
| 7308 | return -1; |
| 7309 | } |
| 7310 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7311 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7312 | return -1; |
| 7313 | } |
| 7314 | return 0; |
| 7315 | } |
| 7316 | |
| 7317 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7318 | struct proxy *defpx, const char *file, int line, |
| 7319 | char **err) |
| 7320 | { |
| 7321 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7322 | file, line, err, &hlua_timeout_session); |
| 7323 | } |
| 7324 | |
| 7325 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7326 | struct proxy *defpx, const char *file, int line, |
| 7327 | char **err) |
| 7328 | { |
| 7329 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7330 | file, line, err, &hlua_timeout_task); |
| 7331 | } |
| 7332 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7333 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7334 | struct proxy *defpx, const char *file, int line, |
| 7335 | char **err) |
| 7336 | { |
| 7337 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7338 | file, line, err, &hlua_timeout_applet); |
| 7339 | } |
| 7340 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7341 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7342 | struct proxy *defpx, const char *file, int line, |
| 7343 | char **err) |
| 7344 | { |
| 7345 | char *error; |
| 7346 | |
| 7347 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7348 | if (*error != '\0') { |
| 7349 | memprintf(err, "%s: invalid number", args[0]); |
| 7350 | return -1; |
| 7351 | } |
| 7352 | return 0; |
| 7353 | } |
| 7354 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7355 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7356 | struct proxy *defpx, const char *file, int line, |
| 7357 | char **err) |
| 7358 | { |
| 7359 | char *error; |
| 7360 | |
| 7361 | if (*(args[1]) == 0) { |
| 7362 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7363 | return -1; |
| 7364 | } |
| 7365 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7366 | if (*error != '\0') { |
| 7367 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7368 | return -1; |
| 7369 | } |
| 7370 | return 0; |
| 7371 | } |
| 7372 | |
| 7373 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7374 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7375 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7376 | * the main lua entry point. |
| 7377 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7378 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7379 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7380 | * |
| 7381 | * In some error case, LUA set an error message in top of the stack. This function |
| 7382 | * 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] | 7383 | * |
| 7384 | * This function can fail with an abort() due to an Lua critical error. |
| 7385 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7386 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7387 | */ |
| 7388 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7389 | struct proxy *defpx, const char *file, int line, |
| 7390 | char **err) |
| 7391 | { |
| 7392 | int error; |
| 7393 | |
| 7394 | /* Just load and compile the file. */ |
| 7395 | error = luaL_loadfile(gL.T, args[1]); |
| 7396 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7397 | 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] | 7398 | lua_pop(gL.T, 1); |
| 7399 | return -1; |
| 7400 | } |
| 7401 | |
| 7402 | /* If no syntax error where detected, execute the code. */ |
| 7403 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7404 | switch (error) { |
| 7405 | case LUA_OK: |
| 7406 | break; |
| 7407 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7408 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7409 | lua_pop(gL.T, 1); |
| 7410 | return -1; |
| 7411 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7412 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7413 | return -1; |
| 7414 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7415 | 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] | 7416 | lua_pop(gL.T, 1); |
| 7417 | return -1; |
| 7418 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7419 | 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] | 7420 | lua_pop(gL.T, 1); |
| 7421 | return -1; |
| 7422 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7423 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7424 | lua_pop(gL.T, 1); |
| 7425 | return -1; |
| 7426 | } |
| 7427 | |
| 7428 | return 0; |
| 7429 | } |
| 7430 | |
| 7431 | /* configuration keywords declaration */ |
| 7432 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7433 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7434 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7435 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7436 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7437 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7438 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7439 | { 0, NULL, NULL }, |
| 7440 | }}; |
| 7441 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7442 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7443 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7444 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7445 | /* This function can fail with an abort() due to an Lua critical error. |
| 7446 | * We are in the initialisation process of HAProxy, this abort() is |
| 7447 | * tolerated. |
| 7448 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7449 | int hlua_post_init() |
| 7450 | { |
| 7451 | struct hlua_init_function *init; |
| 7452 | const char *msg; |
| 7453 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7454 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7455 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7456 | /* Call post initialisation function in safe environement. */ |
| 7457 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7458 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7459 | error = lua_tostring(gL.T, -1); |
| 7460 | else |
| 7461 | error = "critical error"; |
| 7462 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7463 | exit(1); |
| 7464 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7465 | |
| 7466 | #if USE_OPENSSL |
| 7467 | /* Initialize SSL server. */ |
| 7468 | if (socket_ssl.xprt->prepare_srv) { |
| 7469 | int saved_used_backed = global.ssl_used_backend; |
| 7470 | // don't affect maxconn automatic computation |
| 7471 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7472 | global.ssl_used_backend = saved_used_backed; |
| 7473 | } |
| 7474 | #endif |
| 7475 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7476 | hlua_fcn_post_init(gL.T); |
| 7477 | RESET_SAFE_LJMP(gL.T); |
| 7478 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7479 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7480 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7481 | ret = hlua_ctx_resume(&gL, 0); |
| 7482 | switch (ret) { |
| 7483 | case HLUA_E_OK: |
| 7484 | lua_pop(gL.T, -1); |
| 7485 | return 1; |
| 7486 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7487 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7488 | return 0; |
| 7489 | case HLUA_E_ERRMSG: |
| 7490 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7491 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7492 | return 0; |
| 7493 | case HLUA_E_ERR: |
| 7494 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7495 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7496 | return 0; |
| 7497 | } |
| 7498 | } |
| 7499 | return 1; |
| 7500 | } |
| 7501 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7502 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7503 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7504 | * is the previously allocated size or the kind of object in case of a new |
| 7505 | * allocation. <nsize> is the requested new size. |
| 7506 | */ |
| 7507 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7508 | { |
| 7509 | struct hlua_mem_allocator *zone = ud; |
| 7510 | |
| 7511 | if (nsize == 0) { |
| 7512 | /* it's a free */ |
| 7513 | if (ptr) |
| 7514 | zone->allocated -= osize; |
| 7515 | free(ptr); |
| 7516 | return NULL; |
| 7517 | } |
| 7518 | |
| 7519 | if (!ptr) { |
| 7520 | /* it's a new allocation */ |
| 7521 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7522 | return NULL; |
| 7523 | |
| 7524 | ptr = malloc(nsize); |
| 7525 | if (ptr) |
| 7526 | zone->allocated += nsize; |
| 7527 | return ptr; |
| 7528 | } |
| 7529 | |
| 7530 | /* it's a realloc */ |
| 7531 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7532 | return NULL; |
| 7533 | |
| 7534 | ptr = realloc(ptr, nsize); |
| 7535 | if (ptr) |
| 7536 | zone->allocated += nsize - osize; |
| 7537 | return ptr; |
| 7538 | } |
| 7539 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7540 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7541 | * We are in the initialisation process of HAProxy, this abort() is |
| 7542 | * tolerated. |
| 7543 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7544 | void hlua_init(void) |
| 7545 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7546 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7547 | int idx; |
| 7548 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7549 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7550 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7551 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7552 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7553 | struct srv_kw *kw; |
| 7554 | int tmp_error; |
| 7555 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7556 | char *args[] = { /* SSL client configuration. */ |
| 7557 | "ssl", |
| 7558 | "verify", |
| 7559 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7560 | NULL |
| 7561 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7562 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7563 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7564 | /* Init main lua stack. */ |
| 7565 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7566 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7567 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7568 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7569 | hlua_sethlua(&gL); |
| 7570 | gL.Tref = LUA_REFNIL; |
| 7571 | gL.task = NULL; |
| 7572 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7573 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7574 | * the Lua function can fail with an abort. We are in the initialisation |
| 7575 | * process of HAProxy, this abort() is tolerated. |
| 7576 | */ |
| 7577 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7578 | /* Initialise lua. */ |
| 7579 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7580 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7581 | /* Set safe environment for the initialisation. */ |
| 7582 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7583 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7584 | error_msg = lua_tostring(gL.T, -1); |
| 7585 | else |
| 7586 | error_msg = "critical error"; |
| 7587 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7588 | exit(1); |
| 7589 | } |
| 7590 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7591 | /* |
| 7592 | * |
| 7593 | * Create "core" object. |
| 7594 | * |
| 7595 | */ |
| 7596 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7597 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7598 | lua_newtable(gL.T); |
| 7599 | |
| 7600 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7601 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7602 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7603 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7604 | /* Register special functions. */ |
| 7605 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7606 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7607 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7608 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7609 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7610 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7611 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7612 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7613 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7614 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7615 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7616 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7617 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7618 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7619 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7620 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7621 | hlua_class_function(gL.T, "log", hlua_log); |
| 7622 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7623 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7624 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7625 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7626 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7627 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7628 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7629 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7630 | |
| 7631 | /* |
| 7632 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7633 | * Register class Map |
| 7634 | * |
| 7635 | */ |
| 7636 | |
| 7637 | /* This table entry is the object "Map" base. */ |
| 7638 | lua_newtable(gL.T); |
| 7639 | |
| 7640 | /* register pattern types. */ |
| 7641 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7642 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7643 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7644 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7645 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7646 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7647 | |
| 7648 | /* register constructor. */ |
| 7649 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7650 | |
| 7651 | /* Create and fill the metatable. */ |
| 7652 | lua_newtable(gL.T); |
| 7653 | |
| 7654 | /* Create and fille the __index entry. */ |
| 7655 | lua_pushstring(gL.T, "__index"); |
| 7656 | lua_newtable(gL.T); |
| 7657 | |
| 7658 | /* Register . */ |
| 7659 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7660 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7661 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7662 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7663 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7664 | /* Register previous table in the registry with reference and named entry. |
| 7665 | * The function hlua_register_metatable() pops the stack, so we |
| 7666 | * previously create a copy of the table. |
| 7667 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7668 | 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] | 7669 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7670 | |
| 7671 | /* Assign the metatable to the mai Map object. */ |
| 7672 | lua_setmetatable(gL.T, -2); |
| 7673 | |
| 7674 | /* Set a name to the table. */ |
| 7675 | lua_setglobal(gL.T, "Map"); |
| 7676 | |
| 7677 | /* |
| 7678 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7679 | * Register class Channel |
| 7680 | * |
| 7681 | */ |
| 7682 | |
| 7683 | /* Create and fill the metatable. */ |
| 7684 | lua_newtable(gL.T); |
| 7685 | |
| 7686 | /* Create and fille the __index entry. */ |
| 7687 | lua_pushstring(gL.T, "__index"); |
| 7688 | lua_newtable(gL.T); |
| 7689 | |
| 7690 | /* Register . */ |
| 7691 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7692 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7693 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7694 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7695 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7696 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7697 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7698 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7699 | 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] | 7700 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7701 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7702 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7703 | |
| 7704 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7705 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7706 | |
| 7707 | /* |
| 7708 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7709 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7710 | * |
| 7711 | */ |
| 7712 | |
| 7713 | /* Create and fill the metatable. */ |
| 7714 | lua_newtable(gL.T); |
| 7715 | |
| 7716 | /* Create and fille the __index entry. */ |
| 7717 | lua_pushstring(gL.T, "__index"); |
| 7718 | lua_newtable(gL.T); |
| 7719 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7720 | /* Browse existing fetches and create the associated |
| 7721 | * object method. |
| 7722 | */ |
| 7723 | sf = NULL; |
| 7724 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7725 | |
| 7726 | /* Dont register the keywork if the arguments check function are |
| 7727 | * not safe during the runtime. |
| 7728 | */ |
| 7729 | if ((sf->val_args != NULL) && |
| 7730 | (sf->val_args != val_payload_lv) && |
| 7731 | (sf->val_args != val_hdr)) |
| 7732 | continue; |
| 7733 | |
| 7734 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7735 | * by an underscore. |
| 7736 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7737 | strncpy(trash.area, sf->kw, trash.size); |
| 7738 | trash.area[trash.size - 1] = '\0'; |
| 7739 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7740 | if (*p == '.' || *p == '-' || *p == '+') |
| 7741 | *p = '_'; |
| 7742 | |
| 7743 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7744 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7745 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7746 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7747 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7748 | } |
| 7749 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7750 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7751 | |
| 7752 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7753 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7754 | |
| 7755 | /* |
| 7756 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7757 | * Register class Converters |
| 7758 | * |
| 7759 | */ |
| 7760 | |
| 7761 | /* Create and fill the metatable. */ |
| 7762 | lua_newtable(gL.T); |
| 7763 | |
| 7764 | /* Create and fill the __index entry. */ |
| 7765 | lua_pushstring(gL.T, "__index"); |
| 7766 | lua_newtable(gL.T); |
| 7767 | |
| 7768 | /* Browse existing converters and create the associated |
| 7769 | * object method. |
| 7770 | */ |
| 7771 | sc = NULL; |
| 7772 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7773 | /* Dont register the keywork if the arguments check function are |
| 7774 | * not safe during the runtime. |
| 7775 | */ |
| 7776 | if (sc->val_args != NULL) |
| 7777 | continue; |
| 7778 | |
| 7779 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7780 | * by an underscore. |
| 7781 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7782 | strncpy(trash.area, sc->kw, trash.size); |
| 7783 | trash.area[trash.size - 1] = '\0'; |
| 7784 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7785 | if (*p == '.' || *p == '-' || *p == '+') |
| 7786 | *p = '_'; |
| 7787 | |
| 7788 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7789 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7790 | lua_pushlightuserdata(gL.T, sc); |
| 7791 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7792 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7793 | } |
| 7794 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7795 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7796 | |
| 7797 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7798 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7799 | |
| 7800 | /* |
| 7801 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7802 | * Register class HTTP |
| 7803 | * |
| 7804 | */ |
| 7805 | |
| 7806 | /* Create and fill the metatable. */ |
| 7807 | lua_newtable(gL.T); |
| 7808 | |
| 7809 | /* Create and fille the __index entry. */ |
| 7810 | lua_pushstring(gL.T, "__index"); |
| 7811 | lua_newtable(gL.T); |
| 7812 | |
| 7813 | /* Register Lua functions. */ |
| 7814 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7815 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7816 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7817 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7818 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7819 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7820 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7821 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7822 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7823 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7824 | |
| 7825 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7826 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7827 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7828 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7829 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7830 | 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] | 7831 | 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] | 7832 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7833 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7834 | |
| 7835 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7836 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7837 | |
| 7838 | /* |
| 7839 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7840 | * Register class AppletTCP |
| 7841 | * |
| 7842 | */ |
| 7843 | |
| 7844 | /* Create and fill the metatable. */ |
| 7845 | lua_newtable(gL.T); |
| 7846 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7847 | /* Create and fille the __index entry. */ |
| 7848 | lua_pushstring(gL.T, "__index"); |
| 7849 | lua_newtable(gL.T); |
| 7850 | |
| 7851 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7852 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7853 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7854 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7855 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7856 | 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] | 7857 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7858 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7859 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7860 | |
| 7861 | lua_settable(gL.T, -3); |
| 7862 | |
| 7863 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7864 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7865 | |
| 7866 | /* |
| 7867 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7868 | * Register class AppletHTTP |
| 7869 | * |
| 7870 | */ |
| 7871 | |
| 7872 | /* Create and fill the metatable. */ |
| 7873 | lua_newtable(gL.T); |
| 7874 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7875 | /* Create and fille the __index entry. */ |
| 7876 | lua_pushstring(gL.T, "__index"); |
| 7877 | lua_newtable(gL.T); |
| 7878 | |
| 7879 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7880 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7881 | 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] | 7882 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7883 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7884 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7885 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7886 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7887 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7888 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7889 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7890 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7891 | |
| 7892 | lua_settable(gL.T, -3); |
| 7893 | |
| 7894 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7895 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7896 | |
| 7897 | /* |
| 7898 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7899 | * Register class TXN |
| 7900 | * |
| 7901 | */ |
| 7902 | |
| 7903 | /* Create and fill the metatable. */ |
| 7904 | lua_newtable(gL.T); |
| 7905 | |
| 7906 | /* Create and fille the __index entry. */ |
| 7907 | lua_pushstring(gL.T, "__index"); |
| 7908 | lua_newtable(gL.T); |
| 7909 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7910 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7911 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7912 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 7913 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 7914 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 7915 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 7916 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 7917 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 7918 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7919 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 7920 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 7921 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 7922 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7923 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7924 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7925 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7926 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7927 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7928 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7929 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7930 | |
| 7931 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7932 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7933 | |
| 7934 | /* |
| 7935 | * |
| 7936 | * Register class Socket |
| 7937 | * |
| 7938 | */ |
| 7939 | |
| 7940 | /* Create and fill the metatable. */ |
| 7941 | lua_newtable(gL.T); |
| 7942 | |
| 7943 | /* Create and fille the __index entry. */ |
| 7944 | lua_pushstring(gL.T, "__index"); |
| 7945 | lua_newtable(gL.T); |
| 7946 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7947 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7948 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7949 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7950 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7951 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7952 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7953 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7954 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7955 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7956 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7957 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7958 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7959 | 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] | 7960 | |
| 7961 | /* Register the garbage collector entry. */ |
| 7962 | lua_pushstring(gL.T, "__gc"); |
| 7963 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7964 | 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] | 7965 | |
| 7966 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7967 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7968 | |
| 7969 | /* Proxy and server configuration initialisation. */ |
| 7970 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 7971 | init_new_proxy(&socket_proxy); |
| 7972 | socket_proxy.parent = NULL; |
| 7973 | socket_proxy.last_change = now.tv_sec; |
| 7974 | socket_proxy.id = "LUA-SOCKET"; |
| 7975 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 7976 | socket_proxy.maxconn = 0; |
| 7977 | socket_proxy.accept = NULL; |
| 7978 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 7979 | socket_proxy.srv = NULL; |
| 7980 | socket_proxy.conn_retries = 0; |
| 7981 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 7982 | |
| 7983 | /* Init TCP server: unchanged parameters */ |
| 7984 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 7985 | socket_tcp.next = NULL; |
| 7986 | socket_tcp.proxy = &socket_proxy; |
| 7987 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 7988 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7989 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 7990 | socket_tcp.priv_conns = NULL; |
| 7991 | socket_tcp.idle_conns = NULL; |
| 7992 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 7993 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7994 | socket_tcp.last_change = 0; |
| 7995 | socket_tcp.id = "LUA-TCP-CONN"; |
| 7996 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7997 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 7998 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 7999 | |
| 8000 | /* XXX: Copy default parameter from default server, |
| 8001 | * but the default server is not initialized. |
| 8002 | */ |
| 8003 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8004 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8005 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8006 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8007 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8008 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8009 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8010 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8011 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8012 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8013 | |
| 8014 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8015 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8016 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8017 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8018 | socket_tcp.check.server = &socket_tcp; |
| 8019 | |
| 8020 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8021 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8022 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8023 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8024 | socket_tcp.agent.server = &socket_tcp; |
| 8025 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8026 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8027 | |
| 8028 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8029 | /* Init TCP server: unchanged parameters */ |
| 8030 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8031 | socket_ssl.next = NULL; |
| 8032 | socket_ssl.proxy = &socket_proxy; |
| 8033 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8034 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8035 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8036 | socket_ssl.priv_conns = NULL; |
| 8037 | socket_ssl.idle_conns = NULL; |
| 8038 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8039 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8040 | socket_ssl.last_change = 0; |
| 8041 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8042 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8043 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8044 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8045 | |
| 8046 | /* XXX: Copy default parameter from default server, |
| 8047 | * but the default server is not initialized. |
| 8048 | */ |
| 8049 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8050 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8051 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8052 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8053 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8054 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8055 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8056 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8057 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8058 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8059 | |
| 8060 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8061 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8062 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8063 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8064 | socket_ssl.check.server = &socket_ssl; |
| 8065 | |
| 8066 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8067 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8068 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8069 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8070 | socket_ssl.agent.server = &socket_ssl; |
| 8071 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8072 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8073 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8074 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8075 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8076 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8077 | /* |
| 8078 | * |
| 8079 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8080 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8081 | * features like client certificates and ssl_verify. |
| 8082 | * |
| 8083 | */ |
| 8084 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8085 | if (tmp_error != 0) { |
| 8086 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8087 | abort(); /* This must be never arrives because the command line |
| 8088 | not editable by the user. */ |
| 8089 | } |
| 8090 | idx += kw->skip; |
| 8091 | } |
| 8092 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8093 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8094 | |
| 8095 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8096 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8097 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8098 | static void hlua_register_build_options(void) |
| 8099 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8100 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8101 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8102 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8103 | hap_register_build_opts(ptr, 1); |
| 8104 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8105 | |
| 8106 | INITCALL0(STG_REGISTER, hlua_register_build_options); |