Thierry Fournier | e726b14 | 2016-02-11 17:57:57 +0100 | [diff] [blame] | 1 | /* |
| 2 | * Lua unsafe core engine |
| 3 | * |
| 4 | * Copyright 2015-2016 Thierry Fournier <tfournier@arpalert.org> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 13 | #include <ctype.h> |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 14 | #include <limits.h> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 15 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 16 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 17 | #include <lauxlib.h> |
| 18 | #include <lua.h> |
| 19 | #include <lualib.h> |
| 20 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 21 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 22 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 23 | #endif |
| 24 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 25 | #include <ebpttree.h> |
| 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | b059b89 | 2018-10-16 17:57:36 +0200 | [diff] [blame] | 28 | #include <common/compiler.h> |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 29 | #include <common/hathreads.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/initcall.h> |
| 31 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 32 | #include <common/h1.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 45 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 46 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 47 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 48 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 49 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 50 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 51 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 52 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 53 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 54 | #include <proto/payload.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 55 | #include <proto/http_ana.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 56 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 59 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 60 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 61 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 62 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 63 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 64 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 65 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 66 | * macro is used only for identifying the function that can |
| 67 | * not return because a longjmp is executed. |
| 68 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 69 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 70 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 71 | */ |
| 72 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 73 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 74 | #define MAY_LJMP(func) func |
| 75 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 76 | /* This couple of function executes securely some Lua calls outside of |
| 77 | * the lua runtime environment. Each Lua call can return a longjmp |
| 78 | * if it encounter a memory error. |
| 79 | * |
| 80 | * Lua documentation extract: |
| 81 | * |
| 82 | * If an error happens outside any protected environment, Lua calls |
| 83 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 84 | * exiting the host application. Your panic function can avoid this |
| 85 | * exit by never returning (e.g., doing a long jump to your own |
| 86 | * recovery point outside Lua). |
| 87 | * |
| 88 | * The panic function runs as if it were a message handler (see |
| 89 | * §2.3); in particular, the error message is at the top of the |
| 90 | * stack. However, there is no guarantee about stack space. To push |
| 91 | * anything on the stack, the panic function must first check the |
| 92 | * available space (see §4.2). |
| 93 | * |
| 94 | * We must check all the Lua entry point. This includes: |
| 95 | * - The include/proto/hlua.h exported functions |
| 96 | * - the task wrapper function |
| 97 | * - The action wrapper function |
| 98 | * - The converters wrapper function |
| 99 | * - The sample-fetch wrapper functions |
| 100 | * |
| 101 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 102 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 103 | * |
| 104 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 105 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 106 | * because they must be exists in the program stack when the longjmp |
| 107 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 108 | * |
| 109 | * Note that the Lua processing is not really thread safe. It provides |
| 110 | * heavy system which consists to add our own lock function in the Lua |
| 111 | * code and recompile the library. This system will probably not accepted |
| 112 | * by maintainers of various distribs. |
| 113 | * |
| 114 | * Our main excution point of the Lua is the function lua_resume(). A |
| 115 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 116 | * of function and a lua_unlock() at the end of the function. So I |
| 117 | * conclude that the Lua thread safe mode just perform a mutex around |
| 118 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 119 | * easier for distro maintainers. |
| 120 | * |
| 121 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 122 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 123 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 125 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 126 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | static int hlua_panic_safe(lua_State *L) { return 0; } |
| 128 | static int hlua_panic_ljmp(lua_State *L) { longjmp(safe_ljmp_env, 1); } |
| 129 | |
| 130 | #define SET_SAFE_LJMP(__L) \ |
| 131 | ({ \ |
| 132 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 135 | lua_atpanic(__L, hlua_panic_safe); \ |
| 136 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 137 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 138 | } else { \ |
| 139 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 140 | ret = 1; \ |
| 141 | } \ |
| 142 | ret; \ |
| 143 | }) |
| 144 | |
| 145 | /* If we are the last function catching Lua errors, we |
| 146 | * must reset the panic function. |
| 147 | */ |
| 148 | #define RESET_SAFE_LJMP(__L) \ |
| 149 | do { \ |
| 150 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 151 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 152 | } while(0) |
| 153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 154 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 155 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 156 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 158 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 159 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 160 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 161 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 162 | /* The main Lua execution context. */ |
| 163 | struct hlua gL; |
| 164 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | /* This is the memory pool containing struct lua for applets |
| 166 | * (including cli). |
| 167 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 168 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 169 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 170 | /* Used for Socket connection. */ |
| 171 | static struct proxy socket_proxy; |
| 172 | static struct server socket_tcp; |
| 173 | #ifdef USE_OPENSSL |
| 174 | static struct server socket_ssl; |
| 175 | #endif |
| 176 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 177 | /* List head of the function called at the initialisation time. */ |
| 178 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 179 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 180 | /* The following variables contains the reference of the different |
| 181 | * Lua classes. These references are useful for identify metadata |
| 182 | * associated with an object. |
| 183 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 184 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 185 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 186 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 187 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 188 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 189 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 190 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 191 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | static int class_applet_http_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 193 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 194 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 195 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 196 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 197 | * because a task may remain alive during all the haproxy execution. |
| 198 | */ |
| 199 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 200 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 201 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 202 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 203 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 204 | * it is used for preventing infinite loops. |
| 205 | * |
| 206 | * I test the scheer with an infinite loop containing one incrementation |
| 207 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 208 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 209 | * to one interrupt each 10 000 instructions. |
| 210 | * |
| 211 | * configured | Number of |
| 212 | * instructions | loops executed |
| 213 | * between two | in milions |
| 214 | * forced yields | |
| 215 | * ---------------+--------------- |
| 216 | * 10 | 160 |
| 217 | * 500 | 670 |
| 218 | * 1000 | 680 |
| 219 | * 5000 | 700 |
| 220 | * 7000 | 700 |
| 221 | * 8000 | 700 |
| 222 | * 9000 | 710 <- ceil |
| 223 | * 10000 | 710 |
| 224 | * 100000 | 710 |
| 225 | * 1000000 | 710 |
| 226 | * |
| 227 | */ |
| 228 | static unsigned int hlua_nb_instruction = 10000; |
| 229 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 230 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 231 | * be enforced on any memory allocation. |
| 232 | */ |
| 233 | struct hlua_mem_allocator { |
| 234 | size_t allocated; |
| 235 | size_t limit; |
| 236 | }; |
| 237 | |
| 238 | static struct hlua_mem_allocator hlua_global_allocator; |
| 239 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 240 | /* These functions converts types between HAProxy internal args or |
| 241 | * sample and LUA types. Another function permits to check if the |
| 242 | * LUA stack contains arguments according with an required ARG_T |
| 243 | * format. |
| 244 | */ |
| 245 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 246 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 247 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 248 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 249 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 250 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 251 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 252 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 253 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg); |
| 254 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 255 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 256 | do { \ |
| 257 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 258 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 259 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 260 | } while (0) |
| 261 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 262 | /* Used to check an Lua function type in the stack. It creates and |
| 263 | * returns a reference of the function. This function throws an |
| 264 | * error if the rgument is not a "function". |
| 265 | */ |
| 266 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 267 | { |
| 268 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 269 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 270 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 271 | } |
| 272 | lua_pushvalue(L, argno); |
| 273 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 274 | } |
| 275 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 276 | /* Return the string that is of the top of the stack. */ |
| 277 | const char *hlua_get_top_error_string(lua_State *L) |
| 278 | { |
| 279 | if (lua_gettop(L) < 1) |
| 280 | return "unknown error"; |
| 281 | if (lua_type(L, -1) != LUA_TSTRING) |
| 282 | return "unknown error"; |
| 283 | return lua_tostring(L, -1); |
| 284 | } |
| 285 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 286 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 287 | { |
| 288 | lua_Debug ar; |
| 289 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 290 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 291 | int filled = 0; |
| 292 | |
| 293 | while (lua_getstack(L, level++, &ar)) { |
| 294 | |
| 295 | /* Add separator */ |
| 296 | if (filled) |
| 297 | chunk_appendf(msg, ", "); |
| 298 | filled = 1; |
| 299 | |
| 300 | /* Fill fields: |
| 301 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 302 | * 'l': fills in the field currentline; |
| 303 | * 'n': fills in the field name and namewhat; |
| 304 | * 't': fills in the field istailcall; |
| 305 | */ |
| 306 | lua_getinfo(L, "Slnt", &ar); |
| 307 | |
| 308 | /* Append code localisation */ |
| 309 | if (ar.currentline > 0) |
| 310 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 311 | else |
| 312 | chunk_appendf(msg, "%s ", ar.short_src); |
| 313 | |
| 314 | /* |
| 315 | * Get function name |
| 316 | * |
| 317 | * if namewhat is no empty, name is defined. |
| 318 | * what contains "Lua" for Lua function, "C" for C function, |
| 319 | * or "main" for main code. |
| 320 | */ |
| 321 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 322 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 323 | |
| 324 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 325 | chunk_appendf(msg, "main chunk"); |
| 326 | |
| 327 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 328 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 329 | |
| 330 | else /* nothing left... */ |
| 331 | chunk_appendf(msg, "?"); |
| 332 | |
| 333 | |
| 334 | /* Display tailed call */ |
| 335 | if (ar.istailcall) |
| 336 | chunk_appendf(msg, " ..."); |
| 337 | } |
| 338 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 339 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 343 | /* This function check the number of arguments available in the |
| 344 | * stack. If the number of arguments available is not the same |
| 345 | * then <nb> an error is throwed. |
| 346 | */ |
| 347 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 348 | { |
| 349 | if (lua_gettop(L) == nb) |
| 350 | return; |
| 351 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 352 | } |
| 353 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 354 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 355 | * and the line number where the error is encountered. |
| 356 | */ |
| 357 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 358 | { |
| 359 | va_list argp; |
| 360 | va_start(argp, fmt); |
| 361 | luaL_where(L, 1); |
| 362 | lua_pushvfstring(L, fmt, argp); |
| 363 | va_end(argp); |
| 364 | lua_concat(L, 2); |
| 365 | return 1; |
| 366 | } |
| 367 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 368 | /* This functions is used with sample fetch and converters. It |
| 369 | * converts the HAProxy configuration argument in a lua stack |
| 370 | * values. |
| 371 | * |
| 372 | * It takes an array of "arg", and each entry of the array is |
| 373 | * converted and pushed in the LUA stack. |
| 374 | */ |
| 375 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 376 | { |
| 377 | switch (arg->type) { |
| 378 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 379 | case ARGT_TIME: |
| 380 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 381 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 382 | break; |
| 383 | |
| 384 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 385 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 386 | break; |
| 387 | |
| 388 | case ARGT_IPV4: |
| 389 | case ARGT_IPV6: |
| 390 | case ARGT_MSK4: |
| 391 | case ARGT_MSK6: |
| 392 | case ARGT_FE: |
| 393 | case ARGT_BE: |
| 394 | case ARGT_TAB: |
| 395 | case ARGT_SRV: |
| 396 | case ARGT_USR: |
| 397 | case ARGT_MAP: |
| 398 | default: |
| 399 | lua_pushnil(L); |
| 400 | break; |
| 401 | } |
| 402 | return 1; |
| 403 | } |
| 404 | |
| 405 | /* This function take one entrie in an LUA stack at the index "ud", |
| 406 | * and try to convert it in an HAProxy argument entry. This is useful |
| 407 | * with sample fetch wrappers. The input arguments are gived to the |
| 408 | * lua wrapper and converted as arg list by thi function. |
| 409 | */ |
| 410 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 411 | { |
| 412 | switch (lua_type(L, ud)) { |
| 413 | |
| 414 | case LUA_TNUMBER: |
| 415 | case LUA_TBOOLEAN: |
| 416 | arg->type = ARGT_SINT; |
| 417 | arg->data.sint = lua_tointeger(L, ud); |
| 418 | break; |
| 419 | |
| 420 | case LUA_TSTRING: |
| 421 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 422 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 423 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 424 | arg->data.str.size = arg->data.str.data; |
| 425 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 426 | break; |
| 427 | |
| 428 | case LUA_TUSERDATA: |
| 429 | case LUA_TNIL: |
| 430 | case LUA_TTABLE: |
| 431 | case LUA_TFUNCTION: |
| 432 | case LUA_TTHREAD: |
| 433 | case LUA_TLIGHTUSERDATA: |
| 434 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 435 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 436 | break; |
| 437 | } |
| 438 | return 1; |
| 439 | } |
| 440 | |
| 441 | /* the following functions are used to convert a struct sample |
| 442 | * in Lua type. This useful to convert the return of the |
| 443 | * fetchs or converters. |
| 444 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 445 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 446 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 447 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 448 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 450 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 451 | break; |
| 452 | |
| 453 | case SMP_T_BIN: |
| 454 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 455 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 456 | break; |
| 457 | |
| 458 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 459 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 460 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 461 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 462 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 463 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 464 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 465 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 466 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 467 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 468 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 469 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 470 | break; |
| 471 | default: |
| 472 | lua_pushnil(L); |
| 473 | break; |
| 474 | } |
| 475 | break; |
| 476 | |
| 477 | case SMP_T_IPV4: |
| 478 | case SMP_T_IPV6: |
| 479 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 480 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 481 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 482 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 483 | else |
| 484 | lua_pushnil(L); |
| 485 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 486 | default: |
| 487 | lua_pushnil(L); |
| 488 | break; |
| 489 | } |
| 490 | return 1; |
| 491 | } |
| 492 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 493 | /* the following functions are used to convert a struct sample |
| 494 | * in Lua strings. This is useful to convert the return of the |
| 495 | * fetchs or converters. |
| 496 | */ |
| 497 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 498 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 499 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 500 | |
| 501 | case SMP_T_BIN: |
| 502 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 503 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 504 | break; |
| 505 | |
| 506 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 507 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 508 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 509 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 510 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 511 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 512 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 513 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 514 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 515 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 516 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 517 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 518 | break; |
| 519 | default: |
| 520 | lua_pushstring(L, ""); |
| 521 | break; |
| 522 | } |
| 523 | break; |
| 524 | |
| 525 | case SMP_T_SINT: |
| 526 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 527 | case SMP_T_IPV4: |
| 528 | case SMP_T_IPV6: |
| 529 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 530 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 531 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 532 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 533 | else |
| 534 | lua_pushstring(L, ""); |
| 535 | break; |
| 536 | default: |
| 537 | lua_pushstring(L, ""); |
| 538 | break; |
| 539 | } |
| 540 | return 1; |
| 541 | } |
| 542 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 543 | /* the following functions are used to convert an Lua type in a |
| 544 | * struct sample. This is useful to provide data from a converter |
| 545 | * to the LUA code. |
| 546 | */ |
| 547 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 548 | { |
| 549 | switch (lua_type(L, ud)) { |
| 550 | |
| 551 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 552 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 553 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 554 | break; |
| 555 | |
| 556 | |
| 557 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 558 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 559 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 560 | break; |
| 561 | |
| 562 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 563 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 564 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 565 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 566 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 567 | smp->data.u.str.size = smp->data.u.str.data; |
| 568 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 569 | break; |
| 570 | |
| 571 | case LUA_TUSERDATA: |
| 572 | case LUA_TNIL: |
| 573 | case LUA_TTABLE: |
| 574 | case LUA_TFUNCTION: |
| 575 | case LUA_TTHREAD: |
| 576 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 577 | case LUA_TNONE: |
| 578 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 579 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 580 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 581 | break; |
| 582 | } |
| 583 | return 1; |
| 584 | } |
| 585 | |
| 586 | /* This function check the "argp" builded by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 587 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 588 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 589 | * |
| 590 | * This function assumes thant the argp argument contains ARGM_NBARGS + 1 |
| 591 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 592 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 593 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 594 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 595 | { |
| 596 | int min_arg; |
| 597 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 598 | struct proxy *px; |
| 599 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 600 | |
| 601 | idx = 0; |
| 602 | min_arg = ARGM(mask); |
| 603 | mask >>= ARGM_BITS; |
| 604 | |
| 605 | while (1) { |
| 606 | |
| 607 | /* Check oversize. */ |
| 608 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 609 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 610 | } |
| 611 | |
| 612 | /* Check for mandatory arguments. */ |
| 613 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 614 | if (idx < min_arg) { |
| 615 | |
| 616 | /* If miss other argument than the first one, we return an error. */ |
| 617 | if (idx > 0) |
| 618 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 619 | |
| 620 | /* If first argument have a certain type, some default values |
| 621 | * may be used. See the function smp_resolve_args(). |
| 622 | */ |
| 623 | switch (mask & ARGT_MASK) { |
| 624 | |
| 625 | case ARGT_FE: |
| 626 | if (!(p->cap & PR_CAP_FE)) |
| 627 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 628 | argp[idx].data.prx = p; |
| 629 | argp[idx].type = ARGT_FE; |
| 630 | argp[idx+1].type = ARGT_STOP; |
| 631 | break; |
| 632 | |
| 633 | case ARGT_BE: |
| 634 | if (!(p->cap & PR_CAP_BE)) |
| 635 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 636 | argp[idx].data.prx = p; |
| 637 | argp[idx].type = ARGT_BE; |
| 638 | argp[idx+1].type = ARGT_STOP; |
| 639 | break; |
| 640 | |
| 641 | case ARGT_TAB: |
| 642 | argp[idx].data.prx = p; |
| 643 | argp[idx].type = ARGT_TAB; |
| 644 | argp[idx+1].type = ARGT_STOP; |
| 645 | break; |
| 646 | |
| 647 | default: |
| 648 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 649 | break; |
| 650 | } |
| 651 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 652 | return 0; |
| 653 | } |
| 654 | |
| 655 | /* Check for exceed the number of requiered argument. */ |
| 656 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 657 | argp[idx].type != ARGT_STOP) { |
| 658 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 659 | } |
| 660 | |
| 661 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 662 | argp[idx].type == ARGT_STOP) { |
| 663 | return 0; |
| 664 | } |
| 665 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 666 | /* Convert some argument types. */ |
| 667 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 668 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 669 | if (argp[idx].type != ARGT_SINT) |
| 670 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 671 | argp[idx].type = ARGT_SINT; |
| 672 | break; |
| 673 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 674 | case ARGT_TIME: |
| 675 | if (argp[idx].type != ARGT_SINT) |
| 676 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 677 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 678 | break; |
| 679 | |
| 680 | case ARGT_SIZE: |
| 681 | if (argp[idx].type != ARGT_SINT) |
| 682 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 683 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 684 | break; |
| 685 | |
| 686 | case ARGT_FE: |
| 687 | if (argp[idx].type != ARGT_STR) |
| 688 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 689 | memcpy(trash.area, argp[idx].data.str.area, |
| 690 | argp[idx].data.str.data); |
| 691 | trash.area[argp[idx].data.str.data] = 0; |
| 692 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 693 | if (!argp[idx].data.prx) |
| 694 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 695 | argp[idx].type = ARGT_FE; |
| 696 | break; |
| 697 | |
| 698 | case ARGT_BE: |
| 699 | if (argp[idx].type != ARGT_STR) |
| 700 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 701 | memcpy(trash.area, argp[idx].data.str.area, |
| 702 | argp[idx].data.str.data); |
| 703 | trash.area[argp[idx].data.str.data] = 0; |
| 704 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 705 | if (!argp[idx].data.prx) |
| 706 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 707 | argp[idx].type = ARGT_BE; |
| 708 | break; |
| 709 | |
| 710 | case ARGT_TAB: |
| 711 | if (argp[idx].type != ARGT_STR) |
| 712 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 713 | memcpy(trash.area, argp[idx].data.str.area, |
| 714 | argp[idx].data.str.data); |
| 715 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame^] | 716 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 717 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 718 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 719 | argp[idx].type = ARGT_TAB; |
| 720 | break; |
| 721 | |
| 722 | case ARGT_SRV: |
| 723 | if (argp[idx].type != ARGT_STR) |
| 724 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 725 | memcpy(trash.area, argp[idx].data.str.area, |
| 726 | argp[idx].data.str.data); |
| 727 | trash.area[argp[idx].data.str.data] = 0; |
| 728 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 729 | if (sname) { |
| 730 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 731 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 732 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 733 | if (!px) |
| 734 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 735 | } |
| 736 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 737 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 738 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 739 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 740 | argp[idx].data.srv = findserver(px, sname); |
| 741 | if (!argp[idx].data.srv) |
| 742 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 743 | argp[idx].type = ARGT_SRV; |
| 744 | break; |
| 745 | |
| 746 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 747 | memcpy(trash.area, argp[idx].data.str.area, |
| 748 | argp[idx].data.str.data); |
| 749 | trash.area[argp[idx].data.str.data] = 0; |
| 750 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 751 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 752 | argp[idx].type = ARGT_IPV4; |
| 753 | break; |
| 754 | |
| 755 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 756 | memcpy(trash.area, argp[idx].data.str.area, |
| 757 | argp[idx].data.str.data); |
| 758 | trash.area[argp[idx].data.str.data] = 0; |
| 759 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 760 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 761 | argp[idx].type = ARGT_MSK4; |
| 762 | break; |
| 763 | |
| 764 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 765 | memcpy(trash.area, argp[idx].data.str.area, |
| 766 | argp[idx].data.str.data); |
| 767 | trash.area[argp[idx].data.str.data] = 0; |
| 768 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 769 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 770 | argp[idx].type = ARGT_IPV6; |
| 771 | break; |
| 772 | |
| 773 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 774 | memcpy(trash.area, argp[idx].data.str.area, |
| 775 | argp[idx].data.str.data); |
| 776 | trash.area[argp[idx].data.str.data] = 0; |
| 777 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 778 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 779 | argp[idx].type = ARGT_MSK6; |
| 780 | break; |
| 781 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 782 | case ARGT_MAP: |
| 783 | case ARGT_REG: |
| 784 | case ARGT_USR: |
| 785 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 786 | break; |
| 787 | } |
| 788 | |
| 789 | /* Check for type of argument. */ |
| 790 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 791 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 792 | arg_type_names[(mask & ARGT_MASK)], |
| 793 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 794 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 795 | } |
| 796 | |
| 797 | /* Next argument. */ |
| 798 | mask >>= ARGT_BITS; |
| 799 | idx++; |
| 800 | } |
| 801 | } |
| 802 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 803 | /* |
| 804 | * The following functions are used to make correspondance between the the |
| 805 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * |
| 807 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 808 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 809 | */ |
| 810 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 811 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 812 | struct hlua **hlua = lua_getextraspace(L); |
| 813 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 814 | } |
| 815 | static inline void hlua_sethlua(struct hlua *hlua) |
| 816 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 817 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 818 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 819 | } |
| 820 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 821 | /* This function is used to send logs. It try to send on screen (stderr) |
| 822 | * and on the default syslog server. |
| 823 | */ |
| 824 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 825 | { |
| 826 | struct tm tm; |
| 827 | char *p; |
| 828 | |
| 829 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 830 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 831 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 832 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 833 | /* Break the message if exceed the buffer size. */ |
| 834 | *(p-4) = ' '; |
| 835 | *(p-3) = '.'; |
| 836 | *(p-2) = '.'; |
| 837 | *(p-1) = '.'; |
| 838 | break; |
| 839 | } |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 840 | if (isprint(*msg)) |
| 841 | *p = *msg; |
| 842 | else |
| 843 | *p = '.'; |
| 844 | } |
| 845 | *p = '\0'; |
| 846 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 847 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 848 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 849 | get_localtime(date.tv_sec, &tm); |
| 850 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 851 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 852 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 853 | fflush(stderr); |
| 854 | } |
| 855 | } |
| 856 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 857 | /* This function just ensure that the yield will be always |
| 858 | * returned with a timeout and permit to set some flags |
| 859 | */ |
| 860 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 861 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 862 | { |
| 863 | struct hlua *hlua = hlua_gethlua(L); |
| 864 | |
| 865 | /* Set the wake timeout. If timeout is required, we set |
| 866 | * the expiration time. |
| 867 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 868 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 869 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 870 | hlua->flags |= flags; |
| 871 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 872 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 873 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 874 | } |
| 875 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 876 | /* This function initialises the Lua environment stored in the stream. |
| 877 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 878 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 879 | * |
| 880 | * This function is particular. it initialises a new Lua thread. If the |
| 881 | * initialisation fails (example: out of memory error), the lua function |
| 882 | * throws an error (longjmp). |
| 883 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 884 | * In some case (at least one), this function can be called from safe |
| 885 | * environement, so we must not initialise it. While the support of |
| 886 | * threads appear, the safe environment set a lock to ensure only one |
| 887 | * Lua execution at a time. If we initialize safe environment in another |
| 888 | * safe environmenet, we have a dead lock. |
| 889 | * |
| 890 | * set "already_safe" true if the context is initialized form safe |
| 891 | * Lua fonction. |
| 892 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 893 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 894 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 895 | * MUST NOT manipulate the created thread stack state, because it is not |
| 896 | * proctected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 897 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 898 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 899 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 900 | if (!already_safe) { |
| 901 | if (!SET_SAFE_LJMP(gL.T)) { |
| 902 | lua->Tref = LUA_REFNIL; |
| 903 | return 0; |
| 904 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 905 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 906 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 907 | lua->flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 908 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 909 | lua->T = lua_newthread(gL.T); |
| 910 | if (!lua->T) { |
| 911 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 912 | if (!already_safe) |
| 913 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 914 | return 0; |
| 915 | } |
| 916 | hlua_sethlua(lua); |
| 917 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 918 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 919 | if (!already_safe) |
| 920 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 921 | return 1; |
| 922 | } |
| 923 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 924 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 925 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 926 | * is not freed. |
| 927 | */ |
| 928 | void hlua_ctx_destroy(struct hlua *lua) |
| 929 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 930 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 931 | return; |
| 932 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 933 | if (!lua->T) |
| 934 | goto end; |
| 935 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 936 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 937 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 938 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 939 | if (!SET_SAFE_LJMP(lua->T)) |
| 940 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 941 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 943 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 944 | if (!SET_SAFE_LJMP(gL.T)) |
| 945 | return; |
| 946 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 947 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 948 | /* Forces a garbage collecting process. If the Lua program is finished |
| 949 | * without error, we run the GC on the thread pointer. Its freed all |
| 950 | * the unused memory. |
| 951 | * If the thread is finnish with an error or is currently yielded, |
| 952 | * it seems that the GC applied on the thread doesn't clean anything, |
| 953 | * so e run the GC on the main thread. |
| 954 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 955 | * the garbage collection. |
| 956 | */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 957 | if (lua->flags & HLUA_MUST_GC) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 958 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 959 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 960 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 961 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 962 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 963 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 964 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 965 | |
| 966 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 967 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 968 | } |
| 969 | |
| 970 | /* This function is used to restore the Lua context when a coroutine |
| 971 | * fails. This function copy the common memory between old coroutine |
| 972 | * and the new coroutine. The old coroutine is destroyed, and its |
| 973 | * replaced by the new coroutine. |
| 974 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 975 | * as string error message and it is copied in the new stack. |
| 976 | */ |
| 977 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 978 | { |
| 979 | lua_State *T; |
| 980 | int new_ref; |
| 981 | |
| 982 | /* Renew the main LUA stack doesn't have sense. */ |
| 983 | if (lua == &gL) |
| 984 | return 0; |
| 985 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 986 | /* New Lua coroutine. */ |
| 987 | T = lua_newthread(gL.T); |
| 988 | if (!T) |
| 989 | return 0; |
| 990 | |
| 991 | /* Copy last error message. */ |
| 992 | if (keep_msg) |
| 993 | lua_xmove(lua->T, T, 1); |
| 994 | |
| 995 | /* Copy data between the coroutines. */ |
| 996 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 997 | lua_xmove(lua->T, T, 1); |
| 998 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 999 | |
| 1000 | /* Destroy old data. */ |
| 1001 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1002 | |
| 1003 | /* The thread is garbage collected by Lua. */ |
| 1004 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1005 | |
| 1006 | /* Fill the struct with the new coroutine values. */ |
| 1007 | lua->Mref = new_ref; |
| 1008 | lua->T = T; |
| 1009 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1010 | |
| 1011 | /* Set context. */ |
| 1012 | hlua_sethlua(lua); |
| 1013 | |
| 1014 | return 1; |
| 1015 | } |
| 1016 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1017 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1018 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1019 | struct hlua *hlua = hlua_gethlua(L); |
| 1020 | |
| 1021 | /* Lua cannot yield when its returning from a function, |
| 1022 | * so, we can fix the interrupt hook to 1 instruction, |
| 1023 | * expecting that the function is finnished. |
| 1024 | */ |
| 1025 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1026 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1027 | return; |
| 1028 | } |
| 1029 | |
| 1030 | /* restore the interrupt condition. */ |
| 1031 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1032 | |
| 1033 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1034 | * If the state is not yieldable, trying yield causes an error. |
| 1035 | */ |
| 1036 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1037 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1038 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1039 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1040 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1041 | hlua->run_time += now_ms - hlua->start_time; |
| 1042 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1043 | lua_pushfstring(L, "execution timeout"); |
| 1044 | WILL_LJMP(lua_error(L)); |
| 1045 | } |
| 1046 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1047 | /* Update the start time. */ |
| 1048 | hlua->start_time = now_ms; |
| 1049 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1050 | /* Try to interrupt the process at the end of the current |
| 1051 | * unyieldable function. |
| 1052 | */ |
| 1053 | 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] | 1054 | } |
| 1055 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1056 | /* This function start or resumes the Lua stack execution. If the flag |
| 1057 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1058 | * The function return an error. |
| 1059 | * |
| 1060 | * The function can returns 4 values: |
| 1061 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1062 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1063 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1064 | * - 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] | 1065 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1066 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1067 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1068 | * 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] | 1069 | * LUA code. |
| 1070 | */ |
| 1071 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1072 | { |
| 1073 | int ret; |
| 1074 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1075 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1076 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1077 | /* Initialise run time counter. */ |
| 1078 | if (!HLUA_IS_RUNNING(lua)) |
| 1079 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1080 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1081 | /* Lock the whole Lua execution. This lock must be before the |
| 1082 | * label "resume_execution". |
| 1083 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1084 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1085 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1086 | resume_execution: |
| 1087 | |
| 1088 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1089 | * instructions. it is used for preventing infinite loops. |
| 1090 | */ |
| 1091 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1092 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1093 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1094 | HLUA_SET_RUN(lua); |
| 1095 | HLUA_CLR_CTRLYIELD(lua); |
| 1096 | HLUA_CLR_WAKERESWR(lua); |
| 1097 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1098 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1099 | /* Update the start time. */ |
| 1100 | lua->start_time = now_ms; |
| 1101 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1102 | /* Call the function. */ |
| 1103 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1104 | switch (ret) { |
| 1105 | |
| 1106 | case LUA_OK: |
| 1107 | ret = HLUA_E_OK; |
| 1108 | break; |
| 1109 | |
| 1110 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1111 | /* Check if the execution timeout is expired. It it is the case, we |
| 1112 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1113 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1114 | tv_update_date(0, 1); |
| 1115 | lua->run_time += now_ms - lua->start_time; |
| 1116 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1117 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1118 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1119 | break; |
| 1120 | } |
| 1121 | /* Process the forced yield. if the general yield is not allowed or |
| 1122 | * if no task were associated this the current Lua execution |
| 1123 | * coroutine, we resume the execution. Else we want to return in the |
| 1124 | * scheduler and we want to be waked up again, to continue the |
| 1125 | * current Lua execution. So we schedule our own task. |
| 1126 | */ |
| 1127 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1128 | if (!yield_allowed || !lua->task) |
| 1129 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1130 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1131 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1132 | if (!yield_allowed) { |
| 1133 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1134 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1135 | break; |
| 1136 | } |
| 1137 | ret = HLUA_E_AGAIN; |
| 1138 | break; |
| 1139 | |
| 1140 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1141 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1142 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1143 | * because the errors ares the only one mean to return immediately |
| 1144 | * from and lua execution. |
| 1145 | */ |
| 1146 | if (lua->flags & HLUA_EXIT) { |
| 1147 | ret = HLUA_E_OK; |
Thierry FOURNIER | e1587b3 | 2015-08-28 09:54:13 +0200 | [diff] [blame] | 1148 | hlua_ctx_renew(lua, 0); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1149 | break; |
| 1150 | } |
| 1151 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1152 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1153 | if (!lua_checkstack(lua->T, 1)) { |
| 1154 | ret = HLUA_E_ERR; |
| 1155 | break; |
| 1156 | } |
| 1157 | msg = lua_tostring(lua->T, -1); |
| 1158 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1159 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1160 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1161 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1162 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1163 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1164 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | ret = HLUA_E_ERRMSG; |
| 1166 | break; |
| 1167 | |
| 1168 | case LUA_ERRMEM: |
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 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1171 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1172 | break; |
| 1173 | |
| 1174 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1175 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | if (!lua_checkstack(lua->T, 1)) { |
| 1177 | ret = HLUA_E_ERR; |
| 1178 | break; |
| 1179 | } |
| 1180 | msg = lua_tostring(lua->T, -1); |
| 1181 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1182 | lua_pop(lua->T, 1); |
| 1183 | if (msg) |
| 1184 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1185 | else |
| 1186 | lua_pushfstring(lua->T, "message handler error"); |
| 1187 | ret = HLUA_E_ERRMSG; |
| 1188 | break; |
| 1189 | |
| 1190 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1191 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1192 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1193 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1194 | break; |
| 1195 | } |
| 1196 | |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1197 | /* This GC permits to destroy some object when a Lua timeout strikes. */ |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 1198 | if (lua->flags & HLUA_MUST_GC && |
| 1199 | ret != HLUA_E_AGAIN) |
Thierry FOURNIER | 6ab4d8e | 2015-09-27 22:17:19 +0200 | [diff] [blame] | 1200 | lua_gc(lua->T, LUA_GCCOLLECT, 0); |
| 1201 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1202 | switch (ret) { |
| 1203 | case HLUA_E_AGAIN: |
| 1204 | break; |
| 1205 | |
| 1206 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1207 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1208 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1209 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1210 | break; |
| 1211 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1212 | case HLUA_E_ETMOUT: |
| 1213 | case HLUA_E_NOMEM: |
| 1214 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1215 | case HLUA_E_ERR: |
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 | hlua_ctx_renew(lua, 0); |
| 1219 | break; |
| 1220 | |
| 1221 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1222 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1223 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1224 | break; |
| 1225 | } |
| 1226 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1227 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1228 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1229 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1230 | return ret; |
| 1231 | } |
| 1232 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1233 | /* This function exit the current code. */ |
| 1234 | __LJMP static int hlua_done(lua_State *L) |
| 1235 | { |
| 1236 | struct hlua *hlua = hlua_gethlua(L); |
| 1237 | |
| 1238 | hlua->flags |= HLUA_EXIT; |
| 1239 | WILL_LJMP(lua_error(L)); |
| 1240 | |
| 1241 | return 0; |
| 1242 | } |
| 1243 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1244 | /* This function is an LUA binding. It provides a function |
| 1245 | * for deleting ACL from a referenced ACL file. |
| 1246 | */ |
| 1247 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1248 | { |
| 1249 | const char *name; |
| 1250 | const char *key; |
| 1251 | struct pat_ref *ref; |
| 1252 | |
| 1253 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1254 | |
| 1255 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1256 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1257 | |
| 1258 | ref = pat_ref_lookup(name); |
| 1259 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1260 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1261 | |
| 1262 | pat_ref_delete(ref, key); |
| 1263 | return 0; |
| 1264 | } |
| 1265 | |
| 1266 | /* This function is an LUA binding. It provides a function |
| 1267 | * for deleting map entry from a referenced map file. |
| 1268 | */ |
| 1269 | static int hlua_del_map(lua_State *L) |
| 1270 | { |
| 1271 | const char *name; |
| 1272 | const char *key; |
| 1273 | struct pat_ref *ref; |
| 1274 | |
| 1275 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1276 | |
| 1277 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1278 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1279 | |
| 1280 | ref = pat_ref_lookup(name); |
| 1281 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1282 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1283 | |
| 1284 | pat_ref_delete(ref, key); |
| 1285 | return 0; |
| 1286 | } |
| 1287 | |
| 1288 | /* This function is an LUA binding. It provides a function |
| 1289 | * for adding ACL pattern from a referenced ACL file. |
| 1290 | */ |
| 1291 | static int hlua_add_acl(lua_State *L) |
| 1292 | { |
| 1293 | const char *name; |
| 1294 | const char *key; |
| 1295 | struct pat_ref *ref; |
| 1296 | |
| 1297 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1298 | |
| 1299 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1300 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1301 | |
| 1302 | ref = pat_ref_lookup(name); |
| 1303 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1304 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1305 | |
| 1306 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1307 | pat_ref_add(ref, key, NULL, NULL); |
| 1308 | return 0; |
| 1309 | } |
| 1310 | |
| 1311 | /* This function is an LUA binding. It provides a function |
| 1312 | * for setting map pattern and sample from a referenced map |
| 1313 | * file. |
| 1314 | */ |
| 1315 | static int hlua_set_map(lua_State *L) |
| 1316 | { |
| 1317 | const char *name; |
| 1318 | const char *key; |
| 1319 | const char *value; |
| 1320 | struct pat_ref *ref; |
| 1321 | |
| 1322 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1323 | |
| 1324 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1325 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1326 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1327 | |
| 1328 | ref = pat_ref_lookup(name); |
| 1329 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1330 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1331 | |
| 1332 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1333 | pat_ref_set(ref, key, value, NULL); |
| 1334 | else |
| 1335 | pat_ref_add(ref, key, value, NULL); |
| 1336 | return 0; |
| 1337 | } |
| 1338 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1339 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1340 | * an integer or user data. This data is associated with a metatable. This |
| 1341 | * metatable have an original version registred in the global context with |
| 1342 | * the name of the object (_G[<name>] = <metable> ). |
| 1343 | * |
| 1344 | * A metable is a table that modify the standard behavior of a standard |
| 1345 | * access to the associated data. The entries of this new metatable are |
| 1346 | * defined as is: |
| 1347 | * |
| 1348 | * http://lua-users.org/wiki/MetatableEvents |
| 1349 | * |
| 1350 | * __index |
| 1351 | * |
| 1352 | * we access an absent field in a table, the result is nil. This is |
| 1353 | * true, but it is not the whole truth. Actually, such access triggers |
| 1354 | * the interpreter to look for an __index metamethod: If there is no |
| 1355 | * such method, as usually happens, then the access results in nil; |
| 1356 | * otherwise, the metamethod will provide the result. |
| 1357 | * |
| 1358 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1359 | * the key does not appear in the table, but the metatable has an __index |
| 1360 | * property: |
| 1361 | * |
| 1362 | * - if the value is a function, the function is called, passing in the |
| 1363 | * table and the key; the return value of that function is returned as |
| 1364 | * the result. |
| 1365 | * |
| 1366 | * - if the value is another table, the value of the key in that table is |
| 1367 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1368 | * table's metatable has an __index property, then it continues on up) |
| 1369 | * |
| 1370 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1371 | * |
| 1372 | * http://www.lua.org/pil/13.4.1.html |
| 1373 | * |
| 1374 | * __newindex |
| 1375 | * |
| 1376 | * Like __index, but control property assignment. |
| 1377 | * |
| 1378 | * __mode - Control weak references. A string value with one or both |
| 1379 | * of the characters 'k' and 'v' which specifies that the the |
| 1380 | * keys and/or values in the table are weak references. |
| 1381 | * |
| 1382 | * __call - Treat a table like a function. When a table is followed by |
| 1383 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1384 | * a __call key pointing to a function, that function is invoked |
| 1385 | * (passing any specified arguments) and the return value is |
| 1386 | * returned. |
| 1387 | * |
| 1388 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1389 | * called, if the metatable for myTable has a __metatable |
| 1390 | * key, the value of that key is returned instead of the |
| 1391 | * actual metatable. |
| 1392 | * |
| 1393 | * __tostring - Control string representation. When the builtin |
| 1394 | * "tostring( myTable )" function is called, if the metatable |
| 1395 | * for myTable has a __tostring property set to a function, |
| 1396 | * that function is invoked (passing myTable to it) and the |
| 1397 | * return value is used as the string representation. |
| 1398 | * |
| 1399 | * __len - Control table length. When the table length is requested using |
| 1400 | * the length operator ( '#' ), if the metatable for myTable has |
| 1401 | * a __len key pointing to a function, that function is invoked |
| 1402 | * (passing myTable to it) and the return value used as the value |
| 1403 | * of "#myTable". |
| 1404 | * |
| 1405 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1406 | * collected, if the metatable has a __gc field pointing to a |
| 1407 | * function, that function is first invoked, passing the userdata |
| 1408 | * to it. The __gc metamethod is not called for tables. |
| 1409 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1410 | * |
| 1411 | * Special metamethods for redefining standard operators: |
| 1412 | * http://www.lua.org/pil/13.1.html |
| 1413 | * |
| 1414 | * __add "+" |
| 1415 | * __sub "-" |
| 1416 | * __mul "*" |
| 1417 | * __div "/" |
| 1418 | * __unm "!" |
| 1419 | * __pow "^" |
| 1420 | * __concat ".." |
| 1421 | * |
| 1422 | * Special methods for redfining standar relations |
| 1423 | * http://www.lua.org/pil/13.2.html |
| 1424 | * |
| 1425 | * __eq "==" |
| 1426 | * __lt "<" |
| 1427 | * __le "<=" |
| 1428 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1429 | |
| 1430 | /* |
| 1431 | * |
| 1432 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1433 | * Class Map |
| 1434 | * |
| 1435 | * |
| 1436 | */ |
| 1437 | |
| 1438 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1439 | * a class session, otherwise it throws an error. |
| 1440 | */ |
| 1441 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1442 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1443 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1444 | } |
| 1445 | |
| 1446 | /* This function is the map constructor. It don't need |
| 1447 | * the class Map object. It creates and return a new Map |
| 1448 | * object. It must be called only during "body" or "init" |
| 1449 | * context because it process some filesystem accesses. |
| 1450 | */ |
| 1451 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1452 | { |
| 1453 | const char *fn; |
| 1454 | int match = PAT_MATCH_STR; |
| 1455 | struct sample_conv conv; |
| 1456 | const char *file = ""; |
| 1457 | int line = 0; |
| 1458 | lua_Debug ar; |
| 1459 | char *err = NULL; |
| 1460 | struct arg args[2]; |
| 1461 | |
| 1462 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1463 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1464 | |
| 1465 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1466 | |
| 1467 | if (lua_gettop(L) >= 2) { |
| 1468 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1469 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1470 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1471 | } |
| 1472 | |
| 1473 | /* Get Lua filename and line number. */ |
| 1474 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1475 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1476 | if (ar.currentline > 0) { /* is there info? */ |
| 1477 | file = ar.short_src; |
| 1478 | line = ar.currentline; |
| 1479 | } |
| 1480 | } |
| 1481 | |
| 1482 | /* fill fake sample_conv struct. */ |
| 1483 | conv.kw = ""; /* unused. */ |
| 1484 | conv.process = NULL; /* unused. */ |
| 1485 | conv.arg_mask = 0; /* unused. */ |
| 1486 | conv.val_args = NULL; /* unused. */ |
| 1487 | conv.out_type = SMP_T_STR; |
| 1488 | conv.private = (void *)(long)match; |
| 1489 | switch (match) { |
| 1490 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1491 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1492 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1493 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1494 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1495 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1496 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1497 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1498 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1499 | default: |
| 1500 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1501 | } |
| 1502 | |
| 1503 | /* fill fake args. */ |
| 1504 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1505 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1506 | args[1].type = ARGT_STOP; |
| 1507 | |
| 1508 | /* load the map. */ |
| 1509 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1510 | /* error case: we cant use luaL_error because we must |
| 1511 | * free the err variable. |
| 1512 | */ |
| 1513 | luaL_where(L, 1); |
| 1514 | lua_pushfstring(L, "'new': %s.", err); |
| 1515 | lua_concat(L, 2); |
| 1516 | free(err); |
| 1517 | WILL_LJMP(lua_error(L)); |
| 1518 | } |
| 1519 | |
| 1520 | /* create the lua object. */ |
| 1521 | lua_newtable(L); |
| 1522 | lua_pushlightuserdata(L, args[0].data.map); |
| 1523 | lua_rawseti(L, -2, 0); |
| 1524 | |
| 1525 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1526 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1527 | lua_setmetatable(L, -2); |
| 1528 | |
| 1529 | |
| 1530 | return 1; |
| 1531 | } |
| 1532 | |
| 1533 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1534 | { |
| 1535 | struct map_descriptor *desc; |
| 1536 | struct pattern *pat; |
| 1537 | struct sample smp; |
| 1538 | |
| 1539 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1540 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1541 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1542 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1543 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1544 | } |
| 1545 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1546 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1547 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1548 | 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] | 1549 | } |
| 1550 | |
| 1551 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1552 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1553 | if (str) |
| 1554 | lua_pushstring(L, ""); |
| 1555 | else |
| 1556 | lua_pushnil(L); |
| 1557 | return 1; |
| 1558 | } |
| 1559 | |
| 1560 | /* 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] | 1561 | 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] | 1562 | return 1; |
| 1563 | } |
| 1564 | |
| 1565 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1566 | { |
| 1567 | return _hlua_map_lookup(L, 0); |
| 1568 | } |
| 1569 | |
| 1570 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1571 | { |
| 1572 | return _hlua_map_lookup(L, 1); |
| 1573 | } |
| 1574 | |
| 1575 | /* |
| 1576 | * |
| 1577 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1578 | * Class Socket |
| 1579 | * |
| 1580 | * |
| 1581 | */ |
| 1582 | |
| 1583 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1584 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1585 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1586 | } |
| 1587 | |
| 1588 | /* 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] | 1589 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1590 | * received. |
| 1591 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1592 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1594 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1595 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1596 | if (appctx->ctx.hlua_cosocket.die) { |
| 1597 | si_shutw(si); |
| 1598 | si_shutr(si); |
| 1599 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1600 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1601 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1602 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1603 | } |
| 1604 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1605 | /* If we cant write, wakeup the pending write signals. */ |
| 1606 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1607 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1608 | |
| 1609 | /* If we cant read, wakeup the pending read signals. */ |
| 1610 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1611 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1612 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1613 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1614 | * to be notified whenever the connection completes. |
| 1615 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1616 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1617 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1618 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1619 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1620 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1621 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1622 | |
| 1623 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1624 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1625 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1626 | /* 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] | 1627 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1628 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1631 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1632 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1633 | |
| 1634 | /* Some data were injected in the buffer, notify the stream |
| 1635 | * interface. |
| 1636 | */ |
| 1637 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1638 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1639 | |
| 1640 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1641 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1642 | */ |
| 1643 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1644 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1645 | } |
| 1646 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1647 | /* This function is called when the "struct stream" is destroyed. |
| 1648 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1649 | * Wake all the pending signals. |
| 1650 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1651 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1653 | struct xref *peer; |
| 1654 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1655 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1656 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1657 | if (peer) |
| 1658 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | |
| 1660 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1661 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1662 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1663 | } |
| 1664 | |
| 1665 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1666 | * uses this object. If the stream does not exists, just quit. |
| 1667 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1668 | * pending signal can rest in the read and write lists. destroy |
| 1669 | * it. |
| 1670 | */ |
| 1671 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1672 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1673 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1674 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1675 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1676 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1677 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1678 | |
| 1679 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1680 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1681 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1682 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1683 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1684 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1685 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1686 | appctx->ctx.hlua_cosocket.die = 1; |
| 1687 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1688 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1689 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1690 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | return 0; |
| 1692 | } |
| 1693 | |
| 1694 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1695 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1696 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1697 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1699 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1700 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1701 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1702 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1703 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1704 | |
| 1705 | /* Check if we run on the same thread than the xreator thread. |
| 1706 | * We cannot access to the socket if the thread is different. |
| 1707 | */ |
| 1708 | if (socket->tid != tid) |
| 1709 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1710 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1711 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1712 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1713 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1714 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1715 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1716 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1717 | appctx->ctx.hlua_cosocket.die = 1; |
| 1718 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1719 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1720 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1721 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1722 | return 0; |
| 1723 | } |
| 1724 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1725 | /* The close function calls close_helper. |
| 1726 | */ |
| 1727 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1728 | { |
| 1729 | MAY_LJMP(check_args(L, 1, "close")); |
| 1730 | return hlua_socket_close_helper(L); |
| 1731 | } |
| 1732 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1733 | /* This Lua function assumes that the stack contain three parameters. |
| 1734 | * 1 - USERDATA containing a struct socket |
| 1735 | * 2 - INTEGER with values of the macro defined below |
| 1736 | * If the integer is -1, we must read at most one line. |
| 1737 | * If the integer is -2, we ust read all the data until the |
| 1738 | * end of the stream. |
| 1739 | * If the integer is positive value, we must read a number of |
| 1740 | * bytes corresponding to this value. |
| 1741 | */ |
| 1742 | #define HLSR_READ_LINE (-1) |
| 1743 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1744 | __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] | 1745 | { |
| 1746 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1747 | int wanted = lua_tointeger(L, 2); |
| 1748 | struct hlua *hlua = hlua_gethlua(L); |
| 1749 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1750 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1751 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1752 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1753 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1754 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1755 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1756 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1757 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1758 | struct stream_interface *si; |
| 1759 | struct stream *s; |
| 1760 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1761 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1762 | |
| 1763 | /* Check if this lua stack is schedulable. */ |
| 1764 | if (!hlua || !hlua->task) |
| 1765 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1766 | "'frontend', 'backend' or 'task'")); |
| 1767 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1768 | /* Check if we run on the same thread than the xreator thread. |
| 1769 | * We cannot access to the socket if the thread is different. |
| 1770 | */ |
| 1771 | if (socket->tid != tid) |
| 1772 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1773 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1774 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1775 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1776 | if (!peer) |
| 1777 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1778 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1779 | si = appctx->owner; |
| 1780 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1781 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1782 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1783 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1784 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1785 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1786 | if (nblk < 0) /* Connection close. */ |
| 1787 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1788 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1789 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1790 | |
| 1791 | /* remove final \r\n. */ |
| 1792 | if (nblk == 1) { |
| 1793 | if (blk1[len1-1] == '\n') { |
| 1794 | len1--; |
| 1795 | skip_at_end++; |
| 1796 | if (blk1[len1-1] == '\r') { |
| 1797 | len1--; |
| 1798 | skip_at_end++; |
| 1799 | } |
| 1800 | } |
| 1801 | } |
| 1802 | else { |
| 1803 | if (blk2[len2-1] == '\n') { |
| 1804 | len2--; |
| 1805 | skip_at_end++; |
| 1806 | if (blk2[len2-1] == '\r') { |
| 1807 | len2--; |
| 1808 | skip_at_end++; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1812 | } |
| 1813 | |
| 1814 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1815 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1816 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1817 | if (nblk < 0) /* Connection close. */ |
| 1818 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1819 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1820 | goto connection_empty; |
| 1821 | } |
| 1822 | |
| 1823 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1824 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1825 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1826 | if (nblk < 0) /* Connection close. */ |
| 1827 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1828 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1829 | goto connection_empty; |
| 1830 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1831 | missing_bytes = wanted - socket->b.n; |
| 1832 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1833 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1834 | len1 = missing_bytes; |
| 1835 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1836 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1837 | } |
| 1838 | |
| 1839 | len = len1; |
| 1840 | |
| 1841 | luaL_addlstring(&socket->b, blk1, len1); |
| 1842 | if (nblk == 2) { |
| 1843 | len += len2; |
| 1844 | luaL_addlstring(&socket->b, blk2, len2); |
| 1845 | } |
| 1846 | |
| 1847 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1848 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1849 | |
| 1850 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1851 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1852 | |
| 1853 | /* If the pattern reclaim to read all the data |
| 1854 | * in the connection, got out. |
| 1855 | */ |
| 1856 | if (wanted == HLSR_READ_ALL) |
| 1857 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1858 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | goto connection_empty; |
| 1860 | |
| 1861 | /* Return result. */ |
| 1862 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1863 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1864 | return 1; |
| 1865 | |
| 1866 | connection_closed: |
| 1867 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1868 | xref_unlock(&socket->xref, peer); |
| 1869 | |
| 1870 | no_peer: |
| 1871 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1872 | /* If the buffer containds data. */ |
| 1873 | if (socket->b.n > 0) { |
| 1874 | luaL_pushresult(&socket->b); |
| 1875 | return 1; |
| 1876 | } |
| 1877 | lua_pushnil(L); |
| 1878 | lua_pushstring(L, "connection closed."); |
| 1879 | return 2; |
| 1880 | |
| 1881 | connection_empty: |
| 1882 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1883 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1884 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1885 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1886 | } |
| 1887 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1888 | 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] | 1889 | return 0; |
| 1890 | } |
| 1891 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1892 | /* This Lua function gets two parameters. The first one can be string |
| 1893 | * or a number. If the string is "*l", the user requires one line. If |
| 1894 | * 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] | 1895 | * If the value is a number, the user require a number of bytes equal |
| 1896 | * to the value. The default value is "*l" (a line). |
| 1897 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1898 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1899 | * integer takes this values: |
| 1900 | * -1 : read a line |
| 1901 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1902 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1903 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1904 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1905 | * concatenated with the read data. |
| 1906 | */ |
| 1907 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1908 | { |
| 1909 | int wanted = HLSR_READ_LINE; |
| 1910 | const char *pattern; |
| 1911 | int type; |
| 1912 | char *error; |
| 1913 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1914 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | |
| 1916 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1917 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1918 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1919 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1920 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1921 | /* Check if we run on the same thread than the xreator thread. |
| 1922 | * We cannot access to the socket if the thread is different. |
| 1923 | */ |
| 1924 | if (socket->tid != tid) |
| 1925 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1926 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1927 | /* check for pattern. */ |
| 1928 | if (lua_gettop(L) >= 2) { |
| 1929 | type = lua_type(L, 2); |
| 1930 | if (type == LUA_TSTRING) { |
| 1931 | pattern = lua_tostring(L, 2); |
| 1932 | if (strcmp(pattern, "*a") == 0) |
| 1933 | wanted = HLSR_READ_ALL; |
| 1934 | else if (strcmp(pattern, "*l") == 0) |
| 1935 | wanted = HLSR_READ_LINE; |
| 1936 | else { |
| 1937 | wanted = strtoll(pattern, &error, 10); |
| 1938 | if (*error != '\0') |
| 1939 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1940 | } |
| 1941 | } |
| 1942 | else if (type == LUA_TNUMBER) { |
| 1943 | wanted = lua_tointeger(L, 2); |
| 1944 | if (wanted < 0) |
| 1945 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1946 | } |
| 1947 | } |
| 1948 | |
| 1949 | /* Set pattern. */ |
| 1950 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1951 | |
| 1952 | /* Check if we would replace the top by itself. */ |
| 1953 | if (lua_gettop(L) != 2) |
| 1954 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1955 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1956 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1957 | luaL_buffinit(L, &socket->b); |
| 1958 | |
| 1959 | /* Check prefix. */ |
| 1960 | if (lua_gettop(L) >= 3) { |
| 1961 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1962 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1963 | pattern = lua_tolstring(L, 3, &len); |
| 1964 | luaL_addlstring(&socket->b, pattern, len); |
| 1965 | } |
| 1966 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1967 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1968 | } |
| 1969 | |
| 1970 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1971 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1972 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1973 | 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] | 1974 | { |
| 1975 | struct hlua_socket *socket; |
| 1976 | struct hlua *hlua = hlua_gethlua(L); |
| 1977 | struct appctx *appctx; |
| 1978 | size_t buf_len; |
| 1979 | const char *buf; |
| 1980 | int len; |
| 1981 | int send_len; |
| 1982 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1983 | struct xref *peer; |
| 1984 | struct stream_interface *si; |
| 1985 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1986 | |
| 1987 | /* Check if this lua stack is schedulable. */ |
| 1988 | if (!hlua || !hlua->task) |
| 1989 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 1990 | "'frontend', 'backend' or 'task'")); |
| 1991 | |
| 1992 | /* Get object */ |
| 1993 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1994 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1995 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1997 | /* Check if we run on the same thread than the xreator thread. |
| 1998 | * We cannot access to the socket if the thread is different. |
| 1999 | */ |
| 2000 | if (socket->tid != tid) |
| 2001 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2002 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2003 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2004 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2005 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2006 | lua_pushinteger(L, -1); |
| 2007 | return 1; |
| 2008 | } |
| 2009 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2010 | si = appctx->owner; |
| 2011 | s = si_strm(si); |
| 2012 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2013 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2014 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2015 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2016 | lua_pushinteger(L, -1); |
| 2017 | return 1; |
| 2018 | } |
| 2019 | |
| 2020 | /* Update the input buffer data. */ |
| 2021 | buf += sent; |
| 2022 | send_len = buf_len - sent; |
| 2023 | |
| 2024 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2025 | if (sent >= buf_len) { |
| 2026 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2027 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2028 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2029 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2030 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2031 | * the request buffer if its not required. |
| 2032 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2033 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2034 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2035 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2036 | } |
| 2037 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2038 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2039 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2040 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2041 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2042 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2043 | |
| 2044 | /* send data */ |
| 2045 | if (len < send_len) |
| 2046 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2047 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2048 | |
| 2049 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2050 | * the data" (-2) are not expected because the available length |
| 2051 | * is tested. |
| 2052 | * Other unknown error are also not expected. |
| 2053 | */ |
| 2054 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2055 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2056 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2057 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2058 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2059 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2060 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2061 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2062 | return 1; |
| 2063 | } |
| 2064 | |
| 2065 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2066 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2067 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2068 | s->req.rex = TICK_ETERNITY; |
| 2069 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2070 | |
| 2071 | /* Update length sent. */ |
| 2072 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2073 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2074 | |
| 2075 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2076 | if (sent + len >= buf_len) { |
| 2077 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2078 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2079 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2082 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2083 | xref_unlock(&socket->xref, peer); |
| 2084 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2085 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2086 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2087 | 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] | 2088 | return 0; |
| 2089 | } |
| 2090 | |
| 2091 | /* This function initiate the send of data. It just check the input |
| 2092 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2093 | * 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] | 2094 | * "hlua_socket_write_yield" that can yield. |
| 2095 | * |
| 2096 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2097 | * the associated object. The second is a string buffer. The third is |
| 2098 | * a facultative integer that represents where is the buffer position |
| 2099 | * of the start of the data that can send. The first byte is the |
| 2100 | * position "1". The default value is "1". The fourth argument is a |
| 2101 | * facultative integer that represents where is the buffer position |
| 2102 | * of the end of the data that can send. The default is the last byte. |
| 2103 | */ |
| 2104 | static int hlua_socket_send(struct lua_State *L) |
| 2105 | { |
| 2106 | int i; |
| 2107 | int j; |
| 2108 | const char *buf; |
| 2109 | size_t buf_len; |
| 2110 | |
| 2111 | /* Check number of arguments. */ |
| 2112 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2113 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2114 | |
| 2115 | /* Get the string. */ |
| 2116 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2117 | |
| 2118 | /* Get and check j. */ |
| 2119 | if (lua_gettop(L) == 4) { |
| 2120 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2121 | if (j < 0) |
| 2122 | j = buf_len + j + 1; |
| 2123 | if (j > buf_len) |
| 2124 | j = buf_len + 1; |
| 2125 | lua_pop(L, 1); |
| 2126 | } |
| 2127 | else |
| 2128 | j = buf_len; |
| 2129 | |
| 2130 | /* Get and check i. */ |
| 2131 | if (lua_gettop(L) == 3) { |
| 2132 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2133 | if (i < 0) |
| 2134 | i = buf_len + i + 1; |
| 2135 | if (i > buf_len) |
| 2136 | i = buf_len + 1; |
| 2137 | lua_pop(L, 1); |
| 2138 | } else |
| 2139 | i = 1; |
| 2140 | |
| 2141 | /* Check bth i and j. */ |
| 2142 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2143 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2144 | return 1; |
| 2145 | } |
| 2146 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2147 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2148 | return 1; |
| 2149 | } |
| 2150 | if (i == 0) |
| 2151 | i = 1; |
| 2152 | if (j == 0) |
| 2153 | j = 1; |
| 2154 | |
| 2155 | /* Pop the string. */ |
| 2156 | lua_pop(L, 1); |
| 2157 | |
| 2158 | /* Update the buffer length. */ |
| 2159 | buf += i - 1; |
| 2160 | buf_len = j - i + 1; |
| 2161 | lua_pushlstring(L, buf, buf_len); |
| 2162 | |
| 2163 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2164 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2165 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2166 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2167 | } |
| 2168 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2169 | #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] | 2170 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2171 | { |
| 2172 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2173 | int ret; |
| 2174 | int len; |
| 2175 | char *p; |
| 2176 | |
| 2177 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2178 | if (ret <= 0) { |
| 2179 | lua_pushnil(L); |
| 2180 | return 1; |
| 2181 | } |
| 2182 | |
| 2183 | if (ret == AF_UNIX) { |
| 2184 | lua_pushstring(L, buffer+1); |
| 2185 | return 1; |
| 2186 | } |
| 2187 | else if (ret == AF_INET6) { |
| 2188 | buffer[0] = '['; |
| 2189 | len = strlen(buffer); |
| 2190 | buffer[len] = ']'; |
| 2191 | len++; |
| 2192 | buffer[len] = ':'; |
| 2193 | len++; |
| 2194 | p = buffer; |
| 2195 | } |
| 2196 | else if (ret == AF_INET) { |
| 2197 | p = buffer + 1; |
| 2198 | len = strlen(p); |
| 2199 | p[len] = ':'; |
| 2200 | len++; |
| 2201 | } |
| 2202 | else { |
| 2203 | lua_pushnil(L); |
| 2204 | return 1; |
| 2205 | } |
| 2206 | |
| 2207 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2208 | lua_pushnil(L); |
| 2209 | return 1; |
| 2210 | } |
| 2211 | |
| 2212 | lua_pushstring(L, p); |
| 2213 | return 1; |
| 2214 | } |
| 2215 | |
| 2216 | /* Returns information about the peer of the connection. */ |
| 2217 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2218 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2219 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2220 | struct xref *peer; |
| 2221 | struct appctx *appctx; |
| 2222 | struct stream_interface *si; |
| 2223 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2224 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2225 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2226 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2227 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2228 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2229 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2230 | /* Check if we run on the same thread than the xreator thread. |
| 2231 | * We cannot access to the socket if the thread is different. |
| 2232 | */ |
| 2233 | if (socket->tid != tid) |
| 2234 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2235 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2236 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2237 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2238 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | lua_pushnil(L); |
| 2240 | return 1; |
| 2241 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2242 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2243 | si = appctx->owner; |
| 2244 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2245 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2246 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2247 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2248 | lua_pushnil(L); |
| 2249 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2250 | } |
| 2251 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2252 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2253 | xref_unlock(&socket->xref, peer); |
| 2254 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | } |
| 2256 | |
| 2257 | /* Returns information about my connection side. */ |
| 2258 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2259 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2260 | struct hlua_socket *socket; |
| 2261 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2262 | struct appctx *appctx; |
| 2263 | struct xref *peer; |
| 2264 | struct stream_interface *si; |
| 2265 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2266 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2267 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2268 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2269 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2270 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2271 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2272 | /* Check if we run on the same thread than the xreator thread. |
| 2273 | * We cannot access to the socket if the thread is different. |
| 2274 | */ |
| 2275 | if (socket->tid != tid) |
| 2276 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2277 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2278 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2279 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2280 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | lua_pushnil(L); |
| 2282 | return 1; |
| 2283 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2284 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2285 | si = appctx->owner; |
| 2286 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2287 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2288 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2289 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2290 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | lua_pushnil(L); |
| 2292 | return 1; |
| 2293 | } |
| 2294 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2295 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2296 | xref_unlock(&socket->xref, peer); |
| 2297 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2298 | } |
| 2299 | |
| 2300 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2301 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2302 | .obj_type = OBJ_TYPE_APPLET, |
| 2303 | .name = "<LUA_TCP>", |
| 2304 | .fct = hlua_socket_handler, |
| 2305 | .release = hlua_socket_release, |
| 2306 | }; |
| 2307 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2308 | __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] | 2309 | { |
| 2310 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2311 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2312 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2313 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2314 | struct stream_interface *si; |
| 2315 | struct stream *s; |
| 2316 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2317 | /* Check if we run on the same thread than the xreator thread. |
| 2318 | * We cannot access to the socket if the thread is different. |
| 2319 | */ |
| 2320 | if (socket->tid != tid) |
| 2321 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2322 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2323 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2324 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2325 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2326 | lua_pushnil(L); |
| 2327 | lua_pushstring(L, "Can't connect"); |
| 2328 | return 2; |
| 2329 | } |
| 2330 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2331 | si = appctx->owner; |
| 2332 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2333 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2334 | /* Check if we run on the same thread than the xreator thread. |
| 2335 | * We cannot access to the socket if the thread is different. |
| 2336 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2337 | if (socket->tid != tid) { |
| 2338 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2339 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2340 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2341 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2342 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2343 | if (!hlua || channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2344 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2345 | lua_pushnil(L); |
| 2346 | lua_pushstring(L, "Can't connect"); |
| 2347 | return 2; |
| 2348 | } |
| 2349 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2350 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2351 | |
| 2352 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2353 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2354 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2355 | lua_pushinteger(L, 1); |
| 2356 | return 1; |
| 2357 | } |
| 2358 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2359 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2360 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2362 | } |
| 2363 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2364 | 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] | 2365 | return 0; |
| 2366 | } |
| 2367 | |
| 2368 | /* This function fail or initite the connection. */ |
| 2369 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2370 | { |
| 2371 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2372 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2373 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2374 | struct hlua *hlua; |
| 2375 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2376 | int low, high; |
| 2377 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2378 | struct xref *peer; |
| 2379 | struct stream_interface *si; |
| 2380 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2381 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2382 | if (lua_gettop(L) < 2) |
| 2383 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2384 | |
| 2385 | /* Get args. */ |
| 2386 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2387 | |
| 2388 | /* Check if we run on the same thread than the xreator thread. |
| 2389 | * We cannot access to the socket if the thread is different. |
| 2390 | */ |
| 2391 | if (socket->tid != tid) |
| 2392 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2393 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2395 | if (lua_gettop(L) >= 3) { |
| 2396 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2397 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2398 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2399 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2400 | * that are not enclosed within square brackets. |
| 2401 | */ |
| 2402 | if (port > 0) { |
| 2403 | luaL_buffinit(L, &b); |
| 2404 | luaL_addstring(&b, ip); |
| 2405 | luaL_addchar(&b, ':'); |
| 2406 | luaL_pushresult(&b); |
| 2407 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2408 | } |
| 2409 | } |
| 2410 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2411 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2412 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2413 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2414 | lua_pushnil(L); |
| 2415 | return 1; |
| 2416 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2417 | |
| 2418 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2419 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2420 | if (!addr) { |
| 2421 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2422 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2423 | } |
| 2424 | if (low != high) { |
| 2425 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2426 | 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] | 2427 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2428 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2429 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2430 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2431 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2432 | if (port == -1) { |
| 2433 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2434 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2435 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2436 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2437 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2438 | if (port == -1) { |
| 2439 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2440 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2441 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2442 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2443 | } |
| 2444 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2445 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2446 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2447 | si = appctx->owner; |
| 2448 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2449 | |
| 2450 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2451 | xref_unlock(&socket->xref, peer); |
| 2452 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2453 | } |
| 2454 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2455 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2456 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2457 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2458 | |
| 2459 | /* inform the stream that we want to be notified whenever the |
| 2460 | * connection completes. |
| 2461 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2462 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2463 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2464 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2465 | |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2466 | hlua->flags |= HLUA_MUST_GC; |
| 2467 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2468 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2469 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2470 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2471 | } |
| 2472 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2473 | |
| 2474 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2475 | /* Return yield waiting for connection. */ |
| 2476 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2477 | 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] | 2478 | |
| 2479 | return 0; |
| 2480 | } |
| 2481 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2482 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2483 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2484 | { |
| 2485 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2486 | struct xref *peer; |
| 2487 | struct appctx *appctx; |
| 2488 | struct stream_interface *si; |
| 2489 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2490 | |
| 2491 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2492 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2493 | |
| 2494 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2495 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2496 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2497 | lua_pushnil(L); |
| 2498 | return 1; |
| 2499 | } |
| 2500 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2501 | si = appctx->owner; |
| 2502 | s = si_strm(si); |
| 2503 | |
| 2504 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2506 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2507 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2508 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2509 | |
| 2510 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2511 | { |
| 2512 | return 0; |
| 2513 | } |
| 2514 | |
| 2515 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2516 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2517 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2518 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2519 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2520 | struct xref *peer; |
| 2521 | struct appctx *appctx; |
| 2522 | struct stream_interface *si; |
| 2523 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2524 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2525 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2526 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2527 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2528 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2529 | /* convert the timeout to millis */ |
| 2530 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2531 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2532 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2533 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2534 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2535 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2536 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2537 | 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] | 2538 | |
| 2539 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2540 | if (tmout == 0) |
| 2541 | tmout++; /* very small timeouts are adjusted to a minium of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2542 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2543 | /* Check if we run on the same thread than the xreator thread. |
| 2544 | * We cannot access to the socket if the thread is different. |
| 2545 | */ |
| 2546 | if (socket->tid != tid) |
| 2547 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2548 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2549 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2550 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2551 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2552 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2553 | WILL_LJMP(lua_error(L)); |
| 2554 | return 0; |
| 2555 | } |
| 2556 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2557 | si = appctx->owner; |
| 2558 | s = si_strm(si); |
| 2559 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2560 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | s->req.rto = tmout; |
| 2562 | s->req.wto = tmout; |
| 2563 | s->res.rto = tmout; |
| 2564 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2565 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2566 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2567 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2568 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2569 | |
| 2570 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2571 | task_queue(s->task); |
| 2572 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2573 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2574 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2575 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2576 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2577 | } |
| 2578 | |
| 2579 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2580 | { |
| 2581 | struct hlua_socket *socket; |
| 2582 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2583 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2584 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2585 | |
| 2586 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2587 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2588 | hlua_pusherror(L, "socket: full stack"); |
| 2589 | goto out_fail_conf; |
| 2590 | } |
| 2591 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2592 | /* Create the object: obj[0] = userdata. */ |
| 2593 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2594 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2595 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2596 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2597 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2599 | /* Check if the various memory pools are intialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2600 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2601 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2602 | goto out_fail_conf; |
| 2603 | } |
| 2604 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2605 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2607 | lua_setmetatable(L, -2); |
| 2608 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2609 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2610 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2611 | if (!appctx) { |
| 2612 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2613 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2614 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2615 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2616 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2617 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2618 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2619 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2620 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2621 | /* Now create a session, task and stream for this applet */ |
| 2622 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2623 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2624 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2625 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2626 | } |
| 2627 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2628 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2629 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2630 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2631 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2632 | } |
| 2633 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2634 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2635 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2637 | /* Configure "right" stream interface. this "si" is used to connect |
| 2638 | * and retrieve data from the server. The connection is initialized |
| 2639 | * with the "struct server". |
| 2640 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2641 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | |
| 2643 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2644 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2645 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2646 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2647 | return 1; |
| 2648 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2649 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2650 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2651 | out_fail_sess: |
| 2652 | appctx_free(appctx); |
| 2653 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2654 | WILL_LJMP(lua_error(L)); |
| 2655 | return 0; |
| 2656 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2657 | |
| 2658 | /* |
| 2659 | * |
| 2660 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2661 | * Class Channel |
| 2662 | * |
| 2663 | * |
| 2664 | */ |
| 2665 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2666 | /* This function is called before the Lua execution. It stores |
| 2667 | * the differents parsers state before executing some Lua code. |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2668 | */ |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2669 | 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] | 2670 | { |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2671 | c->mode = stream->be->mode; |
| 2672 | switch (c->mode) { |
| 2673 | case PR_MODE_HTTP: |
| 2674 | c->data.http.dir = opt & SMP_OPT_DIR; |
| 2675 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2676 | c->data.http.state = stream->txn->req.msg_state; |
| 2677 | else |
| 2678 | c->data.http.state = stream->txn->rsp.msg_state; |
| 2679 | break; |
| 2680 | default: |
| 2681 | break; |
| 2682 | } |
| 2683 | } |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2684 | |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2685 | /* This function is called after the Lua execution. it |
| 2686 | * returns true if the parser state is consistent, otherwise, |
| 2687 | * it return false. |
| 2688 | * |
| 2689 | * In HTTP mode, the parser state must be in the same state |
| 2690 | * or greater when we exit the function. Even if we do a |
| 2691 | * control yield. This prevent to break the HTTP message |
| 2692 | * from the Lua code. |
| 2693 | */ |
| 2694 | static inline int consistency_check(struct stream *stream, int opt, struct hlua_consistency *c) |
| 2695 | { |
| 2696 | if (c->mode != stream->be->mode) |
| 2697 | return 0; |
| 2698 | |
| 2699 | switch (c->mode) { |
| 2700 | case PR_MODE_HTTP: |
| 2701 | if (c->data.http.dir != (opt & SMP_OPT_DIR)) |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2702 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 2703 | if (c->data.http.dir == SMP_OPT_DIR_REQ) |
| 2704 | return stream->txn->req.msg_state >= c->data.http.state; |
| 2705 | else |
| 2706 | return stream->txn->rsp.msg_state >= c->data.http.state; |
| 2707 | default: |
| 2708 | return 1; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 2709 | } |
| 2710 | return 1; |
| 2711 | } |
| 2712 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2713 | /* Returns the struct hlua_channel join to the class channel in the |
| 2714 | * stack entry "ud" or throws an argument error. |
| 2715 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2716 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2717 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2718 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2719 | } |
| 2720 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2721 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2722 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2723 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2724 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2725 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2726 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2727 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2728 | return 0; |
| 2729 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2730 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2731 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2732 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2733 | |
| 2734 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2735 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2736 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2737 | return 1; |
| 2738 | } |
| 2739 | |
| 2740 | /* Duplicate all the data present in the input channel and put it |
| 2741 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2742 | * the stack if the channel is closed and all the data are consumed, |
| 2743 | * returns 0 if no data are available, otherwise it returns the length |
| 2744 | * of the builded string. |
| 2745 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2746 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2747 | { |
| 2748 | char *blk1; |
| 2749 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2750 | size_t len1; |
| 2751 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2752 | int ret; |
| 2753 | luaL_Buffer b; |
| 2754 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2755 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2756 | if (unlikely(ret == 0)) |
| 2757 | return 0; |
| 2758 | |
| 2759 | if (unlikely(ret < 0)) { |
| 2760 | lua_pushnil(L); |
| 2761 | return -1; |
| 2762 | } |
| 2763 | |
| 2764 | luaL_buffinit(L, &b); |
| 2765 | luaL_addlstring(&b, blk1, len1); |
| 2766 | if (unlikely(ret == 2)) |
| 2767 | luaL_addlstring(&b, blk2, len2); |
| 2768 | luaL_pushresult(&b); |
| 2769 | |
| 2770 | if (unlikely(ret == 2)) |
| 2771 | return len1 + len2; |
| 2772 | return len1; |
| 2773 | } |
| 2774 | |
| 2775 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2776 | * a yield. This function keep the data in the buffer. |
| 2777 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2778 | __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] | 2779 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2780 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2781 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2782 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2783 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2784 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2785 | WILL_LJMP(lua_error(L)); |
| 2786 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2787 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2788 | 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] | 2789 | return 1; |
| 2790 | } |
| 2791 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2792 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2793 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2794 | { |
| 2795 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2796 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2797 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2798 | } |
| 2799 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2800 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2801 | * a yield. This function consumes the data in the buffer. It returns |
| 2802 | * a string containing the data or a nil pointer if no data are available |
| 2803 | * and the channel is closed. |
| 2804 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2805 | __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] | 2806 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2807 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2808 | int ret; |
| 2809 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2810 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2811 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2812 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2813 | WILL_LJMP(lua_error(L)); |
| 2814 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2815 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2816 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2817 | 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] | 2818 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (unlikely(ret == -1)) |
| 2820 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2821 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2822 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2823 | return 1; |
| 2824 | } |
| 2825 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2826 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2827 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2828 | { |
| 2829 | MAY_LJMP(check_args(L, 1, "get")); |
| 2830 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2831 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2832 | } |
| 2833 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2834 | /* This functions consumes and returns one line. If the channel is closed, |
| 2835 | * 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] | 2836 | * 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] | 2837 | * value. |
| 2838 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2839 | __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] | 2840 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2841 | char *blk1; |
| 2842 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2843 | size_t len1; |
| 2844 | size_t len2; |
| 2845 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2846 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | int ret; |
| 2848 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2849 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2850 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2851 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2852 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2853 | WILL_LJMP(lua_error(L)); |
| 2854 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2855 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2856 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2857 | 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] | 2858 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2859 | if (ret == -1) { |
| 2860 | lua_pushnil(L); |
| 2861 | return 1; |
| 2862 | } |
| 2863 | |
| 2864 | luaL_buffinit(L, &b); |
| 2865 | luaL_addlstring(&b, blk1, len1); |
| 2866 | len = len1; |
| 2867 | if (unlikely(ret == 2)) { |
| 2868 | luaL_addlstring(&b, blk2, len2); |
| 2869 | len += len2; |
| 2870 | } |
| 2871 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2872 | 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] | 2873 | return 1; |
| 2874 | } |
| 2875 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2876 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2877 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2878 | { |
| 2879 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2880 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2881 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2882 | } |
| 2883 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2884 | /* This function takes a string as input, and append it at the |
| 2885 | * input side of channel. If the data is too big, but a space |
| 2886 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2887 | * yields. If the data is bigger than the buffer, or if the |
| 2888 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2889 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2890 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2891 | __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] | 2892 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2893 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | size_t len; |
| 2895 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2896 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2897 | int ret; |
| 2898 | int max; |
| 2899 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2900 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2901 | WILL_LJMP(lua_error(L)); |
| 2902 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2903 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2904 | * the request buffer if its not required. |
| 2905 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2906 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2907 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2908 | 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] | 2909 | } |
| 2910 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2911 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | if (max > len - l) |
| 2913 | max = len - l; |
| 2914 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2915 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | if (ret == -2 || ret == -3) { |
| 2917 | lua_pushinteger(L, -1); |
| 2918 | return 1; |
| 2919 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2920 | if (ret == -1) { |
| 2921 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2922 | 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] | 2923 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2924 | l += ret; |
| 2925 | lua_pop(L, 1); |
| 2926 | lua_pushinteger(L, l); |
| 2927 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2928 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2929 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2930 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | * in this case, we cannot add more data, so we cannot yield, |
| 2932 | * we return the amount of copyied data. |
| 2933 | */ |
| 2934 | return 1; |
| 2935 | } |
| 2936 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2937 | 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] | 2938 | return 1; |
| 2939 | } |
| 2940 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2941 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2942 | * 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] | 2943 | * buffer size is too little for the data. |
| 2944 | */ |
| 2945 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2946 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2947 | size_t len; |
| 2948 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2949 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2950 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2951 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2952 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2953 | lua_pushinteger(L, 0); |
| 2954 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2955 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | } |
| 2957 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2958 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2959 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2960 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | * or -1 if the channel is closed or if the buffer size is too |
| 2962 | * little for the data. |
| 2963 | */ |
| 2964 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2965 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2966 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2967 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2968 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2969 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2970 | lua_pushinteger(L, 0); |
| 2971 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2972 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2973 | WILL_LJMP(lua_error(L)); |
| 2974 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2975 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2976 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2977 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2978 | } |
| 2979 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2980 | /* Append data in the output side of the buffer. This data is immediately |
| 2981 | * sent. The function returns the amount of data written. If the buffer |
| 2982 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2983 | * if the channel is closed. |
| 2984 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2985 | __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] | 2986 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2987 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2988 | size_t len; |
| 2989 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2990 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2991 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2992 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2993 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2994 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2995 | WILL_LJMP(lua_error(L)); |
| 2996 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2997 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2998 | lua_pushinteger(L, -1); |
| 2999 | return 1; |
| 3000 | } |
| 3001 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3002 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 3003 | * the request buffer if its not required. |
| 3004 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3005 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 3006 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3007 | 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] | 3008 | } |
| 3009 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3010 | /* The written data will be immediately sent, so we can check |
| 3011 | * the available space without taking in account the reserve. |
| 3012 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3013 | * data, because the buffer will be flushed. |
| 3014 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3015 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3016 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3017 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3018 | * in this case, we cannot add more data, so we cannot yield, |
| 3019 | * we return the amount of copyied data. |
| 3020 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3021 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3022 | return 1; |
| 3023 | |
| 3024 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3025 | if (max > len - l) |
| 3026 | max = len - l; |
| 3027 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3028 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3029 | * detects a non contiguous buffer and realign it. |
| 3030 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 3031 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3032 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3033 | |
| 3034 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3035 | 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] | 3036 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3037 | /* buffer replace considers that the input part is filled. |
| 3038 | * so, I must forward these new data in the output part. |
| 3039 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3040 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3041 | |
| 3042 | l += max; |
| 3043 | lua_pop(L, 1); |
| 3044 | lua_pushinteger(L, l); |
| 3045 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3046 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3047 | * in this case, we cannot add more data, so we cannot yield, |
| 3048 | * we return the amount of copyied data. |
| 3049 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3050 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3051 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3052 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3053 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3054 | if (l < len) { |
| 3055 | /* If we are waiting for space in the response buffer, we |
| 3056 | * must set the flag WAKERESWR. This flag required the task |
| 3057 | * wake up if any activity is detected on the response buffer. |
| 3058 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3059 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3060 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3061 | else |
| 3062 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3063 | 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] | 3064 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3065 | |
| 3066 | return 1; |
| 3067 | } |
| 3068 | |
| 3069 | /* Just a wraper of "_hlua_channel_send". This wrapper permits |
| 3070 | * yield the LUA process, and resume it without checking the |
| 3071 | * input arguments. |
| 3072 | */ |
| 3073 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3074 | { |
| 3075 | MAY_LJMP(check_args(L, 2, "send")); |
| 3076 | lua_pushinteger(L, 0); |
| 3077 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3078 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3079 | } |
| 3080 | |
| 3081 | /* This function forward and amount of butes. The data pass from |
| 3082 | * the input side of the buffer to the output side, and can be |
| 3083 | * forwarded. This function never fails. |
| 3084 | * |
| 3085 | * The Lua function takes an amount of bytes to be forwarded in |
| 3086 | * imput. It returns the number of bytes forwarded. |
| 3087 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3088 | __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] | 3089 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3090 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3091 | int len; |
| 3092 | int l; |
| 3093 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3094 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3095 | |
| 3096 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3097 | |
| 3098 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3099 | WILL_LJMP(lua_error(L)); |
| 3100 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3101 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3102 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3103 | |
| 3104 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3105 | if (max > ci_data(chn)) |
| 3106 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3107 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3108 | l += max; |
| 3109 | |
| 3110 | lua_pop(L, 1); |
| 3111 | lua_pushinteger(L, l); |
| 3112 | |
| 3113 | /* Check if it miss bytes to forward. */ |
| 3114 | if (l < len) { |
| 3115 | /* The the input channel or the output channel are closed, we |
| 3116 | * must return the amount of data forwarded. |
| 3117 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3118 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3119 | return 1; |
| 3120 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3121 | /* If we are waiting for space data in the response buffer, we |
| 3122 | * must set the flag WAKERESWR. This flag required the task |
| 3123 | * wake up if any activity is detected on the response buffer. |
| 3124 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3125 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3126 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3127 | else |
| 3128 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3129 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3130 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3131 | 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] | 3132 | } |
| 3133 | |
| 3134 | return 1; |
| 3135 | } |
| 3136 | |
| 3137 | /* Just check the input and prepare the stack for the previous |
| 3138 | * function "hlua_channel_forward_yield" |
| 3139 | */ |
| 3140 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3141 | { |
| 3142 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3143 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3144 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3145 | |
| 3146 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3147 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3148 | } |
| 3149 | |
| 3150 | /* Just returns the number of bytes available in the input |
| 3151 | * side of the buffer. This function never fails. |
| 3152 | */ |
| 3153 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3154 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3155 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3156 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3158 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3159 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3160 | struct htx *htx = htxbuf(&chn->buf); |
| 3161 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3162 | } |
| 3163 | else |
| 3164 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3165 | return 1; |
| 3166 | } |
| 3167 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3168 | /* Returns true if the channel is full. */ |
| 3169 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3170 | { |
| 3171 | struct channel *chn; |
| 3172 | int rem; |
| 3173 | |
| 3174 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3175 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3176 | |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3177 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3178 | struct htx *htx = htxbuf(&chn->buf); |
| 3179 | |
| 3180 | rem = htx_free_data_space(htx); |
| 3181 | } |
| 3182 | else |
| 3183 | rem = b_room(&chn->buf); |
| 3184 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3185 | rem -= global.tune.maxrewrite; /* Rewrite reserved size */ |
| 3186 | |
| 3187 | lua_pushboolean(L, rem <= 0); |
| 3188 | return 1; |
| 3189 | } |
| 3190 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3191 | /* Just returns the number of bytes available in the output |
| 3192 | * side of the buffer. This function never fails. |
| 3193 | */ |
| 3194 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3195 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3196 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3197 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3198 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3199 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3200 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3201 | return 1; |
| 3202 | } |
| 3203 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3204 | /* |
| 3205 | * |
| 3206 | * |
| 3207 | * Class Fetches |
| 3208 | * |
| 3209 | * |
| 3210 | */ |
| 3211 | |
| 3212 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3213 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3214 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3215 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3216 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3217 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3218 | } |
| 3219 | |
| 3220 | /* This function creates and push in the stack a fetch object according |
| 3221 | * with a current TXN. |
| 3222 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3223 | 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] | 3224 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3225 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3226 | |
| 3227 | /* Check stack size. */ |
| 3228 | if (!lua_checkstack(L, 3)) |
| 3229 | return 0; |
| 3230 | |
| 3231 | /* Create the object: obj[0] = userdata. |
| 3232 | * Note that the base of the Fetches object is the |
| 3233 | * transaction object. |
| 3234 | */ |
| 3235 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3236 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3237 | lua_rawseti(L, -2, 0); |
| 3238 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3239 | hsmp->s = txn->s; |
| 3240 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3241 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3242 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3243 | |
| 3244 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3245 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3246 | lua_setmetatable(L, -2); |
| 3247 | |
| 3248 | return 1; |
| 3249 | } |
| 3250 | |
| 3251 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3252 | * It uses closure argument to store the associated sample-fetch. It |
| 3253 | * returns only one argument or throws an error. An error is thrown |
| 3254 | * only if an error is encountered during the argument parsing. If |
| 3255 | * the "sample-fetch" function fails, nil is returned. |
| 3256 | */ |
| 3257 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3258 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3259 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3260 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3261 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3262 | int i; |
| 3263 | struct sample smp; |
| 3264 | |
| 3265 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3266 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3267 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3268 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3269 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3270 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3271 | /* Check execution authorization. */ |
| 3272 | if (f->use & SMP_USE_HTTP_ANY && |
| 3273 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3274 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3275 | "is not available in Lua services", f->kw); |
| 3276 | WILL_LJMP(lua_error(L)); |
| 3277 | } |
| 3278 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3279 | /* Get extra arguments. */ |
| 3280 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3281 | if (i >= ARGM_NBARGS) |
| 3282 | break; |
| 3283 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3284 | } |
| 3285 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3286 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3287 | |
| 3288 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3289 | 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] | 3290 | |
| 3291 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3292 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3293 | lua_pushfstring(L, "error in arguments"); |
| 3294 | WILL_LJMP(lua_error(L)); |
| 3295 | } |
| 3296 | |
| 3297 | /* Initialise the sample. */ |
| 3298 | memset(&smp, 0, sizeof(smp)); |
| 3299 | |
| 3300 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3301 | 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] | 3302 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3303 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3304 | lua_pushstring(L, ""); |
| 3305 | else |
| 3306 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3307 | return 1; |
| 3308 | } |
| 3309 | |
| 3310 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3311 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3312 | hlua_smp2lua_str(L, &smp); |
| 3313 | else |
| 3314 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3315 | return 1; |
| 3316 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3317 | |
| 3318 | /* |
| 3319 | * |
| 3320 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3321 | * Class Converters |
| 3322 | * |
| 3323 | * |
| 3324 | */ |
| 3325 | |
| 3326 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3327 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3328 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3329 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3330 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3331 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3332 | } |
| 3333 | |
| 3334 | /* This function creates and push in the stack a Converters object |
| 3335 | * according with a current TXN. |
| 3336 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3337 | 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] | 3338 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3339 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | |
| 3341 | /* Check stack size. */ |
| 3342 | if (!lua_checkstack(L, 3)) |
| 3343 | return 0; |
| 3344 | |
| 3345 | /* Create the object: obj[0] = userdata. |
| 3346 | * Note that the base of the Converters object is the |
| 3347 | * same than the TXN object. |
| 3348 | */ |
| 3349 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3350 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3351 | lua_rawseti(L, -2, 0); |
| 3352 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3353 | hsmp->s = txn->s; |
| 3354 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3355 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3356 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3357 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3358 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3359 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3360 | lua_setmetatable(L, -2); |
| 3361 | |
| 3362 | return 1; |
| 3363 | } |
| 3364 | |
| 3365 | /* This function is an LUA binding. It is called with each converter. |
| 3366 | * It uses closure argument to store the associated converter. It |
| 3367 | * returns only one argument or throws an error. An error is thrown |
| 3368 | * only if an error is encountered during the argument parsing. If |
| 3369 | * the converter function function fails, nil is returned. |
| 3370 | */ |
| 3371 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3372 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3373 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3374 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3375 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3376 | int i; |
| 3377 | struct sample smp; |
| 3378 | |
| 3379 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3380 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3382 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3383 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3384 | |
| 3385 | /* Get extra arguments. */ |
| 3386 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3387 | if (i >= ARGM_NBARGS) |
| 3388 | break; |
| 3389 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3390 | } |
| 3391 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3392 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3393 | |
| 3394 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3395 | 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] | 3396 | |
| 3397 | /* Run the special args checker. */ |
| 3398 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3399 | hlua_pusherror(L, "error in arguments"); |
| 3400 | WILL_LJMP(lua_error(L)); |
| 3401 | } |
| 3402 | |
| 3403 | /* Initialise the sample. */ |
| 3404 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3405 | hlua_pusherror(L, "error in the input argument"); |
| 3406 | WILL_LJMP(lua_error(L)); |
| 3407 | } |
| 3408 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3409 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3410 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3411 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3412 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3413 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3414 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3415 | WILL_LJMP(lua_error(L)); |
| 3416 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3417 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3418 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3419 | hlua_pusherror(L, "error during the input argument casting"); |
| 3420 | WILL_LJMP(lua_error(L)); |
| 3421 | } |
| 3422 | |
| 3423 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3424 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3425 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3426 | lua_pushstring(L, ""); |
| 3427 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3428 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3429 | return 1; |
| 3430 | } |
| 3431 | |
| 3432 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3433 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3434 | hlua_smp2lua_str(L, &smp); |
| 3435 | else |
| 3436 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3437 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3438 | } |
| 3439 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3440 | /* |
| 3441 | * |
| 3442 | * |
| 3443 | * Class AppletTCP |
| 3444 | * |
| 3445 | * |
| 3446 | */ |
| 3447 | |
| 3448 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3449 | * a class stream, otherwise it throws an error. |
| 3450 | */ |
| 3451 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3452 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3453 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3454 | } |
| 3455 | |
| 3456 | /* This function creates and push in the stack an Applet object |
| 3457 | * according with a current TXN. |
| 3458 | */ |
| 3459 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3460 | { |
| 3461 | struct hlua_appctx *appctx; |
| 3462 | struct stream_interface *si = ctx->owner; |
| 3463 | struct stream *s = si_strm(si); |
| 3464 | struct proxy *p = s->be; |
| 3465 | |
| 3466 | /* Check stack size. */ |
| 3467 | if (!lua_checkstack(L, 3)) |
| 3468 | return 0; |
| 3469 | |
| 3470 | /* Create the object: obj[0] = userdata. |
| 3471 | * Note that the base of the Converters object is the |
| 3472 | * same than the TXN object. |
| 3473 | */ |
| 3474 | lua_newtable(L); |
| 3475 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3476 | lua_rawseti(L, -2, 0); |
| 3477 | appctx->appctx = ctx; |
| 3478 | appctx->htxn.s = s; |
| 3479 | appctx->htxn.p = p; |
| 3480 | |
| 3481 | /* Create the "f" field that contains a list of fetches. */ |
| 3482 | lua_pushstring(L, "f"); |
| 3483 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3484 | return 0; |
| 3485 | lua_settable(L, -3); |
| 3486 | |
| 3487 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3488 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3489 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3490 | return 0; |
| 3491 | lua_settable(L, -3); |
| 3492 | |
| 3493 | /* Create the "c" field that contains a list of converters. */ |
| 3494 | lua_pushstring(L, "c"); |
| 3495 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3496 | return 0; |
| 3497 | lua_settable(L, -3); |
| 3498 | |
| 3499 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3500 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3501 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3502 | return 0; |
| 3503 | lua_settable(L, -3); |
| 3504 | |
| 3505 | /* Pop a class stream metatable and affect it to the table. */ |
| 3506 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3507 | lua_setmetatable(L, -2); |
| 3508 | |
| 3509 | return 1; |
| 3510 | } |
| 3511 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3512 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3513 | { |
| 3514 | struct hlua_appctx *appctx; |
| 3515 | struct stream *s; |
| 3516 | const char *name; |
| 3517 | size_t len; |
| 3518 | struct sample smp; |
| 3519 | |
| 3520 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3521 | |
| 3522 | /* It is useles to retrieve the stream, but this function |
| 3523 | * runs only in a stream context. |
| 3524 | */ |
| 3525 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3526 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3527 | s = appctx->htxn.s; |
| 3528 | |
| 3529 | /* Converts the third argument in a sample. */ |
| 3530 | hlua_lua2smp(L, 3, &smp); |
| 3531 | |
| 3532 | /* Store the sample in a variable. */ |
| 3533 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3534 | vars_set_by_name(name, len, &smp); |
| 3535 | return 0; |
| 3536 | } |
| 3537 | |
| 3538 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3539 | { |
| 3540 | struct hlua_appctx *appctx; |
| 3541 | struct stream *s; |
| 3542 | const char *name; |
| 3543 | size_t len; |
| 3544 | struct sample smp; |
| 3545 | |
| 3546 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3547 | |
| 3548 | /* It is useles to retrieve the stream, but this function |
| 3549 | * runs only in a stream context. |
| 3550 | */ |
| 3551 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3552 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3553 | s = appctx->htxn.s; |
| 3554 | |
| 3555 | /* Unset the variable. */ |
| 3556 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3557 | vars_unset_by_name(name, len, &smp); |
| 3558 | return 0; |
| 3559 | } |
| 3560 | |
| 3561 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3562 | { |
| 3563 | struct hlua_appctx *appctx; |
| 3564 | struct stream *s; |
| 3565 | const char *name; |
| 3566 | size_t len; |
| 3567 | struct sample smp; |
| 3568 | |
| 3569 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3570 | |
| 3571 | /* It is useles to retrieve the stream, but this function |
| 3572 | * runs only in a stream context. |
| 3573 | */ |
| 3574 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3575 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3576 | s = appctx->htxn.s; |
| 3577 | |
| 3578 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3579 | if (!vars_get_by_name(name, len, &smp)) { |
| 3580 | lua_pushnil(L); |
| 3581 | return 1; |
| 3582 | } |
| 3583 | |
| 3584 | return hlua_smp2lua(L, &smp); |
| 3585 | } |
| 3586 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3587 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3588 | { |
| 3589 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3590 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3591 | struct hlua *hlua; |
| 3592 | |
| 3593 | /* 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] | 3594 | if (!s->hlua) |
| 3595 | return 0; |
| 3596 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3597 | |
| 3598 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3599 | |
| 3600 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3601 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3602 | |
| 3603 | /* Get and store new value. */ |
| 3604 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3605 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3606 | |
| 3607 | return 0; |
| 3608 | } |
| 3609 | |
| 3610 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3611 | { |
| 3612 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3613 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3614 | struct hlua *hlua; |
| 3615 | |
| 3616 | /* 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] | 3617 | if (!s->hlua) { |
| 3618 | lua_pushnil(L); |
| 3619 | return 1; |
| 3620 | } |
| 3621 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3622 | |
| 3623 | /* Push configuration index in the stack. */ |
| 3624 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3625 | |
| 3626 | return 1; |
| 3627 | } |
| 3628 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3629 | /* If expected data not yet available, it returns a yield. This function |
| 3630 | * consumes the data in the buffer. It returns a string containing the |
| 3631 | * data. This string can be empty. |
| 3632 | */ |
| 3633 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3634 | { |
| 3635 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3636 | struct stream_interface *si = appctx->appctx->owner; |
| 3637 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3638 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3639 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3640 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3641 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3642 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3643 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3644 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3645 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3646 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3647 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3648 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3649 | 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] | 3650 | } |
| 3651 | |
| 3652 | /* End of data: commit the total strings and return. */ |
| 3653 | if (ret < 0) { |
| 3654 | luaL_pushresult(&appctx->b); |
| 3655 | return 1; |
| 3656 | } |
| 3657 | |
| 3658 | /* Ensure that the block 2 length is usable. */ |
| 3659 | if (ret == 1) |
| 3660 | len2 = 0; |
| 3661 | |
| 3662 | /* dont check the max length read and dont check. */ |
| 3663 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3664 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3665 | |
| 3666 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3667 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3668 | luaL_pushresult(&appctx->b); |
| 3669 | return 1; |
| 3670 | } |
| 3671 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3672 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3673 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3674 | { |
| 3675 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3676 | |
| 3677 | /* Initialise the string catenation. */ |
| 3678 | luaL_buffinit(L, &appctx->b); |
| 3679 | |
| 3680 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3681 | } |
| 3682 | |
| 3683 | /* If expected data not yet available, it returns a yield. This function |
| 3684 | * consumes the data in the buffer. It returns a string containing the |
| 3685 | * data. This string can be empty. |
| 3686 | */ |
| 3687 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3688 | { |
| 3689 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3690 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3691 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3692 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3693 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3694 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3695 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3696 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3697 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3698 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3699 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3700 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3701 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3702 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3703 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3704 | 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] | 3705 | } |
| 3706 | |
| 3707 | /* End of data: commit the total strings and return. */ |
| 3708 | if (ret < 0) { |
| 3709 | luaL_pushresult(&appctx->b); |
| 3710 | return 1; |
| 3711 | } |
| 3712 | |
| 3713 | /* Ensure that the block 2 length is usable. */ |
| 3714 | if (ret == 1) |
| 3715 | len2 = 0; |
| 3716 | |
| 3717 | if (len == -1) { |
| 3718 | |
| 3719 | /* If len == -1, catenate all the data avalaile and |
| 3720 | * yield because we want to get all the data until |
| 3721 | * the end of data stream. |
| 3722 | */ |
| 3723 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3724 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3725 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3726 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3727 | 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] | 3728 | |
| 3729 | } else { |
| 3730 | |
| 3731 | /* Copy the fisrt block caping to the length required. */ |
| 3732 | if (len1 > len) |
| 3733 | len1 = len; |
| 3734 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3735 | len -= len1; |
| 3736 | |
| 3737 | /* Copy the second block. */ |
| 3738 | if (len2 > len) |
| 3739 | len2 = len; |
| 3740 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3741 | len -= len2; |
| 3742 | |
| 3743 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3744 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3745 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3746 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3747 | if (len > 0) { |
| 3748 | lua_pushinteger(L, len); |
| 3749 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3750 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3751 | 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] | 3752 | } |
| 3753 | |
| 3754 | /* return the result. */ |
| 3755 | luaL_pushresult(&appctx->b); |
| 3756 | return 1; |
| 3757 | } |
| 3758 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3759 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3760 | hlua_pusherror(L, "Lua: internal error"); |
| 3761 | WILL_LJMP(lua_error(L)); |
| 3762 | return 0; |
| 3763 | } |
| 3764 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3765 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3766 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3767 | { |
| 3768 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3769 | int len = -1; |
| 3770 | |
| 3771 | if (lua_gettop(L) > 2) |
| 3772 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3773 | if (lua_gettop(L) >= 2) { |
| 3774 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3775 | lua_pop(L, 1); |
| 3776 | } |
| 3777 | |
| 3778 | /* Confirm or set the required length */ |
| 3779 | lua_pushinteger(L, len); |
| 3780 | |
| 3781 | /* Initialise the string catenation. */ |
| 3782 | luaL_buffinit(L, &appctx->b); |
| 3783 | |
| 3784 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3785 | } |
| 3786 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3787 | /* Append data in the output side of the buffer. This data is immediately |
| 3788 | * sent. The function returns the amount of data written. If the buffer |
| 3789 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3790 | * if the channel is closed. |
| 3791 | */ |
| 3792 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3793 | { |
| 3794 | size_t len; |
| 3795 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3796 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3797 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3798 | struct stream_interface *si = appctx->appctx->owner; |
| 3799 | struct channel *chn = si_ic(si); |
| 3800 | int max; |
| 3801 | |
| 3802 | /* Get the max amount of data which can write as input in the channel. */ |
| 3803 | max = channel_recv_max(chn); |
| 3804 | if (max > (len - l)) |
| 3805 | max = len - l; |
| 3806 | |
| 3807 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3808 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3809 | |
| 3810 | /* update counters. */ |
| 3811 | l += max; |
| 3812 | lua_pop(L, 1); |
| 3813 | lua_pushinteger(L, l); |
| 3814 | |
| 3815 | /* If some data is not send, declares the situation to the |
| 3816 | * applet, and returns a yield. |
| 3817 | */ |
| 3818 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3819 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3820 | 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] | 3821 | } |
| 3822 | |
| 3823 | return 1; |
| 3824 | } |
| 3825 | |
| 3826 | /* Just a wraper of "hlua_applet_tcp_send_yield". This wrapper permits |
| 3827 | * yield the LUA process, and resume it without checking the |
| 3828 | * input arguments. |
| 3829 | */ |
| 3830 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3831 | { |
| 3832 | MAY_LJMP(check_args(L, 2, "send")); |
| 3833 | lua_pushinteger(L, 0); |
| 3834 | |
| 3835 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3836 | } |
| 3837 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3838 | /* |
| 3839 | * |
| 3840 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3841 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3842 | * |
| 3843 | * |
| 3844 | */ |
| 3845 | |
| 3846 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3847 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3848 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3849 | __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] | 3850 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3851 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3852 | } |
| 3853 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3855 | * according with a current TXN. |
| 3856 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3857 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3858 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3859 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3860 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3861 | struct stream_interface *si = ctx->owner; |
| 3862 | struct stream *s = si_strm(si); |
| 3863 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3864 | struct htx *htx; |
| 3865 | struct htx_blk *blk; |
| 3866 | struct htx_sl *sl; |
| 3867 | struct ist path; |
| 3868 | unsigned long long len = 0; |
| 3869 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3870 | |
| 3871 | /* Check stack size. */ |
| 3872 | if (!lua_checkstack(L, 3)) |
| 3873 | return 0; |
| 3874 | |
| 3875 | /* Create the object: obj[0] = userdata. |
| 3876 | * Note that the base of the Converters object is the |
| 3877 | * same than the TXN object. |
| 3878 | */ |
| 3879 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3880 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3881 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3882 | appctx->appctx = ctx; |
| 3883 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3884 | 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] | 3885 | appctx->htxn.s = s; |
| 3886 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3887 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3888 | /* Create the "f" field that contains a list of fetches. */ |
| 3889 | lua_pushstring(L, "f"); |
| 3890 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3891 | return 0; |
| 3892 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3893 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3894 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3895 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3896 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3897 | return 0; |
| 3898 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3899 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3900 | /* Create the "c" field that contains a list of converters. */ |
| 3901 | lua_pushstring(L, "c"); |
| 3902 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3903 | return 0; |
| 3904 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3905 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3906 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3907 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3908 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3909 | return 0; |
| 3910 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3911 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3912 | htx = htxbuf(&s->req.buf); |
| 3913 | blk = htx_get_first_blk(htx); |
| 3914 | BUG_ON(htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
| 3915 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3916 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3917 | /* Stores the request method. */ |
| 3918 | lua_pushstring(L, "method"); |
| 3919 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3920 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3921 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3922 | /* Stores the http version. */ |
| 3923 | lua_pushstring(L, "version"); |
| 3924 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3925 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3926 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3927 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3928 | * the array on the top of the stack. |
| 3929 | */ |
| 3930 | lua_pushstring(L, "headers"); |
| 3931 | htxn.s = s; |
| 3932 | htxn.p = px; |
| 3933 | htxn.dir = SMP_OPT_DIR_REQ; |
| 3934 | if (!hlua_http_get_headers(L, &htxn, &htxn.s->txn->req)) |
| 3935 | return 0; |
| 3936 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3937 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3938 | path = http_get_path(htx_sl_req_uri(sl)); |
| 3939 | if (path.ptr) { |
| 3940 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3941 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3942 | p = path.ptr; |
| 3943 | end = path.ptr + path.len; |
| 3944 | q = p; |
| 3945 | while (q < end && *q != '?') |
| 3946 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3947 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3948 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3949 | lua_pushstring(L, "path"); |
| 3950 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3951 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3952 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3953 | /* Stores the query string. */ |
| 3954 | lua_pushstring(L, "qs"); |
| 3955 | if (*q == '?') |
| 3956 | q++; |
| 3957 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3958 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3959 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3960 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3961 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3962 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3963 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3964 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3965 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3966 | break; |
| 3967 | if (type == HTX_BLK_DATA) |
| 3968 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 3971 | len += htx->extra; |
| 3972 | |
| 3973 | /* Stores the request path. */ |
| 3974 | lua_pushstring(L, "length"); |
| 3975 | lua_pushinteger(L, len); |
| 3976 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3977 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3978 | /* Create an empty array of HTTP request headers. */ |
| 3979 | lua_pushstring(L, "response"); |
| 3980 | lua_newtable(L); |
| 3981 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3982 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3983 | /* Pop a class stream metatable and affect it to the table. */ |
| 3984 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3985 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3986 | |
| 3987 | return 1; |
| 3988 | } |
| 3989 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3990 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3991 | { |
| 3992 | struct hlua_appctx *appctx; |
| 3993 | struct stream *s; |
| 3994 | const char *name; |
| 3995 | size_t len; |
| 3996 | struct sample smp; |
| 3997 | |
| 3998 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 3999 | |
| 4000 | /* It is useles to retrieve the stream, but this function |
| 4001 | * runs only in a stream context. |
| 4002 | */ |
| 4003 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4004 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4005 | s = appctx->htxn.s; |
| 4006 | |
| 4007 | /* Converts the third argument in a sample. */ |
| 4008 | hlua_lua2smp(L, 3, &smp); |
| 4009 | |
| 4010 | /* Store the sample in a variable. */ |
| 4011 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4012 | vars_set_by_name(name, len, &smp); |
| 4013 | return 0; |
| 4014 | } |
| 4015 | |
| 4016 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 4017 | { |
| 4018 | struct hlua_appctx *appctx; |
| 4019 | struct stream *s; |
| 4020 | const char *name; |
| 4021 | size_t len; |
| 4022 | struct sample smp; |
| 4023 | |
| 4024 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4025 | |
| 4026 | /* It is useles to retrieve the stream, but this function |
| 4027 | * runs only in a stream context. |
| 4028 | */ |
| 4029 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4030 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4031 | s = appctx->htxn.s; |
| 4032 | |
| 4033 | /* Unset the variable. */ |
| 4034 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4035 | vars_unset_by_name(name, len, &smp); |
| 4036 | return 0; |
| 4037 | } |
| 4038 | |
| 4039 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4040 | { |
| 4041 | struct hlua_appctx *appctx; |
| 4042 | struct stream *s; |
| 4043 | const char *name; |
| 4044 | size_t len; |
| 4045 | struct sample smp; |
| 4046 | |
| 4047 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4048 | |
| 4049 | /* It is useles to retrieve the stream, but this function |
| 4050 | * runs only in a stream context. |
| 4051 | */ |
| 4052 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4053 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4054 | s = appctx->htxn.s; |
| 4055 | |
| 4056 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4057 | if (!vars_get_by_name(name, len, &smp)) { |
| 4058 | lua_pushnil(L); |
| 4059 | return 1; |
| 4060 | } |
| 4061 | |
| 4062 | return hlua_smp2lua(L, &smp); |
| 4063 | } |
| 4064 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4065 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4066 | { |
| 4067 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4068 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4069 | struct hlua *hlua; |
| 4070 | |
| 4071 | /* 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] | 4072 | if (!s->hlua) |
| 4073 | return 0; |
| 4074 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4075 | |
| 4076 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4077 | |
| 4078 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4079 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4080 | |
| 4081 | /* Get and store new value. */ |
| 4082 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4083 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4084 | |
| 4085 | return 0; |
| 4086 | } |
| 4087 | |
| 4088 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4089 | { |
| 4090 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4091 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4092 | struct hlua *hlua; |
| 4093 | |
| 4094 | /* 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] | 4095 | if (!s->hlua) { |
| 4096 | lua_pushnil(L); |
| 4097 | return 1; |
| 4098 | } |
| 4099 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4100 | |
| 4101 | /* Push configuration index in the stack. */ |
| 4102 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4103 | |
| 4104 | return 1; |
| 4105 | } |
| 4106 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4107 | /* If expected data not yet available, it returns a yield. This function |
| 4108 | * consumes the data in the buffer. It returns a string containing the |
| 4109 | * data. This string can be empty. |
| 4110 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4111 | __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] | 4112 | { |
| 4113 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4114 | struct stream_interface *si = appctx->appctx->owner; |
| 4115 | struct channel *req = si_oc(si); |
| 4116 | struct htx *htx; |
| 4117 | struct htx_blk *blk; |
| 4118 | size_t count; |
| 4119 | int stop = 0; |
| 4120 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4121 | htx = htx_from_buf(&req->buf); |
| 4122 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4123 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4124 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4125 | while (count && !stop && blk) { |
| 4126 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4127 | uint32_t sz = htx_get_blksz(blk); |
| 4128 | struct ist v; |
| 4129 | uint32_t vlen; |
| 4130 | char *nl; |
| 4131 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4132 | if (type == HTX_BLK_EOM) { |
| 4133 | stop = 1; |
| 4134 | break; |
| 4135 | } |
| 4136 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4137 | vlen = sz; |
| 4138 | if (vlen > count) { |
| 4139 | if (type != HTX_BLK_DATA) |
| 4140 | break; |
| 4141 | vlen = count; |
| 4142 | } |
| 4143 | |
| 4144 | switch (type) { |
| 4145 | case HTX_BLK_UNUSED: |
| 4146 | break; |
| 4147 | |
| 4148 | case HTX_BLK_DATA: |
| 4149 | v = htx_get_blk_value(htx, blk); |
| 4150 | v.len = vlen; |
| 4151 | nl = istchr(v, '\n'); |
| 4152 | if (nl != NULL) { |
| 4153 | stop = 1; |
| 4154 | vlen = nl - v.ptr + 1; |
| 4155 | } |
| 4156 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4157 | break; |
| 4158 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4159 | case HTX_BLK_TLR: |
| 4160 | case HTX_BLK_EOM: |
| 4161 | stop = 1; |
| 4162 | break; |
| 4163 | |
| 4164 | default: |
| 4165 | break; |
| 4166 | } |
| 4167 | |
| 4168 | co_set_data(req, co_data(req) - vlen); |
| 4169 | count -= vlen; |
| 4170 | if (sz == vlen) |
| 4171 | blk = htx_remove_blk(htx, blk); |
| 4172 | else { |
| 4173 | htx_cut_data_blk(htx, blk, vlen); |
| 4174 | break; |
| 4175 | } |
| 4176 | } |
| 4177 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4178 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4179 | if (!stop) { |
| 4180 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4181 | 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] | 4182 | } |
| 4183 | |
| 4184 | /* return the result. */ |
| 4185 | luaL_pushresult(&appctx->b); |
| 4186 | return 1; |
| 4187 | } |
| 4188 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4189 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4190 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4191 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4192 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4193 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4194 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4195 | /* Initialise the string catenation. */ |
| 4196 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4197 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4198 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4199 | } |
| 4200 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4201 | /* If expected data not yet available, it returns a yield. This function |
| 4202 | * consumes the data in the buffer. It returns a string containing the |
| 4203 | * data. This string can be empty. |
| 4204 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4205 | __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] | 4206 | { |
| 4207 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4208 | struct stream_interface *si = appctx->appctx->owner; |
| 4209 | struct channel *req = si_oc(si); |
| 4210 | struct htx *htx; |
| 4211 | struct htx_blk *blk; |
| 4212 | size_t count; |
| 4213 | int len; |
| 4214 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4215 | htx = htx_from_buf(&req->buf); |
| 4216 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4217 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4218 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4219 | while (count && len && blk) { |
| 4220 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4221 | uint32_t sz = htx_get_blksz(blk); |
| 4222 | struct ist v; |
| 4223 | uint32_t vlen; |
| 4224 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4225 | if (type == HTX_BLK_EOM) { |
| 4226 | len = 0; |
| 4227 | break; |
| 4228 | } |
| 4229 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4230 | vlen = sz; |
| 4231 | if (len > 0 && vlen > len) |
| 4232 | vlen = len; |
| 4233 | if (vlen > count) { |
| 4234 | if (type != HTX_BLK_DATA) |
| 4235 | break; |
| 4236 | vlen = count; |
| 4237 | } |
| 4238 | |
| 4239 | switch (type) { |
| 4240 | case HTX_BLK_UNUSED: |
| 4241 | break; |
| 4242 | |
| 4243 | case HTX_BLK_DATA: |
| 4244 | v = htx_get_blk_value(htx, blk); |
| 4245 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4246 | break; |
| 4247 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4248 | case HTX_BLK_TLR: |
| 4249 | case HTX_BLK_EOM: |
| 4250 | len = 0; |
| 4251 | break; |
| 4252 | |
| 4253 | default: |
| 4254 | break; |
| 4255 | } |
| 4256 | |
| 4257 | co_set_data(req, co_data(req) - vlen); |
| 4258 | count -= vlen; |
| 4259 | if (len > 0) |
| 4260 | len -= vlen; |
| 4261 | if (sz == vlen) |
| 4262 | blk = htx_remove_blk(htx, blk); |
| 4263 | else { |
| 4264 | htx_cut_data_blk(htx, blk, vlen); |
| 4265 | break; |
| 4266 | } |
| 4267 | } |
| 4268 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4269 | htx_to_buf(htx, &req->buf); |
| 4270 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4271 | /* If we are no other data available, yield waiting for new data. */ |
| 4272 | if (len) { |
| 4273 | if (len > 0) { |
| 4274 | lua_pushinteger(L, len); |
| 4275 | lua_replace(L, 2); |
| 4276 | } |
| 4277 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4278 | 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] | 4279 | } |
| 4280 | |
| 4281 | /* return the result. */ |
| 4282 | luaL_pushresult(&appctx->b); |
| 4283 | return 1; |
| 4284 | } |
| 4285 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4286 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4287 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4288 | { |
| 4289 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4290 | int len = -1; |
| 4291 | |
| 4292 | /* Check arguments. */ |
| 4293 | if (lua_gettop(L) > 2) |
| 4294 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4295 | if (lua_gettop(L) >= 2) { |
| 4296 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4297 | lua_pop(L, 1); |
| 4298 | } |
| 4299 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4300 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4301 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4302 | /* Initialise the string catenation. */ |
| 4303 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4304 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4305 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4306 | } |
| 4307 | |
| 4308 | /* Append data in the output side of the buffer. This data is immediately |
| 4309 | * sent. The function returns the amount of data written. If the buffer |
| 4310 | * cannot contain the data, the function yields. The function returns -1 |
| 4311 | * if the channel is closed. |
| 4312 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4313 | __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] | 4314 | { |
| 4315 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4316 | struct stream_interface *si = appctx->appctx->owner; |
| 4317 | struct channel *res = si_ic(si); |
| 4318 | struct htx *htx = htx_from_buf(&res->buf); |
| 4319 | const char *data; |
| 4320 | size_t len; |
| 4321 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4322 | int max; |
| 4323 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4324 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4325 | if (!max) |
| 4326 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4327 | |
| 4328 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4329 | |
| 4330 | /* Get the max amount of data which can write as input in the channel. */ |
| 4331 | if (max > (len - l)) |
| 4332 | max = len - l; |
| 4333 | |
| 4334 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4335 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4336 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4337 | |
| 4338 | /* update counters. */ |
| 4339 | l += max; |
| 4340 | lua_pop(L, 1); |
| 4341 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4343 | /* If some data is not send, declares the situation to the |
| 4344 | * applet, and returns a yield. |
| 4345 | */ |
| 4346 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4347 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4348 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4349 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4350 | 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] | 4351 | } |
| 4352 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4353 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4354 | return 1; |
| 4355 | } |
| 4356 | |
| 4357 | /* Just a wraper of "hlua_applet_send_yield". This wrapper permits |
| 4358 | * yield the LUA process, and resume it without checking the |
| 4359 | * input arguments. |
| 4360 | */ |
| 4361 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4362 | { |
| 4363 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4364 | |
| 4365 | /* We want to send some data. Headers must be sent. */ |
| 4366 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4367 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4368 | WILL_LJMP(lua_error(L)); |
| 4369 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4370 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4371 | /* This interger is used for followinf the amount of data sent. */ |
| 4372 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4373 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4374 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4375 | } |
| 4376 | |
| 4377 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4378 | { |
| 4379 | const char *name; |
| 4380 | int ret; |
| 4381 | |
| 4382 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4383 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4384 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4385 | |
| 4386 | /* Push in the stack the "response" entry. */ |
| 4387 | ret = lua_getfield(L, 1, "response"); |
| 4388 | if (ret != LUA_TTABLE) { |
| 4389 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4390 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4391 | WILL_LJMP(lua_error(L)); |
| 4392 | } |
| 4393 | |
| 4394 | /* check if the header is already registered if it is not |
| 4395 | * the case, register it. |
| 4396 | */ |
| 4397 | ret = lua_getfield(L, -1, name); |
| 4398 | if (ret == LUA_TNIL) { |
| 4399 | |
| 4400 | /* Entry not found. */ |
| 4401 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4402 | |
| 4403 | /* Insert the new header name in the array in the top of the stack. |
| 4404 | * It left the new array in the top of the stack. |
| 4405 | */ |
| 4406 | lua_newtable(L); |
| 4407 | lua_pushvalue(L, 2); |
| 4408 | lua_pushvalue(L, -2); |
| 4409 | lua_settable(L, -4); |
| 4410 | |
| 4411 | } else if (ret != LUA_TTABLE) { |
| 4412 | |
| 4413 | /* corruption error. */ |
| 4414 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4415 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4416 | WILL_LJMP(lua_error(L)); |
| 4417 | } |
| 4418 | |
| 4419 | /* Now the top od thestack is an array of values. We push |
| 4420 | * the header value as new entry. |
| 4421 | */ |
| 4422 | lua_pushvalue(L, 3); |
| 4423 | ret = lua_rawlen(L, -2); |
| 4424 | lua_rawseti(L, -2, ret + 1); |
| 4425 | lua_pushboolean(L, 1); |
| 4426 | return 1; |
| 4427 | } |
| 4428 | |
| 4429 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4430 | { |
| 4431 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4432 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4433 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4434 | |
| 4435 | if (status < 100 || status > 599) { |
| 4436 | lua_pushboolean(L, 0); |
| 4437 | return 1; |
| 4438 | } |
| 4439 | |
| 4440 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4441 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4442 | lua_pushboolean(L, 1); |
| 4443 | return 1; |
| 4444 | } |
| 4445 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4446 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4447 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4448 | { |
| 4449 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4450 | struct stream_interface *si = appctx->appctx->owner; |
| 4451 | struct channel *res = si_ic(si); |
| 4452 | struct htx *htx; |
| 4453 | struct htx_sl *sl; |
| 4454 | struct h1m h1m; |
| 4455 | const char *status, *reason; |
| 4456 | const char *name, *value; |
| 4457 | size_t nlen, vlen; |
| 4458 | unsigned int flags; |
| 4459 | |
| 4460 | /* Send the message at once. */ |
| 4461 | htx = htx_from_buf(&res->buf); |
| 4462 | h1m_init_res(&h1m); |
| 4463 | |
| 4464 | /* Use the same http version than the request. */ |
| 4465 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4466 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4467 | if (reason == NULL) |
| 4468 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4469 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4470 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4471 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4472 | } |
| 4473 | else { |
| 4474 | flags = HTX_SL_F_IS_RESP; |
| 4475 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4476 | } |
| 4477 | if (!sl) { |
| 4478 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4479 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4480 | WILL_LJMP(lua_error(L)); |
| 4481 | } |
| 4482 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4483 | |
| 4484 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4485 | lua_pushvalue(L, 0); |
| 4486 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4487 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4488 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4489 | WILL_LJMP(lua_error(L)); |
| 4490 | } |
| 4491 | |
| 4492 | /* Browse the list of headers. */ |
| 4493 | lua_pushnil(L); |
| 4494 | while(lua_next(L, -2) != 0) { |
| 4495 | /* We expect a string as -2. */ |
| 4496 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4497 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4498 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4499 | lua_typename(L, lua_type(L, -2))); |
| 4500 | WILL_LJMP(lua_error(L)); |
| 4501 | } |
| 4502 | name = lua_tolstring(L, -2, &nlen); |
| 4503 | |
| 4504 | /* We expect an array as -1. */ |
| 4505 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4506 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4507 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4508 | name, |
| 4509 | lua_typename(L, lua_type(L, -1))); |
| 4510 | WILL_LJMP(lua_error(L)); |
| 4511 | } |
| 4512 | |
| 4513 | /* Browse the table who is on the top of the stack. */ |
| 4514 | lua_pushnil(L); |
| 4515 | while(lua_next(L, -2) != 0) { |
| 4516 | int id; |
| 4517 | |
| 4518 | /* We expect a number as -2. */ |
| 4519 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4520 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4521 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4522 | name, |
| 4523 | lua_typename(L, lua_type(L, -2))); |
| 4524 | WILL_LJMP(lua_error(L)); |
| 4525 | } |
| 4526 | id = lua_tointeger(L, -2); |
| 4527 | |
| 4528 | /* We expect a string as -2. */ |
| 4529 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4530 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4531 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4532 | name, id, |
| 4533 | lua_typename(L, lua_type(L, -1))); |
| 4534 | WILL_LJMP(lua_error(L)); |
| 4535 | } |
| 4536 | value = lua_tolstring(L, -1, &vlen); |
| 4537 | |
| 4538 | /* Simple Protocol checks. */ |
| 4539 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
| 4540 | h1_parse_xfer_enc_header(&h1m, ist2(name, nlen)); |
| 4541 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4542 | struct ist v = ist2(value, vlen); |
| 4543 | int ret; |
| 4544 | |
| 4545 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4546 | if (ret < 0) { |
| 4547 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4548 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4549 | name); |
| 4550 | WILL_LJMP(lua_error(L)); |
| 4551 | } |
| 4552 | else if (ret == 0) |
| 4553 | goto next; /* Skip it */ |
| 4554 | } |
| 4555 | |
| 4556 | /* Add a new header */ |
| 4557 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4558 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4559 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4560 | name); |
| 4561 | WILL_LJMP(lua_error(L)); |
| 4562 | } |
| 4563 | next: |
| 4564 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4565 | lua_pop(L, 1); |
| 4566 | } |
| 4567 | |
| 4568 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4569 | lua_pop(L, 1); |
| 4570 | } |
| 4571 | |
| 4572 | if (h1m.flags & H1_MF_CHNK) |
| 4573 | h1m.flags &= ~H1_MF_CLEN; |
| 4574 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4575 | h1m.flags |= H1_MF_XFER_LEN; |
| 4576 | |
| 4577 | /* Uset HTX start-line flags */ |
| 4578 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4579 | flags |= HTX_SL_F_XFER_ENC; |
| 4580 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4581 | flags |= HTX_SL_F_XFER_LEN; |
| 4582 | if (h1m.flags & H1_MF_CHNK) |
| 4583 | flags |= HTX_SL_F_CHNK; |
| 4584 | else if (h1m.flags & H1_MF_CLEN) |
| 4585 | flags |= HTX_SL_F_CLEN; |
| 4586 | if (h1m.body_len == 0) |
| 4587 | flags |= HTX_SL_F_BODYLESS; |
| 4588 | } |
| 4589 | sl->flags |= flags; |
| 4590 | |
| 4591 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4592 | * and the status code implies the presence of a message body, we must |
| 4593 | * announce a transfer encoding chunked. This is required by haproxy |
| 4594 | * for the keepalive compliance. If the applet annouces a transfer-encoding |
| 4595 | * chunked itslef, don't do anything. |
| 4596 | */ |
| 4597 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4598 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4599 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4600 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4601 | /* Add a new header */ |
| 4602 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4603 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4604 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4605 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4606 | WILL_LJMP(lua_error(L)); |
| 4607 | } |
| 4608 | } |
| 4609 | |
| 4610 | /* Finalize headers. */ |
| 4611 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4612 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4613 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4614 | WILL_LJMP(lua_error(L)); |
| 4615 | } |
| 4616 | |
| 4617 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4618 | b_reset(&res->buf); |
| 4619 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4620 | WILL_LJMP(lua_error(L)); |
| 4621 | } |
| 4622 | |
| 4623 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4624 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4625 | |
| 4626 | /* Headers sent, set the flag. */ |
| 4627 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4628 | return 0; |
| 4629 | |
| 4630 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4631 | /* We will build the status line and the headers of the HTTP response. |
| 4632 | * We will try send at once if its not possible, we give back the hand |
| 4633 | * waiting for more room. |
| 4634 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4635 | __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] | 4636 | { |
| 4637 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4638 | struct stream_interface *si = appctx->appctx->owner; |
| 4639 | struct channel *res = si_ic(si); |
| 4640 | |
| 4641 | if (co_data(res)) { |
| 4642 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4643 | 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] | 4644 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4645 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4646 | } |
| 4647 | |
| 4648 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4649 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4650 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4651 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4652 | } |
| 4653 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4654 | /* |
| 4655 | * |
| 4656 | * |
| 4657 | * Class HTTP |
| 4658 | * |
| 4659 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4660 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4661 | |
| 4662 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4663 | * a class stream, otherwise it throws an error. |
| 4664 | */ |
| 4665 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4667 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4668 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4669 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4670 | /* This function creates and push in the stack a HTTP object |
| 4671 | * according with a current TXN. |
| 4672 | */ |
| 4673 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4674 | { |
| 4675 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4676 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4677 | /* Check stack size. */ |
| 4678 | if (!lua_checkstack(L, 3)) |
| 4679 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4680 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4681 | /* Create the object: obj[0] = userdata. |
| 4682 | * Note that the base of the Converters object is the |
| 4683 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4684 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4685 | lua_newtable(L); |
| 4686 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4687 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4688 | |
| 4689 | htxn->s = txn->s; |
| 4690 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4691 | htxn->dir = txn->dir; |
| 4692 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4693 | |
| 4694 | /* Pop a class stream metatable and affect it to the table. */ |
| 4695 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4696 | lua_setmetatable(L, -2); |
| 4697 | |
| 4698 | return 1; |
| 4699 | } |
| 4700 | |
| 4701 | /* This function creates ans returns an array of HTTP headers. |
| 4702 | * This function does not fails. It is used as wrapper with the |
| 4703 | * 2 following functions. |
| 4704 | */ |
| 4705 | __LJMP static int hlua_http_get_headers(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4706 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4707 | struct htx *htx; |
| 4708 | int32_t pos; |
| 4709 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4710 | /* Create the table. */ |
| 4711 | lua_newtable(L); |
| 4712 | |
| 4713 | if (!htxn->s->txn) |
| 4714 | return 1; |
| 4715 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4716 | htx = htxbuf(&msg->chn->buf); |
| 4717 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4718 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4719 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4720 | struct ist n, v; |
| 4721 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4722 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4723 | if (type == HTX_BLK_HDR) { |
| 4724 | n = htx_get_blk_name(htx,blk); |
| 4725 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4726 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4727 | else if (type == HTX_BLK_EOH) |
| 4728 | break; |
| 4729 | else |
| 4730 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4731 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4732 | /* Check for existing entry: |
| 4733 | * assume that the table is on the top of the stack, and |
| 4734 | * push the key in the stack, the function lua_gettable() |
| 4735 | * perform the lookup. |
| 4736 | */ |
| 4737 | lua_pushlstring(L, n.ptr, n.len); |
| 4738 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4739 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4740 | switch (lua_type(L, -1)) { |
| 4741 | case LUA_TNIL: |
| 4742 | /* Table not found, create it. */ |
| 4743 | lua_pop(L, 1); /* remove the nil value. */ |
| 4744 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4745 | lua_newtable(L); /* create and push empty table. */ |
| 4746 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4747 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4748 | 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] | 4749 | break; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4750 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4751 | case LUA_TTABLE: |
| 4752 | /* Entry found: push the value in the table. */ |
| 4753 | len = lua_rawlen(L, -1); |
| 4754 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4755 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4756 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4757 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4758 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4759 | default: |
| 4760 | /* Other cases are errors. */ |
| 4761 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4762 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4763 | } |
| 4764 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4765 | return 1; |
| 4766 | } |
| 4767 | |
| 4768 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4769 | { |
| 4770 | struct hlua_txn *htxn; |
| 4771 | |
| 4772 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4773 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4774 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4775 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4776 | WILL_LJMP(lua_error(L)); |
| 4777 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4778 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->req); |
| 4779 | } |
| 4780 | |
| 4781 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4782 | { |
| 4783 | struct hlua_txn *htxn; |
| 4784 | |
| 4785 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4786 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4787 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4788 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4789 | WILL_LJMP(lua_error(L)); |
| 4790 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4791 | return hlua_http_get_headers(L, htxn, &htxn->s->txn->rsp); |
| 4792 | } |
| 4793 | |
| 4794 | /* This function replace full header, or just a value in |
| 4795 | * the request or in the response. It is a wrapper fir the |
| 4796 | * 4 following functions. |
| 4797 | */ |
| 4798 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct hlua_txn *htxn, |
| 4799 | struct http_msg *msg, int action) |
| 4800 | { |
| 4801 | size_t name_len; |
| 4802 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4803 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4804 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4805 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4806 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4807 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4808 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4809 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4810 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4811 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4812 | 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] | 4813 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4814 | return 0; |
| 4815 | } |
| 4816 | |
| 4817 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4818 | { |
| 4819 | struct hlua_txn *htxn; |
| 4820 | |
| 4821 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4822 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4823 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4824 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4825 | WILL_LJMP(lua_error(L)); |
| 4826 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4827 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_HDR)); |
| 4828 | } |
| 4829 | |
| 4830 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4831 | { |
| 4832 | struct hlua_txn *htxn; |
| 4833 | |
| 4834 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4835 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4836 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4837 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4838 | WILL_LJMP(lua_error(L)); |
| 4839 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4840 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->rsp, ACT_HTTP_REPLACE_HDR)); |
| 4841 | } |
| 4842 | |
| 4843 | __LJMP static int hlua_http_req_rep_val(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 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4850 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4851 | WILL_LJMP(lua_error(L)); |
| 4852 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4853 | return MAY_LJMP(hlua_http_rep_hdr(L, htxn, &htxn->s->txn->req, ACT_HTTP_REPLACE_VAL)); |
| 4854 | } |
| 4855 | |
| 4856 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4857 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4858 | struct hlua_txn *htxn; |
| 4859 | |
| 4860 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4861 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4862 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4863 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4864 | WILL_LJMP(lua_error(L)); |
| 4865 | |
Thierry FOURNIER | 0ea5c7f | 2015-08-05 19:05:19 +0200 | [diff] [blame] | 4866 | 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] | 4867 | } |
| 4868 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4869 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4870 | * It is a wrapper for the 2 following functions. |
| 4871 | */ |
| 4872 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4873 | { |
| 4874 | size_t len; |
| 4875 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4876 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4877 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4878 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4879 | ctx.blk = NULL; |
| 4880 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4881 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4882 | return 0; |
| 4883 | } |
| 4884 | |
| 4885 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4886 | { |
| 4887 | struct hlua_txn *htxn; |
| 4888 | |
| 4889 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4890 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4891 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4892 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4893 | WILL_LJMP(lua_error(L)); |
| 4894 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4895 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4896 | } |
| 4897 | |
| 4898 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4899 | { |
| 4900 | struct hlua_txn *htxn; |
| 4901 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4902 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4903 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4904 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4905 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4906 | WILL_LJMP(lua_error(L)); |
| 4907 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4908 | return hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4909 | } |
| 4910 | |
| 4911 | /* This function adds an header. It is a wrapper used by |
| 4912 | * the 2 following functions. |
| 4913 | */ |
| 4914 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct hlua_txn *htxn, struct http_msg *msg) |
| 4915 | { |
| 4916 | size_t name_len; |
| 4917 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4918 | size_t value_len; |
| 4919 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4920 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4921 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4922 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4923 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4924 | return 0; |
| 4925 | } |
| 4926 | |
| 4927 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4928 | { |
| 4929 | struct hlua_txn *htxn; |
| 4930 | |
| 4931 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4932 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4933 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4934 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4935 | WILL_LJMP(lua_error(L)); |
| 4936 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4937 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4938 | } |
| 4939 | |
| 4940 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4941 | { |
| 4942 | struct hlua_txn *htxn; |
| 4943 | |
| 4944 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4945 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4946 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4947 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4948 | WILL_LJMP(lua_error(L)); |
| 4949 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4950 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4951 | } |
| 4952 | |
| 4953 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4954 | { |
| 4955 | struct hlua_txn *htxn; |
| 4956 | |
| 4957 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4958 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4959 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4960 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4961 | WILL_LJMP(lua_error(L)); |
| 4962 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4963 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->req); |
| 4964 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4965 | } |
| 4966 | |
| 4967 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4968 | { |
| 4969 | struct hlua_txn *htxn; |
| 4970 | |
| 4971 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4972 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4973 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4974 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4975 | WILL_LJMP(lua_error(L)); |
| 4976 | |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 4977 | hlua_http_del_hdr(L, htxn, &htxn->s->txn->rsp); |
| 4978 | return hlua_http_add_hdr(L, htxn, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4979 | } |
| 4980 | |
| 4981 | /* This function set the method. */ |
| 4982 | static int hlua_http_req_set_meth(lua_State *L) |
| 4983 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4984 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4985 | size_t name_len; |
| 4986 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4987 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 4988 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4989 | WILL_LJMP(lua_error(L)); |
| 4990 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4991 | 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] | 4992 | return 1; |
| 4993 | } |
| 4994 | |
| 4995 | /* This function set the method. */ |
| 4996 | static int hlua_http_req_set_path(lua_State *L) |
| 4997 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4998 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4999 | size_t name_len; |
| 5000 | 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] | 5001 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5002 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5003 | WILL_LJMP(lua_error(L)); |
| 5004 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5005 | 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] | 5006 | return 1; |
| 5007 | } |
| 5008 | |
| 5009 | /* This function set the query-string. */ |
| 5010 | static int hlua_http_req_set_query(lua_State *L) |
| 5011 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5012 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5013 | size_t name_len; |
| 5014 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5016 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5017 | WILL_LJMP(lua_error(L)); |
| 5018 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5019 | /* Check length. */ |
| 5020 | if (name_len > trash.size - 1) { |
| 5021 | lua_pushboolean(L, 0); |
| 5022 | return 1; |
| 5023 | } |
| 5024 | |
| 5025 | /* Add the mark question as prefix. */ |
| 5026 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5027 | trash.area[trash.data++] = '?'; |
| 5028 | memcpy(trash.area + trash.data, name, name_len); |
| 5029 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5030 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5031 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5032 | 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] | 5033 | return 1; |
| 5034 | } |
| 5035 | |
| 5036 | /* This function set the uri. */ |
| 5037 | static int hlua_http_req_set_uri(lua_State *L) |
| 5038 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5039 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5040 | size_t name_len; |
| 5041 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5042 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5043 | if (htxn->dir != SMP_OPT_DIR_REQ || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5044 | WILL_LJMP(lua_error(L)); |
| 5045 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5046 | 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] | 5047 | return 1; |
| 5048 | } |
| 5049 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5050 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5051 | static int hlua_http_res_set_status(lua_State *L) |
| 5052 | { |
| 5053 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5054 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5055 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5056 | |
Christopher Faulet | 301eff8 | 2019-07-26 16:31:34 +0200 | [diff] [blame] | 5057 | if (htxn->dir != SMP_OPT_DIR_RES || !(htxn->flags & HLUA_TXN_HTTP_RDY)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5058 | WILL_LJMP(lua_error(L)); |
| 5059 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5060 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5061 | return 0; |
| 5062 | } |
| 5063 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5064 | /* |
| 5065 | * |
| 5066 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5067 | * Class TXN |
| 5068 | * |
| 5069 | * |
| 5070 | */ |
| 5071 | |
| 5072 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5073 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5074 | */ |
| 5075 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5076 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5077 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5078 | } |
| 5079 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5080 | __LJMP static int hlua_set_var(lua_State *L) |
| 5081 | { |
| 5082 | struct hlua_txn *htxn; |
| 5083 | const char *name; |
| 5084 | size_t len; |
| 5085 | struct sample smp; |
| 5086 | |
| 5087 | MAY_LJMP(check_args(L, 3, "set_var")); |
| 5088 | |
| 5089 | /* It is useles to retrieve the stream, but this function |
| 5090 | * runs only in a stream context. |
| 5091 | */ |
| 5092 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5093 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5094 | |
| 5095 | /* Converts the third argument in a sample. */ |
| 5096 | hlua_lua2smp(L, 3, &smp); |
| 5097 | |
| 5098 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5099 | 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] | 5100 | vars_set_by_name(name, len, &smp); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5101 | return 0; |
| 5102 | } |
| 5103 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5104 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5105 | { |
| 5106 | struct hlua_txn *htxn; |
| 5107 | const char *name; |
| 5108 | size_t len; |
| 5109 | struct sample smp; |
| 5110 | |
| 5111 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5112 | |
| 5113 | /* It is useles to retrieve the stream, but this function |
| 5114 | * runs only in a stream context. |
| 5115 | */ |
| 5116 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5117 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5118 | |
| 5119 | /* Unset the variable. */ |
| 5120 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
| 5121 | vars_unset_by_name(name, len, &smp); |
| 5122 | return 0; |
| 5123 | } |
| 5124 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5125 | __LJMP static int hlua_get_var(lua_State *L) |
| 5126 | { |
| 5127 | struct hlua_txn *htxn; |
| 5128 | const char *name; |
| 5129 | size_t len; |
| 5130 | struct sample smp; |
| 5131 | |
| 5132 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5133 | |
| 5134 | /* It is useles to retrieve the stream, but this function |
| 5135 | * runs only in a stream context. |
| 5136 | */ |
| 5137 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5138 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5139 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5140 | 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] | 5141 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5142 | lua_pushnil(L); |
| 5143 | return 1; |
| 5144 | } |
| 5145 | |
| 5146 | return hlua_smp2lua(L, &smp); |
| 5147 | } |
| 5148 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5149 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5150 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5151 | struct hlua *hlua; |
| 5152 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5153 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5154 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5155 | /* It is useles to retrieve the stream, but this function |
| 5156 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5157 | */ |
| 5158 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5159 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5160 | |
| 5161 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5162 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5163 | |
| 5164 | /* Get and store new value. */ |
| 5165 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5166 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5167 | |
| 5168 | return 0; |
| 5169 | } |
| 5170 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5171 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5172 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5173 | struct hlua *hlua; |
| 5174 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5175 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5176 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5177 | /* It is useles to retrieve the stream, but this function |
| 5178 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5179 | */ |
| 5180 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5181 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5182 | |
| 5183 | /* Push configuration index in the stack. */ |
| 5184 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5185 | |
| 5186 | return 1; |
| 5187 | } |
| 5188 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5189 | /* Create stack entry containing a class TXN. This function |
| 5190 | * return 0 if the stack does not contains free slots, |
| 5191 | * otherwise it returns 1. |
| 5192 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5193 | 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] | 5194 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5195 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5196 | |
| 5197 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5198 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5199 | return 0; |
| 5200 | |
| 5201 | /* NOTE: The allocation never fails. The failure |
| 5202 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5203 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5204 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5205 | /* Create the object: obj[0] = userdata. */ |
| 5206 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5207 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5208 | lua_rawseti(L, -2, 0); |
| 5209 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5210 | htxn->s = s; |
| 5211 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5212 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5213 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5214 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5215 | /* Create the "f" field that contains a list of fetches. */ |
| 5216 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5217 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5218 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5219 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5220 | |
| 5221 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5222 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5223 | 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] | 5224 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5225 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5226 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5227 | /* Create the "c" field that contains a list of converters. */ |
| 5228 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5229 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5230 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5231 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5232 | |
| 5233 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5234 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5235 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5236 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5237 | lua_rawset(L, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5238 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5239 | /* Create the "req" field that contains the request channel object. */ |
| 5240 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5241 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5242 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5243 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5244 | |
| 5245 | /* Create the "res" field that contains the response channel object. */ |
| 5246 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5247 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5248 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5249 | lua_rawset(L, -3); |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5250 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5251 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5252 | lua_pushstring(L, "http"); |
| 5253 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5254 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5255 | return 0; |
| 5256 | } |
| 5257 | else |
| 5258 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5259 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5260 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5261 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5262 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5263 | lua_setmetatable(L, -2); |
| 5264 | |
| 5265 | return 1; |
| 5266 | } |
| 5267 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5268 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5269 | { |
| 5270 | const char *msg; |
| 5271 | struct hlua_txn *htxn; |
| 5272 | |
| 5273 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5274 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5275 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5276 | |
| 5277 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5282 | { |
| 5283 | int level; |
| 5284 | const char *msg; |
| 5285 | struct hlua_txn *htxn; |
| 5286 | |
| 5287 | MAY_LJMP(check_args(L, 3, "log")); |
| 5288 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5289 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5290 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5291 | |
| 5292 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5293 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5294 | |
| 5295 | hlua_sendlog(htxn->s->be, level, msg); |
| 5296 | return 0; |
| 5297 | } |
| 5298 | |
| 5299 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5300 | { |
| 5301 | const char *msg; |
| 5302 | struct hlua_txn *htxn; |
| 5303 | |
| 5304 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5305 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5306 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5307 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5308 | return 0; |
| 5309 | } |
| 5310 | |
| 5311 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5312 | { |
| 5313 | const char *msg; |
| 5314 | struct hlua_txn *htxn; |
| 5315 | |
| 5316 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5317 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5318 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5319 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5320 | return 0; |
| 5321 | } |
| 5322 | |
| 5323 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5324 | { |
| 5325 | const char *msg; |
| 5326 | struct hlua_txn *htxn; |
| 5327 | |
| 5328 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5329 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5330 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5331 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5332 | return 0; |
| 5333 | } |
| 5334 | |
| 5335 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5336 | { |
| 5337 | const char *msg; |
| 5338 | struct hlua_txn *htxn; |
| 5339 | |
| 5340 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5341 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5342 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5343 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5344 | return 0; |
| 5345 | } |
| 5346 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5347 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5348 | { |
| 5349 | struct hlua_txn *htxn; |
| 5350 | int ll; |
| 5351 | |
| 5352 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5353 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5354 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5355 | |
| 5356 | if (ll < 0 || ll > 7) |
| 5357 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5358 | |
| 5359 | htxn->s->logs.level = ll; |
| 5360 | return 0; |
| 5361 | } |
| 5362 | |
| 5363 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5364 | { |
| 5365 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5366 | int tos; |
| 5367 | |
| 5368 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5369 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5370 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5371 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5372 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5373 | return 0; |
| 5374 | } |
| 5375 | |
| 5376 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5377 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5378 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5379 | int mark; |
| 5380 | |
| 5381 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5382 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5383 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5384 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5385 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5386 | return 0; |
| 5387 | } |
| 5388 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5389 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5390 | { |
| 5391 | struct hlua_txn *htxn; |
| 5392 | |
| 5393 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5394 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5395 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5396 | return 0; |
| 5397 | } |
| 5398 | |
| 5399 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5400 | { |
| 5401 | struct hlua_txn *htxn; |
| 5402 | |
| 5403 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5404 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5405 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5406 | return 0; |
| 5407 | } |
| 5408 | |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5409 | /* This function is an Lua binding that send pending data |
| 5410 | * to the client, and close the stream interface. |
| 5411 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5412 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5413 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5414 | struct hlua_txn *htxn; |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5415 | struct hlua *hlua; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5416 | struct channel *ic, *oc; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5417 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5418 | MAY_LJMP(check_args(L, 1, "close")); |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5419 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5420 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5421 | |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5422 | /* If the flags NOTERM is set, we cannot terminate the http |
| 5423 | * session, so we just end the execution of the current |
| 5424 | * lua code. |
| 5425 | */ |
| 5426 | if (htxn->flags & HLUA_TXN_NOTERM) { |
| 5427 | WILL_LJMP(hlua_done(L)); |
| 5428 | return 0; |
| 5429 | } |
| 5430 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5431 | ic = &htxn->s->req; |
| 5432 | oc = &htxn->s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5433 | |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5434 | if (IS_HTX_STRM(htxn->s)) { |
| 5435 | htxn->s->txn->status = 0; |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5436 | http_reply_and_close(htxn->s, 0, NULL); |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5437 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5438 | else { |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5439 | channel_auto_read(ic); |
| 5440 | channel_abort(ic); |
| 5441 | channel_auto_close(ic); |
| 5442 | channel_erase(ic); |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5443 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5444 | oc->wex = tick_add_ifset(now_ms, oc->wto); |
| 5445 | channel_auto_read(oc); |
| 5446 | channel_auto_close(oc); |
| 5447 | channel_shutr_now(oc); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5448 | |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5449 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5450 | |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5451 | ic->analysers &= AN_REQ_FLT_END; |
| 5452 | oc->analysers &= AN_RES_FLT_END; |
| 5453 | |
| 5454 | if (!(htxn->s->flags & SF_ERR_MASK)) // this is not really an error but it is |
| 5455 | htxn->s->flags |= SF_ERR_LOCAL; // to mark that it comes from the proxy |
| 5456 | |
Thierry FOURNIER | 9bd52d4 | 2016-07-14 11:45:33 +0200 | [diff] [blame] | 5457 | hlua->flags |= HLUA_STOP; |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5458 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5459 | return 0; |
| 5460 | } |
| 5461 | |
| 5462 | __LJMP static int hlua_log(lua_State *L) |
| 5463 | { |
| 5464 | int level; |
| 5465 | const char *msg; |
| 5466 | |
| 5467 | MAY_LJMP(check_args(L, 2, "log")); |
| 5468 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5469 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5470 | |
| 5471 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5472 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5473 | |
| 5474 | hlua_sendlog(NULL, level, msg); |
| 5475 | return 0; |
| 5476 | } |
| 5477 | |
| 5478 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5479 | { |
| 5480 | const char *msg; |
| 5481 | |
| 5482 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5483 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5484 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5485 | return 0; |
| 5486 | } |
| 5487 | |
| 5488 | __LJMP static int hlua_log_info(lua_State *L) |
| 5489 | { |
| 5490 | const char *msg; |
| 5491 | |
| 5492 | MAY_LJMP(check_args(L, 1, "info")); |
| 5493 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5494 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5495 | return 0; |
| 5496 | } |
| 5497 | |
| 5498 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5499 | { |
| 5500 | const char *msg; |
| 5501 | |
| 5502 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5503 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5504 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5505 | return 0; |
| 5506 | } |
| 5507 | |
| 5508 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5509 | { |
| 5510 | const char *msg; |
| 5511 | |
| 5512 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5513 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5514 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5515 | return 0; |
| 5516 | } |
| 5517 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5518 | __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] | 5519 | { |
| 5520 | int wakeup_ms = lua_tointeger(L, -1); |
| 5521 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5522 | 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] | 5523 | return 0; |
| 5524 | } |
| 5525 | |
| 5526 | __LJMP static int hlua_sleep(lua_State *L) |
| 5527 | { |
| 5528 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5529 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5530 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5531 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5532 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5533 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5534 | wakeup_ms = tick_add(now_ms, delay); |
| 5535 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5536 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5537 | 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] | 5538 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5539 | } |
| 5540 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5541 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5542 | { |
| 5543 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5544 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5545 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5546 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5547 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5548 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5549 | wakeup_ms = tick_add(now_ms, delay); |
| 5550 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5551 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5552 | 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] | 5553 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5554 | } |
| 5555 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5556 | /* This functionis an LUA binding. it permits to give back |
| 5557 | * the hand at the HAProxy scheduler. It is used when the |
| 5558 | * LUA processing consumes a lot of time. |
| 5559 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5560 | __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] | 5561 | { |
| 5562 | return 0; |
| 5563 | } |
| 5564 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5565 | __LJMP static int hlua_yield(lua_State *L) |
| 5566 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5567 | 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] | 5568 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5569 | } |
| 5570 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5571 | /* This function change the nice of the currently executed |
| 5572 | * task. It is used set low or high priority at the current |
| 5573 | * task. |
| 5574 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5575 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5576 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5577 | struct hlua *hlua; |
| 5578 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5579 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5580 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5581 | hlua = hlua_gethlua(L); |
| 5582 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5583 | |
| 5584 | /* If he task is not set, I'm in a start mode. */ |
| 5585 | if (!hlua || !hlua->task) |
| 5586 | return 0; |
| 5587 | |
| 5588 | if (nice < -1024) |
| 5589 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5590 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5591 | nice = 1024; |
| 5592 | |
| 5593 | hlua->task->nice = nice; |
| 5594 | return 0; |
| 5595 | } |
| 5596 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5597 | /* 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] | 5598 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 5599 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 5600 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5601 | * |
| 5602 | * Task wrapper are longjmp safe because the only one Lua code |
| 5603 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5604 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 5605 | 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] | 5606 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 5607 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5608 | enum hlua_exec status; |
| 5609 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 5610 | if (task->thread_mask == MAX_THREADS_MASK) |
| 5611 | task_set_affinity(task, tid_bit); |
| 5612 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5613 | /* If it is the first call to the task, we must initialize the |
| 5614 | * execution timeouts. |
| 5615 | */ |
| 5616 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 5617 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5618 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5619 | /* Execute the Lua code. */ |
| 5620 | status = hlua_ctx_resume(hlua, 1); |
| 5621 | |
| 5622 | switch (status) { |
| 5623 | /* finished or yield */ |
| 5624 | case HLUA_E_OK: |
| 5625 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5626 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 5627 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5628 | break; |
| 5629 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 5630 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 5631 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 5632 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5633 | break; |
| 5634 | |
| 5635 | /* finished with error. */ |
| 5636 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5637 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5638 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5639 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5640 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5641 | break; |
| 5642 | |
| 5643 | case HLUA_E_ERR: |
| 5644 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5645 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5646 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 5647 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5648 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5649 | break; |
| 5650 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 5651 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5652 | } |
| 5653 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5654 | /* This function is an LUA binding that register LUA function to be |
| 5655 | * executed after the HAProxy configuration parsing and before the |
| 5656 | * HAProxy scheduler starts. This function expect only one LUA |
| 5657 | * argument that is a function. This function returns nothing, but |
| 5658 | * throws if an error is encountered. |
| 5659 | */ |
| 5660 | __LJMP static int hlua_register_init(lua_State *L) |
| 5661 | { |
| 5662 | struct hlua_init_function *init; |
| 5663 | int ref; |
| 5664 | |
| 5665 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 5666 | |
| 5667 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5668 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 5669 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5670 | if (!init) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5671 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 5672 | |
| 5673 | init->function_ref = ref; |
| 5674 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 5675 | return 0; |
| 5676 | } |
| 5677 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5678 | /* This functio is an LUA binding. It permits to register a task |
| 5679 | * executed in parallel of the main HAroxy activity. The task is |
| 5680 | * created and it is set in the HAProxy scheduler. It can be called |
| 5681 | * from the "init" section, "post init" or during the runtime. |
| 5682 | * |
| 5683 | * Lua prototype: |
| 5684 | * |
| 5685 | * <none> core.register_task(<function>) |
| 5686 | */ |
| 5687 | static int hlua_register_task(lua_State *L) |
| 5688 | { |
| 5689 | struct hlua *hlua; |
| 5690 | struct task *task; |
| 5691 | int ref; |
| 5692 | |
| 5693 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 5694 | |
| 5695 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 5696 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5697 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5698 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5699 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5700 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 5701 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 5702 | if (!task) |
| 5703 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 5704 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5705 | task->context = hlua; |
| 5706 | task->process = hlua_process_task; |
| 5707 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5708 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 5709 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 5710 | |
| 5711 | /* Restore the function in the stack. */ |
| 5712 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 5713 | hlua->nargs = 0; |
| 5714 | |
| 5715 | /* Schedule task. */ |
| 5716 | task_schedule(task, now_ms); |
| 5717 | |
| 5718 | return 0; |
| 5719 | } |
| 5720 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5721 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 5722 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 5723 | * resume converters. |
| 5724 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5725 | 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] | 5726 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5727 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5728 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5729 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5730 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5731 | if (!stream) |
| 5732 | return 0; |
| 5733 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5734 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5735 | * Lua context can be not initialized. This behavior |
| 5736 | * permits to save performances because a systematic |
| 5737 | * Lua initialization cause 5% performances loss. |
| 5738 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5739 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5740 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5741 | if (!stream->hlua) { |
| 5742 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5743 | return 0; |
| 5744 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5745 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5746 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 5747 | return 0; |
| 5748 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5749 | } |
| 5750 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5751 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5752 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5753 | |
| 5754 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5755 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5756 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5757 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5758 | else |
| 5759 | error = "critical error"; |
| 5760 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5761 | return 0; |
| 5762 | } |
| 5763 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5764 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5765 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5766 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5767 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5768 | return 0; |
| 5769 | } |
| 5770 | |
| 5771 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5772 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5773 | |
| 5774 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5775 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5776 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5777 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5778 | return 0; |
| 5779 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5780 | hlua_smp2lua(stream->hlua->T, smp); |
| 5781 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5782 | |
| 5783 | /* push keywords in the stack. */ |
| 5784 | if (arg_p) { |
| 5785 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5786 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5787 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5788 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5789 | return 0; |
| 5790 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5791 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5792 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5793 | } |
| 5794 | } |
| 5795 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5796 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5797 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5798 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5799 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5800 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5801 | } |
| 5802 | |
| 5803 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5804 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5805 | /* finished. */ |
| 5806 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5807 | /* If the stack is empty, the function fails. */ |
| 5808 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5809 | return 0; |
| 5810 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5811 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5812 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5813 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5814 | return 1; |
| 5815 | |
| 5816 | /* yield. */ |
| 5817 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5818 | 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] | 5819 | return 0; |
| 5820 | |
| 5821 | /* finished with error. */ |
| 5822 | case HLUA_E_ERRMSG: |
| 5823 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5824 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5825 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5826 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5827 | return 0; |
| 5828 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5829 | case HLUA_E_ETMOUT: |
| 5830 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 5831 | return 0; |
| 5832 | |
| 5833 | case HLUA_E_NOMEM: |
| 5834 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 5835 | return 0; |
| 5836 | |
| 5837 | case HLUA_E_YIELD: |
| 5838 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 5839 | return 0; |
| 5840 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 5841 | case HLUA_E_ERR: |
| 5842 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5843 | 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] | 5844 | |
| 5845 | default: |
| 5846 | return 0; |
| 5847 | } |
| 5848 | } |
| 5849 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5850 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 5851 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5852 | * resume sample-fetches. This function will be called by the sample |
| 5853 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5854 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 5855 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 5856 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5857 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5858 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 5859 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5860 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 5861 | const struct buffer msg = { }; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5862 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5863 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 5864 | if (!stream) |
| 5865 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 5866 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5867 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5868 | * Lua context can be not initialized. This behavior |
| 5869 | * permits to save performances because a systematic |
| 5870 | * Lua initialization cause 5% performances loss. |
| 5871 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5872 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 5873 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5874 | if (!stream->hlua) { |
| 5875 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5876 | return 0; |
| 5877 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 5878 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5879 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 5880 | return 0; |
| 5881 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 5882 | } |
| 5883 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5884 | consistency_set(stream, smp->opt, &stream->hlua->cons); |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5885 | |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5886 | if (stream->be->mode == PR_MODE_HTTP) { |
| 5887 | if ((smp->opt & SMP_OPT_DIR) == SMP_OPT_DIR_REQ) |
| 5888 | hflags |= ((stream->txn->req.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 5889 | else |
| 5890 | hflags |= ((stream->txn->rsp.msg_state < HTTP_MSG_BODY) ? 0 : HLUA_TXN_HTTP_RDY); |
| 5891 | } |
| 5892 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5893 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5894 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5895 | |
| 5896 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5897 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 5898 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 5899 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 5900 | else |
| 5901 | error = "critical error"; |
| 5902 | 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] | 5903 | return 0; |
| 5904 | } |
| 5905 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5906 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5907 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5908 | 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] | 5909 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5910 | return 0; |
| 5911 | } |
| 5912 | |
| 5913 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5914 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5915 | |
| 5916 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 5917 | if (!hlua_txn_new(stream->hlua->T, stream, smp->px, smp->opt & SMP_OPT_DIR, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5918 | 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] | 5919 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5920 | return 0; |
| 5921 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5922 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5923 | |
| 5924 | /* push keywords in the stack. */ |
| 5925 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 5926 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5927 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5928 | 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] | 5929 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5930 | return 0; |
| 5931 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5932 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 5933 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5934 | } |
| 5935 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5936 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5937 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 5938 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 5939 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5940 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5941 | } |
| 5942 | |
| 5943 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5944 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5945 | /* finished. */ |
| 5946 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5947 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5948 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 5949 | return 0; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 5950 | } |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 5951 | /* If the stack is empty, the function fails. */ |
| 5952 | if (lua_gettop(stream->hlua->T) <= 0) |
| 5953 | return 0; |
| 5954 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5955 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5956 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 5957 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5958 | |
| 5959 | /* Set the end of execution flag. */ |
| 5960 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 5961 | return 1; |
| 5962 | |
| 5963 | /* yield. */ |
| 5964 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5965 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5966 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5967 | 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] | 5968 | return 0; |
| 5969 | |
| 5970 | /* finished with error. */ |
| 5971 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5972 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5973 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5974 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 5975 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5976 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 5977 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5978 | return 0; |
| 5979 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5980 | case HLUA_E_ETMOUT: |
| 5981 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5982 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5983 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 5984 | return 0; |
| 5985 | |
| 5986 | case HLUA_E_NOMEM: |
| 5987 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5988 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5989 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 5990 | return 0; |
| 5991 | |
| 5992 | case HLUA_E_YIELD: |
| 5993 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 5994 | si_retnclose(&stream->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 5995 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 5996 | return 0; |
| 5997 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 5998 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 5999 | if (!consistency_check(stream, smp->opt, &stream->hlua->cons)) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6000 | si_retnclose(&stream->si[0], &msg); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6001 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6002 | 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] | 6003 | |
| 6004 | default: |
| 6005 | return 0; |
| 6006 | } |
| 6007 | } |
| 6008 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6009 | /* This function is an LUA binding used for registering |
| 6010 | * "sample-conv" functions. It expects a converter name used |
| 6011 | * in the haproxy configuration file, and an LUA function. |
| 6012 | */ |
| 6013 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6014 | { |
| 6015 | struct sample_conv_kw_list *sck; |
| 6016 | const char *name; |
| 6017 | int ref; |
| 6018 | int len; |
| 6019 | struct hlua_function *fcn; |
| 6020 | |
| 6021 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6022 | |
| 6023 | /* First argument : converter name. */ |
| 6024 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6025 | |
| 6026 | /* Second argument : lua function. */ |
| 6027 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6028 | |
| 6029 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6030 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6031 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6032 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6033 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6034 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6035 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6036 | |
| 6037 | /* Fill fcn. */ |
| 6038 | fcn->name = strdup(name); |
| 6039 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6040 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6041 | fcn->function_ref = ref; |
| 6042 | |
| 6043 | /* List head */ |
| 6044 | sck->list.n = sck->list.p = NULL; |
| 6045 | |
| 6046 | /* converter keyword. */ |
| 6047 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6048 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6049 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6050 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6051 | |
| 6052 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6053 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6054 | 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] | 6055 | sck->kw[0].val_args = NULL; |
| 6056 | sck->kw[0].in_type = SMP_T_STR; |
| 6057 | sck->kw[0].out_type = SMP_T_STR; |
| 6058 | sck->kw[0].private = fcn; |
| 6059 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6060 | /* Register this new converter */ |
| 6061 | sample_register_convs(sck); |
| 6062 | |
| 6063 | return 0; |
| 6064 | } |
| 6065 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6066 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6067 | * "sample-fetch" functions. It expects a converter name used |
| 6068 | * in the haproxy configuration file, and an LUA function. |
| 6069 | */ |
| 6070 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6071 | { |
| 6072 | const char *name; |
| 6073 | int ref; |
| 6074 | int len; |
| 6075 | struct sample_fetch_kw_list *sfk; |
| 6076 | struct hlua_function *fcn; |
| 6077 | |
| 6078 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6079 | |
| 6080 | /* First argument : sample-fetch name. */ |
| 6081 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6082 | |
| 6083 | /* Second argument : lua function. */ |
| 6084 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6085 | |
| 6086 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6087 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6088 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6089 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6090 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6091 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6092 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6093 | |
| 6094 | /* Fill fcn. */ |
| 6095 | fcn->name = strdup(name); |
| 6096 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6097 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6098 | fcn->function_ref = ref; |
| 6099 | |
| 6100 | /* List head */ |
| 6101 | sfk->list.n = sfk->list.p = NULL; |
| 6102 | |
| 6103 | /* sample-fetch keyword. */ |
| 6104 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6105 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6106 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6107 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6108 | |
| 6109 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6110 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6111 | 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] | 6112 | sfk->kw[0].val_args = NULL; |
| 6113 | sfk->kw[0].out_type = SMP_T_STR; |
| 6114 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6115 | sfk->kw[0].val = 0; |
| 6116 | sfk->kw[0].private = fcn; |
| 6117 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6118 | /* Register this new fetch. */ |
| 6119 | sample_register_fetches(sfk); |
| 6120 | |
| 6121 | return 0; |
| 6122 | } |
| 6123 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6124 | /* This function is a wrapper to execute each LUA function declared |
| 6125 | * as an action wrapper during the initialisation period. This function |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6126 | * return ACT_RET_CONT if the processing is finished (with or without |
| 6127 | * error) and return ACT_RET_YIELD if the function must be called again |
| 6128 | * because the LUA returns a yield. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6129 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6130 | 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] | 6131 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6132 | { |
| 6133 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6134 | unsigned int hflags = 0; |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6135 | int dir; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6136 | const char *error; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 6137 | const struct buffer msg = { }; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6138 | |
| 6139 | switch (rule->from) { |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6140 | case ACT_F_TCP_REQ_CNT: ; dir = SMP_OPT_DIR_REQ; break; |
| 6141 | case ACT_F_TCP_RES_CNT: ; dir = SMP_OPT_DIR_RES; break; |
| 6142 | case ACT_F_HTTP_REQ: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_REQ; break; |
| 6143 | case ACT_F_HTTP_RES: hflags = HLUA_TXN_HTTP_RDY ; dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6144 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6145 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6146 | return ACT_RET_CONT; |
| 6147 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6148 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6149 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6150 | * Lua context can be not initialized. This behavior |
| 6151 | * permits to save performances because a systematic |
| 6152 | * Lua initialization cause 5% performances loss. |
| 6153 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6154 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6155 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6156 | if (!s->hlua) { |
| 6157 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6158 | rule->arg.hlua_rule->fcn.name); |
| 6159 | return ACT_RET_CONT; |
| 6160 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6161 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6162 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6163 | rule->arg.hlua_rule->fcn.name); |
| 6164 | return ACT_RET_CONT; |
| 6165 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6166 | } |
| 6167 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6168 | consistency_set(s, dir, &s->hlua->cons); |
| 6169 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6170 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6171 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6172 | |
| 6173 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6174 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6175 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6176 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6177 | else |
| 6178 | error = "critical error"; |
| 6179 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6180 | rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6181 | return ACT_RET_CONT; |
| 6182 | } |
| 6183 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6184 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6186 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6187 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6188 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6189 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6190 | } |
| 6191 | |
| 6192 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | 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] | 6194 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6195 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6196 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6197 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6198 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6199 | RESET_SAFE_LJMP(s->hlua->T); |
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 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6202 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6203 | |
| 6204 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6205 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6206 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6207 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6208 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6210 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6211 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6212 | lua_pushstring(s->hlua->T, *arg); |
| 6213 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6214 | } |
| 6215 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6216 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6217 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6218 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6219 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6220 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6221 | } |
| 6222 | |
| 6223 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6224 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_FLAG_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6225 | /* finished. */ |
| 6226 | case HLUA_E_OK: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6227 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6228 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6229 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6230 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6231 | if (s->hlua->flags & HLUA_STOP) |
Christopher Faulet | 8f1aa77 | 2019-07-04 11:27:15 +0200 | [diff] [blame] | 6232 | return ACT_RET_DONE; |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6233 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6234 | |
| 6235 | /* yield. */ |
| 6236 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6237 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6238 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6239 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6240 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6241 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6242 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6243 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6244 | /* Some actions can be wake up when a "write" event |
| 6245 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6246 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6247 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6248 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6249 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6250 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6251 | s->req.flags |= CF_WAKE_WRITE; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6252 | /* We can quit the function without consistency check |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6253 | * because HAProxy is not able to manipulate data, it |
| 6254 | * is only allowed to call me again. */ |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6255 | return ACT_RET_YIELD; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6256 | |
| 6257 | /* finished with error. */ |
| 6258 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6259 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6260 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6261 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6262 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6263 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6264 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6265 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6266 | lua_pop(s->hlua->T, 1); |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6267 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6268 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6269 | case HLUA_E_ETMOUT: |
| 6270 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6271 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6272 | return ACT_RET_ERR; |
| 6273 | } |
| 6274 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
| 6275 | return 0; |
| 6276 | |
| 6277 | case HLUA_E_NOMEM: |
| 6278 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6279 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6280 | return ACT_RET_ERR; |
| 6281 | } |
| 6282 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
| 6283 | return 0; |
| 6284 | |
| 6285 | case HLUA_E_YIELD: |
| 6286 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6287 | si_retnclose(&s->si[0], &msg); |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6288 | return ACT_RET_ERR; |
| 6289 | } |
| 6290 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6291 | rule->arg.hlua_rule->fcn.name); |
| 6292 | return 0; |
| 6293 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6294 | case HLUA_E_ERR: |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6295 | if (!consistency_check(s, dir, &s->hlua->cons)) { |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 6296 | si_retnclose(&s->si[0], &msg); |
Thierry FOURNIER | d75cb0f | 2015-09-25 19:22:44 +0200 | [diff] [blame] | 6297 | return ACT_RET_ERR; |
Thierry FOURNIER | 11cfb3d | 2016-12-13 13:06:23 +0100 | [diff] [blame] | 6298 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6299 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6300 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6301 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6302 | |
| 6303 | default: |
Thierry FOURNIER | 24ff6c6 | 2015-08-06 08:52:53 +0200 | [diff] [blame] | 6304 | return ACT_RET_CONT; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6305 | } |
| 6306 | } |
| 6307 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6308 | 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] | 6309 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6310 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6311 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6312 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6313 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6314 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6315 | } |
| 6316 | |
| 6317 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6318 | { |
| 6319 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6320 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6321 | struct task *task; |
| 6322 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6323 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6324 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6325 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6326 | if (!hlua) { |
| 6327 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6328 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6329 | return 0; |
| 6330 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6331 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6332 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6333 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6334 | |
| 6335 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6336 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6337 | if (!task) { |
| 6338 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6339 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6340 | return 0; |
| 6341 | } |
| 6342 | task->nice = 0; |
| 6343 | task->context = ctx; |
| 6344 | task->process = hlua_applet_wakeup; |
| 6345 | ctx->ctx.hlua_apptcp.task = task; |
| 6346 | |
| 6347 | /* In the execution wrappers linked with a stream, the |
| 6348 | * Lua context can be not initialized. This behavior |
| 6349 | * permits to save performances because a systematic |
| 6350 | * Lua initialization cause 5% performances loss. |
| 6351 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6352 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6353 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6354 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6355 | return 0; |
| 6356 | } |
| 6357 | |
| 6358 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6359 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6360 | |
| 6361 | /* The following Lua calls can fail. */ |
| 6362 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6363 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6364 | error = lua_tostring(hlua->T, -1); |
| 6365 | else |
| 6366 | error = "critical error"; |
| 6367 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6368 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6369 | return 0; |
| 6370 | } |
| 6371 | |
| 6372 | /* Check stack available size. */ |
| 6373 | if (!lua_checkstack(hlua->T, 1)) { |
| 6374 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6375 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6376 | RESET_SAFE_LJMP(hlua->T); |
| 6377 | return 0; |
| 6378 | } |
| 6379 | |
| 6380 | /* Restore the function in the stack. */ |
| 6381 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6382 | |
| 6383 | /* Create and and push object stream in the stack. */ |
| 6384 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6385 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6386 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6387 | RESET_SAFE_LJMP(hlua->T); |
| 6388 | return 0; |
| 6389 | } |
| 6390 | hlua->nargs = 1; |
| 6391 | |
| 6392 | /* push keywords in the stack. */ |
| 6393 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6394 | if (!lua_checkstack(hlua->T, 1)) { |
| 6395 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6396 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6397 | RESET_SAFE_LJMP(hlua->T); |
| 6398 | return 0; |
| 6399 | } |
| 6400 | lua_pushstring(hlua->T, *arg); |
| 6401 | hlua->nargs++; |
| 6402 | } |
| 6403 | |
| 6404 | RESET_SAFE_LJMP(hlua->T); |
| 6405 | |
| 6406 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6407 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6408 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6409 | |
| 6410 | return 1; |
| 6411 | } |
| 6412 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6413 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6414 | { |
| 6415 | struct stream_interface *si = ctx->owner; |
| 6416 | struct stream *strm = si_strm(si); |
| 6417 | struct channel *res = si_ic(si); |
| 6418 | struct act_rule *rule = ctx->rule; |
| 6419 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6420 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6421 | |
| 6422 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6423 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6424 | /* eat the whole request */ |
| 6425 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6426 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6427 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6428 | |
| 6429 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6430 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6431 | return; |
| 6432 | |
| 6433 | /* Execute the function. */ |
| 6434 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6435 | /* finished. */ |
| 6436 | case HLUA_E_OK: |
| 6437 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6438 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6439 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6440 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6441 | res->flags |= CF_READ_NULL; |
| 6442 | si_shutr(si); |
| 6443 | return; |
| 6444 | |
| 6445 | /* yield. */ |
| 6446 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6447 | if (hlua->wake_time != TICK_ETERNITY) |
| 6448 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6449 | return; |
| 6450 | |
| 6451 | /* finished with error. */ |
| 6452 | case HLUA_E_ERRMSG: |
| 6453 | /* Display log. */ |
| 6454 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6455 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6456 | lua_pop(hlua->T, 1); |
| 6457 | goto error; |
| 6458 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6459 | case HLUA_E_ETMOUT: |
| 6460 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6461 | rule->arg.hlua_rule->fcn.name); |
| 6462 | goto error; |
| 6463 | |
| 6464 | case HLUA_E_NOMEM: |
| 6465 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6466 | rule->arg.hlua_rule->fcn.name); |
| 6467 | goto error; |
| 6468 | |
| 6469 | case HLUA_E_YIELD: /* unexpected */ |
| 6470 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6471 | rule->arg.hlua_rule->fcn.name); |
| 6472 | goto error; |
| 6473 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6474 | case HLUA_E_ERR: |
| 6475 | /* Display log. */ |
| 6476 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6477 | rule->arg.hlua_rule->fcn.name); |
| 6478 | goto error; |
| 6479 | |
| 6480 | default: |
| 6481 | goto error; |
| 6482 | } |
| 6483 | |
| 6484 | error: |
| 6485 | |
| 6486 | /* For all other cases, just close the stream. */ |
| 6487 | si_shutw(si); |
| 6488 | si_shutr(si); |
| 6489 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6490 | } |
| 6491 | |
| 6492 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6493 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6494 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6495 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6496 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6497 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6498 | } |
| 6499 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6500 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6501 | * an errors occurs and -1 if more data are required for initializing |
| 6502 | * the applet. |
| 6503 | */ |
| 6504 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6505 | { |
| 6506 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6507 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6508 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6509 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6510 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6511 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6512 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6513 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6514 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6515 | if (!hlua) { |
| 6516 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6517 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6518 | return 0; |
| 6519 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6520 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6521 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6522 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6523 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6524 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6525 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6526 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6527 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6528 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6529 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6530 | if (!task) { |
| 6531 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6532 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6533 | return 0; |
| 6534 | } |
| 6535 | task->nice = 0; |
| 6536 | task->context = ctx; |
| 6537 | task->process = hlua_applet_wakeup; |
| 6538 | ctx->ctx.hlua_apphttp.task = task; |
| 6539 | |
| 6540 | /* In the execution wrappers linked with a stream, the |
| 6541 | * Lua context can be not initialized. This behavior |
| 6542 | * permits to save performances because a systematic |
| 6543 | * Lua initialization cause 5% performances loss. |
| 6544 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6545 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6546 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6547 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6548 | return 0; |
| 6549 | } |
| 6550 | |
| 6551 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6552 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6553 | |
| 6554 | /* The following Lua calls can fail. */ |
| 6555 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6556 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6557 | error = lua_tostring(hlua->T, -1); |
| 6558 | else |
| 6559 | error = "critical error"; |
| 6560 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6561 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6562 | return 0; |
| 6563 | } |
| 6564 | |
| 6565 | /* Check stack available size. */ |
| 6566 | if (!lua_checkstack(hlua->T, 1)) { |
| 6567 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6568 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6569 | RESET_SAFE_LJMP(hlua->T); |
| 6570 | return 0; |
| 6571 | } |
| 6572 | |
| 6573 | /* Restore the function in the stack. */ |
| 6574 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6575 | |
| 6576 | /* Create and and push object stream in the stack. */ |
| 6577 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6578 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6579 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6580 | RESET_SAFE_LJMP(hlua->T); |
| 6581 | return 0; |
| 6582 | } |
| 6583 | hlua->nargs = 1; |
| 6584 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6585 | /* push keywords in the stack. */ |
| 6586 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6587 | if (!lua_checkstack(hlua->T, 1)) { |
| 6588 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6589 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6590 | RESET_SAFE_LJMP(hlua->T); |
| 6591 | return 0; |
| 6592 | } |
| 6593 | lua_pushstring(hlua->T, *arg); |
| 6594 | hlua->nargs++; |
| 6595 | } |
| 6596 | |
| 6597 | RESET_SAFE_LJMP(hlua->T); |
| 6598 | |
| 6599 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6600 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6601 | |
| 6602 | return 1; |
| 6603 | } |
| 6604 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6605 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6606 | { |
| 6607 | struct stream_interface *si = ctx->owner; |
| 6608 | struct stream *strm = si_strm(si); |
| 6609 | struct channel *req = si_oc(si); |
| 6610 | struct channel *res = si_ic(si); |
| 6611 | struct act_rule *rule = ctx->rule; |
| 6612 | struct proxy *px = strm->be; |
| 6613 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 6614 | struct htx *req_htx, *res_htx; |
| 6615 | |
| 6616 | res_htx = htx_from_buf(&res->buf); |
| 6617 | |
| 6618 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6619 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6620 | goto out; |
| 6621 | |
| 6622 | /* Check if the input buffer is avalaible. */ |
| 6623 | if (!b_size(&res->buf)) { |
| 6624 | si_rx_room_blk(si); |
| 6625 | goto out; |
| 6626 | } |
| 6627 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6628 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6629 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6630 | |
| 6631 | /* Set the currently running flag. */ |
| 6632 | if (!HLUA_IS_RUNNING(hlua) && |
| 6633 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6634 | struct htx_blk *blk; |
| 6635 | size_t count = co_data(req); |
| 6636 | |
| 6637 | if (!count) { |
| 6638 | si_cant_get(si); |
| 6639 | goto out; |
| 6640 | } |
| 6641 | |
| 6642 | /* We need to flush the request header. This left the body for |
| 6643 | * the Lua. |
| 6644 | */ |
| 6645 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 6646 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6647 | while (count && blk) { |
| 6648 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 6649 | uint32_t sz = htx_get_blksz(blk); |
| 6650 | |
| 6651 | if (sz > count) { |
| 6652 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6653 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6654 | goto out; |
| 6655 | } |
| 6656 | |
| 6657 | count -= sz; |
| 6658 | co_set_data(req, co_data(req) - sz); |
| 6659 | blk = htx_remove_blk(req_htx, blk); |
| 6660 | |
| 6661 | if (type == HTX_BLK_EOH) |
| 6662 | break; |
| 6663 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 6664 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6665 | } |
| 6666 | |
| 6667 | /* Executes The applet if it is not done. */ |
| 6668 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 6669 | |
| 6670 | /* Execute the function. */ |
| 6671 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6672 | /* finished. */ |
| 6673 | case HLUA_E_OK: |
| 6674 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6675 | break; |
| 6676 | |
| 6677 | /* yield. */ |
| 6678 | case HLUA_E_AGAIN: |
| 6679 | if (hlua->wake_time != TICK_ETERNITY) |
| 6680 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6681 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6682 | |
| 6683 | /* finished with error. */ |
| 6684 | case HLUA_E_ERRMSG: |
| 6685 | /* Display log. */ |
| 6686 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6687 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6688 | lua_pop(hlua->T, 1); |
| 6689 | goto error; |
| 6690 | |
| 6691 | case HLUA_E_ETMOUT: |
| 6692 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 6693 | rule->arg.hlua_rule->fcn.name); |
| 6694 | goto error; |
| 6695 | |
| 6696 | case HLUA_E_NOMEM: |
| 6697 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 6698 | rule->arg.hlua_rule->fcn.name); |
| 6699 | goto error; |
| 6700 | |
| 6701 | case HLUA_E_YIELD: /* unexpected */ |
| 6702 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 6703 | rule->arg.hlua_rule->fcn.name); |
| 6704 | goto error; |
| 6705 | |
| 6706 | case HLUA_E_ERR: |
| 6707 | /* Display log. */ |
| 6708 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 6709 | rule->arg.hlua_rule->fcn.name); |
| 6710 | goto error; |
| 6711 | |
| 6712 | default: |
| 6713 | goto error; |
| 6714 | } |
| 6715 | } |
| 6716 | |
| 6717 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6718 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 6719 | goto done; |
| 6720 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6721 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 6722 | goto error; |
| 6723 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 6724 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6725 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 6726 | si_rx_room_blk(si); |
| 6727 | goto out; |
| 6728 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6729 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6730 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 6731 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6732 | } |
| 6733 | |
| 6734 | done: |
| 6735 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6736 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6737 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 6738 | si_shutr(si); |
| 6739 | } |
| 6740 | |
| 6741 | /* eat the whole request */ |
| 6742 | if (co_data(req)) { |
| 6743 | req_htx = htx_from_buf(&req->buf); |
| 6744 | co_htx_skip(req, req_htx, co_data(req)); |
| 6745 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6746 | } |
| 6747 | } |
| 6748 | |
| 6749 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6750 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6751 | return; |
| 6752 | |
| 6753 | error: |
| 6754 | |
| 6755 | /* If we are in HTTP mode, and we are not send any |
| 6756 | * data, return a 500 server error in best effort: |
| 6757 | * if there is no room available in the buffer, |
| 6758 | * just close the connection. |
| 6759 | */ |
| 6760 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 6761 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6762 | |
| 6763 | channel_erase(res); |
| 6764 | res->buf.data = b_data(err); |
| 6765 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 6766 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 6767 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 6768 | } |
| 6769 | if (!(strm->flags & SF_ERR_MASK)) |
| 6770 | strm->flags |= SF_ERR_RESOURCE; |
| 6771 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 6772 | goto done; |
| 6773 | } |
| 6774 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6775 | static void hlua_applet_http_release(struct appctx *ctx) |
| 6776 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6777 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6778 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6779 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6780 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6781 | } |
| 6782 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6783 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
| 6784 | * succes case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6785 | * |
| 6786 | * This function can fail with an abort() due to an Lua critical error. |
| 6787 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 6788 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6789 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6790 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 6791 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6792 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6793 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6794 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6795 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6796 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6797 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6798 | if (!rule->arg.hlua_rule) { |
| 6799 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6800 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6801 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6802 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6803 | /* Memory for arguments. */ |
| 6804 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 6805 | if (!rule->arg.hlua_rule->args) { |
| 6806 | memprintf(err, "out of memory error"); |
| 6807 | return ACT_RET_PRS_ERR; |
| 6808 | } |
| 6809 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6810 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6811 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6812 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6813 | /* Expect some arguments */ |
| 6814 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6815 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6816 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 6817 | return ACT_RET_PRS_ERR; |
| 6818 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 6819 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6820 | if (!rule->arg.hlua_rule->args[i]) { |
| 6821 | memprintf(err, "out of memory error"); |
| 6822 | return ACT_RET_PRS_ERR; |
| 6823 | } |
| 6824 | (*cur_arg)++; |
| 6825 | } |
| 6826 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6827 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 6828 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6829 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 6830 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6831 | } |
| 6832 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6833 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 6834 | struct act_rule *rule, char **err) |
| 6835 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6836 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6837 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 6838 | /* HTTP applets are forbidden in tcp-request rules. |
| 6839 | * HTTP applet request requires everything initilized by |
| 6840 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
| 6841 | * The applet will be immediately initilized, but its before |
| 6842 | * the call of this analyzer. |
| 6843 | */ |
| 6844 | if (rule->from != ACT_F_HTTP_REQ) { |
| 6845 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 6846 | return ACT_RET_PRS_ERR; |
| 6847 | } |
| 6848 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6849 | /* Memory for the rule. */ |
| 6850 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6851 | if (!rule->arg.hlua_rule) { |
| 6852 | memprintf(err, "out of memory error"); |
| 6853 | return ACT_RET_PRS_ERR; |
| 6854 | } |
| 6855 | |
| 6856 | /* Reference the Lua function and store the reference. */ |
| 6857 | rule->arg.hlua_rule->fcn = *fcn; |
| 6858 | |
| 6859 | /* TODO: later accept arguments. */ |
| 6860 | rule->arg.hlua_rule->args = NULL; |
| 6861 | |
| 6862 | /* Add applet pointer in the rule. */ |
| 6863 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6864 | rule->applet.name = fcn->name; |
| 6865 | rule->applet.init = hlua_applet_http_init; |
| 6866 | rule->applet.fct = hlua_applet_http_fct; |
| 6867 | rule->applet.release = hlua_applet_http_release; |
| 6868 | rule->applet.timeout = hlua_timeout_applet; |
| 6869 | |
| 6870 | return ACT_RET_PRS_OK; |
| 6871 | } |
| 6872 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6873 | /* This function is an LUA binding used for registering |
| 6874 | * "sample-conv" functions. It expects a converter name used |
| 6875 | * in the haproxy configuration file, and an LUA function. |
| 6876 | */ |
| 6877 | __LJMP static int hlua_register_action(lua_State *L) |
| 6878 | { |
| 6879 | struct action_kw_list *akl; |
| 6880 | const char *name; |
| 6881 | int ref; |
| 6882 | int len; |
| 6883 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6884 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6885 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6886 | /* Initialise the number of expected arguments at 0. */ |
| 6887 | nargs = 0; |
| 6888 | |
| 6889 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 6890 | 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] | 6891 | |
| 6892 | /* First argument : converter name. */ |
| 6893 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6894 | |
| 6895 | /* Second argument : environment. */ |
| 6896 | if (lua_type(L, 2) != LUA_TTABLE) |
| 6897 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6898 | |
| 6899 | /* Third argument : lua function. */ |
| 6900 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 6901 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6902 | /* 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] | 6903 | if (lua_gettop(L) >= 4) |
| 6904 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 6905 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6906 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6907 | lua_pushnil(L); |
| 6908 | while (lua_next(L, 2) != 0) { |
| 6909 | if (lua_type(L, -1) != LUA_TSTRING) |
| 6910 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 6911 | |
| 6912 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 6913 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6914 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6915 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6916 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6917 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6918 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6919 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6920 | |
| 6921 | /* Fill fcn. */ |
| 6922 | fcn->name = strdup(name); |
| 6923 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6924 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6925 | fcn->function_ref = ref; |
| 6926 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 6927 | /* Set the expected number od arguments. */ |
| 6928 | fcn->nargs = nargs; |
| 6929 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6930 | /* List head */ |
| 6931 | akl->list.n = akl->list.p = NULL; |
| 6932 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6933 | /* action keyword. */ |
| 6934 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6935 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6936 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6937 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6938 | |
| 6939 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 6940 | |
| 6941 | akl->kw[0].match_pfx = 0; |
| 6942 | akl->kw[0].private = fcn; |
| 6943 | akl->kw[0].parse = action_register_lua; |
| 6944 | |
| 6945 | /* select the action registering point. */ |
| 6946 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 6947 | tcp_req_cont_keywords_register(akl); |
| 6948 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 6949 | tcp_res_cont_keywords_register(akl); |
| 6950 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 6951 | http_req_keywords_register(akl); |
| 6952 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 6953 | http_res_keywords_register(akl); |
| 6954 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6955 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6956 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 6957 | "are expected.", lua_tostring(L, -1))); |
| 6958 | |
| 6959 | /* pop the environment string. */ |
| 6960 | lua_pop(L, 1); |
| 6961 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6962 | return ACT_RET_PRS_OK; |
| 6963 | } |
| 6964 | |
| 6965 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 6966 | struct act_rule *rule, char **err) |
| 6967 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6968 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6969 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 6970 | if (px->mode == PR_MODE_HTTP) { |
| 6971 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6972 | return ACT_RET_PRS_ERR; |
| 6973 | } |
| 6974 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6975 | /* Memory for the rule. */ |
| 6976 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 6977 | if (!rule->arg.hlua_rule) { |
| 6978 | memprintf(err, "out of memory error"); |
| 6979 | return ACT_RET_PRS_ERR; |
| 6980 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 6981 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6982 | /* Reference the Lua function and store the reference. */ |
| 6983 | rule->arg.hlua_rule->fcn = *fcn; |
| 6984 | |
| 6985 | /* TODO: later accept arguments. */ |
| 6986 | rule->arg.hlua_rule->args = NULL; |
| 6987 | |
| 6988 | /* Add applet pointer in the rule. */ |
| 6989 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 6990 | rule->applet.name = fcn->name; |
| 6991 | rule->applet.init = hlua_applet_tcp_init; |
| 6992 | rule->applet.fct = hlua_applet_tcp_fct; |
| 6993 | rule->applet.release = hlua_applet_tcp_release; |
| 6994 | rule->applet.timeout = hlua_timeout_applet; |
| 6995 | |
| 6996 | return 0; |
| 6997 | } |
| 6998 | |
| 6999 | /* This function is an LUA binding used for registering |
| 7000 | * "sample-conv" functions. It expects a converter name used |
| 7001 | * in the haproxy configuration file, and an LUA function. |
| 7002 | */ |
| 7003 | __LJMP static int hlua_register_service(lua_State *L) |
| 7004 | { |
| 7005 | struct action_kw_list *akl; |
| 7006 | const char *name; |
| 7007 | const char *env; |
| 7008 | int ref; |
| 7009 | int len; |
| 7010 | struct hlua_function *fcn; |
| 7011 | |
| 7012 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7013 | |
| 7014 | /* First argument : converter name. */ |
| 7015 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7016 | |
| 7017 | /* Second argument : environment. */ |
| 7018 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7019 | |
| 7020 | /* Third argument : lua function. */ |
| 7021 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7022 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7023 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7024 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7025 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7026 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7027 | fcn = calloc(1, sizeof(*fcn)); |
| 7028 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7029 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7030 | |
| 7031 | /* Fill fcn. */ |
| 7032 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7033 | fcn->name = calloc(1, len); |
| 7034 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7035 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7036 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7037 | fcn->function_ref = ref; |
| 7038 | |
| 7039 | /* List head */ |
| 7040 | akl->list.n = akl->list.p = NULL; |
| 7041 | |
| 7042 | /* converter keyword. */ |
| 7043 | len = strlen("lua.") + strlen(name) + 1; |
| 7044 | akl->kw[0].kw = calloc(1, len); |
| 7045 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7046 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7047 | |
| 7048 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7049 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7050 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7051 | if (strcmp(env, "tcp") == 0) |
| 7052 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7053 | else if (strcmp(env, "http") == 0) |
| 7054 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7055 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7056 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7057 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7058 | |
| 7059 | akl->kw[0].match_pfx = 0; |
| 7060 | akl->kw[0].private = fcn; |
| 7061 | |
| 7062 | /* End of array. */ |
| 7063 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7064 | |
| 7065 | /* Register this new converter */ |
| 7066 | service_keywords_register(akl); |
| 7067 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7068 | return 0; |
| 7069 | } |
| 7070 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7071 | /* This function initialises Lua cli handler. It copies the |
| 7072 | * arguments in the Lua stack and create channel IO objects. |
| 7073 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7074 | 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] | 7075 | { |
| 7076 | struct hlua *hlua; |
| 7077 | struct hlua_function *fcn; |
| 7078 | int i; |
| 7079 | const char *error; |
| 7080 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7081 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7082 | appctx->ctx.hlua_cli.fcn = private; |
| 7083 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7084 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7085 | if (!hlua) { |
| 7086 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7087 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7088 | } |
| 7089 | HLUA_INIT(hlua); |
| 7090 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7091 | |
| 7092 | /* Create task used by signal to wakeup applets. |
| 7093 | * We use the same wakeup fonction than the Lua applet_tcp and |
| 7094 | * applet_http. It is absolutely compatible. |
| 7095 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7096 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7097 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7098 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7099 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7100 | } |
| 7101 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7102 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7103 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7104 | |
| 7105 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7106 | 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] | 7107 | 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] | 7108 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7109 | } |
| 7110 | |
| 7111 | /* The following Lua calls can fail. */ |
| 7112 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7113 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7114 | error = lua_tostring(hlua->T, -1); |
| 7115 | else |
| 7116 | error = "critical error"; |
| 7117 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7118 | goto error; |
| 7119 | } |
| 7120 | |
| 7121 | /* Check stack available size. */ |
| 7122 | if (!lua_checkstack(hlua->T, 2)) { |
| 7123 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7124 | goto error; |
| 7125 | } |
| 7126 | |
| 7127 | /* Restore the function in the stack. */ |
| 7128 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7129 | |
| 7130 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7131 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7132 | */ |
| 7133 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7134 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7135 | goto error; |
| 7136 | } |
| 7137 | hlua->nargs = 1; |
| 7138 | |
| 7139 | /* push keywords in the stack. */ |
| 7140 | for (i = 0; *args[i]; i++) { |
| 7141 | /* Check stack available size. */ |
| 7142 | if (!lua_checkstack(hlua->T, 1)) { |
| 7143 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7144 | goto error; |
| 7145 | } |
| 7146 | lua_pushstring(hlua->T, args[i]); |
| 7147 | hlua->nargs++; |
| 7148 | } |
| 7149 | |
| 7150 | /* We must initialize the execution timeouts. */ |
| 7151 | hlua->max_time = hlua_timeout_session; |
| 7152 | |
| 7153 | /* At this point the execution is safe. */ |
| 7154 | RESET_SAFE_LJMP(hlua->T); |
| 7155 | |
| 7156 | /* It's ok */ |
| 7157 | return 0; |
| 7158 | |
| 7159 | /* It's not ok. */ |
| 7160 | error: |
| 7161 | RESET_SAFE_LJMP(hlua->T); |
| 7162 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7163 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7164 | return 1; |
| 7165 | } |
| 7166 | |
| 7167 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7168 | { |
| 7169 | struct hlua *hlua; |
| 7170 | struct stream_interface *si; |
| 7171 | struct hlua_function *fcn; |
| 7172 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7173 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7174 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7175 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7176 | |
| 7177 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7178 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7179 | return 1; |
| 7180 | |
| 7181 | /* Execute the function. */ |
| 7182 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7183 | |
| 7184 | /* finished. */ |
| 7185 | case HLUA_E_OK: |
| 7186 | return 1; |
| 7187 | |
| 7188 | /* yield. */ |
| 7189 | case HLUA_E_AGAIN: |
| 7190 | /* We want write. */ |
| 7191 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7192 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7193 | /* Set the timeout. */ |
| 7194 | if (hlua->wake_time != TICK_ETERNITY) |
| 7195 | task_schedule(hlua->task, hlua->wake_time); |
| 7196 | return 0; |
| 7197 | |
| 7198 | /* finished with error. */ |
| 7199 | case HLUA_E_ERRMSG: |
| 7200 | /* Display log. */ |
| 7201 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7202 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7203 | lua_pop(hlua->T, 1); |
| 7204 | return 1; |
| 7205 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7206 | case HLUA_E_ETMOUT: |
| 7207 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7208 | fcn->name); |
| 7209 | return 1; |
| 7210 | |
| 7211 | case HLUA_E_NOMEM: |
| 7212 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7213 | fcn->name); |
| 7214 | return 1; |
| 7215 | |
| 7216 | case HLUA_E_YIELD: /* unexpected */ |
| 7217 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7218 | fcn->name); |
| 7219 | return 1; |
| 7220 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7221 | case HLUA_E_ERR: |
| 7222 | /* Display log. */ |
| 7223 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7224 | fcn->name); |
| 7225 | return 1; |
| 7226 | |
| 7227 | default: |
| 7228 | return 1; |
| 7229 | } |
| 7230 | |
| 7231 | return 1; |
| 7232 | } |
| 7233 | |
| 7234 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7235 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7236 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7237 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7238 | } |
| 7239 | |
| 7240 | /* This function is an LUA binding used for registering |
| 7241 | * new keywords in the cli. It expects a list of keywords |
| 7242 | * which are the "path". It is limited to 5 keywords. A |
| 7243 | * description of the command, a function to be executed |
| 7244 | * for the parsing and a function for io handlers. |
| 7245 | */ |
| 7246 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7247 | { |
| 7248 | struct cli_kw_list *cli_kws; |
| 7249 | const char *message; |
| 7250 | int ref_io; |
| 7251 | int len; |
| 7252 | struct hlua_function *fcn; |
| 7253 | int index; |
| 7254 | int i; |
| 7255 | |
| 7256 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7257 | |
| 7258 | /* First argument : an array of maximum 5 keywords. */ |
| 7259 | if (!lua_istable(L, 1)) |
| 7260 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7261 | |
| 7262 | /* Second argument : string with contextual message. */ |
| 7263 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7264 | |
| 7265 | /* Third and fourth argument : lua function. */ |
| 7266 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7267 | |
| 7268 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7269 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7270 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7271 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7272 | fcn = calloc(1, sizeof(*fcn)); |
| 7273 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7274 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7275 | |
| 7276 | /* Fill path. */ |
| 7277 | index = 0; |
| 7278 | lua_pushnil(L); |
| 7279 | while(lua_next(L, 1) != 0) { |
| 7280 | if (index >= 5) |
| 7281 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7282 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7283 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7284 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7285 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7286 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7287 | index++; |
| 7288 | lua_pop(L, 1); |
| 7289 | } |
| 7290 | |
| 7291 | /* Copy help message. */ |
| 7292 | cli_kws->kw[0].usage = strdup(message); |
| 7293 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7294 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7295 | |
| 7296 | /* Fill fcn io handler. */ |
| 7297 | len = strlen("<lua.cli>") + 1; |
| 7298 | for (i = 0; i < index; i++) |
| 7299 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7300 | fcn->name = calloc(1, len); |
| 7301 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7302 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7303 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7304 | for (i = 0; i < index; i++) { |
| 7305 | strncat((char *)fcn->name, ".", len); |
| 7306 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7307 | } |
| 7308 | strncat((char *)fcn->name, ">", len); |
| 7309 | fcn->function_ref = ref_io; |
| 7310 | |
| 7311 | /* Fill last entries. */ |
| 7312 | cli_kws->kw[0].private = fcn; |
| 7313 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7314 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7315 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7316 | |
| 7317 | /* Register this new converter */ |
| 7318 | cli_register_kw(cli_kws); |
| 7319 | |
| 7320 | return 0; |
| 7321 | } |
| 7322 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7323 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7324 | struct proxy *defpx, const char *file, int line, |
| 7325 | char **err, unsigned int *timeout) |
| 7326 | { |
| 7327 | const char *error; |
| 7328 | |
| 7329 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7330 | if (error == PARSE_TIME_OVER) { |
| 7331 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7332 | args[1], args[0]); |
| 7333 | return -1; |
| 7334 | } |
| 7335 | else if (error == PARSE_TIME_UNDER) { |
| 7336 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7337 | args[1], args[0]); |
| 7338 | return -1; |
| 7339 | } |
| 7340 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7341 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7342 | return -1; |
| 7343 | } |
| 7344 | return 0; |
| 7345 | } |
| 7346 | |
| 7347 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7348 | struct proxy *defpx, const char *file, int line, |
| 7349 | char **err) |
| 7350 | { |
| 7351 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7352 | file, line, err, &hlua_timeout_session); |
| 7353 | } |
| 7354 | |
| 7355 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7356 | struct proxy *defpx, const char *file, int line, |
| 7357 | char **err) |
| 7358 | { |
| 7359 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7360 | file, line, err, &hlua_timeout_task); |
| 7361 | } |
| 7362 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7363 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7364 | struct proxy *defpx, const char *file, int line, |
| 7365 | char **err) |
| 7366 | { |
| 7367 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7368 | file, line, err, &hlua_timeout_applet); |
| 7369 | } |
| 7370 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7371 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7372 | struct proxy *defpx, const char *file, int line, |
| 7373 | char **err) |
| 7374 | { |
| 7375 | char *error; |
| 7376 | |
| 7377 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7378 | if (*error != '\0') { |
| 7379 | memprintf(err, "%s: invalid number", args[0]); |
| 7380 | return -1; |
| 7381 | } |
| 7382 | return 0; |
| 7383 | } |
| 7384 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7385 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7386 | struct proxy *defpx, const char *file, int line, |
| 7387 | char **err) |
| 7388 | { |
| 7389 | char *error; |
| 7390 | |
| 7391 | if (*(args[1]) == 0) { |
| 7392 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7393 | return -1; |
| 7394 | } |
| 7395 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7396 | if (*error != '\0') { |
| 7397 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7398 | return -1; |
| 7399 | } |
| 7400 | return 0; |
| 7401 | } |
| 7402 | |
| 7403 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7404 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7405 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7406 | * the main lua entry point. |
| 7407 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7408 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7409 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7410 | * |
| 7411 | * In some error case, LUA set an error message in top of the stack. This function |
| 7412 | * 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] | 7413 | * |
| 7414 | * This function can fail with an abort() due to an Lua critical error. |
| 7415 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7416 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7417 | */ |
| 7418 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7419 | struct proxy *defpx, const char *file, int line, |
| 7420 | char **err) |
| 7421 | { |
| 7422 | int error; |
| 7423 | |
| 7424 | /* Just load and compile the file. */ |
| 7425 | error = luaL_loadfile(gL.T, args[1]); |
| 7426 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7427 | 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] | 7428 | lua_pop(gL.T, 1); |
| 7429 | return -1; |
| 7430 | } |
| 7431 | |
| 7432 | /* If no syntax error where detected, execute the code. */ |
| 7433 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7434 | switch (error) { |
| 7435 | case LUA_OK: |
| 7436 | break; |
| 7437 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7438 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7439 | lua_pop(gL.T, 1); |
| 7440 | return -1; |
| 7441 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7442 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7443 | return -1; |
| 7444 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7445 | 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] | 7446 | lua_pop(gL.T, 1); |
| 7447 | return -1; |
| 7448 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7449 | 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] | 7450 | lua_pop(gL.T, 1); |
| 7451 | return -1; |
| 7452 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7453 | memprintf(err, "Lua unknonwn error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7454 | lua_pop(gL.T, 1); |
| 7455 | return -1; |
| 7456 | } |
| 7457 | |
| 7458 | return 0; |
| 7459 | } |
| 7460 | |
| 7461 | /* configuration keywords declaration */ |
| 7462 | static struct cfg_kw_list cfg_kws = {{ },{ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7463 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7464 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7465 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7466 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7467 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7468 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7469 | { 0, NULL, NULL }, |
| 7470 | }}; |
| 7471 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7472 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7473 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7474 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7475 | /* This function can fail with an abort() due to an Lua critical error. |
| 7476 | * We are in the initialisation process of HAProxy, this abort() is |
| 7477 | * tolerated. |
| 7478 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7479 | int hlua_post_init() |
| 7480 | { |
| 7481 | struct hlua_init_function *init; |
| 7482 | const char *msg; |
| 7483 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7484 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7485 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7486 | /* Call post initialisation function in safe environement. */ |
| 7487 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7488 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7489 | error = lua_tostring(gL.T, -1); |
| 7490 | else |
| 7491 | error = "critical error"; |
| 7492 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7493 | exit(1); |
| 7494 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7495 | |
| 7496 | #if USE_OPENSSL |
| 7497 | /* Initialize SSL server. */ |
| 7498 | if (socket_ssl.xprt->prepare_srv) { |
| 7499 | int saved_used_backed = global.ssl_used_backend; |
| 7500 | // don't affect maxconn automatic computation |
| 7501 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7502 | global.ssl_used_backend = saved_used_backed; |
| 7503 | } |
| 7504 | #endif |
| 7505 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7506 | hlua_fcn_post_init(gL.T); |
| 7507 | RESET_SAFE_LJMP(gL.T); |
| 7508 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7509 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7510 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7511 | ret = hlua_ctx_resume(&gL, 0); |
| 7512 | switch (ret) { |
| 7513 | case HLUA_E_OK: |
| 7514 | lua_pop(gL.T, -1); |
| 7515 | return 1; |
| 7516 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7517 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7518 | return 0; |
| 7519 | case HLUA_E_ERRMSG: |
| 7520 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7521 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7522 | return 0; |
| 7523 | case HLUA_E_ERR: |
| 7524 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7525 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7526 | return 0; |
| 7527 | } |
| 7528 | } |
| 7529 | return 1; |
| 7530 | } |
| 7531 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7532 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7533 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7534 | * is the previously allocated size or the kind of object in case of a new |
| 7535 | * allocation. <nsize> is the requested new size. |
| 7536 | */ |
| 7537 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7538 | { |
| 7539 | struct hlua_mem_allocator *zone = ud; |
| 7540 | |
| 7541 | if (nsize == 0) { |
| 7542 | /* it's a free */ |
| 7543 | if (ptr) |
| 7544 | zone->allocated -= osize; |
| 7545 | free(ptr); |
| 7546 | return NULL; |
| 7547 | } |
| 7548 | |
| 7549 | if (!ptr) { |
| 7550 | /* it's a new allocation */ |
| 7551 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7552 | return NULL; |
| 7553 | |
| 7554 | ptr = malloc(nsize); |
| 7555 | if (ptr) |
| 7556 | zone->allocated += nsize; |
| 7557 | return ptr; |
| 7558 | } |
| 7559 | |
| 7560 | /* it's a realloc */ |
| 7561 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 7562 | return NULL; |
| 7563 | |
| 7564 | ptr = realloc(ptr, nsize); |
| 7565 | if (ptr) |
| 7566 | zone->allocated += nsize - osize; |
| 7567 | return ptr; |
| 7568 | } |
| 7569 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7570 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 7571 | * We are in the initialisation process of HAProxy, this abort() is |
| 7572 | * tolerated. |
| 7573 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 7574 | void hlua_init(void) |
| 7575 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7576 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7577 | int idx; |
| 7578 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7579 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7580 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7581 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7582 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7583 | struct srv_kw *kw; |
| 7584 | int tmp_error; |
| 7585 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7586 | char *args[] = { /* SSL client configuration. */ |
| 7587 | "ssl", |
| 7588 | "verify", |
| 7589 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 7590 | NULL |
| 7591 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7592 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7593 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7594 | /* Init main lua stack. */ |
| 7595 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 7596 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 7597 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 7598 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7599 | hlua_sethlua(&gL); |
| 7600 | gL.Tref = LUA_REFNIL; |
| 7601 | gL.task = NULL; |
| 7602 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7603 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7604 | * the Lua function can fail with an abort. We are in the initialisation |
| 7605 | * process of HAProxy, this abort() is tolerated. |
| 7606 | */ |
| 7607 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 7608 | /* Initialise lua. */ |
| 7609 | luaL_openlibs(gL.T); |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7610 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7611 | /* Set safe environment for the initialisation. */ |
| 7612 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7613 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7614 | error_msg = lua_tostring(gL.T, -1); |
| 7615 | else |
| 7616 | error_msg = "critical error"; |
| 7617 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 7618 | exit(1); |
| 7619 | } |
| 7620 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7621 | /* |
| 7622 | * |
| 7623 | * Create "core" object. |
| 7624 | * |
| 7625 | */ |
| 7626 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 7627 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7628 | lua_newtable(gL.T); |
| 7629 | |
| 7630 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 7631 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7632 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 7633 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7634 | /* Register special functions. */ |
| 7635 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 7636 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 7637 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 7638 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7639 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7640 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7641 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 7642 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 7643 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 7644 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 7645 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 7646 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 7647 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 7648 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 7649 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7650 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 7651 | hlua_class_function(gL.T, "log", hlua_log); |
| 7652 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 7653 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 7654 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 7655 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 7656 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 7657 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7658 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 7659 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7660 | |
| 7661 | /* |
| 7662 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7663 | * Register class Map |
| 7664 | * |
| 7665 | */ |
| 7666 | |
| 7667 | /* This table entry is the object "Map" base. */ |
| 7668 | lua_newtable(gL.T); |
| 7669 | |
| 7670 | /* register pattern types. */ |
| 7671 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 7672 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7673 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7674 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 7675 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 7676 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7677 | |
| 7678 | /* register constructor. */ |
| 7679 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 7680 | |
| 7681 | /* Create and fill the metatable. */ |
| 7682 | lua_newtable(gL.T); |
| 7683 | |
| 7684 | /* Create and fille the __index entry. */ |
| 7685 | lua_pushstring(gL.T, "__index"); |
| 7686 | lua_newtable(gL.T); |
| 7687 | |
| 7688 | /* Register . */ |
| 7689 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 7690 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 7691 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7692 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7693 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7694 | /* Register previous table in the registry with reference and named entry. |
| 7695 | * The function hlua_register_metatable() pops the stack, so we |
| 7696 | * previously create a copy of the table. |
| 7697 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7698 | 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] | 7699 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 7700 | |
| 7701 | /* Assign the metatable to the mai Map object. */ |
| 7702 | lua_setmetatable(gL.T, -2); |
| 7703 | |
| 7704 | /* Set a name to the table. */ |
| 7705 | lua_setglobal(gL.T, "Map"); |
| 7706 | |
| 7707 | /* |
| 7708 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7709 | * Register class Channel |
| 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 | |
| 7720 | /* Register . */ |
| 7721 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 7722 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 7723 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 7724 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 7725 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 7726 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 7727 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 7728 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 7729 | 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] | 7730 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7731 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7732 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7733 | |
| 7734 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7735 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 7736 | |
| 7737 | /* |
| 7738 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7739 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7740 | * |
| 7741 | */ |
| 7742 | |
| 7743 | /* Create and fill the metatable. */ |
| 7744 | lua_newtable(gL.T); |
| 7745 | |
| 7746 | /* Create and fille the __index entry. */ |
| 7747 | lua_pushstring(gL.T, "__index"); |
| 7748 | lua_newtable(gL.T); |
| 7749 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7750 | /* Browse existing fetches and create the associated |
| 7751 | * object method. |
| 7752 | */ |
| 7753 | sf = NULL; |
| 7754 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 7755 | |
| 7756 | /* Dont register the keywork if the arguments check function are |
| 7757 | * not safe during the runtime. |
| 7758 | */ |
| 7759 | if ((sf->val_args != NULL) && |
| 7760 | (sf->val_args != val_payload_lv) && |
| 7761 | (sf->val_args != val_hdr)) |
| 7762 | continue; |
| 7763 | |
| 7764 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7765 | * by an underscore. |
| 7766 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7767 | strncpy(trash.area, sf->kw, trash.size); |
| 7768 | trash.area[trash.size - 1] = '\0'; |
| 7769 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7770 | if (*p == '.' || *p == '-' || *p == '+') |
| 7771 | *p = '_'; |
| 7772 | |
| 7773 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7774 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 7775 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7776 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7777 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 7778 | } |
| 7779 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7780 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7781 | |
| 7782 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7783 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7784 | |
| 7785 | /* |
| 7786 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7787 | * Register class Converters |
| 7788 | * |
| 7789 | */ |
| 7790 | |
| 7791 | /* Create and fill the metatable. */ |
| 7792 | lua_newtable(gL.T); |
| 7793 | |
| 7794 | /* Create and fill the __index entry. */ |
| 7795 | lua_pushstring(gL.T, "__index"); |
| 7796 | lua_newtable(gL.T); |
| 7797 | |
| 7798 | /* Browse existing converters and create the associated |
| 7799 | * object method. |
| 7800 | */ |
| 7801 | sc = NULL; |
| 7802 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 7803 | /* Dont register the keywork if the arguments check function are |
| 7804 | * not safe during the runtime. |
| 7805 | */ |
| 7806 | if (sc->val_args != NULL) |
| 7807 | continue; |
| 7808 | |
| 7809 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 7810 | * by an underscore. |
| 7811 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7812 | strncpy(trash.area, sc->kw, trash.size); |
| 7813 | trash.area[trash.size - 1] = '\0'; |
| 7814 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7815 | if (*p == '.' || *p == '-' || *p == '+') |
| 7816 | *p = '_'; |
| 7817 | |
| 7818 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 7819 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7820 | lua_pushlightuserdata(gL.T, sc); |
| 7821 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7822 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7823 | } |
| 7824 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7825 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7826 | |
| 7827 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7828 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 7829 | |
| 7830 | /* |
| 7831 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7832 | * Register class HTTP |
| 7833 | * |
| 7834 | */ |
| 7835 | |
| 7836 | /* Create and fill the metatable. */ |
| 7837 | lua_newtable(gL.T); |
| 7838 | |
| 7839 | /* Create and fille the __index entry. */ |
| 7840 | lua_pushstring(gL.T, "__index"); |
| 7841 | lua_newtable(gL.T); |
| 7842 | |
| 7843 | /* Register Lua functions. */ |
| 7844 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 7845 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 7846 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 7847 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 7848 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 7849 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 7850 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 7851 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 7852 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 7853 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 7854 | |
| 7855 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 7856 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 7857 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 7858 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 7859 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 7860 | 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] | 7861 | 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] | 7862 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7863 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7864 | |
| 7865 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7866 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 7867 | |
| 7868 | /* |
| 7869 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7870 | * Register class AppletTCP |
| 7871 | * |
| 7872 | */ |
| 7873 | |
| 7874 | /* Create and fill the metatable. */ |
| 7875 | lua_newtable(gL.T); |
| 7876 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7877 | /* Create and fille the __index entry. */ |
| 7878 | lua_pushstring(gL.T, "__index"); |
| 7879 | lua_newtable(gL.T); |
| 7880 | |
| 7881 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 7882 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 7883 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 7884 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 7885 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 7886 | 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] | 7887 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 7888 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 7889 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7890 | |
| 7891 | lua_settable(gL.T, -3); |
| 7892 | |
| 7893 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7894 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7895 | |
| 7896 | /* |
| 7897 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7898 | * Register class AppletHTTP |
| 7899 | * |
| 7900 | */ |
| 7901 | |
| 7902 | /* Create and fill the metatable. */ |
| 7903 | lua_newtable(gL.T); |
| 7904 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7905 | /* Create and fille the __index entry. */ |
| 7906 | lua_pushstring(gL.T, "__index"); |
| 7907 | lua_newtable(gL.T); |
| 7908 | |
| 7909 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 7910 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 7911 | 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] | 7912 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 7913 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 7914 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7915 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 7916 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 7917 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 7918 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 7919 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 7920 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 7921 | |
| 7922 | lua_settable(gL.T, -3); |
| 7923 | |
| 7924 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7925 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7926 | |
| 7927 | /* |
| 7928 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 7929 | * Register class TXN |
| 7930 | * |
| 7931 | */ |
| 7932 | |
| 7933 | /* Create and fill the metatable. */ |
| 7934 | lua_newtable(gL.T); |
| 7935 | |
| 7936 | /* Create and fille the __index entry. */ |
| 7937 | lua_pushstring(gL.T, "__index"); |
| 7938 | lua_newtable(gL.T); |
| 7939 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7940 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 7941 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 7942 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 7943 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 7944 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 7945 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 7946 | hlua_class_function(gL.T, "done", hlua_txn_done); |
| 7947 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 7948 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 7949 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 7950 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 7951 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 7952 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 7953 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 7954 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 7955 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 7956 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 7957 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 7958 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7959 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 7960 | |
| 7961 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7962 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7963 | |
| 7964 | /* |
| 7965 | * |
| 7966 | * Register class Socket |
| 7967 | * |
| 7968 | */ |
| 7969 | |
| 7970 | /* Create and fill the metatable. */ |
| 7971 | lua_newtable(gL.T); |
| 7972 | |
| 7973 | /* Create and fille the __index entry. */ |
| 7974 | lua_pushstring(gL.T, "__index"); |
| 7975 | lua_newtable(gL.T); |
| 7976 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7977 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7978 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 7979 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7980 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 7981 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 7982 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 7983 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 7984 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 7985 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 7986 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 7987 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 7988 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7989 | 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] | 7990 | |
| 7991 | /* Register the garbage collector entry. */ |
| 7992 | lua_pushstring(gL.T, "__gc"); |
| 7993 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 7994 | 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] | 7995 | |
| 7996 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 7997 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 7998 | |
| 7999 | /* Proxy and server configuration initialisation. */ |
| 8000 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8001 | init_new_proxy(&socket_proxy); |
| 8002 | socket_proxy.parent = NULL; |
| 8003 | socket_proxy.last_change = now.tv_sec; |
| 8004 | socket_proxy.id = "LUA-SOCKET"; |
| 8005 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8006 | socket_proxy.maxconn = 0; |
| 8007 | socket_proxy.accept = NULL; |
| 8008 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8009 | socket_proxy.srv = NULL; |
| 8010 | socket_proxy.conn_retries = 0; |
| 8011 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8012 | |
| 8013 | /* Init TCP server: unchanged parameters */ |
| 8014 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8015 | socket_tcp.next = NULL; |
| 8016 | socket_tcp.proxy = &socket_proxy; |
| 8017 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8018 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8019 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8020 | socket_tcp.priv_conns = NULL; |
| 8021 | socket_tcp.idle_conns = NULL; |
| 8022 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8023 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8024 | socket_tcp.last_change = 0; |
| 8025 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8026 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8027 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8028 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8029 | |
| 8030 | /* XXX: Copy default parameter from default server, |
| 8031 | * but the default server is not initialized. |
| 8032 | */ |
| 8033 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8034 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8035 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8036 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8037 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8038 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8039 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8040 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8041 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8042 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8043 | |
| 8044 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8045 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8046 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8047 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8048 | socket_tcp.check.server = &socket_tcp; |
| 8049 | |
| 8050 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8051 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8052 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8053 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8054 | socket_tcp.agent.server = &socket_tcp; |
| 8055 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8056 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8057 | |
| 8058 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8059 | /* Init TCP server: unchanged parameters */ |
| 8060 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8061 | socket_ssl.next = NULL; |
| 8062 | socket_ssl.proxy = &socket_proxy; |
| 8063 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8064 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8065 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8066 | socket_ssl.priv_conns = NULL; |
| 8067 | socket_ssl.idle_conns = NULL; |
| 8068 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8069 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8070 | socket_ssl.last_change = 0; |
| 8071 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8072 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8073 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8074 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8075 | |
| 8076 | /* XXX: Copy default parameter from default server, |
| 8077 | * but the default server is not initialized. |
| 8078 | */ |
| 8079 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8080 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8081 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8082 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8083 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8084 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8085 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8086 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8087 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8088 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8089 | |
| 8090 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8091 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8092 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8093 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8094 | socket_ssl.check.server = &socket_ssl; |
| 8095 | |
| 8096 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8097 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8098 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8099 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8100 | socket_ssl.agent.server = &socket_ssl; |
| 8101 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8102 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8103 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8104 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8105 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8106 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8107 | /* |
| 8108 | * |
| 8109 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8110 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8111 | * features like client certificates and ssl_verify. |
| 8112 | * |
| 8113 | */ |
| 8114 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8115 | if (tmp_error != 0) { |
| 8116 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8117 | abort(); /* This must be never arrives because the command line |
| 8118 | not editable by the user. */ |
| 8119 | } |
| 8120 | idx += kw->skip; |
| 8121 | } |
| 8122 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8123 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8124 | |
| 8125 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8126 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8127 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8128 | static void hlua_register_build_options(void) |
| 8129 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8130 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8131 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8132 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8133 | hap_register_build_opts(ptr, 1); |
| 8134 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8135 | |
| 8136 | INITCALL0(STG_REGISTER, hlua_register_build_options); |