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> |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 14 | #include <setjmp.h> |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 15 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 16 | #include <lauxlib.h> |
| 17 | #include <lua.h> |
| 18 | #include <lualib.h> |
| 19 | |
Thierry FOURNIER | 463119c | 2015-03-10 00:35:36 +0100 | [diff] [blame] | 20 | #if !defined(LUA_VERSION_NUM) || LUA_VERSION_NUM < 503 |
| 21 | #error "Requires Lua 5.3 or later." |
Cyril Bonté | dc0306e | 2015-03-02 00:08:40 +0100 | [diff] [blame] | 22 | #endif |
| 23 | |
Willy Tarreau | 4c7e4b7 | 2020-05-27 12:58:42 +0200 | [diff] [blame] | 24 | #include <haproxy/api.h> |
Willy Tarreau | 8d2b777 | 2020-05-27 10:58:19 +0200 | [diff] [blame] | 25 | #include <import/ebpttree.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 26 | |
| 27 | #include <common/cfgparse.h> |
Willy Tarreau | 3f567e4 | 2020-05-28 15:29:19 +0200 | [diff] [blame] | 28 | #include <haproxy/thread.h> |
Willy Tarreau | 7cd8b6e | 2020-06-02 17:32:26 +0200 | [diff] [blame^] | 29 | #include <haproxy/regex.h> |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 30 | #include <common/xref.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 31 | #include <common/h1.h> |
Willy Tarreau | 889faf4 | 2020-06-01 12:09:26 +0200 | [diff] [blame] | 32 | #include <common/standard.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 33 | |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 34 | #include <types/cli.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 35 | #include <types/hlua.h> |
| 36 | #include <types/proxy.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 37 | #include <types/stats.h> |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 38 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 39 | #include <proto/arg.h> |
Willy Tarreau | 8a8d83b | 2015-04-13 13:24:54 +0200 | [diff] [blame] | 40 | #include <proto/applet.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 41 | #include <proto/channel.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 42 | #include <proto/cli.h> |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 43 | #include <proto/connection.h> |
William Lallemand | 9ed6203 | 2016-11-21 17:49:11 +0100 | [diff] [blame] | 44 | #include <proto/stats.h> |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 45 | #include <proto/hlua.h> |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 46 | #include <proto/hlua_fcn.h> |
Willy Tarreau | 79e5733 | 2018-10-02 16:01:16 +0200 | [diff] [blame] | 47 | #include <proto/http_fetch.h> |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 48 | #include <proto/http_htx.h> |
Willy Tarreau | 61c112a | 2018-10-02 16:43:32 +0200 | [diff] [blame] | 49 | #include <proto/http_rules.h> |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 50 | #include <proto/map.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 51 | #include <proto/obj_type.h> |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 52 | #include <proto/queue.h> |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 53 | #include <proto/pattern.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 54 | #include <proto/payload.h> |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 55 | #include <proto/http_ana.h> |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 56 | #include <proto/sample.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 57 | #include <proto/server.h> |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 58 | #include <proto/session.h> |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 59 | #include <proto/stream.h> |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 60 | #include <proto/stream_interface.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 61 | #include <proto/task.h> |
Willy Tarreau | 3971310 | 2016-11-25 15:49:32 +0100 | [diff] [blame] | 62 | #include <proto/tcp_rules.h> |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 63 | #include <proto/vars.h> |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 64 | |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 65 | /* Lua uses longjmp to perform yield or throwing errors. This |
| 66 | * macro is used only for identifying the function that can |
| 67 | * not return because a longjmp is executed. |
| 68 | * __LJMP marks a prototype of hlua file that can use longjmp. |
| 69 | * WILL_LJMP() marks an lua function that will use longjmp. |
| 70 | * MAY_LJMP() marks an lua function that may use longjmp. |
| 71 | */ |
| 72 | #define __LJMP |
Willy Tarreau | 4e7cc33 | 2018-10-20 17:45:48 +0200 | [diff] [blame] | 73 | #define WILL_LJMP(func) do { func; my_unreachable(); } while(0) |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 74 | #define MAY_LJMP(func) func |
| 75 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 76 | /* This couple of function executes securely some Lua calls outside of |
| 77 | * the lua runtime environment. Each Lua call can return a longjmp |
| 78 | * if it encounter a memory error. |
| 79 | * |
| 80 | * Lua documentation extract: |
| 81 | * |
| 82 | * If an error happens outside any protected environment, Lua calls |
| 83 | * a panic function (see lua_atpanic) and then calls abort, thus |
| 84 | * exiting the host application. Your panic function can avoid this |
| 85 | * exit by never returning (e.g., doing a long jump to your own |
| 86 | * recovery point outside Lua). |
| 87 | * |
| 88 | * The panic function runs as if it were a message handler (see |
| 89 | * §2.3); in particular, the error message is at the top of the |
| 90 | * stack. However, there is no guarantee about stack space. To push |
| 91 | * anything on the stack, the panic function must first check the |
| 92 | * available space (see §4.2). |
| 93 | * |
| 94 | * We must check all the Lua entry point. This includes: |
| 95 | * - The include/proto/hlua.h exported functions |
| 96 | * - the task wrapper function |
| 97 | * - The action wrapper function |
| 98 | * - The converters wrapper function |
| 99 | * - The sample-fetch wrapper functions |
| 100 | * |
| 101 | * It is tolerated that the initilisation function returns an abort. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 102 | * Before each Lua abort, an error message is written on stderr. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 103 | * |
| 104 | * The macro SET_SAFE_LJMP initialise the longjmp. The Macro |
| 105 | * RESET_SAFE_LJMP reset the longjmp. These function must be macro |
| 106 | * because they must be exists in the program stack when the longjmp |
| 107 | * is called. |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 108 | * |
| 109 | * Note that the Lua processing is not really thread safe. It provides |
| 110 | * heavy system which consists to add our own lock function in the Lua |
| 111 | * code and recompile the library. This system will probably not accepted |
| 112 | * by maintainers of various distribs. |
| 113 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 114 | * Our main execution point of the Lua is the function lua_resume(). A |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 115 | * quick looking on the Lua sources displays a lua_lock() a the start |
| 116 | * of function and a lua_unlock() at the end of the function. So I |
| 117 | * conclude that the Lua thread safe mode just perform a mutex around |
| 118 | * all execution. So I prefer to do this in the HAProxy code, it will be |
| 119 | * easier for distro maintainers. |
| 120 | * |
| 121 | * Note that the HAProxy lua functions rounded by the macro SET_SAFE_LJMP |
| 122 | * and RESET_SAFE_LJMP manipulates the Lua stack, so it will be careful |
| 123 | * to set mutex around these functions. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 124 | */ |
Willy Tarreau | 86abe44 | 2018-11-25 20:12:18 +0100 | [diff] [blame] | 125 | __decl_spinlock(hlua_global_lock); |
Thierry FOURNIER | ffbad79 | 2017-07-12 11:39:04 +0200 | [diff] [blame] | 126 | THREAD_LOCAL jmp_buf safe_ljmp_env; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 127 | static int hlua_panic_safe(lua_State *L) { return 0; } |
Willy Tarreau | 9d6bb5a | 2020-02-06 15:55:41 +0100 | [diff] [blame] | 128 | static int hlua_panic_ljmp(lua_State *L) { WILL_LJMP(longjmp(safe_ljmp_env, 1)); } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 129 | |
| 130 | #define SET_SAFE_LJMP(__L) \ |
| 131 | ({ \ |
| 132 | int ret; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 133 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 134 | if (setjmp(safe_ljmp_env) != 0) { \ |
| 135 | lua_atpanic(__L, hlua_panic_safe); \ |
| 136 | ret = 0; \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 137 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 138 | } else { \ |
| 139 | lua_atpanic(__L, hlua_panic_ljmp); \ |
| 140 | ret = 1; \ |
| 141 | } \ |
| 142 | ret; \ |
| 143 | }) |
| 144 | |
| 145 | /* If we are the last function catching Lua errors, we |
| 146 | * must reset the panic function. |
| 147 | */ |
| 148 | #define RESET_SAFE_LJMP(__L) \ |
| 149 | do { \ |
| 150 | lua_atpanic(__L, hlua_panic_safe); \ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 151 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); \ |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 152 | } while(0) |
| 153 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 154 | /* Applet status flags */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 155 | #define APPLET_DONE 0x01 /* applet processing is done. */ |
Christopher Faulet | 18c2e8d | 2019-03-01 12:02:08 +0100 | [diff] [blame] | 156 | /* unused: 0x02 */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 157 | #define APPLET_HDR_SENT 0x04 /* Response header sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 158 | /* unused: 0x08, 0x10 */ |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 159 | #define APPLET_HTTP11 0x20 /* Last chunk sent. */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 160 | #define APPLET_RSP_SENT 0x40 /* The response was fully sent */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 161 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 162 | /* The main Lua execution context. */ |
| 163 | struct hlua gL; |
| 164 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 165 | /* This is the memory pool containing struct lua for applets |
| 166 | * (including cli). |
| 167 | */ |
Willy Tarreau | 8ceae72 | 2018-11-26 11:58:30 +0100 | [diff] [blame] | 168 | DECLARE_STATIC_POOL(pool_head_hlua, "hlua", sizeof(struct hlua)); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 169 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 170 | /* Used for Socket connection. */ |
| 171 | static struct proxy socket_proxy; |
| 172 | static struct server socket_tcp; |
| 173 | #ifdef USE_OPENSSL |
| 174 | static struct server socket_ssl; |
| 175 | #endif |
| 176 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 177 | /* List head of the function called at the initialisation time. */ |
| 178 | struct list hlua_init_functions = LIST_HEAD_INIT(hlua_init_functions); |
| 179 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 180 | /* The following variables contains the reference of the different |
| 181 | * Lua classes. These references are useful for identify metadata |
| 182 | * associated with an object. |
| 183 | */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 184 | static int class_txn_ref; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 185 | static int class_socket_ref; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 186 | static int class_channel_ref; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 187 | static int class_fetches_ref; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 188 | static int class_converters_ref; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 189 | static int class_http_ref; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 190 | static int class_map_ref; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 191 | static int class_applet_tcp_ref; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 192 | static int class_applet_http_ref; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 193 | static int class_txn_reply_ref; |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 194 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 195 | /* Global Lua execution timeout. By default Lua, execution linked |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 196 | * with stream (actions, sample-fetches and converters) have a |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 197 | * short timeout. Lua linked with tasks doesn't have a timeout |
| 198 | * because a task may remain alive during all the haproxy execution. |
| 199 | */ |
| 200 | static unsigned int hlua_timeout_session = 4000; /* session timeout. */ |
| 201 | static unsigned int hlua_timeout_task = TICK_ETERNITY; /* task timeout. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 202 | static unsigned int hlua_timeout_applet = 4000; /* applet timeout. */ |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 203 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 204 | /* Interrupts the Lua processing each "hlua_nb_instruction" instructions. |
| 205 | * it is used for preventing infinite loops. |
| 206 | * |
| 207 | * I test the scheer with an infinite loop containing one incrementation |
| 208 | * and one test. I run this loop between 10 seconds, I raise a ceil of |
| 209 | * 710M loops from one interrupt each 9000 instructions, so I fix the value |
| 210 | * to one interrupt each 10 000 instructions. |
| 211 | * |
| 212 | * configured | Number of |
| 213 | * instructions | loops executed |
| 214 | * between two | in milions |
| 215 | * forced yields | |
| 216 | * ---------------+--------------- |
| 217 | * 10 | 160 |
| 218 | * 500 | 670 |
| 219 | * 1000 | 680 |
| 220 | * 5000 | 700 |
| 221 | * 7000 | 700 |
| 222 | * 8000 | 700 |
| 223 | * 9000 | 710 <- ceil |
| 224 | * 10000 | 710 |
| 225 | * 100000 | 710 |
| 226 | * 1000000 | 710 |
| 227 | * |
| 228 | */ |
| 229 | static unsigned int hlua_nb_instruction = 10000; |
| 230 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 231 | /* Descriptor for the memory allocation state. If limit is not null, it will |
| 232 | * be enforced on any memory allocation. |
| 233 | */ |
| 234 | struct hlua_mem_allocator { |
| 235 | size_t allocated; |
| 236 | size_t limit; |
| 237 | }; |
| 238 | |
| 239 | static struct hlua_mem_allocator hlua_global_allocator; |
| 240 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 241 | /* These functions converts types between HAProxy internal args or |
| 242 | * sample and LUA types. Another function permits to check if the |
| 243 | * LUA stack contains arguments according with an required ARG_T |
| 244 | * format. |
| 245 | */ |
| 246 | static int hlua_arg2lua(lua_State *L, const struct arg *arg); |
| 247 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 248 | __LJMP static int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 249 | uint64_t mask, struct proxy *p); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 250 | static int hlua_smp2lua(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 251 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 252 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp); |
| 253 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 254 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 255 | |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 256 | #define SEND_ERR(__be, __fmt, __args...) \ |
| 257 | do { \ |
| 258 | send_log(__be, LOG_ERR, __fmt, ## __args); \ |
| 259 | if (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE)) \ |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 260 | ha_alert(__fmt, ## __args); \ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 261 | } while (0) |
| 262 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 263 | /* Used to check an Lua function type in the stack. It creates and |
| 264 | * returns a reference of the function. This function throws an |
| 265 | * error if the rgument is not a "function". |
| 266 | */ |
| 267 | __LJMP unsigned int hlua_checkfunction(lua_State *L, int argno) |
| 268 | { |
| 269 | if (!lua_isfunction(L, argno)) { |
Thierry FOURNIER | fd1e955 | 2018-02-23 18:41:18 +0100 | [diff] [blame] | 270 | const char *msg = lua_pushfstring(L, "function expected, got %s", luaL_typename(L, argno)); |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 271 | WILL_LJMP(luaL_argerror(L, argno, msg)); |
| 272 | } |
| 273 | lua_pushvalue(L, argno); |
| 274 | return luaL_ref(L, LUA_REGISTRYINDEX); |
| 275 | } |
| 276 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 277 | /* Return the string that is of the top of the stack. */ |
| 278 | const char *hlua_get_top_error_string(lua_State *L) |
| 279 | { |
| 280 | if (lua_gettop(L) < 1) |
| 281 | return "unknown error"; |
| 282 | if (lua_type(L, -1) != LUA_TSTRING) |
| 283 | return "unknown error"; |
| 284 | return lua_tostring(L, -1); |
| 285 | } |
| 286 | |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 287 | __LJMP static const char *hlua_traceback(lua_State *L) |
| 288 | { |
| 289 | lua_Debug ar; |
| 290 | int level = 0; |
Willy Tarreau | 83061a8 | 2018-07-13 11:56:34 +0200 | [diff] [blame] | 291 | struct buffer *msg = get_trash_chunk(); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 292 | int filled = 0; |
| 293 | |
| 294 | while (lua_getstack(L, level++, &ar)) { |
| 295 | |
| 296 | /* Add separator */ |
| 297 | if (filled) |
| 298 | chunk_appendf(msg, ", "); |
| 299 | filled = 1; |
| 300 | |
| 301 | /* Fill fields: |
| 302 | * 'S': fills in the fields source, short_src, linedefined, lastlinedefined, and what; |
| 303 | * 'l': fills in the field currentline; |
| 304 | * 'n': fills in the field name and namewhat; |
| 305 | * 't': fills in the field istailcall; |
| 306 | */ |
| 307 | lua_getinfo(L, "Slnt", &ar); |
| 308 | |
| 309 | /* Append code localisation */ |
| 310 | if (ar.currentline > 0) |
| 311 | chunk_appendf(msg, "%s:%d ", ar.short_src, ar.currentline); |
| 312 | else |
| 313 | chunk_appendf(msg, "%s ", ar.short_src); |
| 314 | |
| 315 | /* |
| 316 | * Get function name |
| 317 | * |
| 318 | * if namewhat is no empty, name is defined. |
| 319 | * what contains "Lua" for Lua function, "C" for C function, |
| 320 | * or "main" for main code. |
| 321 | */ |
| 322 | if (*ar.namewhat != '\0' && ar.name != NULL) /* is there a name from code? */ |
| 323 | chunk_appendf(msg, "%s '%s'", ar.namewhat, ar.name); /* use it */ |
| 324 | |
| 325 | else if (*ar.what == 'm') /* "main", the code is not executed in a function */ |
| 326 | chunk_appendf(msg, "main chunk"); |
| 327 | |
| 328 | else if (*ar.what != 'C') /* for Lua functions, use <file:line> */ |
| 329 | chunk_appendf(msg, "C function line %d", ar.linedefined); |
| 330 | |
| 331 | else /* nothing left... */ |
| 332 | chunk_appendf(msg, "?"); |
| 333 | |
| 334 | |
| 335 | /* Display tailed call */ |
| 336 | if (ar.istailcall) |
| 337 | chunk_appendf(msg, " ..."); |
| 338 | } |
| 339 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 340 | return msg->area; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 341 | } |
| 342 | |
| 343 | |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 344 | /* This function check the number of arguments available in the |
| 345 | * stack. If the number of arguments available is not the same |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 346 | * then <nb> an error is thrown. |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 347 | */ |
| 348 | __LJMP static inline void check_args(lua_State *L, int nb, char *fcn) |
| 349 | { |
| 350 | if (lua_gettop(L) == nb) |
| 351 | return; |
| 352 | WILL_LJMP(luaL_error(L, "'%s' needs %d arguments", fcn, nb)); |
| 353 | } |
| 354 | |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 355 | /* This function pushes an error string prefixed by the file name |
Thierry FOURNIER | e8b9a40 | 2015-02-25 18:48:12 +0100 | [diff] [blame] | 356 | * and the line number where the error is encountered. |
| 357 | */ |
| 358 | static int hlua_pusherror(lua_State *L, const char *fmt, ...) |
| 359 | { |
| 360 | va_list argp; |
| 361 | va_start(argp, fmt); |
| 362 | luaL_where(L, 1); |
| 363 | lua_pushvfstring(L, fmt, argp); |
| 364 | va_end(argp); |
| 365 | lua_concat(L, 2); |
| 366 | return 1; |
| 367 | } |
| 368 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 369 | /* This functions is used with sample fetch and converters. It |
| 370 | * converts the HAProxy configuration argument in a lua stack |
| 371 | * values. |
| 372 | * |
| 373 | * It takes an array of "arg", and each entry of the array is |
| 374 | * converted and pushed in the LUA stack. |
| 375 | */ |
| 376 | static int hlua_arg2lua(lua_State *L, const struct arg *arg) |
| 377 | { |
| 378 | switch (arg->type) { |
| 379 | case ARGT_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 380 | case ARGT_TIME: |
| 381 | case ARGT_SIZE: |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 382 | lua_pushinteger(L, arg->data.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 383 | break; |
| 384 | |
| 385 | case ARGT_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 386 | lua_pushlstring(L, arg->data.str.area, arg->data.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 387 | break; |
| 388 | |
| 389 | case ARGT_IPV4: |
| 390 | case ARGT_IPV6: |
| 391 | case ARGT_MSK4: |
| 392 | case ARGT_MSK6: |
| 393 | case ARGT_FE: |
| 394 | case ARGT_BE: |
| 395 | case ARGT_TAB: |
| 396 | case ARGT_SRV: |
| 397 | case ARGT_USR: |
| 398 | case ARGT_MAP: |
| 399 | default: |
| 400 | lua_pushnil(L); |
| 401 | break; |
| 402 | } |
| 403 | return 1; |
| 404 | } |
| 405 | |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 406 | /* This function take one entry in an LUA stack at the index "ud", |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 407 | * and try to convert it in an HAProxy argument entry. This is useful |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 408 | * with sample fetch wrappers. The input arguments are given to the |
| 409 | * lua wrapper and converted as arg list by the function. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 410 | */ |
| 411 | static int hlua_lua2arg(lua_State *L, int ud, struct arg *arg) |
| 412 | { |
| 413 | switch (lua_type(L, ud)) { |
| 414 | |
| 415 | case LUA_TNUMBER: |
| 416 | case LUA_TBOOLEAN: |
| 417 | arg->type = ARGT_SINT; |
| 418 | arg->data.sint = lua_tointeger(L, ud); |
| 419 | break; |
| 420 | |
| 421 | case LUA_TSTRING: |
| 422 | arg->type = ARGT_STR; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 423 | arg->data.str.area = (char *)lua_tolstring(L, ud, &arg->data.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 424 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 425 | arg->data.str.size = arg->data.str.data; |
| 426 | arg->data.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 427 | break; |
| 428 | |
| 429 | case LUA_TUSERDATA: |
| 430 | case LUA_TNIL: |
| 431 | case LUA_TTABLE: |
| 432 | case LUA_TFUNCTION: |
| 433 | case LUA_TTHREAD: |
| 434 | case LUA_TLIGHTUSERDATA: |
| 435 | arg->type = ARGT_SINT; |
Thierry FOURNIER | bf65cd4 | 2015-07-20 17:45:02 +0200 | [diff] [blame] | 436 | arg->data.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 437 | break; |
| 438 | } |
| 439 | return 1; |
| 440 | } |
| 441 | |
| 442 | /* the following functions are used to convert a struct sample |
| 443 | * in Lua type. This useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 444 | * fetches or converters. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 445 | */ |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 446 | static int hlua_smp2lua(lua_State *L, struct sample *smp) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 447 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 448 | switch (smp->data.type) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 449 | case SMP_T_SINT: |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 450 | case SMP_T_BOOL: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 451 | lua_pushinteger(L, smp->data.u.sint); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 452 | break; |
| 453 | |
| 454 | case SMP_T_BIN: |
| 455 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 456 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 457 | break; |
| 458 | |
| 459 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 460 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 461 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 462 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 463 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 464 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 465 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 466 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 467 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 468 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 469 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 470 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 471 | break; |
| 472 | default: |
| 473 | lua_pushnil(L); |
| 474 | break; |
| 475 | } |
| 476 | break; |
| 477 | |
| 478 | case SMP_T_IPV4: |
| 479 | case SMP_T_IPV6: |
| 480 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 481 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 482 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 483 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Willy Tarreau | 5eadada | 2015-03-10 17:28:54 +0100 | [diff] [blame] | 484 | else |
| 485 | lua_pushnil(L); |
| 486 | break; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 487 | default: |
| 488 | lua_pushnil(L); |
| 489 | break; |
| 490 | } |
| 491 | return 1; |
| 492 | } |
| 493 | |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 494 | /* the following functions are used to convert a struct sample |
| 495 | * in Lua strings. This is useful to convert the return of the |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 496 | * fetches or converters. |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 497 | */ |
| 498 | static int hlua_smp2lua_str(lua_State *L, struct sample *smp) |
| 499 | { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 500 | switch (smp->data.type) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 501 | |
| 502 | case SMP_T_BIN: |
| 503 | case SMP_T_STR: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 504 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 505 | break; |
| 506 | |
| 507 | case SMP_T_METH: |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 508 | switch (smp->data.u.meth.meth) { |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 509 | case HTTP_METH_OPTIONS: lua_pushstring(L, "OPTIONS"); break; |
| 510 | case HTTP_METH_GET: lua_pushstring(L, "GET"); break; |
| 511 | case HTTP_METH_HEAD: lua_pushstring(L, "HEAD"); break; |
| 512 | case HTTP_METH_POST: lua_pushstring(L, "POST"); break; |
| 513 | case HTTP_METH_PUT: lua_pushstring(L, "PUT"); break; |
| 514 | case HTTP_METH_DELETE: lua_pushstring(L, "DELETE"); break; |
| 515 | case HTTP_METH_TRACE: lua_pushstring(L, "TRACE"); break; |
| 516 | case HTTP_METH_CONNECT: lua_pushstring(L, "CONNECT"); break; |
| 517 | case HTTP_METH_OTHER: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 518 | lua_pushlstring(L, smp->data.u.meth.str.area, smp->data.u.meth.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 519 | break; |
| 520 | default: |
| 521 | lua_pushstring(L, ""); |
| 522 | break; |
| 523 | } |
| 524 | break; |
| 525 | |
| 526 | case SMP_T_SINT: |
| 527 | case SMP_T_BOOL: |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 528 | case SMP_T_IPV4: |
| 529 | case SMP_T_IPV6: |
| 530 | case SMP_T_ADDR: /* This type is never used to qualify a sample. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 531 | if (sample_casts[smp->data.type][SMP_T_STR] && |
| 532 | sample_casts[smp->data.type][SMP_T_STR](smp)) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 533 | lua_pushlstring(L, smp->data.u.str.area, smp->data.u.str.data); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 534 | else |
| 535 | lua_pushstring(L, ""); |
| 536 | break; |
| 537 | default: |
| 538 | lua_pushstring(L, ""); |
| 539 | break; |
| 540 | } |
| 541 | return 1; |
| 542 | } |
| 543 | |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 544 | /* the following functions are used to convert an Lua type in a |
| 545 | * struct sample. This is useful to provide data from a converter |
| 546 | * to the LUA code. |
| 547 | */ |
| 548 | static int hlua_lua2smp(lua_State *L, int ud, struct sample *smp) |
| 549 | { |
| 550 | switch (lua_type(L, ud)) { |
| 551 | |
| 552 | case LUA_TNUMBER: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 553 | smp->data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 554 | smp->data.u.sint = lua_tointeger(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 555 | break; |
| 556 | |
| 557 | |
| 558 | case LUA_TBOOLEAN: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 559 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 560 | smp->data.u.sint = lua_toboolean(L, ud); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 561 | break; |
| 562 | |
| 563 | case LUA_TSTRING: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 564 | smp->data.type = SMP_T_STR; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 565 | smp->flags |= SMP_F_CONST; |
Tim Duesterhus | 2e89dec | 2019-09-29 23:03:08 +0200 | [diff] [blame] | 566 | smp->data.u.str.area = (char *)lua_tolstring(L, ud, &smp->data.u.str.data); |
Tim Duesterhus | 29d2e8a | 2019-09-29 23:03:07 +0200 | [diff] [blame] | 567 | /* We don't know the actual size of the underlying allocation, so be conservative. */ |
| 568 | smp->data.u.str.size = smp->data.u.str.data; |
| 569 | smp->data.u.str.head = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 570 | break; |
| 571 | |
| 572 | case LUA_TUSERDATA: |
| 573 | case LUA_TNIL: |
| 574 | case LUA_TTABLE: |
| 575 | case LUA_TFUNCTION: |
| 576 | case LUA_TTHREAD: |
| 577 | case LUA_TLIGHTUSERDATA: |
Thierry FOURNIER | 93405e1 | 2015-08-26 14:19:03 +0200 | [diff] [blame] | 578 | case LUA_TNONE: |
| 579 | default: |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 580 | smp->data.type = SMP_T_BOOL; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 581 | smp->data.u.sint = 0; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 582 | break; |
| 583 | } |
| 584 | return 1; |
| 585 | } |
| 586 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 587 | /* This function check the "argp" built by another conversion function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 588 | * is in accord with the expected argp defined by the "mask". The function |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 589 | * returns true or false. It can be adjust the types if there compatibles. |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 590 | * |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 591 | * This function assumes that the argp argument contains ARGM_NBARGS + 1 |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 592 | * entries. |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 593 | */ |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 594 | __LJMP int hlua_lua2arg_check(lua_State *L, int first, struct arg *argp, |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 595 | uint64_t mask, struct proxy *p) |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 596 | { |
| 597 | int min_arg; |
| 598 | int idx; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 599 | struct proxy *px; |
| 600 | char *sname, *pname; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 601 | |
| 602 | idx = 0; |
| 603 | min_arg = ARGM(mask); |
| 604 | mask >>= ARGM_BITS; |
| 605 | |
| 606 | while (1) { |
| 607 | |
| 608 | /* Check oversize. */ |
| 609 | if (idx >= ARGM_NBARGS && argp[idx].type != ARGT_STOP) { |
Cyril Bonté | 577a36a | 2015-03-02 00:08:38 +0100 | [diff] [blame] | 610 | WILL_LJMP(luaL_argerror(L, first + idx, "Malformed argument mask")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 611 | } |
| 612 | |
| 613 | /* Check for mandatory arguments. */ |
| 614 | if (argp[idx].type == ARGT_STOP) { |
Thierry FOURNIER | 3caa039 | 2015-03-13 13:38:17 +0100 | [diff] [blame] | 615 | if (idx < min_arg) { |
| 616 | |
| 617 | /* If miss other argument than the first one, we return an error. */ |
| 618 | if (idx > 0) |
| 619 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 620 | |
| 621 | /* If first argument have a certain type, some default values |
| 622 | * may be used. See the function smp_resolve_args(). |
| 623 | */ |
| 624 | switch (mask & ARGT_MASK) { |
| 625 | |
| 626 | case ARGT_FE: |
| 627 | if (!(p->cap & PR_CAP_FE)) |
| 628 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 629 | argp[idx].data.prx = p; |
| 630 | argp[idx].type = ARGT_FE; |
| 631 | argp[idx+1].type = ARGT_STOP; |
| 632 | break; |
| 633 | |
| 634 | case ARGT_BE: |
| 635 | if (!(p->cap & PR_CAP_BE)) |
| 636 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 637 | argp[idx].data.prx = p; |
| 638 | argp[idx].type = ARGT_BE; |
| 639 | argp[idx+1].type = ARGT_STOP; |
| 640 | break; |
| 641 | |
| 642 | case ARGT_TAB: |
| 643 | argp[idx].data.prx = p; |
| 644 | argp[idx].type = ARGT_TAB; |
| 645 | argp[idx+1].type = ARGT_STOP; |
| 646 | break; |
| 647 | |
| 648 | default: |
| 649 | WILL_LJMP(luaL_argerror(L, first + idx, "Mandatory argument expected")); |
| 650 | break; |
| 651 | } |
| 652 | } |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 653 | return 0; |
| 654 | } |
| 655 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 656 | /* Check for exceed the number of required argument. */ |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 657 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 658 | argp[idx].type != ARGT_STOP) { |
| 659 | WILL_LJMP(luaL_argerror(L, first + idx, "Last argument expected")); |
| 660 | } |
| 661 | |
| 662 | if ((mask & ARGT_MASK) == ARGT_STOP && |
| 663 | argp[idx].type == ARGT_STOP) { |
| 664 | return 0; |
| 665 | } |
| 666 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 667 | /* Convert some argument types. */ |
| 668 | switch (mask & ARGT_MASK) { |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 669 | case ARGT_SINT: |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 670 | if (argp[idx].type != ARGT_SINT) |
| 671 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
| 672 | argp[idx].type = ARGT_SINT; |
| 673 | break; |
| 674 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 675 | case ARGT_TIME: |
| 676 | if (argp[idx].type != ARGT_SINT) |
| 677 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 678 | argp[idx].type = ARGT_TIME; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 679 | break; |
| 680 | |
| 681 | case ARGT_SIZE: |
| 682 | if (argp[idx].type != ARGT_SINT) |
| 683 | WILL_LJMP(luaL_argerror(L, first + idx, "integer expected")); |
Thierry FOURNIER | 29176f3 | 2015-07-07 00:41:29 +0200 | [diff] [blame] | 684 | argp[idx].type = ARGT_SIZE; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 685 | break; |
| 686 | |
| 687 | case ARGT_FE: |
| 688 | if (argp[idx].type != ARGT_STR) |
| 689 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 690 | memcpy(trash.area, argp[idx].data.str.area, |
| 691 | argp[idx].data.str.data); |
| 692 | trash.area[argp[idx].data.str.data] = 0; |
| 693 | argp[idx].data.prx = proxy_fe_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 694 | if (!argp[idx].data.prx) |
| 695 | WILL_LJMP(luaL_argerror(L, first + idx, "frontend doesn't exist")); |
| 696 | argp[idx].type = ARGT_FE; |
| 697 | break; |
| 698 | |
| 699 | case ARGT_BE: |
| 700 | if (argp[idx].type != ARGT_STR) |
| 701 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 702 | memcpy(trash.area, argp[idx].data.str.area, |
| 703 | argp[idx].data.str.data); |
| 704 | trash.area[argp[idx].data.str.data] = 0; |
| 705 | argp[idx].data.prx = proxy_be_by_name(trash.area); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 706 | if (!argp[idx].data.prx) |
| 707 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 708 | argp[idx].type = ARGT_BE; |
| 709 | break; |
| 710 | |
| 711 | case ARGT_TAB: |
| 712 | if (argp[idx].type != ARGT_STR) |
| 713 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 714 | memcpy(trash.area, argp[idx].data.str.area, |
| 715 | argp[idx].data.str.data); |
| 716 | trash.area[argp[idx].data.str.data] = 0; |
Tim Duesterhus | 9fe7c63 | 2019-09-29 23:03:09 +0200 | [diff] [blame] | 717 | argp[idx].data.t = stktable_find_by_name(trash.area); |
| 718 | if (!argp[idx].data.t) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 719 | WILL_LJMP(luaL_argerror(L, first + idx, "table doesn't exist")); |
| 720 | argp[idx].type = ARGT_TAB; |
| 721 | break; |
| 722 | |
| 723 | case ARGT_SRV: |
| 724 | if (argp[idx].type != ARGT_STR) |
| 725 | WILL_LJMP(luaL_argerror(L, first + idx, "string expected")); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 726 | memcpy(trash.area, argp[idx].data.str.area, |
| 727 | argp[idx].data.str.data); |
| 728 | trash.area[argp[idx].data.str.data] = 0; |
| 729 | sname = strrchr(trash.area, '/'); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 730 | if (sname) { |
| 731 | *sname++ = '\0'; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 732 | pname = trash.area; |
Willy Tarreau | 9e0bb10 | 2015-05-26 11:24:42 +0200 | [diff] [blame] | 733 | px = proxy_be_by_name(pname); |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 734 | if (!px) |
| 735 | WILL_LJMP(luaL_argerror(L, first + idx, "backend doesn't exist")); |
| 736 | } |
| 737 | else { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 738 | sname = trash.area; |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 739 | px = p; |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 740 | } |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 741 | argp[idx].data.srv = findserver(px, sname); |
| 742 | if (!argp[idx].data.srv) |
| 743 | WILL_LJMP(luaL_argerror(L, first + idx, "server doesn't exist")); |
| 744 | argp[idx].type = ARGT_SRV; |
| 745 | break; |
| 746 | |
| 747 | case ARGT_IPV4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 748 | memcpy(trash.area, argp[idx].data.str.area, |
| 749 | argp[idx].data.str.data); |
| 750 | trash.area[argp[idx].data.str.data] = 0; |
| 751 | if (inet_pton(AF_INET, trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 752 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 address")); |
| 753 | argp[idx].type = ARGT_IPV4; |
| 754 | break; |
| 755 | |
| 756 | case ARGT_MSK4: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 757 | memcpy(trash.area, argp[idx].data.str.area, |
| 758 | argp[idx].data.str.data); |
| 759 | trash.area[argp[idx].data.str.data] = 0; |
| 760 | if (!str2mask(trash.area, &argp[idx].data.ipv4)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 761 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv4 mask")); |
| 762 | argp[idx].type = ARGT_MSK4; |
| 763 | break; |
| 764 | |
| 765 | case ARGT_IPV6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 766 | memcpy(trash.area, argp[idx].data.str.area, |
| 767 | argp[idx].data.str.data); |
| 768 | trash.area[argp[idx].data.str.data] = 0; |
| 769 | if (inet_pton(AF_INET6, trash.area, &argp[idx].data.ipv6)) |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 770 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 address")); |
| 771 | argp[idx].type = ARGT_IPV6; |
| 772 | break; |
| 773 | |
| 774 | case ARGT_MSK6: |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 775 | memcpy(trash.area, argp[idx].data.str.area, |
| 776 | argp[idx].data.str.data); |
| 777 | trash.area[argp[idx].data.str.data] = 0; |
| 778 | if (!str2mask6(trash.area, &argp[idx].data.ipv6)) |
Tim Duesterhus | b814da6 | 2018-01-25 16:24:50 +0100 | [diff] [blame] | 779 | WILL_LJMP(luaL_argerror(L, first + idx, "invalid IPv6 mask")); |
| 780 | argp[idx].type = ARGT_MSK6; |
| 781 | break; |
| 782 | |
Thierry FOURNIER | 7f4942a | 2015-03-12 18:28:50 +0100 | [diff] [blame] | 783 | case ARGT_MAP: |
| 784 | case ARGT_REG: |
| 785 | case ARGT_USR: |
| 786 | WILL_LJMP(luaL_argerror(L, first + idx, "type not yet supported")); |
Thierry FOURNIER | 55da165 | 2015-01-23 11:36:30 +0100 | [diff] [blame] | 787 | break; |
| 788 | } |
| 789 | |
| 790 | /* Check for type of argument. */ |
| 791 | if ((mask & ARGT_MASK) != argp[idx].type) { |
| 792 | const char *msg = lua_pushfstring(L, "'%s' expected, got '%s'", |
| 793 | arg_type_names[(mask & ARGT_MASK)], |
| 794 | arg_type_names[argp[idx].type & ARGT_MASK]); |
| 795 | WILL_LJMP(luaL_argerror(L, first + idx, msg)); |
| 796 | } |
| 797 | |
| 798 | /* Next argument. */ |
| 799 | mask >>= ARGT_BITS; |
| 800 | idx++; |
| 801 | } |
| 802 | } |
| 803 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 804 | /* |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 805 | * The following functions are used to make correspondence between the the |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 806 | * executed lua pointer and the "struct hlua *" that contain the context. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 807 | * |
| 808 | * - hlua_gethlua : return the hlua context associated with an lua_State. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 809 | * - hlua_sethlua : create the association between hlua context and lua_state. |
| 810 | */ |
| 811 | static inline struct hlua *hlua_gethlua(lua_State *L) |
| 812 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 813 | struct hlua **hlua = lua_getextraspace(L); |
| 814 | return *hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 815 | } |
| 816 | static inline void hlua_sethlua(struct hlua *hlua) |
| 817 | { |
Thierry FOURNIER | 38c5fd6 | 2015-03-10 02:40:29 +0100 | [diff] [blame] | 818 | struct hlua **hlua_store = lua_getextraspace(hlua->T); |
| 819 | *hlua_store = hlua; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 820 | } |
| 821 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 822 | /* This function is used to send logs. It try to send on screen (stderr) |
| 823 | * and on the default syslog server. |
| 824 | */ |
| 825 | static inline void hlua_sendlog(struct proxy *px, int level, const char *msg) |
| 826 | { |
| 827 | struct tm tm; |
| 828 | char *p; |
| 829 | |
| 830 | /* Cleanup the log message. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 831 | p = trash.area; |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 832 | for (; *msg != '\0'; msg++, p++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 833 | if (p >= trash.area + trash.size - 1) { |
Thierry FOURNIER | ccf0063 | 2015-09-16 12:47:03 +0200 | [diff] [blame] | 834 | /* Break the message if exceed the buffer size. */ |
| 835 | *(p-4) = ' '; |
| 836 | *(p-3) = '.'; |
| 837 | *(p-2) = '.'; |
| 838 | *(p-1) = '.'; |
| 839 | break; |
| 840 | } |
Willy Tarreau | 9080711 | 2020-02-25 08:16:33 +0100 | [diff] [blame] | 841 | if (isprint((unsigned char)*msg)) |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 842 | *p = *msg; |
| 843 | else |
| 844 | *p = '.'; |
| 845 | } |
| 846 | *p = '\0'; |
| 847 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 848 | send_log(px, level, "%s\n", trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 849 | if (!(global.mode & MODE_QUIET) || (global.mode & (MODE_VERBOSE | MODE_STARTING))) { |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 850 | get_localtime(date.tv_sec, &tm); |
| 851 | fprintf(stderr, "[%s] %03d/%02d%02d%02d (%d) : %s\n", |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 852 | log_levels[level], tm.tm_yday, tm.tm_hour, tm.tm_min, tm.tm_sec, |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 853 | (int)getpid(), trash.area); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 854 | fflush(stderr); |
| 855 | } |
| 856 | } |
| 857 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 858 | /* This function just ensure that the yield will be always |
| 859 | * returned with a timeout and permit to set some flags |
| 860 | */ |
| 861 | __LJMP void hlua_yieldk(lua_State *L, int nresults, int ctx, |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 862 | lua_KFunction k, int timeout, unsigned int flags) |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 863 | { |
| 864 | struct hlua *hlua = hlua_gethlua(L); |
| 865 | |
| 866 | /* Set the wake timeout. If timeout is required, we set |
| 867 | * the expiration time. |
| 868 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 869 | hlua->wake_time = timeout; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 870 | |
Thierry FOURNIER | 4abd3ae | 2015-03-03 17:29:06 +0100 | [diff] [blame] | 871 | hlua->flags |= flags; |
| 872 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 873 | /* Process the yield. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 874 | MAY_LJMP(lua_yieldk(L, nresults, ctx, k)); |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 875 | } |
| 876 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 877 | /* This function initialises the Lua environment stored in the stream. |
| 878 | * It must be called at the start of the stream. This function creates |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 879 | * an LUA coroutine. It can not be use to crete the main LUA context. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 880 | * |
| 881 | * This function is particular. it initialises a new Lua thread. If the |
| 882 | * initialisation fails (example: out of memory error), the lua function |
| 883 | * throws an error (longjmp). |
| 884 | * |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 885 | * In some case (at least one), this function can be called from safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 886 | * environment, so we must not initialise it. While the support of |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 887 | * threads appear, the safe environment set a lock to ensure only one |
| 888 | * Lua execution at a time. If we initialize safe environment in another |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 889 | * safe environment, we have a dead lock. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 890 | * |
| 891 | * set "already_safe" true if the context is initialized form safe |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 892 | * Lua function. |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 893 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 894 | * This function manipulates two Lua stacks: the main and the thread. Only |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 895 | * the main stack can fail. The thread is not manipulated. This function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 896 | * MUST NOT manipulate the created thread stack state, because it is not |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 897 | * protected against errors thrown by the thread stack. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 898 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 899 | int hlua_ctx_init(struct hlua *lua, struct task *task, int already_safe) |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 900 | { |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 901 | if (!already_safe) { |
| 902 | if (!SET_SAFE_LJMP(gL.T)) { |
| 903 | lua->Tref = LUA_REFNIL; |
| 904 | return 0; |
| 905 | } |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 906 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 907 | lua->Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 908 | lua->flags = 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 909 | lua->gc_count = 0; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 910 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 911 | LIST_INIT(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 912 | lua->T = lua_newthread(gL.T); |
| 913 | if (!lua->T) { |
| 914 | lua->Tref = LUA_REFNIL; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 915 | if (!already_safe) |
| 916 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 917 | return 0; |
| 918 | } |
| 919 | hlua_sethlua(lua); |
| 920 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 921 | lua->task = task; |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 922 | if (!already_safe) |
| 923 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 924 | return 1; |
| 925 | } |
| 926 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 927 | /* Used to destroy the Lua coroutine when the attached stream or task |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 928 | * is destroyed. The destroy also the memory context. The struct "lua" |
| 929 | * is not freed. |
| 930 | */ |
| 931 | void hlua_ctx_destroy(struct hlua *lua) |
| 932 | { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 933 | if (!lua) |
Thierry FOURNIER | a718b29 | 2015-03-04 16:48:34 +0100 | [diff] [blame] | 934 | return; |
| 935 | |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 936 | if (!lua->T) |
| 937 | goto end; |
| 938 | |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 939 | /* Purge all the pending signals. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 940 | notification_purge(&lua->com); |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 941 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 942 | if (!SET_SAFE_LJMP(lua->T)) |
| 943 | return; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 944 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 945 | RESET_SAFE_LJMP(lua->T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 946 | |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 947 | if (!SET_SAFE_LJMP(gL.T)) |
| 948 | return; |
| 949 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 950 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 951 | /* Forces a garbage collecting process. If the Lua program is finished |
| 952 | * without error, we run the GC on the thread pointer. Its freed all |
| 953 | * the unused memory. |
| 954 | * If the thread is finnish with an error or is currently yielded, |
| 955 | * it seems that the GC applied on the thread doesn't clean anything, |
| 956 | * so e run the GC on the main thread. |
| 957 | * NOTE: maybe this action locks all the Lua threads untiml the en of |
| 958 | * the garbage collection. |
| 959 | */ |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 960 | if (lua->gc_count) { |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 961 | if (!SET_SAFE_LJMP(gL.T)) |
Thierry FOURNIER | 75d0208 | 2017-07-12 13:41:33 +0200 | [diff] [blame] | 962 | return; |
Thierry FOURNIER | 7bd10d5 | 2017-07-17 00:44:40 +0200 | [diff] [blame] | 963 | lua_gc(gL.T, LUA_GCCOLLECT, 0); |
| 964 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 965 | } |
Thierry FOURNIER | 5a50a85 | 2015-09-23 16:59:28 +0200 | [diff] [blame] | 966 | |
Thierry FOURNIER | a7b536b | 2015-09-21 22:50:24 +0200 | [diff] [blame] | 967 | lua->T = NULL; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 968 | |
| 969 | end: |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 970 | pool_free(pool_head_hlua, lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 971 | } |
| 972 | |
| 973 | /* This function is used to restore the Lua context when a coroutine |
| 974 | * fails. This function copy the common memory between old coroutine |
| 975 | * and the new coroutine. The old coroutine is destroyed, and its |
| 976 | * replaced by the new coroutine. |
| 977 | * If the flag "keep_msg" is set, the last entry of the old is assumed |
| 978 | * as string error message and it is copied in the new stack. |
| 979 | */ |
| 980 | static int hlua_ctx_renew(struct hlua *lua, int keep_msg) |
| 981 | { |
| 982 | lua_State *T; |
| 983 | int new_ref; |
| 984 | |
| 985 | /* Renew the main LUA stack doesn't have sense. */ |
| 986 | if (lua == &gL) |
| 987 | return 0; |
| 988 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 989 | /* New Lua coroutine. */ |
| 990 | T = lua_newthread(gL.T); |
| 991 | if (!T) |
| 992 | return 0; |
| 993 | |
| 994 | /* Copy last error message. */ |
| 995 | if (keep_msg) |
| 996 | lua_xmove(lua->T, T, 1); |
| 997 | |
| 998 | /* Copy data between the coroutines. */ |
| 999 | lua_rawgeti(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1000 | lua_xmove(lua->T, T, 1); |
| 1001 | new_ref = luaL_ref(T, LUA_REGISTRYINDEX); /* Valur poped. */ |
| 1002 | |
| 1003 | /* Destroy old data. */ |
| 1004 | luaL_unref(lua->T, LUA_REGISTRYINDEX, lua->Mref); |
| 1005 | |
| 1006 | /* The thread is garbage collected by Lua. */ |
| 1007 | luaL_unref(gL.T, LUA_REGISTRYINDEX, lua->Tref); |
| 1008 | |
| 1009 | /* Fill the struct with the new coroutine values. */ |
| 1010 | lua->Mref = new_ref; |
| 1011 | lua->T = T; |
| 1012 | lua->Tref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 1013 | |
| 1014 | /* Set context. */ |
| 1015 | hlua_sethlua(lua); |
| 1016 | |
| 1017 | return 1; |
| 1018 | } |
| 1019 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1020 | void hlua_hook(lua_State *L, lua_Debug *ar) |
| 1021 | { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1022 | struct hlua *hlua = hlua_gethlua(L); |
| 1023 | |
| 1024 | /* Lua cannot yield when its returning from a function, |
| 1025 | * so, we can fix the interrupt hook to 1 instruction, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1026 | * expecting that the function is finished. |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1027 | */ |
| 1028 | if (lua_gethookmask(L) & LUA_MASKRET) { |
| 1029 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, 1); |
| 1030 | return; |
| 1031 | } |
| 1032 | |
| 1033 | /* restore the interrupt condition. */ |
| 1034 | lua_sethook(hlua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1035 | |
| 1036 | /* If we interrupt the Lua processing in yieldable state, we yield. |
| 1037 | * If the state is not yieldable, trying yield causes an error. |
| 1038 | */ |
| 1039 | if (lua_isyieldable(L)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1040 | MAY_LJMP(hlua_yieldk(L, 0, 0, NULL, TICK_ETERNITY, HLUA_CTRLYIELD)); |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1041 | |
Thierry FOURNIER | a85cfb1 | 2015-03-13 14:50:06 +0100 | [diff] [blame] | 1042 | /* If we cannot yield, update the clock and check the timeout. */ |
| 1043 | tv_update_date(0, 1); |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1044 | hlua->run_time += now_ms - hlua->start_time; |
| 1045 | if (hlua->max_time && hlua->run_time >= hlua->max_time) { |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1046 | lua_pushfstring(L, "execution timeout"); |
| 1047 | WILL_LJMP(lua_error(L)); |
| 1048 | } |
| 1049 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1050 | /* Update the start time. */ |
| 1051 | hlua->start_time = now_ms; |
| 1052 | |
Thierry FOURNIER | cae49c9 | 2015-03-06 14:05:24 +0100 | [diff] [blame] | 1053 | /* Try to interrupt the process at the end of the current |
| 1054 | * unyieldable function. |
| 1055 | */ |
| 1056 | lua_sethook(hlua->T, hlua_hook, LUA_MASKRET|LUA_MASKCOUNT, hlua_nb_instruction); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1057 | } |
| 1058 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1059 | /* This function start or resumes the Lua stack execution. If the flag |
| 1060 | * "yield_allowed" if no set and the LUA stack execution returns a yield |
| 1061 | * The function return an error. |
| 1062 | * |
| 1063 | * The function can returns 4 values: |
| 1064 | * - HLUA_E_OK : The execution is terminated without any errors. |
| 1065 | * - HLUA_E_AGAIN : The execution must continue at the next associated |
| 1066 | * task wakeup. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1067 | * - HLUA_E_ERRMSG : An error has occurred, an error message is set in |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1068 | * the top of the stack. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1069 | * - HLUA_E_ERR : An error has occurred without error message. |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1070 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1071 | * If an error occurred, the stack is renewed and it is ready to run new |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1072 | * LUA code. |
| 1073 | */ |
| 1074 | static enum hlua_exec hlua_ctx_resume(struct hlua *lua, int yield_allowed) |
| 1075 | { |
| 1076 | int ret; |
| 1077 | const char *msg; |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1078 | const char *trace; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1079 | |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1080 | /* Initialise run time counter. */ |
| 1081 | if (!HLUA_IS_RUNNING(lua)) |
| 1082 | lua->run_time = 0; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1083 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1084 | /* Lock the whole Lua execution. This lock must be before the |
| 1085 | * label "resume_execution". |
| 1086 | */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1087 | HA_SPIN_LOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1088 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1089 | resume_execution: |
| 1090 | |
| 1091 | /* This hook interrupts the Lua processing each 'hlua_nb_instruction' |
| 1092 | * instructions. it is used for preventing infinite loops. |
| 1093 | */ |
| 1094 | lua_sethook(lua->T, hlua_hook, LUA_MASKCOUNT, hlua_nb_instruction); |
| 1095 | |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1096 | /* Remove all flags except the running flags. */ |
Thierry FOURNIER | 2f3867f | 2015-09-28 01:02:01 +0200 | [diff] [blame] | 1097 | HLUA_SET_RUN(lua); |
| 1098 | HLUA_CLR_CTRLYIELD(lua); |
| 1099 | HLUA_CLR_WAKERESWR(lua); |
| 1100 | HLUA_CLR_WAKEREQWR(lua); |
Thierry FOURNIER | 1bfc09b | 2015-03-05 17:10:14 +0100 | [diff] [blame] | 1101 | |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1102 | /* Update the start time and reset wake_time. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1103 | lua->start_time = now_ms; |
Christopher Faulet | bc275a9 | 2020-02-26 14:55:16 +0100 | [diff] [blame] | 1104 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1105 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1106 | /* Call the function. */ |
| 1107 | ret = lua_resume(lua->T, gL.T, lua->nargs); |
| 1108 | switch (ret) { |
| 1109 | |
| 1110 | case LUA_OK: |
| 1111 | ret = HLUA_E_OK; |
| 1112 | break; |
| 1113 | |
| 1114 | case LUA_YIELD: |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1115 | /* Check if the execution timeout is expired. It it is the case, we |
| 1116 | * break the Lua execution. |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1117 | */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 1118 | tv_update_date(0, 1); |
| 1119 | lua->run_time += now_ms - lua->start_time; |
| 1120 | if (lua->max_time && lua->run_time > lua->max_time) { |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1121 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1122 | ret = HLUA_E_ETMOUT; |
Thierry FOURNIER | dc9ca96 | 2015-03-05 11:16:52 +0100 | [diff] [blame] | 1123 | break; |
| 1124 | } |
| 1125 | /* Process the forced yield. if the general yield is not allowed or |
| 1126 | * if no task were associated this the current Lua execution |
| 1127 | * coroutine, we resume the execution. Else we want to return in the |
| 1128 | * scheduler and we want to be waked up again, to continue the |
| 1129 | * current Lua execution. So we schedule our own task. |
| 1130 | */ |
| 1131 | if (HLUA_IS_CTRLYIELDING(lua)) { |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1132 | if (!yield_allowed || !lua->task) |
| 1133 | goto resume_execution; |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1134 | task_wakeup(lua->task, TASK_WOKEN_MSG); |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 1135 | } |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1136 | if (!yield_allowed) { |
| 1137 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1138 | ret = HLUA_E_YIELD; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1139 | break; |
| 1140 | } |
| 1141 | ret = HLUA_E_AGAIN; |
| 1142 | break; |
| 1143 | |
| 1144 | case LUA_ERRRUN: |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1145 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1146 | /* Special exit case. The traditional exit is returned as an error |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1147 | * because the errors ares the only one mean to return immediately |
| 1148 | * from and lua execution. |
| 1149 | */ |
| 1150 | if (lua->flags & HLUA_EXIT) { |
| 1151 | ret = HLUA_E_OK; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 1152 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1153 | break; |
| 1154 | } |
| 1155 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1156 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1157 | if (!lua_checkstack(lua->T, 1)) { |
| 1158 | ret = HLUA_E_ERR; |
| 1159 | break; |
| 1160 | } |
| 1161 | msg = lua_tostring(lua->T, -1); |
| 1162 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1163 | lua_pop(lua->T, 1); |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1164 | trace = hlua_traceback(lua->T); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1165 | if (msg) |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1166 | lua_pushfstring(lua->T, "runtime error: %s from %s", msg, trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1167 | else |
Thierry FOURNIER | fc044c9 | 2018-06-07 14:40:48 +0200 | [diff] [blame] | 1168 | lua_pushfstring(lua->T, "unknown runtime error from %s", trace); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1169 | ret = HLUA_E_ERRMSG; |
| 1170 | break; |
| 1171 | |
| 1172 | case LUA_ERRMEM: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1173 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1174 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1175 | ret = HLUA_E_NOMEM; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1176 | break; |
| 1177 | |
| 1178 | case LUA_ERRERR: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1179 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1180 | if (!lua_checkstack(lua->T, 1)) { |
| 1181 | ret = HLUA_E_ERR; |
| 1182 | break; |
| 1183 | } |
| 1184 | msg = lua_tostring(lua->T, -1); |
| 1185 | lua_settop(lua->T, 0); /* Empty the stack. */ |
| 1186 | lua_pop(lua->T, 1); |
| 1187 | if (msg) |
| 1188 | lua_pushfstring(lua->T, "message handler error: %s", msg); |
| 1189 | else |
| 1190 | lua_pushfstring(lua->T, "message handler error"); |
| 1191 | ret = HLUA_E_ERRMSG; |
| 1192 | break; |
| 1193 | |
| 1194 | default: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 1195 | lua->wake_time = TICK_ETERNITY; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1196 | lua_settop(lua->T, 0); /* Empty the stack. */ |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1197 | ret = HLUA_E_ERR; |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1198 | break; |
| 1199 | } |
| 1200 | |
| 1201 | switch (ret) { |
| 1202 | case HLUA_E_AGAIN: |
| 1203 | break; |
| 1204 | |
| 1205 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1206 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1207 | hlua_ctx_renew(lua, 1); |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1208 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1209 | break; |
| 1210 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 1211 | case HLUA_E_ETMOUT: |
| 1212 | case HLUA_E_NOMEM: |
| 1213 | case HLUA_E_YIELD: |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1214 | case HLUA_E_ERR: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1215 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1216 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1217 | hlua_ctx_renew(lua, 0); |
| 1218 | break; |
| 1219 | |
| 1220 | case HLUA_E_OK: |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 1221 | HLUA_CLR_RUN(lua); |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1222 | notification_purge(&lua->com); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1223 | break; |
| 1224 | } |
| 1225 | |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1226 | /* This is the main exit point, remove the Lua lock. */ |
Christopher Faulet | 2a944ee | 2017-11-07 10:42:54 +0100 | [diff] [blame] | 1227 | HA_SPIN_UNLOCK(LUA_LOCK, &hlua_global_lock); |
Thierry FOURNIER | 61ba0e2 | 2017-07-12 11:41:21 +0200 | [diff] [blame] | 1228 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 1229 | return ret; |
| 1230 | } |
| 1231 | |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 1232 | /* This function exit the current code. */ |
| 1233 | __LJMP static int hlua_done(lua_State *L) |
| 1234 | { |
| 1235 | struct hlua *hlua = hlua_gethlua(L); |
| 1236 | |
| 1237 | hlua->flags |= HLUA_EXIT; |
| 1238 | WILL_LJMP(lua_error(L)); |
| 1239 | |
| 1240 | return 0; |
| 1241 | } |
| 1242 | |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1243 | /* This function is an LUA binding. It provides a function |
| 1244 | * for deleting ACL from a referenced ACL file. |
| 1245 | */ |
| 1246 | __LJMP static int hlua_del_acl(lua_State *L) |
| 1247 | { |
| 1248 | const char *name; |
| 1249 | const char *key; |
| 1250 | struct pat_ref *ref; |
| 1251 | |
| 1252 | MAY_LJMP(check_args(L, 2, "del_acl")); |
| 1253 | |
| 1254 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1255 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1256 | |
| 1257 | ref = pat_ref_lookup(name); |
| 1258 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1259 | WILL_LJMP(luaL_error(L, "'del_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1260 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1261 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1262 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1263 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1264 | return 0; |
| 1265 | } |
| 1266 | |
| 1267 | /* This function is an LUA binding. It provides a function |
| 1268 | * for deleting map entry from a referenced map file. |
| 1269 | */ |
| 1270 | static int hlua_del_map(lua_State *L) |
| 1271 | { |
| 1272 | const char *name; |
| 1273 | const char *key; |
| 1274 | struct pat_ref *ref; |
| 1275 | |
| 1276 | MAY_LJMP(check_args(L, 2, "del_map")); |
| 1277 | |
| 1278 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1279 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1280 | |
| 1281 | ref = pat_ref_lookup(name); |
| 1282 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1283 | WILL_LJMP(luaL_error(L, "'del_map': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1284 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1285 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1286 | pat_ref_delete(ref, key); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1287 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1288 | return 0; |
| 1289 | } |
| 1290 | |
| 1291 | /* This function is an LUA binding. It provides a function |
| 1292 | * for adding ACL pattern from a referenced ACL file. |
| 1293 | */ |
| 1294 | static int hlua_add_acl(lua_State *L) |
| 1295 | { |
| 1296 | const char *name; |
| 1297 | const char *key; |
| 1298 | struct pat_ref *ref; |
| 1299 | |
| 1300 | MAY_LJMP(check_args(L, 2, "add_acl")); |
| 1301 | |
| 1302 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1303 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1304 | |
| 1305 | ref = pat_ref_lookup(name); |
| 1306 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1307 | WILL_LJMP(luaL_error(L, "'add_acl': unknown acl file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1308 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1309 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1310 | if (pat_ref_find_elt(ref, key) == NULL) |
| 1311 | pat_ref_add(ref, key, NULL, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1312 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1313 | return 0; |
| 1314 | } |
| 1315 | |
| 1316 | /* This function is an LUA binding. It provides a function |
| 1317 | * for setting map pattern and sample from a referenced map |
| 1318 | * file. |
| 1319 | */ |
| 1320 | static int hlua_set_map(lua_State *L) |
| 1321 | { |
| 1322 | const char *name; |
| 1323 | const char *key; |
| 1324 | const char *value; |
| 1325 | struct pat_ref *ref; |
| 1326 | |
| 1327 | MAY_LJMP(check_args(L, 3, "set_map")); |
| 1328 | |
| 1329 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1330 | key = MAY_LJMP(luaL_checkstring(L, 2)); |
| 1331 | value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 1332 | |
| 1333 | ref = pat_ref_lookup(name); |
| 1334 | if (!ref) |
Vincent Bernat | a72db18 | 2015-10-06 16:05:59 +0200 | [diff] [blame] | 1335 | WILL_LJMP(luaL_error(L, "'set_map': unknown map file '%s'", name)); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1336 | |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1337 | HA_SPIN_LOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1338 | if (pat_ref_find_elt(ref, key) != NULL) |
| 1339 | pat_ref_set(ref, key, value, NULL); |
| 1340 | else |
| 1341 | pat_ref_add(ref, key, value, NULL); |
Christopher Faulet | 5cf2dfc | 2020-06-03 18:39:16 +0200 | [diff] [blame] | 1342 | HA_SPIN_UNLOCK(PATREF_LOCK, &ref->lock); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 1343 | return 0; |
| 1344 | } |
| 1345 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1346 | /* A class is a lot of memory that contain data. This data can be a table, |
| 1347 | * an integer or user data. This data is associated with a metatable. This |
Ilya Shipitsin | c02a23f | 2020-05-06 00:53:22 +0500 | [diff] [blame] | 1348 | * metatable have an original version registered in the global context with |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1349 | * the name of the object (_G[<name>] = <metable> ). |
| 1350 | * |
| 1351 | * A metable is a table that modify the standard behavior of a standard |
| 1352 | * access to the associated data. The entries of this new metatable are |
| 1353 | * defined as is: |
| 1354 | * |
| 1355 | * http://lua-users.org/wiki/MetatableEvents |
| 1356 | * |
| 1357 | * __index |
| 1358 | * |
| 1359 | * we access an absent field in a table, the result is nil. This is |
| 1360 | * true, but it is not the whole truth. Actually, such access triggers |
| 1361 | * the interpreter to look for an __index metamethod: If there is no |
| 1362 | * such method, as usually happens, then the access results in nil; |
| 1363 | * otherwise, the metamethod will provide the result. |
| 1364 | * |
| 1365 | * Control 'prototype' inheritance. When accessing "myTable[key]" and |
| 1366 | * the key does not appear in the table, but the metatable has an __index |
| 1367 | * property: |
| 1368 | * |
| 1369 | * - if the value is a function, the function is called, passing in the |
| 1370 | * table and the key; the return value of that function is returned as |
| 1371 | * the result. |
| 1372 | * |
| 1373 | * - if the value is another table, the value of the key in that table is |
| 1374 | * asked for and returned (and if it doesn't exist in that table, but that |
| 1375 | * table's metatable has an __index property, then it continues on up) |
| 1376 | * |
| 1377 | * - Use "rawget(myTable,key)" to skip this metamethod. |
| 1378 | * |
| 1379 | * http://www.lua.org/pil/13.4.1.html |
| 1380 | * |
| 1381 | * __newindex |
| 1382 | * |
| 1383 | * Like __index, but control property assignment. |
| 1384 | * |
| 1385 | * __mode - Control weak references. A string value with one or both |
| 1386 | * of the characters 'k' and 'v' which specifies that the the |
| 1387 | * keys and/or values in the table are weak references. |
| 1388 | * |
| 1389 | * __call - Treat a table like a function. When a table is followed by |
| 1390 | * parenthesis such as "myTable( 'foo' )" and the metatable has |
| 1391 | * a __call key pointing to a function, that function is invoked |
| 1392 | * (passing any specified arguments) and the return value is |
| 1393 | * returned. |
| 1394 | * |
| 1395 | * __metatable - Hide the metatable. When "getmetatable( myTable )" is |
| 1396 | * called, if the metatable for myTable has a __metatable |
| 1397 | * key, the value of that key is returned instead of the |
| 1398 | * actual metatable. |
| 1399 | * |
| 1400 | * __tostring - Control string representation. When the builtin |
| 1401 | * "tostring( myTable )" function is called, if the metatable |
| 1402 | * for myTable has a __tostring property set to a function, |
| 1403 | * that function is invoked (passing myTable to it) and the |
| 1404 | * return value is used as the string representation. |
| 1405 | * |
| 1406 | * __len - Control table length. When the table length is requested using |
| 1407 | * the length operator ( '#' ), if the metatable for myTable has |
| 1408 | * a __len key pointing to a function, that function is invoked |
| 1409 | * (passing myTable to it) and the return value used as the value |
| 1410 | * of "#myTable". |
| 1411 | * |
| 1412 | * __gc - Userdata finalizer code. When userdata is set to be garbage |
| 1413 | * collected, if the metatable has a __gc field pointing to a |
| 1414 | * function, that function is first invoked, passing the userdata |
| 1415 | * to it. The __gc metamethod is not called for tables. |
| 1416 | * (See http://lua-users.org/lists/lua-l/2006-11/msg00508.html) |
| 1417 | * |
| 1418 | * Special metamethods for redefining standard operators: |
| 1419 | * http://www.lua.org/pil/13.1.html |
| 1420 | * |
| 1421 | * __add "+" |
| 1422 | * __sub "-" |
| 1423 | * __mul "*" |
| 1424 | * __div "/" |
| 1425 | * __unm "!" |
| 1426 | * __pow "^" |
| 1427 | * __concat ".." |
| 1428 | * |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 1429 | * Special methods for redefining standard relations |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 1430 | * http://www.lua.org/pil/13.2.html |
| 1431 | * |
| 1432 | * __eq "==" |
| 1433 | * __lt "<" |
| 1434 | * __le "<=" |
| 1435 | */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1436 | |
| 1437 | /* |
| 1438 | * |
| 1439 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1440 | * Class Map |
| 1441 | * |
| 1442 | * |
| 1443 | */ |
| 1444 | |
| 1445 | /* Returns a struct hlua_map if the stack entry "ud" is |
| 1446 | * a class session, otherwise it throws an error. |
| 1447 | */ |
| 1448 | __LJMP static struct map_descriptor *hlua_checkmap(lua_State *L, int ud) |
| 1449 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1450 | return MAY_LJMP(hlua_checkudata(L, ud, class_map_ref)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1451 | } |
| 1452 | |
| 1453 | /* This function is the map constructor. It don't need |
| 1454 | * the class Map object. It creates and return a new Map |
| 1455 | * object. It must be called only during "body" or "init" |
| 1456 | * context because it process some filesystem accesses. |
| 1457 | */ |
| 1458 | __LJMP static int hlua_map_new(struct lua_State *L) |
| 1459 | { |
| 1460 | const char *fn; |
| 1461 | int match = PAT_MATCH_STR; |
| 1462 | struct sample_conv conv; |
| 1463 | const char *file = ""; |
| 1464 | int line = 0; |
| 1465 | lua_Debug ar; |
| 1466 | char *err = NULL; |
| 1467 | struct arg args[2]; |
| 1468 | |
| 1469 | if (lua_gettop(L) < 1 || lua_gettop(L) > 2) |
| 1470 | WILL_LJMP(luaL_error(L, "'new' needs at least 1 argument.")); |
| 1471 | |
| 1472 | fn = MAY_LJMP(luaL_checkstring(L, 1)); |
| 1473 | |
| 1474 | if (lua_gettop(L) >= 2) { |
| 1475 | match = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 1476 | if (match < 0 || match >= PAT_MATCH_NUM) |
| 1477 | WILL_LJMP(luaL_error(L, "'new' needs a valid match method.")); |
| 1478 | } |
| 1479 | |
| 1480 | /* Get Lua filename and line number. */ |
| 1481 | if (lua_getstack(L, 1, &ar)) { /* check function at level */ |
| 1482 | lua_getinfo(L, "Sl", &ar); /* get info about it */ |
| 1483 | if (ar.currentline > 0) { /* is there info? */ |
| 1484 | file = ar.short_src; |
| 1485 | line = ar.currentline; |
| 1486 | } |
| 1487 | } |
| 1488 | |
| 1489 | /* fill fake sample_conv struct. */ |
| 1490 | conv.kw = ""; /* unused. */ |
| 1491 | conv.process = NULL; /* unused. */ |
| 1492 | conv.arg_mask = 0; /* unused. */ |
| 1493 | conv.val_args = NULL; /* unused. */ |
| 1494 | conv.out_type = SMP_T_STR; |
| 1495 | conv.private = (void *)(long)match; |
| 1496 | switch (match) { |
| 1497 | case PAT_MATCH_STR: conv.in_type = SMP_T_STR; break; |
| 1498 | case PAT_MATCH_BEG: conv.in_type = SMP_T_STR; break; |
| 1499 | case PAT_MATCH_SUB: conv.in_type = SMP_T_STR; break; |
| 1500 | case PAT_MATCH_DIR: conv.in_type = SMP_T_STR; break; |
| 1501 | case PAT_MATCH_DOM: conv.in_type = SMP_T_STR; break; |
| 1502 | case PAT_MATCH_END: conv.in_type = SMP_T_STR; break; |
| 1503 | case PAT_MATCH_REG: conv.in_type = SMP_T_STR; break; |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1504 | case PAT_MATCH_INT: conv.in_type = SMP_T_SINT; break; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1505 | case PAT_MATCH_IP: conv.in_type = SMP_T_ADDR; break; |
| 1506 | default: |
| 1507 | WILL_LJMP(luaL_error(L, "'new' doesn't support this match mode.")); |
| 1508 | } |
| 1509 | |
| 1510 | /* fill fake args. */ |
| 1511 | args[0].type = ARGT_STR; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1512 | args[0].data.str.area = (char *)fn; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1513 | args[1].type = ARGT_STOP; |
| 1514 | |
| 1515 | /* load the map. */ |
| 1516 | if (!sample_load_map(args, &conv, file, line, &err)) { |
| 1517 | /* error case: we cant use luaL_error because we must |
| 1518 | * free the err variable. |
| 1519 | */ |
| 1520 | luaL_where(L, 1); |
| 1521 | lua_pushfstring(L, "'new': %s.", err); |
| 1522 | lua_concat(L, 2); |
| 1523 | free(err); |
| 1524 | WILL_LJMP(lua_error(L)); |
| 1525 | } |
| 1526 | |
| 1527 | /* create the lua object. */ |
| 1528 | lua_newtable(L); |
| 1529 | lua_pushlightuserdata(L, args[0].data.map); |
| 1530 | lua_rawseti(L, -2, 0); |
| 1531 | |
| 1532 | /* Pop a class Map metatable and affect it to the userdata. */ |
| 1533 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_map_ref); |
| 1534 | lua_setmetatable(L, -2); |
| 1535 | |
| 1536 | |
| 1537 | return 1; |
| 1538 | } |
| 1539 | |
| 1540 | __LJMP static inline int _hlua_map_lookup(struct lua_State *L, int str) |
| 1541 | { |
| 1542 | struct map_descriptor *desc; |
| 1543 | struct pattern *pat; |
| 1544 | struct sample smp; |
| 1545 | |
| 1546 | MAY_LJMP(check_args(L, 2, "lookup")); |
| 1547 | desc = MAY_LJMP(hlua_checkmap(L, 1)); |
Thierry FOURNIER | 07ee64e | 2015-07-06 23:43:03 +0200 | [diff] [blame] | 1548 | if (desc->pat.expect_type == SMP_T_SINT) { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1549 | smp.data.type = SMP_T_SINT; |
Thierry FOURNIER | 136f9d3 | 2015-08-19 09:07:19 +0200 | [diff] [blame] | 1550 | smp.data.u.sint = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1551 | } |
| 1552 | else { |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 1553 | smp.data.type = SMP_T_STR; |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1554 | smp.flags = SMP_F_CONST; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 1555 | 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] | 1556 | } |
| 1557 | |
| 1558 | pat = pattern_exec_match(&desc->pat, &smp, 1); |
Thierry FOURNIER | 503bb09 | 2015-08-19 08:35:43 +0200 | [diff] [blame] | 1559 | if (!pat || !pat->data) { |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 1560 | if (str) |
| 1561 | lua_pushstring(L, ""); |
| 1562 | else |
| 1563 | lua_pushnil(L); |
| 1564 | return 1; |
| 1565 | } |
| 1566 | |
| 1567 | /* 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] | 1568 | 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] | 1569 | return 1; |
| 1570 | } |
| 1571 | |
| 1572 | __LJMP static int hlua_map_lookup(struct lua_State *L) |
| 1573 | { |
| 1574 | return _hlua_map_lookup(L, 0); |
| 1575 | } |
| 1576 | |
| 1577 | __LJMP static int hlua_map_slookup(struct lua_State *L) |
| 1578 | { |
| 1579 | return _hlua_map_lookup(L, 1); |
| 1580 | } |
| 1581 | |
| 1582 | /* |
| 1583 | * |
| 1584 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1585 | * Class Socket |
| 1586 | * |
| 1587 | * |
| 1588 | */ |
| 1589 | |
| 1590 | __LJMP static struct hlua_socket *hlua_checksocket(lua_State *L, int ud) |
| 1591 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 1592 | return MAY_LJMP(hlua_checkudata(L, ud, class_socket_ref)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1593 | } |
| 1594 | |
| 1595 | /* 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] | 1596 | * connection. It is used for notify space available to send or data |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1597 | * received. |
| 1598 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1599 | static void hlua_socket_handler(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1600 | { |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1601 | struct stream_interface *si = appctx->owner; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1602 | |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1603 | if (appctx->ctx.hlua_cosocket.die) { |
| 1604 | si_shutw(si); |
| 1605 | si_shutr(si); |
| 1606 | si_ic(si)->flags |= CF_READ_NULL; |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1607 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1608 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1609 | stream_shutdown(si_strm(si), SF_ERR_KILLED); |
| 1610 | } |
| 1611 | |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1612 | /* If we cant write, wakeup the pending write signals. */ |
| 1613 | if (channel_output_closed(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1614 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1615 | |
| 1616 | /* If we cant read, wakeup the pending read signals. */ |
| 1617 | if (channel_input_closed(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1618 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 6d695e6 | 2015-09-27 19:29:38 +0200 | [diff] [blame] | 1619 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1620 | /* if the connection is not established, inform the stream that we want |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1621 | * to be notified whenever the connection completes. |
| 1622 | */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 1623 | if (si_opposite(si)->state < SI_ST_EST) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 1624 | si_cant_get(si); |
Willy Tarreau | 12c2423 | 2018-12-06 15:29:50 +0100 | [diff] [blame] | 1625 | si_rx_conn_blk(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1626 | si_rx_endp_more(si); |
Willy Tarreau | d4da196 | 2015-04-20 01:31:23 +0200 | [diff] [blame] | 1627 | return; |
Thierry FOURNIER | 316e319 | 2015-09-04 18:25:53 +0200 | [diff] [blame] | 1628 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1629 | |
| 1630 | /* This function is called after the connect. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 1631 | appctx->ctx.hlua_cosocket.connected = 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1632 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1633 | /* Wake the tasks which wants to write if the buffer have available space. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1634 | if (channel_may_recv(si_ic(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1635 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1636 | |
| 1637 | /* Wake the tasks which wants to read if the buffer contains data. */ |
Thierry FOURNIER | eba6f64 | 2015-09-26 22:01:07 +0200 | [diff] [blame] | 1638 | if (!channel_is_empty(si_oc(si))) |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1639 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1640 | |
| 1641 | /* Some data were injected in the buffer, notify the stream |
| 1642 | * interface. |
| 1643 | */ |
| 1644 | if (!channel_is_empty(si_ic(si))) |
Willy Tarreau | 14bfe9a | 2018-12-19 15:19:27 +0100 | [diff] [blame] | 1645 | si_update(si); |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1646 | |
| 1647 | /* If write notifications are registered, we considers we want |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1648 | * to write, so we clear the blocking flag. |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 1649 | */ |
| 1650 | if (notification_registered(&appctx->ctx.hlua_cosocket.wake_on_write)) |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 1651 | si_rx_endp_more(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1652 | } |
| 1653 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1654 | /* This function is called when the "struct stream" is destroyed. |
| 1655 | * Remove the link from the object to this stream. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1656 | * Wake all the pending signals. |
| 1657 | */ |
Willy Tarreau | 00a37f0 | 2015-04-13 12:05:19 +0200 | [diff] [blame] | 1658 | static void hlua_socket_release(struct appctx *appctx) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1659 | { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1660 | struct xref *peer; |
| 1661 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1662 | /* Remove my link in the original object. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1663 | peer = xref_get_peer_and_lock(&appctx->ctx.hlua_cosocket.xref); |
| 1664 | if (peer) |
| 1665 | xref_disconnect(&appctx->ctx.hlua_cosocket.xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1666 | |
| 1667 | /* Wake all the task waiting for me. */ |
Thierry FOURNIER | d697596 | 2017-07-12 14:31:10 +0200 | [diff] [blame] | 1668 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_read); |
| 1669 | notification_wake(&appctx->ctx.hlua_cosocket.wake_on_write); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1670 | } |
| 1671 | |
| 1672 | /* If the garbage collectio of the object is launch, nobody |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1673 | * uses this object. If the stream does not exists, just quit. |
| 1674 | * Send the shutdown signal to the stream. In some cases, |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1675 | * pending signal can rest in the read and write lists. destroy |
| 1676 | * it. |
| 1677 | */ |
| 1678 | __LJMP static int hlua_socket_gc(lua_State *L) |
| 1679 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1680 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1681 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1682 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1683 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1684 | MAY_LJMP(check_args(L, 1, "__gc")); |
| 1685 | |
| 1686 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1687 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1688 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1689 | return 0; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1690 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1691 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1692 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1693 | appctx->ctx.hlua_cosocket.die = 1; |
| 1694 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1695 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1696 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1697 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1698 | return 0; |
| 1699 | } |
| 1700 | |
| 1701 | /* The close function send shutdown signal and break the |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 1702 | * links between the stream and the object. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1703 | */ |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1704 | __LJMP static int hlua_socket_close_helper(lua_State *L) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1705 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1706 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1707 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1708 | struct xref *peer; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1709 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1710 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1711 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1712 | |
| 1713 | /* Check if we run on the same thread than the xreator thread. |
| 1714 | * We cannot access to the socket if the thread is different. |
| 1715 | */ |
| 1716 | if (socket->tid != tid) |
| 1717 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1718 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1719 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1720 | if (!peer) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1721 | return 0; |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 1722 | |
| 1723 | hlua->gc_count--; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1724 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1725 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1726 | /* Set the flag which destroy the session. */ |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 1727 | appctx->ctx.hlua_cosocket.die = 1; |
| 1728 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1729 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1730 | /* Remove all reference between the Lua stack and the coroutine stream. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1731 | xref_disconnect(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1732 | return 0; |
| 1733 | } |
| 1734 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 1735 | /* The close function calls close_helper. |
| 1736 | */ |
| 1737 | __LJMP static int hlua_socket_close(lua_State *L) |
| 1738 | { |
| 1739 | MAY_LJMP(check_args(L, 1, "close")); |
| 1740 | return hlua_socket_close_helper(L); |
| 1741 | } |
| 1742 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1743 | /* This Lua function assumes that the stack contain three parameters. |
| 1744 | * 1 - USERDATA containing a struct socket |
| 1745 | * 2 - INTEGER with values of the macro defined below |
| 1746 | * If the integer is -1, we must read at most one line. |
| 1747 | * If the integer is -2, we ust read all the data until the |
| 1748 | * end of the stream. |
| 1749 | * If the integer is positive value, we must read a number of |
| 1750 | * bytes corresponding to this value. |
| 1751 | */ |
| 1752 | #define HLSR_READ_LINE (-1) |
| 1753 | #define HLSR_READ_ALL (-2) |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1754 | __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] | 1755 | { |
| 1756 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 1757 | int wanted = lua_tointeger(L, 2); |
| 1758 | struct hlua *hlua = hlua_gethlua(L); |
| 1759 | struct appctx *appctx; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1760 | size_t len; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1761 | int nblk; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1762 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1763 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 1764 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 1765 | size_t len2; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1766 | int skip_at_end = 0; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 1767 | struct channel *oc; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1768 | struct stream_interface *si; |
| 1769 | struct stream *s; |
| 1770 | struct xref *peer; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1771 | int missing_bytes; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1772 | |
| 1773 | /* Check if this lua stack is schedulable. */ |
| 1774 | if (!hlua || !hlua->task) |
| 1775 | WILL_LJMP(luaL_error(L, "The 'receive' function is only allowed in " |
| 1776 | "'frontend', 'backend' or 'task'")); |
| 1777 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1778 | /* Check if we run on the same thread than the xreator thread. |
| 1779 | * We cannot access to the socket if the thread is different. |
| 1780 | */ |
| 1781 | if (socket->tid != tid) |
| 1782 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1783 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1784 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1785 | peer = xref_get_peer_and_lock(&socket->xref); |
| 1786 | if (!peer) |
| 1787 | goto no_peer; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1788 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 1789 | si = appctx->owner; |
| 1790 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1791 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1792 | oc = &s->res; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1793 | if (wanted == HLSR_READ_LINE) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1794 | /* Read line. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1795 | nblk = co_getline_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1796 | if (nblk < 0) /* Connection close. */ |
| 1797 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1798 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1799 | goto connection_empty; |
Thierry FOURNIER | 0054392 | 2015-03-09 18:35:06 +0100 | [diff] [blame] | 1800 | |
| 1801 | /* remove final \r\n. */ |
| 1802 | if (nblk == 1) { |
| 1803 | if (blk1[len1-1] == '\n') { |
| 1804 | len1--; |
| 1805 | skip_at_end++; |
| 1806 | if (blk1[len1-1] == '\r') { |
| 1807 | len1--; |
| 1808 | skip_at_end++; |
| 1809 | } |
| 1810 | } |
| 1811 | } |
| 1812 | else { |
| 1813 | if (blk2[len2-1] == '\n') { |
| 1814 | len2--; |
| 1815 | skip_at_end++; |
| 1816 | if (blk2[len2-1] == '\r') { |
| 1817 | len2--; |
| 1818 | skip_at_end++; |
| 1819 | } |
| 1820 | } |
| 1821 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1822 | } |
| 1823 | |
| 1824 | else if (wanted == HLSR_READ_ALL) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1825 | /* Read all the available data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1826 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1827 | if (nblk < 0) /* Connection close. */ |
| 1828 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1829 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1830 | goto connection_empty; |
| 1831 | } |
| 1832 | |
| 1833 | else { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1834 | /* Read a block of data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1835 | nblk = co_getblk_nc(oc, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1836 | if (nblk < 0) /* Connection close. */ |
| 1837 | goto connection_closed; |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 1838 | if (nblk == 0) /* No data available. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1839 | goto connection_empty; |
| 1840 | |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1841 | missing_bytes = wanted - socket->b.n; |
| 1842 | if (len1 > missing_bytes) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1843 | nblk = 1; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1844 | len1 = missing_bytes; |
| 1845 | } if (nblk == 2 && len1 + len2 > missing_bytes) |
| 1846 | len2 = missing_bytes - len1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1847 | } |
| 1848 | |
| 1849 | len = len1; |
| 1850 | |
| 1851 | luaL_addlstring(&socket->b, blk1, len1); |
| 1852 | if (nblk == 2) { |
| 1853 | len += len2; |
| 1854 | luaL_addlstring(&socket->b, blk2, len2); |
| 1855 | } |
| 1856 | |
| 1857 | /* Consume data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 1858 | co_skip(oc, len + skip_at_end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1859 | |
| 1860 | /* Don't wait anything. */ |
Thierry FOURNIER | 7e4ee47 | 2018-05-25 15:03:50 +0200 | [diff] [blame] | 1861 | appctx_wakeup(appctx); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1862 | |
| 1863 | /* If the pattern reclaim to read all the data |
| 1864 | * in the connection, got out. |
| 1865 | */ |
| 1866 | if (wanted == HLSR_READ_ALL) |
| 1867 | goto connection_empty; |
Thierry FOURNIER | 8c126c7 | 2018-05-25 16:27:44 +0200 | [diff] [blame] | 1868 | else if (wanted >= 0 && socket->b.n < wanted) |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1869 | goto connection_empty; |
| 1870 | |
| 1871 | /* Return result. */ |
| 1872 | luaL_pushresult(&socket->b); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1873 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1874 | return 1; |
| 1875 | |
| 1876 | connection_closed: |
| 1877 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1878 | xref_unlock(&socket->xref, peer); |
| 1879 | |
| 1880 | no_peer: |
| 1881 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1882 | /* If the buffer containds data. */ |
| 1883 | if (socket->b.n > 0) { |
| 1884 | luaL_pushresult(&socket->b); |
| 1885 | return 1; |
| 1886 | } |
| 1887 | lua_pushnil(L); |
| 1888 | lua_pushstring(L, "connection closed."); |
| 1889 | return 2; |
| 1890 | |
| 1891 | connection_empty: |
| 1892 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1893 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_read, hlua->task)) { |
| 1894 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1895 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 1896 | } |
| 1897 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 1898 | 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] | 1899 | return 0; |
| 1900 | } |
| 1901 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1902 | /* This Lua function gets two parameters. The first one can be string |
| 1903 | * or a number. If the string is "*l", the user requires one line. If |
| 1904 | * 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] | 1905 | * If the value is a number, the user require a number of bytes equal |
| 1906 | * to the value. The default value is "*l" (a line). |
| 1907 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1908 | * This parameter with a variable type is converted in integer. This |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1909 | * integer takes this values: |
| 1910 | * -1 : read a line |
| 1911 | * -2 : read all the stream |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1912 | * >0 : amount of bytes. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1913 | * |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1914 | * The second parameter is optional. It contains a string that must be |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1915 | * concatenated with the read data. |
| 1916 | */ |
| 1917 | __LJMP static int hlua_socket_receive(struct lua_State *L) |
| 1918 | { |
| 1919 | int wanted = HLSR_READ_LINE; |
| 1920 | const char *pattern; |
| 1921 | int type; |
| 1922 | char *error; |
| 1923 | size_t len; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1924 | struct hlua_socket *socket; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1925 | |
| 1926 | if (lua_gettop(L) < 1 || lua_gettop(L) > 3) |
| 1927 | WILL_LJMP(luaL_error(L, "The 'receive' function requires between 1 and 3 arguments.")); |
| 1928 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 1929 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1930 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 1931 | /* Check if we run on the same thread than the xreator thread. |
| 1932 | * We cannot access to the socket if the thread is different. |
| 1933 | */ |
| 1934 | if (socket->tid != tid) |
| 1935 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 1936 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1937 | /* check for pattern. */ |
| 1938 | if (lua_gettop(L) >= 2) { |
| 1939 | type = lua_type(L, 2); |
| 1940 | if (type == LUA_TSTRING) { |
| 1941 | pattern = lua_tostring(L, 2); |
| 1942 | if (strcmp(pattern, "*a") == 0) |
| 1943 | wanted = HLSR_READ_ALL; |
| 1944 | else if (strcmp(pattern, "*l") == 0) |
| 1945 | wanted = HLSR_READ_LINE; |
| 1946 | else { |
| 1947 | wanted = strtoll(pattern, &error, 10); |
| 1948 | if (*error != '\0') |
| 1949 | WILL_LJMP(luaL_error(L, "Unsupported pattern.")); |
| 1950 | } |
| 1951 | } |
| 1952 | else if (type == LUA_TNUMBER) { |
| 1953 | wanted = lua_tointeger(L, 2); |
| 1954 | if (wanted < 0) |
| 1955 | WILL_LJMP(luaL_error(L, "Unsupported size.")); |
| 1956 | } |
| 1957 | } |
| 1958 | |
| 1959 | /* Set pattern. */ |
| 1960 | lua_pushinteger(L, wanted); |
Tim Duesterhus | c6e377e | 2018-01-04 19:32:13 +0100 | [diff] [blame] | 1961 | |
| 1962 | /* Check if we would replace the top by itself. */ |
| 1963 | if (lua_gettop(L) != 2) |
| 1964 | lua_replace(L, 2); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1965 | |
Tim Duesterhus | b33754c | 2018-01-04 19:32:14 +0100 | [diff] [blame] | 1966 | /* init buffer, and fill it with prefix. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1967 | luaL_buffinit(L, &socket->b); |
| 1968 | |
| 1969 | /* Check prefix. */ |
| 1970 | if (lua_gettop(L) >= 3) { |
| 1971 | if (lua_type(L, 3) != LUA_TSTRING) |
| 1972 | WILL_LJMP(luaL_error(L, "Expect a 'string' for the prefix")); |
| 1973 | pattern = lua_tolstring(L, 3, &len); |
| 1974 | luaL_addlstring(&socket->b, pattern, len); |
| 1975 | } |
| 1976 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1977 | return __LJMP(hlua_socket_receive_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1978 | } |
| 1979 | |
| 1980 | /* Write the Lua input string in the output buffer. |
Mark Lakes | 22154b4 | 2018-01-29 14:38:40 -0800 | [diff] [blame] | 1981 | * This function returns a yield if no space is available. |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1982 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 1983 | 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] | 1984 | { |
| 1985 | struct hlua_socket *socket; |
| 1986 | struct hlua *hlua = hlua_gethlua(L); |
| 1987 | struct appctx *appctx; |
| 1988 | size_t buf_len; |
| 1989 | const char *buf; |
| 1990 | int len; |
| 1991 | int send_len; |
| 1992 | int sent; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 1993 | struct xref *peer; |
| 1994 | struct stream_interface *si; |
| 1995 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 1996 | |
| 1997 | /* Check if this lua stack is schedulable. */ |
| 1998 | if (!hlua || !hlua->task) |
| 1999 | WILL_LJMP(luaL_error(L, "The 'write' function is only allowed in " |
| 2000 | "'frontend', 'backend' or 'task'")); |
| 2001 | |
| 2002 | /* Get object */ |
| 2003 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2004 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2005 | sent = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2006 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2007 | /* Check if we run on the same thread than the xreator thread. |
| 2008 | * We cannot access to the socket if the thread is different. |
| 2009 | */ |
| 2010 | if (socket->tid != tid) |
| 2011 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2012 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2013 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2014 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2015 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2016 | lua_pushinteger(L, -1); |
| 2017 | return 1; |
| 2018 | } |
| 2019 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2020 | si = appctx->owner; |
| 2021 | s = si_strm(si); |
| 2022 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2023 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2024 | if (channel_output_closed(&s->req)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2025 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2026 | lua_pushinteger(L, -1); |
| 2027 | return 1; |
| 2028 | } |
| 2029 | |
| 2030 | /* Update the input buffer data. */ |
| 2031 | buf += sent; |
| 2032 | send_len = buf_len - sent; |
| 2033 | |
| 2034 | /* All the data are sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2035 | if (sent >= buf_len) { |
| 2036 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2037 | return 1; /* Implicitly return the length sent. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2038 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2039 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2040 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2041 | * the request buffer if its not required. |
| 2042 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2043 | if (s->req.buf.size == 0) { |
Willy Tarreau | 581abd3 | 2018-10-25 10:21:41 +0200 | [diff] [blame] | 2044 | if (!si_alloc_ibuf(si, &appctx->buffer_wait)) |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2045 | goto hlua_socket_write_yield_return; |
Thierry FOURNIER | 486d52a | 2015-03-09 17:51:43 +0100 | [diff] [blame] | 2046 | } |
| 2047 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2048 | /* Check for available space. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2049 | len = b_room(&s->req.buf); |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2050 | if (len <= 0) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2051 | goto hlua_socket_write_yield_return; |
Christopher Faulet | 33834b1 | 2016-12-19 09:29:06 +0100 | [diff] [blame] | 2052 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2053 | |
| 2054 | /* send data */ |
| 2055 | if (len < send_len) |
| 2056 | send_len = len; |
Thierry FOURNIER | 66b8919 | 2018-05-27 01:14:47 +0200 | [diff] [blame] | 2057 | len = ci_putblk(&s->req, buf, send_len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2058 | |
| 2059 | /* "Not enough space" (-1), "Buffer too little to contain |
| 2060 | * the data" (-2) are not expected because the available length |
| 2061 | * is tested. |
| 2062 | * Other unknown error are also not expected. |
| 2063 | */ |
| 2064 | if (len <= 0) { |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2065 | if (len == -1) |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2066 | s->req.flags |= CF_WAKE_WRITE; |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2067 | |
sada | 05ed330 | 2018-05-11 11:48:18 -0700 | [diff] [blame] | 2068 | MAY_LJMP(hlua_socket_close_helper(L)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2069 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2070 | lua_pushinteger(L, -1); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2071 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2072 | return 1; |
| 2073 | } |
| 2074 | |
| 2075 | /* update buffers. */ |
Thierry FOURNIER | 101b976 | 2018-05-27 01:27:40 +0200 | [diff] [blame] | 2076 | appctx_wakeup(appctx); |
Willy Tarreau | de70fa1 | 2015-09-26 11:25:05 +0200 | [diff] [blame] | 2077 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2078 | s->req.rex = TICK_ETERNITY; |
| 2079 | s->res.wex = TICK_ETERNITY; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2080 | |
| 2081 | /* Update length sent. */ |
| 2082 | lua_pop(L, 1); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2083 | lua_pushinteger(L, sent + len); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2084 | |
| 2085 | /* All the data buffer is sent ? */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2086 | if (sent + len >= buf_len) { |
| 2087 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2088 | return 1; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2089 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2090 | |
| 2091 | hlua_socket_write_yield_return: |
Thierry FOURNIER | ba42fcd | 2018-05-27 00:59:48 +0200 | [diff] [blame] | 2092 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2093 | xref_unlock(&socket->xref, peer); |
| 2094 | WILL_LJMP(luaL_error(L, "out of memory")); |
| 2095 | } |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2096 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2097 | 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] | 2098 | return 0; |
| 2099 | } |
| 2100 | |
| 2101 | /* This function initiate the send of data. It just check the input |
| 2102 | * parameters and push an integer in the Lua stack that contain the |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2103 | * 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] | 2104 | * "hlua_socket_write_yield" that can yield. |
| 2105 | * |
| 2106 | * The Lua function gets between 3 and 4 parameters. The first one is |
| 2107 | * the associated object. The second is a string buffer. The third is |
| 2108 | * a facultative integer that represents where is the buffer position |
| 2109 | * of the start of the data that can send. The first byte is the |
| 2110 | * position "1". The default value is "1". The fourth argument is a |
| 2111 | * facultative integer that represents where is the buffer position |
| 2112 | * of the end of the data that can send. The default is the last byte. |
| 2113 | */ |
| 2114 | static int hlua_socket_send(struct lua_State *L) |
| 2115 | { |
| 2116 | int i; |
| 2117 | int j; |
| 2118 | const char *buf; |
| 2119 | size_t buf_len; |
| 2120 | |
| 2121 | /* Check number of arguments. */ |
| 2122 | if (lua_gettop(L) < 2 || lua_gettop(L) > 4) |
| 2123 | WILL_LJMP(luaL_error(L, "'send' needs between 2 and 4 arguments")); |
| 2124 | |
| 2125 | /* Get the string. */ |
| 2126 | buf = MAY_LJMP(luaL_checklstring(L, 2, &buf_len)); |
| 2127 | |
| 2128 | /* Get and check j. */ |
| 2129 | if (lua_gettop(L) == 4) { |
| 2130 | j = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 2131 | if (j < 0) |
| 2132 | j = buf_len + j + 1; |
| 2133 | if (j > buf_len) |
| 2134 | j = buf_len + 1; |
| 2135 | lua_pop(L, 1); |
| 2136 | } |
| 2137 | else |
| 2138 | j = buf_len; |
| 2139 | |
| 2140 | /* Get and check i. */ |
| 2141 | if (lua_gettop(L) == 3) { |
| 2142 | i = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2143 | if (i < 0) |
| 2144 | i = buf_len + i + 1; |
| 2145 | if (i > buf_len) |
| 2146 | i = buf_len + 1; |
| 2147 | lua_pop(L, 1); |
| 2148 | } else |
| 2149 | i = 1; |
| 2150 | |
| 2151 | /* Check bth i and j. */ |
| 2152 | if (i > j) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2153 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2154 | return 1; |
| 2155 | } |
| 2156 | if (i == 0 && j == 0) { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2157 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2158 | return 1; |
| 2159 | } |
| 2160 | if (i == 0) |
| 2161 | i = 1; |
| 2162 | if (j == 0) |
| 2163 | j = 1; |
| 2164 | |
| 2165 | /* Pop the string. */ |
| 2166 | lua_pop(L, 1); |
| 2167 | |
| 2168 | /* Update the buffer length. */ |
| 2169 | buf += i - 1; |
| 2170 | buf_len = j - i + 1; |
| 2171 | lua_pushlstring(L, buf, buf_len); |
| 2172 | |
| 2173 | /* This unsigned is used to remember the amount of sent data. */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2174 | lua_pushinteger(L, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2175 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2176 | return MAY_LJMP(hlua_socket_write_yield(L, 0, 0)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2177 | } |
| 2178 | |
Willy Tarreau | 22b0a68 | 2015-06-17 19:43:49 +0200 | [diff] [blame] | 2179 | #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] | 2180 | __LJMP static inline int hlua_socket_info(struct lua_State *L, struct sockaddr_storage *addr) |
| 2181 | { |
| 2182 | static char buffer[SOCKET_INFO_MAX_LEN]; |
| 2183 | int ret; |
| 2184 | int len; |
| 2185 | char *p; |
| 2186 | |
| 2187 | ret = addr_to_str(addr, buffer+1, SOCKET_INFO_MAX_LEN-1); |
| 2188 | if (ret <= 0) { |
| 2189 | lua_pushnil(L); |
| 2190 | return 1; |
| 2191 | } |
| 2192 | |
| 2193 | if (ret == AF_UNIX) { |
| 2194 | lua_pushstring(L, buffer+1); |
| 2195 | return 1; |
| 2196 | } |
| 2197 | else if (ret == AF_INET6) { |
| 2198 | buffer[0] = '['; |
| 2199 | len = strlen(buffer); |
| 2200 | buffer[len] = ']'; |
| 2201 | len++; |
| 2202 | buffer[len] = ':'; |
| 2203 | len++; |
| 2204 | p = buffer; |
| 2205 | } |
| 2206 | else if (ret == AF_INET) { |
| 2207 | p = buffer + 1; |
| 2208 | len = strlen(p); |
| 2209 | p[len] = ':'; |
| 2210 | len++; |
| 2211 | } |
| 2212 | else { |
| 2213 | lua_pushnil(L); |
| 2214 | return 1; |
| 2215 | } |
| 2216 | |
| 2217 | if (port_to_str(addr, p + len, SOCKET_INFO_MAX_LEN-1 - len) <= 0) { |
| 2218 | lua_pushnil(L); |
| 2219 | return 1; |
| 2220 | } |
| 2221 | |
| 2222 | lua_pushstring(L, p); |
| 2223 | return 1; |
| 2224 | } |
| 2225 | |
| 2226 | /* Returns information about the peer of the connection. */ |
| 2227 | __LJMP static int hlua_socket_getpeername(struct lua_State *L) |
| 2228 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2229 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2230 | struct xref *peer; |
| 2231 | struct appctx *appctx; |
| 2232 | struct stream_interface *si; |
| 2233 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2234 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2235 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2236 | MAY_LJMP(check_args(L, 1, "getpeername")); |
| 2237 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2238 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2239 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2240 | /* Check if we run on the same thread than the xreator thread. |
| 2241 | * We cannot access to the socket if the thread is different. |
| 2242 | */ |
| 2243 | if (socket->tid != tid) |
| 2244 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2245 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2246 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2247 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2248 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2249 | lua_pushnil(L); |
| 2250 | return 1; |
| 2251 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2252 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2253 | si = appctx->owner; |
| 2254 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2255 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2256 | if (!s->target_addr) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2257 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | a71f642 | 2016-11-16 17:00:14 +0100 | [diff] [blame] | 2258 | lua_pushnil(L); |
| 2259 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2260 | } |
| 2261 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2262 | ret = MAY_LJMP(hlua_socket_info(L, s->target_addr)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2263 | xref_unlock(&socket->xref, peer); |
| 2264 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2265 | } |
| 2266 | |
| 2267 | /* Returns information about my connection side. */ |
| 2268 | static int hlua_socket_getsockname(struct lua_State *L) |
| 2269 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2270 | struct hlua_socket *socket; |
| 2271 | struct connection *conn; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2272 | struct appctx *appctx; |
| 2273 | struct xref *peer; |
| 2274 | struct stream_interface *si; |
| 2275 | struct stream *s; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2276 | int ret; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2277 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2278 | MAY_LJMP(check_args(L, 1, "getsockname")); |
| 2279 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2280 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2281 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2282 | /* Check if we run on the same thread than the xreator thread. |
| 2283 | * We cannot access to the socket if the thread is different. |
| 2284 | */ |
| 2285 | if (socket->tid != tid) |
| 2286 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2287 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2288 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2289 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2290 | if (!peer) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2291 | lua_pushnil(L); |
| 2292 | return 1; |
| 2293 | } |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2294 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2295 | si = appctx->owner; |
| 2296 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2297 | |
Olivier Houchard | 9aaf778 | 2017-09-13 18:30:23 +0200 | [diff] [blame] | 2298 | conn = cs_conn(objt_cs(s->si[1].end)); |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2299 | if (!conn || !conn_get_src(conn)) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2300 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2301 | lua_pushnil(L); |
| 2302 | return 1; |
| 2303 | } |
| 2304 | |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2305 | ret = hlua_socket_info(L, conn->src); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2306 | xref_unlock(&socket->xref, peer); |
| 2307 | return ret; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2308 | } |
| 2309 | |
| 2310 | /* This struct define the applet. */ |
Willy Tarreau | 3057645 | 2015-04-13 13:50:30 +0200 | [diff] [blame] | 2311 | static struct applet update_applet = { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2312 | .obj_type = OBJ_TYPE_APPLET, |
| 2313 | .name = "<LUA_TCP>", |
| 2314 | .fct = hlua_socket_handler, |
| 2315 | .release = hlua_socket_release, |
| 2316 | }; |
| 2317 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2318 | __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] | 2319 | { |
| 2320 | struct hlua_socket *socket = MAY_LJMP(hlua_checksocket(L, 1)); |
| 2321 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2322 | struct xref *peer; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2323 | struct appctx *appctx; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2324 | struct stream_interface *si; |
| 2325 | struct stream *s; |
| 2326 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2327 | /* Check if we run on the same thread than the xreator thread. |
| 2328 | * We cannot access to the socket if the thread is different. |
| 2329 | */ |
| 2330 | if (socket->tid != tid) |
| 2331 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2332 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2333 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2334 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2335 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2336 | lua_pushnil(L); |
| 2337 | lua_pushstring(L, "Can't connect"); |
| 2338 | return 2; |
| 2339 | } |
| 2340 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2341 | si = appctx->owner; |
| 2342 | s = si_strm(si); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2343 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2344 | /* Check if we run on the same thread than the xreator thread. |
| 2345 | * We cannot access to the socket if the thread is different. |
| 2346 | */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2347 | if (socket->tid != tid) { |
| 2348 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2349 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2350 | } |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2351 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2352 | /* Check for connection close. */ |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2353 | if (!hlua || channel_output_closed(&s->req)) { |
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_pushnil(L); |
| 2356 | lua_pushstring(L, "Can't connect"); |
| 2357 | return 2; |
| 2358 | } |
| 2359 | |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 2360 | appctx = __objt_appctx(s->si[0].end); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2361 | |
| 2362 | /* Check for connection established. */ |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2363 | if (appctx->ctx.hlua_cosocket.connected) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2364 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2365 | lua_pushinteger(L, 1); |
| 2366 | return 1; |
| 2367 | } |
| 2368 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2369 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2370 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2371 | WILL_LJMP(luaL_error(L, "out of memory error")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2372 | } |
| 2373 | xref_unlock(&socket->xref, peer); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2374 | 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] | 2375 | return 0; |
| 2376 | } |
| 2377 | |
| 2378 | /* This function fail or initite the connection. */ |
| 2379 | __LJMP static int hlua_socket_connect(struct lua_State *L) |
| 2380 | { |
| 2381 | struct hlua_socket *socket; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2382 | int port = -1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2383 | const char *ip; |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2384 | struct hlua *hlua; |
| 2385 | struct appctx *appctx; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2386 | int low, high; |
| 2387 | struct sockaddr_storage *addr; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2388 | struct xref *peer; |
| 2389 | struct stream_interface *si; |
| 2390 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2391 | |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2392 | if (lua_gettop(L) < 2) |
| 2393 | WILL_LJMP(luaL_error(L, "connect: need at least 2 arguments")); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2394 | |
| 2395 | /* Get args. */ |
| 2396 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2397 | |
| 2398 | /* Check if we run on the same thread than the xreator thread. |
| 2399 | * We cannot access to the socket if the thread is different. |
| 2400 | */ |
| 2401 | if (socket->tid != tid) |
| 2402 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2403 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2404 | ip = MAY_LJMP(luaL_checkstring(L, 2)); |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2405 | if (lua_gettop(L) >= 3) { |
| 2406 | luaL_Buffer b; |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2407 | port = MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2408 | |
Tim Duesterhus | 6edab86 | 2018-01-06 19:04:45 +0100 | [diff] [blame] | 2409 | /* Force the ip to end with a colon, to support IPv6 addresses |
| 2410 | * that are not enclosed within square brackets. |
| 2411 | */ |
| 2412 | if (port > 0) { |
| 2413 | luaL_buffinit(L, &b); |
| 2414 | luaL_addstring(&b, ip); |
| 2415 | luaL_addchar(&b, ':'); |
| 2416 | luaL_pushresult(&b); |
| 2417 | ip = lua_tolstring(L, lua_gettop(L), NULL); |
| 2418 | } |
| 2419 | } |
| 2420 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2421 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2422 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2423 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2424 | lua_pushnil(L); |
| 2425 | return 1; |
| 2426 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2427 | |
| 2428 | /* Parse ip address. */ |
Willy Tarreau | 48ef4c9 | 2017-01-06 18:32:38 +0100 | [diff] [blame] | 2429 | addr = str2sa_range(ip, NULL, &low, &high, NULL, NULL, NULL, 0); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2430 | if (!addr) { |
| 2431 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2432 | WILL_LJMP(luaL_error(L, "connect: cannot parse destination address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2433 | } |
| 2434 | if (low != high) { |
| 2435 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2436 | WILL_LJMP(luaL_error(L, "connect: port ranges not supported : address '%s'", ip)); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2437 | } |
Willy Tarreau | 9da9a6f | 2019-07-17 14:49:44 +0200 | [diff] [blame] | 2438 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2439 | /* Set port. */ |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2440 | if (low == 0) { |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2441 | if (addr->ss_family == AF_INET) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2442 | if (port == -1) { |
| 2443 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2444 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2445 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2446 | ((struct sockaddr_in *)addr)->sin_port = htons(port); |
| 2447 | } else if (addr->ss_family == AF_INET6) { |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2448 | if (port == -1) { |
| 2449 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2450 | WILL_LJMP(luaL_error(L, "connect: port missing")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2451 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2452 | ((struct sockaddr_in6 *)addr)->sin6_port = htons(port); |
Thierry FOURNIER | c2f5653 | 2015-09-26 20:23:30 +0200 | [diff] [blame] | 2453 | } |
| 2454 | } |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2455 | |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2456 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2457 | si = appctx->owner; |
| 2458 | s = si_strm(si); |
Willy Tarreau | 1c8d32b | 2019-07-18 15:47:45 +0200 | [diff] [blame] | 2459 | |
| 2460 | if (!sockaddr_alloc(&s->target_addr)) { |
| 2461 | xref_unlock(&socket->xref, peer); |
| 2462 | WILL_LJMP(luaL_error(L, "connect: internal error")); |
| 2463 | } |
| 2464 | *s->target_addr = *addr; |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2465 | s->flags |= SF_ADDR_SET; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2466 | |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2467 | hlua = hlua_gethlua(L); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2468 | |
| 2469 | /* inform the stream that we want to be notified whenever the |
| 2470 | * connection completes. |
| 2471 | */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 2472 | si_cant_get(&s->si[0]); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 2473 | si_rx_endp_more(&s->si[0]); |
Thierry FOURNIER | 8c8fbbe | 2015-09-26 17:02:35 +0200 | [diff] [blame] | 2474 | appctx_wakeup(appctx); |
Willy Tarreau | bdc97a8 | 2015-08-24 15:42:28 +0200 | [diff] [blame] | 2475 | |
Willy Tarreau | f31af93 | 2020-01-14 09:59:38 +0100 | [diff] [blame] | 2476 | hlua->gc_count++; |
Thierry FOURNIER | 7c39ab4 | 2015-09-27 22:53:33 +0200 | [diff] [blame] | 2477 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2478 | if (!notification_new(&hlua->com, &appctx->ctx.hlua_cosocket.wake_on_write, hlua->task)) { |
| 2479 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 95ad96a | 2015-03-09 18:12:40 +0100 | [diff] [blame] | 2480 | WILL_LJMP(luaL_error(L, "out of memory")); |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2481 | } |
| 2482 | xref_unlock(&socket->xref, peer); |
PiBa-NL | 706d5ee | 2018-05-05 23:51:42 +0200 | [diff] [blame] | 2483 | |
| 2484 | task_wakeup(s->task, TASK_WOKEN_INIT); |
| 2485 | /* Return yield waiting for connection. */ |
| 2486 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2487 | 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] | 2488 | |
| 2489 | return 0; |
| 2490 | } |
| 2491 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2492 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2493 | __LJMP static int hlua_socket_connect_ssl(struct lua_State *L) |
| 2494 | { |
| 2495 | struct hlua_socket *socket; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2496 | struct xref *peer; |
| 2497 | struct appctx *appctx; |
| 2498 | struct stream_interface *si; |
| 2499 | struct stream *s; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2500 | |
| 2501 | MAY_LJMP(check_args(L, 3, "connect_ssl")); |
| 2502 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2503 | |
| 2504 | /* check for connection break. If some data where read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2505 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2506 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2507 | lua_pushnil(L); |
| 2508 | return 1; |
| 2509 | } |
| 2510 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2511 | si = appctx->owner; |
| 2512 | s = si_strm(si); |
| 2513 | |
| 2514 | s->target = &socket_ssl.obj_type; |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2515 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2516 | return MAY_LJMP(hlua_socket_connect(L)); |
| 2517 | } |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 2518 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2519 | |
| 2520 | __LJMP static int hlua_socket_setoption(struct lua_State *L) |
| 2521 | { |
| 2522 | return 0; |
| 2523 | } |
| 2524 | |
| 2525 | __LJMP static int hlua_socket_settimeout(struct lua_State *L) |
| 2526 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2527 | struct hlua_socket *socket; |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2528 | int tmout; |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2529 | double dtmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2530 | struct xref *peer; |
| 2531 | struct appctx *appctx; |
| 2532 | struct stream_interface *si; |
| 2533 | struct stream *s; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2534 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2535 | MAY_LJMP(check_args(L, 2, "settimeout")); |
| 2536 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2537 | socket = MAY_LJMP(hlua_checksocket(L, 1)); |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2538 | |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2539 | /* convert the timeout to millis */ |
| 2540 | dtmout = MAY_LJMP(luaL_checknumber(L, 2)) * 1000; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2541 | |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2542 | /* Check for negative values */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2543 | if (dtmout < 0) |
Thierry Fournier | 17a921b | 2018-03-08 09:59:02 +0100 | [diff] [blame] | 2544 | WILL_LJMP(luaL_error(L, "settimeout: cannot set negatives values")); |
| 2545 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2546 | if (dtmout > INT_MAX) /* overflow check */ |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2547 | 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] | 2548 | |
| 2549 | tmout = MS_TO_TICKS((int)dtmout); |
Cyril Bonté | 7ee465f | 2018-08-19 22:08:50 +0200 | [diff] [blame] | 2550 | if (tmout == 0) |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2551 | tmout++; /* very small timeouts are adjusted to a minimum of 1ms */ |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2552 | |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2553 | /* Check if we run on the same thread than the xreator thread. |
| 2554 | * We cannot access to the socket if the thread is different. |
| 2555 | */ |
| 2556 | if (socket->tid != tid) |
| 2557 | WILL_LJMP(luaL_error(L, "connect: cannot use socket on other thread")); |
| 2558 | |
Mark Lakes | 56cc125 | 2018-03-27 09:48:06 +0200 | [diff] [blame] | 2559 | /* check for connection break. If some data were read, return it. */ |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2560 | peer = xref_get_peer_and_lock(&socket->xref); |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2561 | if (!peer) { |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2562 | hlua_pusherror(L, "socket: not yet initialised, you can't set timeouts."); |
| 2563 | WILL_LJMP(lua_error(L)); |
| 2564 | return 0; |
| 2565 | } |
| 2566 | appctx = container_of(peer, struct appctx, ctx.hlua_cosocket.xref); |
| 2567 | si = appctx->owner; |
| 2568 | s = si_strm(si); |
| 2569 | |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2570 | s->sess->fe->timeout.connect = tmout; |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2571 | s->req.rto = tmout; |
| 2572 | s->req.wto = tmout; |
| 2573 | s->res.rto = tmout; |
| 2574 | s->res.wto = tmout; |
Cyril Bonté | 7bb6345 | 2018-08-17 23:51:02 +0200 | [diff] [blame] | 2575 | s->req.rex = tick_add_ifset(now_ms, tmout); |
| 2576 | s->req.wex = tick_add_ifset(now_ms, tmout); |
| 2577 | s->res.rex = tick_add_ifset(now_ms, tmout); |
| 2578 | s->res.wex = tick_add_ifset(now_ms, tmout); |
| 2579 | |
| 2580 | s->task->expire = tick_add_ifset(now_ms, tmout); |
| 2581 | task_queue(s->task); |
| 2582 | |
Thierry FOURNIER | 952939d | 2017-09-01 14:17:32 +0200 | [diff] [blame] | 2583 | xref_unlock(&socket->xref, peer); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2584 | |
Thierry Fournier | e9636f1 | 2018-03-08 09:54:32 +0100 | [diff] [blame] | 2585 | lua_pushinteger(L, 1); |
Tim Duesterhus | 119a5f1 | 2018-01-06 19:16:25 +0100 | [diff] [blame] | 2586 | return 1; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2587 | } |
| 2588 | |
| 2589 | __LJMP static int hlua_socket_new(lua_State *L) |
| 2590 | { |
| 2591 | struct hlua_socket *socket; |
| 2592 | struct appctx *appctx; |
Willy Tarreau | 15b5e14 | 2015-04-04 14:38:25 +0200 | [diff] [blame] | 2593 | struct session *sess; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2594 | struct stream *strm; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2595 | |
| 2596 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2597 | if (!lua_checkstack(L, 3)) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2598 | hlua_pusherror(L, "socket: full stack"); |
| 2599 | goto out_fail_conf; |
| 2600 | } |
| 2601 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2602 | /* Create the object: obj[0] = userdata. */ |
| 2603 | lua_newtable(L); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2604 | socket = MAY_LJMP(lua_newuserdata(L, sizeof(*socket))); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2605 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2606 | memset(socket, 0, sizeof(*socket)); |
Thierry FOURNIER | 94a6bfc | 2017-07-12 12:10:44 +0200 | [diff] [blame] | 2607 | socket->tid = tid; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2608 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2609 | /* Check if the various memory pools are initialized. */ |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 2610 | if (!pool_head_stream || !pool_head_buffer) { |
Thierry FOURNIER | 4a6170c | 2015-03-09 17:07:10 +0100 | [diff] [blame] | 2611 | hlua_pusherror(L, "socket: uninitialized pools."); |
| 2612 | goto out_fail_conf; |
| 2613 | } |
| 2614 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 2615 | /* Pop a class stream metatable and affect it to the userdata. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2616 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_socket_ref); |
| 2617 | lua_setmetatable(L, -2); |
| 2618 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2619 | /* Create the applet context */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 2620 | appctx = appctx_new(&update_applet, tid_bit); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2621 | if (!appctx) { |
| 2622 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | feb7640 | 2015-04-03 14:10:06 +0200 | [diff] [blame] | 2623 | goto out_fail_conf; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2624 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2625 | |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2626 | appctx->ctx.hlua_cosocket.connected = 0; |
Thierry FOURNIER | b13b20a | 2017-07-16 20:48:54 +0200 | [diff] [blame] | 2627 | appctx->ctx.hlua_cosocket.die = 0; |
Thierry FOURNIER | 18d0990 | 2016-12-16 09:25:38 +0100 | [diff] [blame] | 2628 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_write); |
| 2629 | LIST_INIT(&appctx->ctx.hlua_cosocket.wake_on_read); |
Willy Tarreau | b2bf833 | 2015-04-04 15:58:58 +0200 | [diff] [blame] | 2630 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2631 | /* Now create a session, task and stream for this applet */ |
| 2632 | sess = session_new(&socket_proxy, NULL, &appctx->obj_type); |
| 2633 | if (!sess) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2634 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2635 | goto out_fail_sess; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2636 | } |
| 2637 | |
Willy Tarreau | 87787ac | 2017-08-28 16:22:54 +0200 | [diff] [blame] | 2638 | strm = stream_new(sess, &appctx->obj_type); |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2639 | if (!strm) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2640 | hlua_pusherror(L, "socket: out of memory"); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2641 | goto out_fail_stream; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2642 | } |
| 2643 | |
Thierry FOURNIER | 2da788e | 2017-09-11 18:37:23 +0200 | [diff] [blame] | 2644 | /* Initialise cross reference between stream and Lua socket object. */ |
| 2645 | xref_create(&socket->xref, &appctx->ctx.hlua_cosocket.xref); |
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 | /* Configure "right" stream interface. this "si" is used to connect |
| 2648 | * and retrieve data from the server. The connection is initialized |
| 2649 | * with the "struct server". |
| 2650 | */ |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2651 | si_set_state(&strm->si[1], SI_ST_ASS); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2652 | |
| 2653 | /* Force destination server. */ |
Willy Tarreau | 5a0b25d | 2019-07-18 18:01:14 +0200 | [diff] [blame] | 2654 | strm->flags |= SF_DIRECT | SF_ASSIGNED | SF_BE_ASSIGNED; |
Willy Tarreau | 61cf7c8 | 2015-04-06 00:48:33 +0200 | [diff] [blame] | 2655 | strm->target = &socket_tcp.obj_type; |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2656 | |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2657 | return 1; |
| 2658 | |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2659 | out_fail_stream: |
Willy Tarreau | 11c3624 | 2015-04-04 15:54:03 +0200 | [diff] [blame] | 2660 | session_free(sess); |
Willy Tarreau | d420a97 | 2015-04-06 00:39:18 +0200 | [diff] [blame] | 2661 | out_fail_sess: |
| 2662 | appctx_free(appctx); |
| 2663 | out_fail_conf: |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 2664 | WILL_LJMP(lua_error(L)); |
| 2665 | return 0; |
| 2666 | } |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 2667 | |
| 2668 | /* |
| 2669 | * |
| 2670 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2671 | * Class Channel |
| 2672 | * |
| 2673 | * |
| 2674 | */ |
| 2675 | |
| 2676 | /* Returns the struct hlua_channel join to the class channel in the |
| 2677 | * stack entry "ud" or throws an argument error. |
| 2678 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2679 | __LJMP static struct channel *hlua_checkchannel(lua_State *L, int ud) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2680 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 2681 | return MAY_LJMP(hlua_checkudata(L, ud, class_channel_ref)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2682 | } |
| 2683 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2684 | /* Pushes the channel onto the top of the stack. If the stask does not have a |
| 2685 | * free slots, the function fails and returns 0; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2686 | */ |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 2687 | static int hlua_channel_new(lua_State *L, struct channel *channel) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2688 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2689 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2690 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2691 | return 0; |
| 2692 | |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2693 | lua_newtable(L); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2694 | lua_pushlightuserdata(L, channel); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 2695 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2696 | |
| 2697 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 2698 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_channel_ref); |
| 2699 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2700 | return 1; |
| 2701 | } |
| 2702 | |
| 2703 | /* Duplicate all the data present in the input channel and put it |
| 2704 | * in a string LUA variables. Returns -1 and push a nil value in |
| 2705 | * the stack if the channel is closed and all the data are consumed, |
| 2706 | * returns 0 if no data are available, otherwise it returns the length |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 2707 | * of the built string. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2708 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2709 | static inline int _hlua_channel_dup(struct channel *chn, lua_State *L) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2710 | { |
| 2711 | char *blk1; |
| 2712 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2713 | size_t len1; |
| 2714 | size_t len2; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2715 | int ret; |
| 2716 | luaL_Buffer b; |
| 2717 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2718 | ret = ci_getblk_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2719 | if (unlikely(ret == 0)) |
| 2720 | return 0; |
| 2721 | |
| 2722 | if (unlikely(ret < 0)) { |
| 2723 | lua_pushnil(L); |
| 2724 | return -1; |
| 2725 | } |
| 2726 | |
| 2727 | luaL_buffinit(L, &b); |
| 2728 | luaL_addlstring(&b, blk1, len1); |
| 2729 | if (unlikely(ret == 2)) |
| 2730 | luaL_addlstring(&b, blk2, len2); |
| 2731 | luaL_pushresult(&b); |
| 2732 | |
| 2733 | if (unlikely(ret == 2)) |
| 2734 | return len1 + len2; |
| 2735 | return len1; |
| 2736 | } |
| 2737 | |
| 2738 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2739 | * a yield. This function keep the data in the buffer. |
| 2740 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2741 | __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] | 2742 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2743 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2744 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2745 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2746 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2747 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2748 | WILL_LJMP(lua_error(L)); |
| 2749 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2750 | if (_hlua_channel_dup(chn, L) == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2751 | 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] | 2752 | return 1; |
| 2753 | } |
| 2754 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2755 | /* Check arguments for the function "hlua_channel_dup_yield". */ |
| 2756 | __LJMP static int hlua_channel_dup(lua_State *L) |
| 2757 | { |
| 2758 | MAY_LJMP(check_args(L, 1, "dup")); |
| 2759 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2760 | return MAY_LJMP(hlua_channel_dup_yield(L, 0, 0)); |
| 2761 | } |
| 2762 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2763 | /* "_hlua_channel_dup" wrapper. If no data are available, it returns |
| 2764 | * a yield. This function consumes the data in the buffer. It returns |
| 2765 | * a string containing the data or a nil pointer if no data are available |
| 2766 | * and the channel is closed. |
| 2767 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2768 | __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] | 2769 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2770 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2771 | int ret; |
| 2772 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2773 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2774 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2775 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2776 | WILL_LJMP(lua_error(L)); |
| 2777 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2778 | ret = _hlua_channel_dup(chn, L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2779 | if (unlikely(ret == 0)) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2780 | 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] | 2781 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2782 | if (unlikely(ret == -1)) |
| 2783 | return 1; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2784 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2785 | b_sub(&chn->buf, ret); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2786 | return 1; |
| 2787 | } |
| 2788 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2789 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2790 | __LJMP static int hlua_channel_get(lua_State *L) |
| 2791 | { |
| 2792 | MAY_LJMP(check_args(L, 1, "get")); |
| 2793 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2794 | return MAY_LJMP(hlua_channel_get_yield(L, 0, 0)); |
| 2795 | } |
| 2796 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2797 | /* This functions consumes and returns one line. If the channel is closed, |
| 2798 | * 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] | 2799 | * 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] | 2800 | * value. |
| 2801 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2802 | __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] | 2803 | { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2804 | char *blk1; |
| 2805 | char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 2806 | size_t len1; |
| 2807 | size_t len2; |
| 2808 | size_t len; |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2809 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2810 | int ret; |
| 2811 | luaL_Buffer b; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2812 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2813 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2814 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2815 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2816 | WILL_LJMP(lua_error(L)); |
| 2817 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2818 | ret = ci_getline_nc(chn, &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2819 | if (ret == 0) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2820 | 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] | 2821 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2822 | if (ret == -1) { |
| 2823 | lua_pushnil(L); |
| 2824 | return 1; |
| 2825 | } |
| 2826 | |
| 2827 | luaL_buffinit(L, &b); |
| 2828 | luaL_addlstring(&b, blk1, len1); |
| 2829 | len = len1; |
| 2830 | if (unlikely(ret == 2)) { |
| 2831 | luaL_addlstring(&b, blk2, len2); |
| 2832 | len += len2; |
| 2833 | } |
| 2834 | luaL_pushresult(&b); |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2835 | 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] | 2836 | return 1; |
| 2837 | } |
| 2838 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2839 | /* Check arguments for the function "hlua_channel_getline_yield". */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2840 | __LJMP static int hlua_channel_getline(lua_State *L) |
| 2841 | { |
| 2842 | MAY_LJMP(check_args(L, 1, "getline")); |
| 2843 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2844 | return MAY_LJMP(hlua_channel_getline_yield(L, 0, 0)); |
| 2845 | } |
| 2846 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2847 | /* This function takes a string as input, and append it at the |
| 2848 | * input side of channel. If the data is too big, but a space |
| 2849 | * is probably available after sending some data, the function |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2850 | * yields. If the data is bigger than the buffer, or if the |
| 2851 | * channel is closed, it returns -1. Otherwise, it returns the |
| 2852 | * amount of data written. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2853 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2854 | __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] | 2855 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2856 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2857 | size_t len; |
| 2858 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2859 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2860 | int ret; |
| 2861 | int max; |
| 2862 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2863 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2864 | WILL_LJMP(lua_error(L)); |
| 2865 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2866 | /* Check if the buffer is available because HAProxy doesn't allocate |
Christopher Faulet | a73e59b | 2016-12-09 17:30:18 +0100 | [diff] [blame] | 2867 | * the request buffer if its not required. |
| 2868 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2869 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2870 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2871 | 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] | 2872 | } |
| 2873 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2874 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2875 | if (max > len - l) |
| 2876 | max = len - l; |
| 2877 | |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 2878 | ret = ci_putblk(chn, str + l, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2879 | if (ret == -2 || ret == -3) { |
| 2880 | lua_pushinteger(L, -1); |
| 2881 | return 1; |
| 2882 | } |
Willy Tarreau | bc18da1 | 2015-03-13 14:00:47 +0100 | [diff] [blame] | 2883 | if (ret == -1) { |
| 2884 | chn->flags |= CF_WAKE_WRITE; |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2885 | 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] | 2886 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2887 | l += ret; |
| 2888 | lua_pop(L, 1); |
| 2889 | lua_pushinteger(L, l); |
| 2890 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2891 | max = channel_recv_limit(chn) - b_data(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2892 | if (max == 0 && co_data(chn) == 0) { |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2893 | /* There are no space available, and the output buffer is empty. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2894 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2895 | * we return the amount of copied data. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2896 | */ |
| 2897 | return 1; |
| 2898 | } |
| 2899 | if (l < len) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2900 | 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] | 2901 | return 1; |
| 2902 | } |
| 2903 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2904 | /* Just a wrapper of "hlua_channel_append_yield". It returns the length |
| 2905 | * 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] | 2906 | * buffer size is too little for the data. |
| 2907 | */ |
| 2908 | __LJMP static int hlua_channel_append(lua_State *L) |
| 2909 | { |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2910 | size_t len; |
| 2911 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2912 | MAY_LJMP(check_args(L, 2, "append")); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2913 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 2914 | MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2915 | MAY_LJMP(luaL_checkinteger(L, 3)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2916 | lua_pushinteger(L, 0); |
| 2917 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2918 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2919 | } |
| 2920 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2921 | /* Just a wrapper of "hlua_channel_append_yield". This wrapper starts |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2922 | * his process by cleaning the buffer. The result is a replacement |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2923 | * of the current data. It returns the length of the written string, |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2924 | * or -1 if the channel is closed or if the buffer size is too |
| 2925 | * little for the data. |
| 2926 | */ |
| 2927 | __LJMP static int hlua_channel_set(lua_State *L) |
| 2928 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2929 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2930 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2931 | MAY_LJMP(check_args(L, 2, "set")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 2932 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2933 | lua_pushinteger(L, 0); |
| 2934 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2935 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2936 | WILL_LJMP(lua_error(L)); |
| 2937 | |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2938 | b_set_data(&chn->buf, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2939 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2940 | return MAY_LJMP(hlua_channel_append_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2941 | } |
| 2942 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2943 | /* Append data in the output side of the buffer. This data is immediately |
| 2944 | * sent. The function returns the amount of data written. If the buffer |
| 2945 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2946 | * if the channel is closed. |
| 2947 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 2948 | __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] | 2949 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2950 | struct channel *chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2951 | size_t len; |
| 2952 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 2953 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 2954 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 2955 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2956 | |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 2957 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 2958 | WILL_LJMP(lua_error(L)); |
| 2959 | |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 2960 | if (unlikely(channel_output_closed(chn))) { |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2961 | lua_pushinteger(L, -1); |
| 2962 | return 1; |
| 2963 | } |
| 2964 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2965 | /* Check if the buffer is available because HAProxy doesn't allocate |
Thierry FOURNIER | 3e3a608 | 2015-03-05 17:06:12 +0100 | [diff] [blame] | 2966 | * the request buffer if its not required. |
| 2967 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2968 | if (chn->buf.size == 0) { |
Willy Tarreau | 4b962a4 | 2018-11-15 11:03:21 +0100 | [diff] [blame] | 2969 | si_rx_buff_blk(chn_prod(chn)); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 2970 | 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] | 2971 | } |
| 2972 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2973 | /* The written data will be immediately sent, so we can check |
| 2974 | * the available space without taking in account the reserve. |
| 2975 | * The reserve is guaranteed for the processing of incoming |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2976 | * data, because the buffer will be flushed. |
| 2977 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2978 | max = b_room(&chn->buf); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2979 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2980 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2981 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 2982 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2983 | */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 2984 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2985 | return 1; |
| 2986 | |
| 2987 | /* Adjust the real required length. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 2988 | if (max > len - l) |
| 2989 | max = len - l; |
| 2990 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 2991 | /* The buffer available size may be not contiguous. This test |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2992 | * detects a non contiguous buffer and realign it. |
| 2993 | */ |
Willy Tarreau | 3f67999 | 2018-06-15 15:06:42 +0200 | [diff] [blame] | 2994 | if (ci_space_for_replace(chn) < max) |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 2995 | channel_slow_realign(chn, trash.area); |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 2996 | |
| 2997 | /* Copy input data in the buffer. */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 2998 | 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] | 2999 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3000 | /* buffer replace considers that the input part is filled. |
| 3001 | * so, I must forward these new data in the output part. |
| 3002 | */ |
Willy Tarreau | bcbd393 | 2018-06-06 07:13:22 +0200 | [diff] [blame] | 3003 | c_adv(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3004 | |
| 3005 | l += max; |
| 3006 | lua_pop(L, 1); |
| 3007 | lua_pushinteger(L, l); |
| 3008 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3009 | /* If there is no space available, and the output buffer is empty. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3010 | * in this case, we cannot add more data, so we cannot yield, |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3011 | * we return the amount of copied data. |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3012 | */ |
Willy Tarreau | c9fa048 | 2018-07-10 17:43:27 +0200 | [diff] [blame] | 3013 | max = b_room(&chn->buf); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3014 | if (max == 0 && co_data(chn) == 0) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3015 | return 1; |
Thierry FOURNIER | deb5d73 | 2015-03-06 01:07:45 +0100 | [diff] [blame] | 3016 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3017 | if (l < len) { |
| 3018 | /* If we are waiting for space in the response buffer, we |
| 3019 | * must set the flag WAKERESWR. This flag required the task |
| 3020 | * wake up if any activity is detected on the response buffer. |
| 3021 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3022 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3023 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3024 | else |
| 3025 | HLUA_SET_WAKEREQWR(hlua); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3026 | 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] | 3027 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3028 | |
| 3029 | return 1; |
| 3030 | } |
| 3031 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3032 | /* Just a wrapper of "_hlua_channel_send". This wrapper permits |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3033 | * yield the LUA process, and resume it without checking the |
| 3034 | * input arguments. |
| 3035 | */ |
| 3036 | __LJMP static int hlua_channel_send(lua_State *L) |
| 3037 | { |
| 3038 | MAY_LJMP(check_args(L, 2, "send")); |
| 3039 | lua_pushinteger(L, 0); |
| 3040 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3041 | return MAY_LJMP(hlua_channel_send_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3042 | } |
| 3043 | |
| 3044 | /* This function forward and amount of butes. The data pass from |
| 3045 | * the input side of the buffer to the output side, and can be |
| 3046 | * forwarded. This function never fails. |
| 3047 | * |
| 3048 | * The Lua function takes an amount of bytes to be forwarded in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3049 | * input. It returns the number of bytes forwarded. |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3050 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3051 | __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] | 3052 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3053 | struct channel *chn; |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3054 | int len; |
| 3055 | int l; |
| 3056 | int max; |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3057 | struct hlua *hlua = hlua_gethlua(L); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3058 | |
| 3059 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 3f829a4 | 2018-12-13 21:56:45 +0100 | [diff] [blame] | 3060 | |
| 3061 | if (chn_strm(chn)->be->mode == PR_MODE_HTTP) |
| 3062 | WILL_LJMP(lua_error(L)); |
| 3063 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3064 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3065 | l = MAY_LJMP(luaL_checkinteger(L, -1)); |
| 3066 | |
| 3067 | max = len - l; |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3068 | if (max > ci_data(chn)) |
| 3069 | max = ci_data(chn); |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3070 | channel_forward(chn, max); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3071 | l += max; |
| 3072 | |
| 3073 | lua_pop(L, 1); |
| 3074 | lua_pushinteger(L, l); |
| 3075 | |
| 3076 | /* Check if it miss bytes to forward. */ |
| 3077 | if (l < len) { |
| 3078 | /* The the input channel or the output channel are closed, we |
| 3079 | * must return the amount of data forwarded. |
| 3080 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3081 | if (channel_input_closed(chn) || channel_output_closed(chn)) |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3082 | return 1; |
| 3083 | |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3084 | /* If we are waiting for space data in the response buffer, we |
| 3085 | * must set the flag WAKERESWR. This flag required the task |
| 3086 | * wake up if any activity is detected on the response buffer. |
| 3087 | */ |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3088 | if (chn->flags & CF_ISRESP) |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3089 | HLUA_SET_WAKERESWR(hlua); |
Thierry FOURNIER | 53e08ec | 2015-03-06 00:35:53 +0100 | [diff] [blame] | 3090 | else |
| 3091 | HLUA_SET_WAKEREQWR(hlua); |
Thierry FOURNIER | ef6a211 | 2015-03-05 17:45:34 +0100 | [diff] [blame] | 3092 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3093 | /* Otherwise, we can yield waiting for new data in the inpout side. */ |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3094 | 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] | 3095 | } |
| 3096 | |
| 3097 | return 1; |
| 3098 | } |
| 3099 | |
| 3100 | /* Just check the input and prepare the stack for the previous |
| 3101 | * function "hlua_channel_forward_yield" |
| 3102 | */ |
| 3103 | __LJMP static int hlua_channel_forward(lua_State *L) |
| 3104 | { |
| 3105 | MAY_LJMP(check_args(L, 2, "forward")); |
| 3106 | MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3107 | MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3108 | |
| 3109 | lua_pushinteger(L, 0); |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 3110 | return MAY_LJMP(hlua_channel_forward_yield(L, 0, 0)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3111 | } |
| 3112 | |
| 3113 | /* Just returns the number of bytes available in the input |
| 3114 | * side of the buffer. This function never fails. |
| 3115 | */ |
| 3116 | __LJMP static int hlua_channel_get_in_len(lua_State *L) |
| 3117 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3118 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3119 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3120 | MAY_LJMP(check_args(L, 1, "get_in_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3121 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | a3ceac1 | 2018-12-14 13:39:09 +0100 | [diff] [blame] | 3122 | if (IS_HTX_STRM(chn_strm(chn))) { |
| 3123 | struct htx *htx = htxbuf(&chn->buf); |
| 3124 | lua_pushinteger(L, htx->data - co_data(chn)); |
| 3125 | } |
| 3126 | else |
| 3127 | lua_pushinteger(L, ci_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3128 | return 1; |
| 3129 | } |
| 3130 | |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3131 | /* Returns true if the channel is full. */ |
| 3132 | __LJMP static int hlua_channel_is_full(lua_State *L) |
| 3133 | { |
| 3134 | struct channel *chn; |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3135 | |
| 3136 | MAY_LJMP(check_args(L, 1, "is_full")); |
| 3137 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Christopher Faulet | 0ec740e | 2020-02-26 11:59:19 +0100 | [diff] [blame] | 3138 | /* ignore the reserve, we are not on a producer side (ie in an |
| 3139 | * applet). |
| 3140 | */ |
| 3141 | lua_pushboolean(L, channel_full(chn, 0)); |
Thierry FOURNIER / OZON.IO | 65192f3 | 2016-11-07 15:28:40 +0100 | [diff] [blame] | 3142 | return 1; |
| 3143 | } |
| 3144 | |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 3145 | /* Returns true if the channel is the response channel. */ |
| 3146 | __LJMP static int hlua_channel_is_resp(lua_State *L) |
| 3147 | { |
| 3148 | struct channel *chn; |
| 3149 | |
| 3150 | MAY_LJMP(check_args(L, 1, "is_resp")); |
| 3151 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
| 3152 | |
| 3153 | lua_pushboolean(L, !!(chn->flags & CF_ISRESP)); |
| 3154 | return 1; |
| 3155 | } |
| 3156 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3157 | /* Just returns the number of bytes available in the output |
| 3158 | * side of the buffer. This function never fails. |
| 3159 | */ |
| 3160 | __LJMP static int hlua_channel_get_out_len(lua_State *L) |
| 3161 | { |
Willy Tarreau | 47860ed | 2015-03-10 14:07:50 +0100 | [diff] [blame] | 3162 | struct channel *chn; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3163 | |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3164 | MAY_LJMP(check_args(L, 1, "get_out_len")); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 3165 | chn = MAY_LJMP(hlua_checkchannel(L, 1)); |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 3166 | lua_pushinteger(L, co_data(chn)); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3167 | return 1; |
| 3168 | } |
| 3169 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3170 | /* |
| 3171 | * |
| 3172 | * |
| 3173 | * Class Fetches |
| 3174 | * |
| 3175 | * |
| 3176 | */ |
| 3177 | |
| 3178 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3179 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3180 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3181 | __LJMP static struct hlua_smp *hlua_checkfetches(lua_State *L, int ud) |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3182 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3183 | return MAY_LJMP(hlua_checkudata(L, ud, class_fetches_ref)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3184 | } |
| 3185 | |
| 3186 | /* This function creates and push in the stack a fetch object according |
| 3187 | * with a current TXN. |
| 3188 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3189 | 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] | 3190 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3191 | struct hlua_smp *hsmp; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3192 | |
| 3193 | /* Check stack size. */ |
| 3194 | if (!lua_checkstack(L, 3)) |
| 3195 | return 0; |
| 3196 | |
| 3197 | /* Create the object: obj[0] = userdata. |
| 3198 | * Note that the base of the Fetches object is the |
| 3199 | * transaction object. |
| 3200 | */ |
| 3201 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3202 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3203 | lua_rawseti(L, -2, 0); |
| 3204 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3205 | hsmp->s = txn->s; |
| 3206 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3207 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3208 | hsmp->flags = flags; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3209 | |
| 3210 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 3211 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_fetches_ref); |
| 3212 | lua_setmetatable(L, -2); |
| 3213 | |
| 3214 | return 1; |
| 3215 | } |
| 3216 | |
| 3217 | /* This function is an LUA binding. It is called with each sample-fetch. |
| 3218 | * It uses closure argument to store the associated sample-fetch. It |
| 3219 | * returns only one argument or throws an error. An error is thrown |
| 3220 | * only if an error is encountered during the argument parsing. If |
| 3221 | * the "sample-fetch" function fails, nil is returned. |
| 3222 | */ |
| 3223 | __LJMP static int hlua_run_sample_fetch(lua_State *L) |
| 3224 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3225 | struct hlua_smp *hsmp; |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3226 | struct sample_fetch *f; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3227 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3228 | int i; |
| 3229 | struct sample smp; |
| 3230 | |
| 3231 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3232 | f = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3233 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3234 | /* Get traditional arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3235 | hsmp = MAY_LJMP(hlua_checkfetches(L, 1)); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3236 | |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 3237 | /* Check execution authorization. */ |
| 3238 | if (f->use & SMP_USE_HTTP_ANY && |
| 3239 | !(hsmp->flags & HLUA_F_MAY_USE_HTTP)) { |
| 3240 | lua_pushfstring(L, "the sample-fetch '%s' needs an HTTP parser which " |
| 3241 | "is not available in Lua services", f->kw); |
| 3242 | WILL_LJMP(lua_error(L)); |
| 3243 | } |
| 3244 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3245 | /* Get extra arguments. */ |
| 3246 | for (i = 0; i < lua_gettop(L) - 1; i++) { |
| 3247 | if (i >= ARGM_NBARGS) |
| 3248 | break; |
| 3249 | hlua_lua2arg(L, i + 2, &args[i]); |
| 3250 | } |
| 3251 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3252 | args[i].data.str.area = NULL; |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3253 | |
| 3254 | /* Check arguments. */ |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 3255 | 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] | 3256 | |
| 3257 | /* Run the special args checker. */ |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 3258 | if (f->val_args && !f->val_args(args, NULL)) { |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3259 | lua_pushfstring(L, "error in arguments"); |
| 3260 | WILL_LJMP(lua_error(L)); |
| 3261 | } |
| 3262 | |
| 3263 | /* Initialise the sample. */ |
| 3264 | memset(&smp, 0, sizeof(smp)); |
| 3265 | |
| 3266 | /* Run the sample fetch process. */ |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3267 | 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] | 3268 | if (!f->process(args, &smp, f->kw, f->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3269 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3270 | lua_pushstring(L, ""); |
| 3271 | else |
| 3272 | lua_pushnil(L); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3273 | return 1; |
| 3274 | } |
| 3275 | |
| 3276 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3277 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3278 | hlua_smp2lua_str(L, &smp); |
| 3279 | else |
| 3280 | hlua_smp2lua(L, &smp); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 3281 | return 1; |
| 3282 | } |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 3283 | |
| 3284 | /* |
| 3285 | * |
| 3286 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3287 | * Class Converters |
| 3288 | * |
| 3289 | * |
| 3290 | */ |
| 3291 | |
| 3292 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3293 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3294 | */ |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3295 | __LJMP static struct hlua_smp *hlua_checkconverters(lua_State *L, int ud) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3296 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3297 | return MAY_LJMP(hlua_checkudata(L, ud, class_converters_ref)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3298 | } |
| 3299 | |
| 3300 | /* This function creates and push in the stack a Converters object |
| 3301 | * according with a current TXN. |
| 3302 | */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3303 | 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] | 3304 | { |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3305 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3306 | |
| 3307 | /* Check stack size. */ |
| 3308 | if (!lua_checkstack(L, 3)) |
| 3309 | return 0; |
| 3310 | |
| 3311 | /* Create the object: obj[0] = userdata. |
| 3312 | * Note that the base of the Converters object is the |
| 3313 | * same than the TXN object. |
| 3314 | */ |
| 3315 | lua_newtable(L); |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3316 | hsmp = lua_newuserdata(L, sizeof(*hsmp)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3317 | lua_rawseti(L, -2, 0); |
| 3318 | |
Willy Tarreau | 7073c47 | 2015-04-06 11:15:40 +0200 | [diff] [blame] | 3319 | hsmp->s = txn->s; |
| 3320 | hsmp->p = txn->p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 3321 | hsmp->dir = txn->dir; |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3322 | hsmp->flags = flags; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3323 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3324 | /* Pop a class stream metatable and affect it to the table. */ |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3325 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_converters_ref); |
| 3326 | lua_setmetatable(L, -2); |
| 3327 | |
| 3328 | return 1; |
| 3329 | } |
| 3330 | |
| 3331 | /* This function is an LUA binding. It is called with each converter. |
| 3332 | * It uses closure argument to store the associated converter. It |
| 3333 | * returns only one argument or throws an error. An error is thrown |
| 3334 | * only if an error is encountered during the argument parsing. If |
| 3335 | * the converter function function fails, nil is returned. |
| 3336 | */ |
| 3337 | __LJMP static int hlua_run_sample_conv(lua_State *L) |
| 3338 | { |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3339 | struct hlua_smp *hsmp; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3340 | struct sample_conv *conv; |
Frédéric Lécaille | f874a83 | 2018-06-15 13:56:04 +0200 | [diff] [blame] | 3341 | struct arg args[ARGM_NBARGS + 1] = {{0}}; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3342 | int i; |
| 3343 | struct sample smp; |
| 3344 | |
| 3345 | /* Get closure arguments. */ |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3346 | conv = lua_touserdata(L, lua_upvalueindex(1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3347 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3348 | /* Get traditional arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3349 | hsmp = MAY_LJMP(hlua_checkconverters(L, 1)); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3350 | |
| 3351 | /* Get extra arguments. */ |
| 3352 | for (i = 0; i < lua_gettop(L) - 2; i++) { |
| 3353 | if (i >= ARGM_NBARGS) |
| 3354 | break; |
| 3355 | hlua_lua2arg(L, i + 3, &args[i]); |
| 3356 | } |
| 3357 | args[i].type = ARGT_STOP; |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 3358 | args[i].data.str.area = NULL; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3359 | |
| 3360 | /* Check arguments. */ |
Willy Tarreau | da5f108 | 2015-04-06 11:17:13 +0200 | [diff] [blame] | 3361 | 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] | 3362 | |
| 3363 | /* Run the special args checker. */ |
| 3364 | if (conv->val_args && !conv->val_args(args, conv, "", 0, NULL)) { |
| 3365 | hlua_pusherror(L, "error in arguments"); |
| 3366 | WILL_LJMP(lua_error(L)); |
| 3367 | } |
| 3368 | |
| 3369 | /* Initialise the sample. */ |
| 3370 | if (!hlua_lua2smp(L, 2, &smp)) { |
| 3371 | hlua_pusherror(L, "error in the input argument"); |
| 3372 | WILL_LJMP(lua_error(L)); |
| 3373 | } |
| 3374 | |
Willy Tarreau | 1777ea6 | 2016-03-10 16:15:46 +0100 | [diff] [blame] | 3375 | smp_set_owner(&smp, hsmp->p, hsmp->s->sess, hsmp->s, hsmp->dir & SMP_OPT_DIR); |
| 3376 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3377 | /* Apply expected cast. */ |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3378 | if (!sample_casts[smp.data.type][conv->in_type]) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3379 | hlua_pusherror(L, "invalid input argument: cannot cast '%s' to '%s'", |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3380 | smp_to_type[smp.data.type], smp_to_type[conv->in_type]); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3381 | WILL_LJMP(lua_error(L)); |
| 3382 | } |
Thierry FOURNIER | 8c542ca | 2015-08-19 09:00:18 +0200 | [diff] [blame] | 3383 | if (sample_casts[smp.data.type][conv->in_type] != c_none && |
| 3384 | !sample_casts[smp.data.type][conv->in_type](&smp)) { |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3385 | hlua_pusherror(L, "error during the input argument casting"); |
| 3386 | WILL_LJMP(lua_error(L)); |
| 3387 | } |
| 3388 | |
| 3389 | /* Run the sample conversion process. */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 3390 | if (!conv->process(args, &smp, conv->private)) { |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3391 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3392 | lua_pushstring(L, ""); |
| 3393 | else |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3394 | lua_pushnil(L); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3395 | return 1; |
| 3396 | } |
| 3397 | |
| 3398 | /* Convert the returned sample in lua value. */ |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3399 | if (hsmp->flags & HLUA_F_AS_STRING) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 3400 | hlua_smp2lua_str(L, &smp); |
| 3401 | else |
| 3402 | hlua_smp2lua(L, &smp); |
Willy Tarreau | a678b43 | 2015-08-28 10:14:59 +0200 | [diff] [blame] | 3403 | return 1; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3404 | } |
| 3405 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3406 | /* |
| 3407 | * |
| 3408 | * |
| 3409 | * Class AppletTCP |
| 3410 | * |
| 3411 | * |
| 3412 | */ |
| 3413 | |
| 3414 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 3415 | * a class stream, otherwise it throws an error. |
| 3416 | */ |
| 3417 | __LJMP static struct hlua_appctx *hlua_checkapplet_tcp(lua_State *L, int ud) |
| 3418 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3419 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_tcp_ref)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3420 | } |
| 3421 | |
| 3422 | /* This function creates and push in the stack an Applet object |
| 3423 | * according with a current TXN. |
| 3424 | */ |
| 3425 | static int hlua_applet_tcp_new(lua_State *L, struct appctx *ctx) |
| 3426 | { |
| 3427 | struct hlua_appctx *appctx; |
| 3428 | struct stream_interface *si = ctx->owner; |
| 3429 | struct stream *s = si_strm(si); |
| 3430 | struct proxy *p = s->be; |
| 3431 | |
| 3432 | /* Check stack size. */ |
| 3433 | if (!lua_checkstack(L, 3)) |
| 3434 | return 0; |
| 3435 | |
| 3436 | /* Create the object: obj[0] = userdata. |
| 3437 | * Note that the base of the Converters object is the |
| 3438 | * same than the TXN object. |
| 3439 | */ |
| 3440 | lua_newtable(L); |
| 3441 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
| 3442 | lua_rawseti(L, -2, 0); |
| 3443 | appctx->appctx = ctx; |
| 3444 | appctx->htxn.s = s; |
| 3445 | appctx->htxn.p = p; |
| 3446 | |
| 3447 | /* Create the "f" field that contains a list of fetches. */ |
| 3448 | lua_pushstring(L, "f"); |
| 3449 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3450 | return 0; |
| 3451 | lua_settable(L, -3); |
| 3452 | |
| 3453 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3454 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3455 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3456 | return 0; |
| 3457 | lua_settable(L, -3); |
| 3458 | |
| 3459 | /* Create the "c" field that contains a list of converters. */ |
| 3460 | lua_pushstring(L, "c"); |
| 3461 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3462 | return 0; |
| 3463 | lua_settable(L, -3); |
| 3464 | |
| 3465 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3466 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3467 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3468 | return 0; |
| 3469 | lua_settable(L, -3); |
| 3470 | |
| 3471 | /* Pop a class stream metatable and affect it to the table. */ |
| 3472 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_tcp_ref); |
| 3473 | lua_setmetatable(L, -2); |
| 3474 | |
| 3475 | return 1; |
| 3476 | } |
| 3477 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3478 | __LJMP static int hlua_applet_tcp_set_var(lua_State *L) |
| 3479 | { |
| 3480 | struct hlua_appctx *appctx; |
| 3481 | struct stream *s; |
| 3482 | const char *name; |
| 3483 | size_t len; |
| 3484 | struct sample smp; |
| 3485 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3486 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3487 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3488 | |
| 3489 | /* It is useles to retrieve the stream, but this function |
| 3490 | * runs only in a stream context. |
| 3491 | */ |
| 3492 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3493 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3494 | s = appctx->htxn.s; |
| 3495 | |
| 3496 | /* Converts the third argument in a sample. */ |
| 3497 | hlua_lua2smp(L, 3, &smp); |
| 3498 | |
| 3499 | /* Store the sample in a variable. */ |
| 3500 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3501 | |
| 3502 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3503 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3504 | else |
| 3505 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3506 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3507 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3508 | } |
| 3509 | |
| 3510 | __LJMP static int hlua_applet_tcp_unset_var(lua_State *L) |
| 3511 | { |
| 3512 | struct hlua_appctx *appctx; |
| 3513 | struct stream *s; |
| 3514 | const char *name; |
| 3515 | size_t len; |
| 3516 | struct sample smp; |
| 3517 | |
| 3518 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 3519 | |
| 3520 | /* It is useles to retrieve the stream, but this function |
| 3521 | * runs only in a stream context. |
| 3522 | */ |
| 3523 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3524 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3525 | s = appctx->htxn.s; |
| 3526 | |
| 3527 | /* Unset the variable. */ |
| 3528 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3529 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 3530 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3531 | } |
| 3532 | |
| 3533 | __LJMP static int hlua_applet_tcp_get_var(lua_State *L) |
| 3534 | { |
| 3535 | struct hlua_appctx *appctx; |
| 3536 | struct stream *s; |
| 3537 | const char *name; |
| 3538 | size_t len; |
| 3539 | struct sample smp; |
| 3540 | |
| 3541 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 3542 | |
| 3543 | /* It is useles to retrieve the stream, but this function |
| 3544 | * runs only in a stream context. |
| 3545 | */ |
| 3546 | appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3547 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3548 | s = appctx->htxn.s; |
| 3549 | |
| 3550 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 3551 | if (!vars_get_by_name(name, len, &smp)) { |
| 3552 | lua_pushnil(L); |
| 3553 | return 1; |
| 3554 | } |
| 3555 | |
| 3556 | return hlua_smp2lua(L, &smp); |
| 3557 | } |
| 3558 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3559 | __LJMP static int hlua_applet_tcp_set_priv(lua_State *L) |
| 3560 | { |
| 3561 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3562 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3563 | struct hlua *hlua; |
| 3564 | |
| 3565 | /* 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] | 3566 | if (!s->hlua) |
| 3567 | return 0; |
| 3568 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3569 | |
| 3570 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 3571 | |
| 3572 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 3573 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3574 | |
| 3575 | /* Get and store new value. */ |
| 3576 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 3577 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 3578 | |
| 3579 | return 0; |
| 3580 | } |
| 3581 | |
| 3582 | __LJMP static int hlua_applet_tcp_get_priv(lua_State *L) |
| 3583 | { |
| 3584 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3585 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 3586 | struct hlua *hlua; |
| 3587 | |
| 3588 | /* 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] | 3589 | if (!s->hlua) { |
| 3590 | lua_pushnil(L); |
| 3591 | return 1; |
| 3592 | } |
| 3593 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 3594 | |
| 3595 | /* Push configuration index in the stack. */ |
| 3596 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 3597 | |
| 3598 | return 1; |
| 3599 | } |
| 3600 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3601 | /* If expected data not yet available, it returns a yield. This function |
| 3602 | * consumes the data in the buffer. It returns a string containing the |
| 3603 | * data. This string can be empty. |
| 3604 | */ |
| 3605 | __LJMP static int hlua_applet_tcp_getline_yield(lua_State *L, int status, lua_KContext ctx) |
| 3606 | { |
| 3607 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3608 | struct stream_interface *si = appctx->appctx->owner; |
| 3609 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3610 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3611 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3612 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3613 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3614 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3615 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3616 | ret = co_getline_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3617 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3618 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3619 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3620 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3621 | 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] | 3622 | } |
| 3623 | |
| 3624 | /* End of data: commit the total strings and return. */ |
| 3625 | if (ret < 0) { |
| 3626 | luaL_pushresult(&appctx->b); |
| 3627 | return 1; |
| 3628 | } |
| 3629 | |
| 3630 | /* Ensure that the block 2 length is usable. */ |
| 3631 | if (ret == 1) |
| 3632 | len2 = 0; |
| 3633 | |
| 3634 | /* dont check the max length read and dont check. */ |
| 3635 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3636 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3637 | |
| 3638 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3639 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3640 | luaL_pushresult(&appctx->b); |
| 3641 | return 1; |
| 3642 | } |
| 3643 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3644 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3645 | __LJMP static int hlua_applet_tcp_getline(lua_State *L) |
| 3646 | { |
| 3647 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3648 | |
| 3649 | /* Initialise the string catenation. */ |
| 3650 | luaL_buffinit(L, &appctx->b); |
| 3651 | |
| 3652 | return MAY_LJMP(hlua_applet_tcp_getline_yield(L, 0, 0)); |
| 3653 | } |
| 3654 | |
| 3655 | /* If expected data not yet available, it returns a yield. This function |
| 3656 | * consumes the data in the buffer. It returns a string containing the |
| 3657 | * data. This string can be empty. |
| 3658 | */ |
| 3659 | __LJMP static int hlua_applet_tcp_recv_yield(lua_State *L, int status, lua_KContext ctx) |
| 3660 | { |
| 3661 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3662 | struct stream_interface *si = appctx->appctx->owner; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3663 | size_t len = MAY_LJMP(luaL_checkinteger(L, 2)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3664 | int ret; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3665 | const char *blk1; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3666 | size_t len1; |
Willy Tarreau | 206ba83 | 2018-06-14 15:27:31 +0200 | [diff] [blame] | 3667 | const char *blk2; |
Willy Tarreau | 55f3ce1 | 2018-07-18 11:49:27 +0200 | [diff] [blame] | 3668 | size_t len2; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3669 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3670 | /* Read the maximum amount of data available. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3671 | ret = co_getblk_nc(si_oc(si), &blk1, &len1, &blk2, &len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3672 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3673 | /* Data not yet available. return yield. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3674 | if (ret == 0) { |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3675 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3676 | 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] | 3677 | } |
| 3678 | |
| 3679 | /* End of data: commit the total strings and return. */ |
| 3680 | if (ret < 0) { |
| 3681 | luaL_pushresult(&appctx->b); |
| 3682 | return 1; |
| 3683 | } |
| 3684 | |
| 3685 | /* Ensure that the block 2 length is usable. */ |
| 3686 | if (ret == 1) |
| 3687 | len2 = 0; |
| 3688 | |
| 3689 | if (len == -1) { |
| 3690 | |
| 3691 | /* If len == -1, catenate all the data avalaile and |
| 3692 | * yield because we want to get all the data until |
| 3693 | * the end of data stream. |
| 3694 | */ |
| 3695 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3696 | luaL_addlstring(&appctx->b, blk2, len2); |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3697 | co_skip(si_oc(si), len1 + len2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3698 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3699 | 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] | 3700 | |
| 3701 | } else { |
| 3702 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3703 | /* Copy the first block caping to the length required. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3704 | if (len1 > len) |
| 3705 | len1 = len; |
| 3706 | luaL_addlstring(&appctx->b, blk1, len1); |
| 3707 | len -= len1; |
| 3708 | |
| 3709 | /* Copy the second block. */ |
| 3710 | if (len2 > len) |
| 3711 | len2 = len; |
| 3712 | luaL_addlstring(&appctx->b, blk2, len2); |
| 3713 | len -= len2; |
| 3714 | |
| 3715 | /* Consume input channel output buffer data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3716 | co_skip(si_oc(si), len1 + len2); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3717 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3718 | /* If there is no other data available, yield waiting for new data. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3719 | if (len > 0) { |
| 3720 | lua_pushinteger(L, len); |
| 3721 | lua_replace(L, 2); |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 3722 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3723 | 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] | 3724 | } |
| 3725 | |
| 3726 | /* return the result. */ |
| 3727 | luaL_pushresult(&appctx->b); |
| 3728 | return 1; |
| 3729 | } |
| 3730 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3731 | /* we never execute this */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3732 | hlua_pusherror(L, "Lua: internal error"); |
| 3733 | WILL_LJMP(lua_error(L)); |
| 3734 | return 0; |
| 3735 | } |
| 3736 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3737 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3738 | __LJMP static int hlua_applet_tcp_recv(lua_State *L) |
| 3739 | { |
| 3740 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3741 | int len = -1; |
| 3742 | |
| 3743 | if (lua_gettop(L) > 2) |
| 3744 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 3745 | if (lua_gettop(L) >= 2) { |
| 3746 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 3747 | lua_pop(L, 1); |
| 3748 | } |
| 3749 | |
| 3750 | /* Confirm or set the required length */ |
| 3751 | lua_pushinteger(L, len); |
| 3752 | |
| 3753 | /* Initialise the string catenation. */ |
| 3754 | luaL_buffinit(L, &appctx->b); |
| 3755 | |
| 3756 | return MAY_LJMP(hlua_applet_tcp_recv_yield(L, 0, 0)); |
| 3757 | } |
| 3758 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 3759 | /* Append data in the output side of the buffer. This data is immediately |
| 3760 | * sent. The function returns the amount of data written. If the buffer |
| 3761 | * cannot contain the data, the function yields. The function returns -1 |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3762 | * if the channel is closed. |
| 3763 | */ |
| 3764 | __LJMP static int hlua_applet_tcp_send_yield(lua_State *L, int status, lua_KContext ctx) |
| 3765 | { |
| 3766 | size_t len; |
| 3767 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_tcp(L, 1)); |
| 3768 | const char *str = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3769 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 3770 | struct stream_interface *si = appctx->appctx->owner; |
| 3771 | struct channel *chn = si_ic(si); |
| 3772 | int max; |
| 3773 | |
| 3774 | /* Get the max amount of data which can write as input in the channel. */ |
| 3775 | max = channel_recv_max(chn); |
| 3776 | if (max > (len - l)) |
| 3777 | max = len - l; |
| 3778 | |
| 3779 | /* Copy data. */ |
Willy Tarreau | 06d80a9 | 2017-10-19 14:32:15 +0200 | [diff] [blame] | 3780 | ci_putblk(chn, str + l, max); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3781 | |
| 3782 | /* update counters. */ |
| 3783 | l += max; |
| 3784 | lua_pop(L, 1); |
| 3785 | lua_pushinteger(L, l); |
| 3786 | |
| 3787 | /* If some data is not send, declares the situation to the |
| 3788 | * applet, and returns a yield. |
| 3789 | */ |
| 3790 | if (l < len) { |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 3791 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 3792 | 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] | 3793 | } |
| 3794 | |
| 3795 | return 1; |
| 3796 | } |
| 3797 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 3798 | /* Just a wrapper of "hlua_applet_tcp_send_yield". This wrapper permits |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 3799 | * yield the LUA process, and resume it without checking the |
| 3800 | * input arguments. |
| 3801 | */ |
| 3802 | __LJMP static int hlua_applet_tcp_send(lua_State *L) |
| 3803 | { |
| 3804 | MAY_LJMP(check_args(L, 2, "send")); |
| 3805 | lua_pushinteger(L, 0); |
| 3806 | |
| 3807 | return MAY_LJMP(hlua_applet_tcp_send_yield(L, 0, 0)); |
| 3808 | } |
| 3809 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 3810 | /* |
| 3811 | * |
| 3812 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3813 | * Class AppletHTTP |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3814 | * |
| 3815 | * |
| 3816 | */ |
| 3817 | |
| 3818 | /* Returns a struct hlua_txn if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 3819 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3820 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3821 | __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] | 3822 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 3823 | return MAY_LJMP(hlua_checkudata(L, ud, class_applet_http_ref)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3824 | } |
| 3825 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3826 | /* This function creates and push in the stack an Applet object |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3827 | * according with a current TXN. |
| 3828 | */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3829 | static int hlua_applet_http_new(lua_State *L, struct appctx *ctx) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3830 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3831 | struct hlua_appctx *appctx; |
Thierry FOURNIER | 841475e | 2015-12-11 17:10:09 +0100 | [diff] [blame] | 3832 | struct hlua_txn htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3833 | struct stream_interface *si = ctx->owner; |
| 3834 | struct stream *s = si_strm(si); |
| 3835 | struct proxy *px = s->be; |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3836 | struct htx *htx; |
| 3837 | struct htx_blk *blk; |
| 3838 | struct htx_sl *sl; |
| 3839 | struct ist path; |
| 3840 | unsigned long long len = 0; |
| 3841 | int32_t pos; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3842 | |
| 3843 | /* Check stack size. */ |
| 3844 | if (!lua_checkstack(L, 3)) |
| 3845 | return 0; |
| 3846 | |
| 3847 | /* Create the object: obj[0] = userdata. |
| 3848 | * Note that the base of the Converters object is the |
| 3849 | * same than the TXN object. |
| 3850 | */ |
| 3851 | lua_newtable(L); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3852 | appctx = lua_newuserdata(L, sizeof(*appctx)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3853 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3854 | appctx->appctx = ctx; |
| 3855 | appctx->appctx->ctx.hlua_apphttp.status = 200; /* Default status code returned. */ |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 3856 | 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] | 3857 | appctx->htxn.s = s; |
| 3858 | appctx->htxn.p = px; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3859 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3860 | /* Create the "f" field that contains a list of fetches. */ |
| 3861 | lua_pushstring(L, "f"); |
| 3862 | if (!hlua_fetches_new(L, &appctx->htxn, 0)) |
| 3863 | return 0; |
| 3864 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3865 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3866 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 3867 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3868 | if (!hlua_fetches_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3869 | return 0; |
| 3870 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3871 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3872 | /* Create the "c" field that contains a list of converters. */ |
| 3873 | lua_pushstring(L, "c"); |
| 3874 | if (!hlua_converters_new(L, &appctx->htxn, 0)) |
| 3875 | return 0; |
| 3876 | lua_settable(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3877 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3878 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 3879 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 3880 | if (!hlua_converters_new(L, &appctx->htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3881 | return 0; |
| 3882 | lua_settable(L, -3); |
Willy Tarreau | eee5b51 | 2015-04-03 23:46:31 +0200 | [diff] [blame] | 3883 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3884 | htx = htxbuf(&s->req.buf); |
| 3885 | blk = htx_get_first_blk(htx); |
Christopher Faulet | ea00973 | 2019-11-18 15:50:25 +0100 | [diff] [blame] | 3886 | BUG_ON(!blk || htx_get_blk_type(blk) != HTX_BLK_REQ_SL); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3887 | sl = htx_get_blk_ptr(htx, blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3888 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3889 | /* Stores the request method. */ |
| 3890 | lua_pushstring(L, "method"); |
| 3891 | lua_pushlstring(L, HTX_SL_REQ_MPTR(sl), HTX_SL_REQ_MLEN(sl)); |
| 3892 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3893 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3894 | /* Stores the http version. */ |
| 3895 | lua_pushstring(L, "version"); |
| 3896 | lua_pushlstring(L, HTX_SL_REQ_VPTR(sl), HTX_SL_REQ_VLEN(sl)); |
| 3897 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3898 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3899 | /* creates an array of headers. hlua_http_get_headers() crates and push |
| 3900 | * the array on the top of the stack. |
| 3901 | */ |
| 3902 | lua_pushstring(L, "headers"); |
| 3903 | htxn.s = s; |
| 3904 | htxn.p = px; |
| 3905 | htxn.dir = SMP_OPT_DIR_REQ; |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 3906 | if (!hlua_http_get_headers(L, &htxn.s->txn->req)) |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3907 | return 0; |
| 3908 | lua_settable(L, -3); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3909 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3910 | path = http_get_path(htx_sl_req_uri(sl)); |
Tim Duesterhus | ed52637 | 2020-03-05 17:56:33 +0100 | [diff] [blame] | 3911 | if (isttest(path)) { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3912 | char *p, *q, *end; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3913 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3914 | p = path.ptr; |
| 3915 | end = path.ptr + path.len; |
| 3916 | q = p; |
| 3917 | while (q < end && *q != '?') |
| 3918 | q++; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3919 | |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3920 | /* Stores the request path. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3921 | lua_pushstring(L, "path"); |
| 3922 | lua_pushlstring(L, p, q - p); |
Thierry FOURNIER | 7d38863 | 2017-02-22 02:06:16 +0100 | [diff] [blame] | 3923 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3924 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3925 | /* Stores the query string. */ |
| 3926 | lua_pushstring(L, "qs"); |
| 3927 | if (*q == '?') |
| 3928 | q++; |
| 3929 | lua_pushlstring(L, q, end - q); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3930 | lua_settable(L, -3); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3931 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3932 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3933 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 3934 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 3935 | enum htx_blk_type type = htx_get_blk_type(blk); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 3936 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 3937 | if (type == HTX_BLK_EOM || type == HTX_BLK_TLR || type == HTX_BLK_EOT) |
| 3938 | break; |
| 3939 | if (type == HTX_BLK_DATA) |
| 3940 | len += htx_get_blksz(blk); |
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 | if (htx->extra != ULLONG_MAX) |
| 3943 | len += htx->extra; |
| 3944 | |
| 3945 | /* Stores the request path. */ |
| 3946 | lua_pushstring(L, "length"); |
| 3947 | lua_pushinteger(L, len); |
| 3948 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3949 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3950 | /* Create an empty array of HTTP request headers. */ |
| 3951 | lua_pushstring(L, "response"); |
| 3952 | lua_newtable(L); |
| 3953 | lua_settable(L, -3); |
Thierry FOURNIER | 04c57b3 | 2015-03-18 13:43:10 +0100 | [diff] [blame] | 3954 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 3955 | /* Pop a class stream metatable and affect it to the table. */ |
| 3956 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_applet_http_ref); |
| 3957 | lua_setmetatable(L, -2); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 3958 | |
| 3959 | return 1; |
| 3960 | } |
| 3961 | |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3962 | __LJMP static int hlua_applet_http_set_var(lua_State *L) |
| 3963 | { |
| 3964 | struct hlua_appctx *appctx; |
| 3965 | struct stream *s; |
| 3966 | const char *name; |
| 3967 | size_t len; |
| 3968 | struct sample smp; |
| 3969 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3970 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 3971 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3972 | |
| 3973 | /* It is useles to retrieve the stream, but this function |
| 3974 | * runs only in a stream context. |
| 3975 | */ |
| 3976 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 3977 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 3978 | s = appctx->htxn.s; |
| 3979 | |
| 3980 | /* Converts the third argument in a sample. */ |
| 3981 | hlua_lua2smp(L, 3, &smp); |
| 3982 | |
| 3983 | /* Store the sample in a variable. */ |
| 3984 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 3985 | |
| 3986 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 3987 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 3988 | else |
| 3989 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 3990 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 3991 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 3992 | } |
| 3993 | |
| 3994 | __LJMP static int hlua_applet_http_unset_var(lua_State *L) |
| 3995 | { |
| 3996 | struct hlua_appctx *appctx; |
| 3997 | struct stream *s; |
| 3998 | const char *name; |
| 3999 | size_t len; |
| 4000 | struct sample smp; |
| 4001 | |
| 4002 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 4003 | |
| 4004 | /* It is useles to retrieve the stream, but this function |
| 4005 | * runs only in a stream context. |
| 4006 | */ |
| 4007 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4008 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4009 | s = appctx->htxn.s; |
| 4010 | |
| 4011 | /* Unset the variable. */ |
| 4012 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 4013 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 4014 | return 1; |
Thierry FOURNIER / OZON.IO | 4394a2c | 2016-12-12 12:31:54 +0100 | [diff] [blame] | 4015 | } |
| 4016 | |
| 4017 | __LJMP static int hlua_applet_http_get_var(lua_State *L) |
| 4018 | { |
| 4019 | struct hlua_appctx *appctx; |
| 4020 | struct stream *s; |
| 4021 | const char *name; |
| 4022 | size_t len; |
| 4023 | struct sample smp; |
| 4024 | |
| 4025 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 4026 | |
| 4027 | /* It is useles to retrieve the stream, but this function |
| 4028 | * runs only in a stream context. |
| 4029 | */ |
| 4030 | appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4031 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4032 | s = appctx->htxn.s; |
| 4033 | |
| 4034 | smp_set_owner(&smp, s->be, s->sess, s, 0); |
| 4035 | if (!vars_get_by_name(name, len, &smp)) { |
| 4036 | lua_pushnil(L); |
| 4037 | return 1; |
| 4038 | } |
| 4039 | |
| 4040 | return hlua_smp2lua(L, &smp); |
| 4041 | } |
| 4042 | |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4043 | __LJMP static int hlua_applet_http_set_priv(lua_State *L) |
| 4044 | { |
| 4045 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4046 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4047 | struct hlua *hlua; |
| 4048 | |
| 4049 | /* 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] | 4050 | if (!s->hlua) |
| 4051 | return 0; |
| 4052 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4053 | |
| 4054 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 4055 | |
| 4056 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 4057 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4058 | |
| 4059 | /* Get and store new value. */ |
| 4060 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 4061 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 4062 | |
| 4063 | return 0; |
| 4064 | } |
| 4065 | |
| 4066 | __LJMP static int hlua_applet_http_get_priv(lua_State *L) |
| 4067 | { |
| 4068 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4069 | struct stream *s = appctx->htxn.s; |
Thierry FOURNIER | 3b0a6d4 | 2016-12-16 08:48:32 +0100 | [diff] [blame] | 4070 | struct hlua *hlua; |
| 4071 | |
| 4072 | /* 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] | 4073 | if (!s->hlua) { |
| 4074 | lua_pushnil(L); |
| 4075 | return 1; |
| 4076 | } |
| 4077 | hlua = s->hlua; |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 4078 | |
| 4079 | /* Push configuration index in the stack. */ |
| 4080 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 4081 | |
| 4082 | return 1; |
| 4083 | } |
| 4084 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4085 | /* If expected data not yet available, it returns a yield. This function |
| 4086 | * consumes the data in the buffer. It returns a string containing the |
| 4087 | * data. This string can be empty. |
| 4088 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4089 | __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] | 4090 | { |
| 4091 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4092 | struct stream_interface *si = appctx->appctx->owner; |
| 4093 | struct channel *req = si_oc(si); |
| 4094 | struct htx *htx; |
| 4095 | struct htx_blk *blk; |
| 4096 | size_t count; |
| 4097 | int stop = 0; |
| 4098 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4099 | htx = htx_from_buf(&req->buf); |
| 4100 | count = co_data(req); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 4101 | blk = htx_get_first_blk(htx); |
Christopher Faulet | cc26b13 | 2018-12-18 21:20:57 +0100 | [diff] [blame] | 4102 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4103 | while (count && !stop && blk) { |
| 4104 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4105 | uint32_t sz = htx_get_blksz(blk); |
| 4106 | struct ist v; |
| 4107 | uint32_t vlen; |
| 4108 | char *nl; |
| 4109 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4110 | if (type == HTX_BLK_EOM) { |
| 4111 | stop = 1; |
| 4112 | break; |
| 4113 | } |
| 4114 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4115 | vlen = sz; |
| 4116 | if (vlen > count) { |
| 4117 | if (type != HTX_BLK_DATA) |
| 4118 | break; |
| 4119 | vlen = count; |
| 4120 | } |
| 4121 | |
| 4122 | switch (type) { |
| 4123 | case HTX_BLK_UNUSED: |
| 4124 | break; |
| 4125 | |
| 4126 | case HTX_BLK_DATA: |
| 4127 | v = htx_get_blk_value(htx, blk); |
| 4128 | v.len = vlen; |
| 4129 | nl = istchr(v, '\n'); |
| 4130 | if (nl != NULL) { |
| 4131 | stop = 1; |
| 4132 | vlen = nl - v.ptr + 1; |
| 4133 | } |
| 4134 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4135 | break; |
| 4136 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4137 | case HTX_BLK_TLR: |
| 4138 | case HTX_BLK_EOM: |
| 4139 | stop = 1; |
| 4140 | break; |
| 4141 | |
| 4142 | default: |
| 4143 | break; |
| 4144 | } |
| 4145 | |
| 4146 | co_set_data(req, co_data(req) - vlen); |
| 4147 | count -= vlen; |
| 4148 | if (sz == vlen) |
| 4149 | blk = htx_remove_blk(htx, blk); |
| 4150 | else { |
| 4151 | htx_cut_data_blk(htx, blk, vlen); |
| 4152 | break; |
| 4153 | } |
| 4154 | } |
| 4155 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4156 | htx_to_buf(htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4157 | if (!stop) { |
| 4158 | si_cant_get(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4159 | 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] | 4160 | } |
| 4161 | |
| 4162 | /* return the result. */ |
| 4163 | luaL_pushresult(&appctx->b); |
| 4164 | return 1; |
| 4165 | } |
| 4166 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4167 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4168 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4169 | __LJMP static int hlua_applet_http_getline(lua_State *L) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4170 | { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4171 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4172 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4173 | /* Initialise the string catenation. */ |
| 4174 | luaL_buffinit(L, &appctx->b); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4175 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4176 | return MAY_LJMP(hlua_applet_http_getline_yield(L, 0, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4177 | } |
| 4178 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4179 | /* If expected data not yet available, it returns a yield. This function |
| 4180 | * consumes the data in the buffer. It returns a string containing the |
| 4181 | * data. This string can be empty. |
| 4182 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4183 | __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] | 4184 | { |
| 4185 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4186 | struct stream_interface *si = appctx->appctx->owner; |
| 4187 | struct channel *req = si_oc(si); |
| 4188 | struct htx *htx; |
| 4189 | struct htx_blk *blk; |
| 4190 | size_t count; |
| 4191 | int len; |
| 4192 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4193 | htx = htx_from_buf(&req->buf); |
| 4194 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4195 | count = co_data(req); |
Christopher Faulet | 29f1758 | 2019-05-23 11:03:26 +0200 | [diff] [blame] | 4196 | blk = htx_get_head_blk(htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4197 | while (count && len && blk) { |
| 4198 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4199 | uint32_t sz = htx_get_blksz(blk); |
| 4200 | struct ist v; |
| 4201 | uint32_t vlen; |
| 4202 | |
Christopher Faulet | e461e34 | 2018-12-18 16:43:35 +0100 | [diff] [blame] | 4203 | if (type == HTX_BLK_EOM) { |
| 4204 | len = 0; |
| 4205 | break; |
| 4206 | } |
| 4207 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4208 | vlen = sz; |
| 4209 | if (len > 0 && vlen > len) |
| 4210 | vlen = len; |
| 4211 | if (vlen > count) { |
| 4212 | if (type != HTX_BLK_DATA) |
| 4213 | break; |
| 4214 | vlen = count; |
| 4215 | } |
| 4216 | |
| 4217 | switch (type) { |
| 4218 | case HTX_BLK_UNUSED: |
| 4219 | break; |
| 4220 | |
| 4221 | case HTX_BLK_DATA: |
| 4222 | v = htx_get_blk_value(htx, blk); |
| 4223 | luaL_addlstring(&appctx->b, v.ptr, vlen); |
| 4224 | break; |
| 4225 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4226 | case HTX_BLK_TLR: |
| 4227 | case HTX_BLK_EOM: |
| 4228 | len = 0; |
| 4229 | break; |
| 4230 | |
| 4231 | default: |
| 4232 | break; |
| 4233 | } |
| 4234 | |
| 4235 | co_set_data(req, co_data(req) - vlen); |
| 4236 | count -= vlen; |
| 4237 | if (len > 0) |
| 4238 | len -= vlen; |
| 4239 | if (sz == vlen) |
| 4240 | blk = htx_remove_blk(htx, blk); |
| 4241 | else { |
| 4242 | htx_cut_data_blk(htx, blk, vlen); |
| 4243 | break; |
| 4244 | } |
| 4245 | } |
| 4246 | |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4247 | htx_to_buf(htx, &req->buf); |
| 4248 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4249 | /* If we are no other data available, yield waiting for new data. */ |
| 4250 | if (len) { |
| 4251 | if (len > 0) { |
| 4252 | lua_pushinteger(L, len); |
| 4253 | lua_replace(L, 2); |
| 4254 | } |
| 4255 | si_cant_get(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4256 | 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] | 4257 | } |
| 4258 | |
| 4259 | /* return the result. */ |
| 4260 | luaL_pushresult(&appctx->b); |
| 4261 | return 1; |
| 4262 | } |
| 4263 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4264 | /* Check arguments for the function "hlua_channel_get_yield". */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4265 | __LJMP static int hlua_applet_http_recv(lua_State *L) |
| 4266 | { |
| 4267 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4268 | int len = -1; |
| 4269 | |
| 4270 | /* Check arguments. */ |
| 4271 | if (lua_gettop(L) > 2) |
| 4272 | WILL_LJMP(luaL_error(L, "The 'recv' function requires between 1 and 2 arguments.")); |
| 4273 | if (lua_gettop(L) >= 2) { |
| 4274 | len = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 4275 | lua_pop(L, 1); |
| 4276 | } |
| 4277 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4278 | lua_pushinteger(L, len); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4279 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4280 | /* Initialise the string catenation. */ |
| 4281 | luaL_buffinit(L, &appctx->b); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4282 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4283 | return MAY_LJMP(hlua_applet_http_recv_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4284 | } |
| 4285 | |
| 4286 | /* Append data in the output side of the buffer. This data is immediately |
| 4287 | * sent. The function returns the amount of data written. If the buffer |
| 4288 | * cannot contain the data, the function yields. The function returns -1 |
| 4289 | * if the channel is closed. |
| 4290 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4291 | __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] | 4292 | { |
| 4293 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4294 | struct stream_interface *si = appctx->appctx->owner; |
| 4295 | struct channel *res = si_ic(si); |
| 4296 | struct htx *htx = htx_from_buf(&res->buf); |
| 4297 | const char *data; |
| 4298 | size_t len; |
| 4299 | int l = MAY_LJMP(luaL_checkinteger(L, 3)); |
| 4300 | int max; |
| 4301 | |
Christopher Faulet | 9060fc0 | 2019-07-03 11:39:30 +0200 | [diff] [blame] | 4302 | max = htx_get_max_blksz(htx, channel_htx_recv_max(res, htx)); |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4303 | if (!max) |
| 4304 | goto snd_yield; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4305 | |
| 4306 | data = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 4307 | |
| 4308 | /* Get the max amount of data which can write as input in the channel. */ |
| 4309 | if (max > (len - l)) |
| 4310 | max = len - l; |
| 4311 | |
| 4312 | /* Copy data. */ |
Willy Tarreau | 0a7ef02 | 2019-05-28 10:30:11 +0200 | [diff] [blame] | 4313 | max = htx_add_data(htx, ist2(data + l, max)); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4314 | channel_add_input(res, max); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4315 | |
| 4316 | /* update counters. */ |
| 4317 | l += max; |
| 4318 | lua_pop(L, 1); |
| 4319 | lua_pushinteger(L, l); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4320 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4321 | /* If some data is not send, declares the situation to the |
| 4322 | * applet, and returns a yield. |
| 4323 | */ |
| 4324 | if (l < len) { |
Christopher Faulet | 4b0e9b2 | 2019-01-09 12:16:58 +0100 | [diff] [blame] | 4325 | snd_yield: |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 4326 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4327 | si_rx_room_blk(si); |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 4328 | 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] | 4329 | } |
| 4330 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4331 | htx_to_buf(htx, &res->buf); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4332 | return 1; |
| 4333 | } |
| 4334 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4335 | /* Just a wrapper of "hlua_applet_send_yield". This wrapper permits |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4336 | * yield the LUA process, and resume it without checking the |
| 4337 | * input arguments. |
| 4338 | */ |
| 4339 | __LJMP static int hlua_applet_http_send(lua_State *L) |
| 4340 | { |
| 4341 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4342 | |
| 4343 | /* We want to send some data. Headers must be sent. */ |
| 4344 | if (!(appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
| 4345 | hlua_pusherror(L, "Lua: 'send' you must call start_response() before sending data."); |
| 4346 | WILL_LJMP(lua_error(L)); |
| 4347 | } |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4348 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4349 | /* This integer is used for followinf the amount of data sent. */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4350 | lua_pushinteger(L, 0); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4351 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4352 | return MAY_LJMP(hlua_applet_http_send_yield(L, 0, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4353 | } |
| 4354 | |
| 4355 | __LJMP static int hlua_applet_http_addheader(lua_State *L) |
| 4356 | { |
| 4357 | const char *name; |
| 4358 | int ret; |
| 4359 | |
| 4360 | MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4361 | name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 4362 | MAY_LJMP(luaL_checkstring(L, 3)); |
| 4363 | |
| 4364 | /* Push in the stack the "response" entry. */ |
| 4365 | ret = lua_getfield(L, 1, "response"); |
| 4366 | if (ret != LUA_TTABLE) { |
| 4367 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response'] " |
| 4368 | "is expected as an array. %s found", lua_typename(L, ret)); |
| 4369 | WILL_LJMP(lua_error(L)); |
| 4370 | } |
| 4371 | |
| 4372 | /* check if the header is already registered if it is not |
| 4373 | * the case, register it. |
| 4374 | */ |
| 4375 | ret = lua_getfield(L, -1, name); |
| 4376 | if (ret == LUA_TNIL) { |
| 4377 | |
| 4378 | /* Entry not found. */ |
| 4379 | lua_pop(L, 1); /* remove the nil. The "response" table is the top of the stack. */ |
| 4380 | |
| 4381 | /* Insert the new header name in the array in the top of the stack. |
| 4382 | * It left the new array in the top of the stack. |
| 4383 | */ |
| 4384 | lua_newtable(L); |
| 4385 | lua_pushvalue(L, 2); |
| 4386 | lua_pushvalue(L, -2); |
| 4387 | lua_settable(L, -4); |
| 4388 | |
| 4389 | } else if (ret != LUA_TTABLE) { |
| 4390 | |
| 4391 | /* corruption error. */ |
| 4392 | hlua_pusherror(L, "Lua: 'add_header' internal error: AppletHTTP['response']['%s'] " |
| 4393 | "is expected as an array. %s found", name, lua_typename(L, ret)); |
| 4394 | WILL_LJMP(lua_error(L)); |
| 4395 | } |
| 4396 | |
| 4397 | /* Now the top od thestack is an array of values. We push |
| 4398 | * the header value as new entry. |
| 4399 | */ |
| 4400 | lua_pushvalue(L, 3); |
| 4401 | ret = lua_rawlen(L, -2); |
| 4402 | lua_rawseti(L, -2, ret + 1); |
| 4403 | lua_pushboolean(L, 1); |
| 4404 | return 1; |
| 4405 | } |
| 4406 | |
| 4407 | __LJMP static int hlua_applet_http_status(lua_State *L) |
| 4408 | { |
| 4409 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4410 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4411 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4412 | |
| 4413 | if (status < 100 || status > 599) { |
| 4414 | lua_pushboolean(L, 0); |
| 4415 | return 1; |
| 4416 | } |
| 4417 | |
| 4418 | appctx->appctx->ctx.hlua_apphttp.status = status; |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 4419 | appctx->appctx->ctx.hlua_apphttp.reason = reason; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4420 | lua_pushboolean(L, 1); |
| 4421 | return 1; |
| 4422 | } |
| 4423 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4424 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4425 | __LJMP static int hlua_applet_http_send_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4426 | { |
| 4427 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4428 | struct stream_interface *si = appctx->appctx->owner; |
| 4429 | struct channel *res = si_ic(si); |
| 4430 | struct htx *htx; |
| 4431 | struct htx_sl *sl; |
| 4432 | struct h1m h1m; |
| 4433 | const char *status, *reason; |
| 4434 | const char *name, *value; |
| 4435 | size_t nlen, vlen; |
| 4436 | unsigned int flags; |
| 4437 | |
| 4438 | /* Send the message at once. */ |
| 4439 | htx = htx_from_buf(&res->buf); |
| 4440 | h1m_init_res(&h1m); |
| 4441 | |
| 4442 | /* Use the same http version than the request. */ |
| 4443 | status = ultoa_r(appctx->appctx->ctx.hlua_apphttp.status, trash.area, trash.size); |
| 4444 | reason = appctx->appctx->ctx.hlua_apphttp.reason; |
| 4445 | if (reason == NULL) |
| 4446 | reason = http_get_reason(appctx->appctx->ctx.hlua_apphttp.status); |
| 4447 | if (appctx->appctx->ctx.hlua_apphttp.flags & APPLET_HTTP11) { |
| 4448 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 4449 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), ist(status), ist(reason)); |
| 4450 | } |
| 4451 | else { |
| 4452 | flags = HTX_SL_F_IS_RESP; |
| 4453 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), ist(status), ist(reason)); |
| 4454 | } |
| 4455 | if (!sl) { |
| 4456 | hlua_pusherror(L, "Lua applet http '%s': Failed to create response.\n", |
| 4457 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4458 | WILL_LJMP(lua_error(L)); |
| 4459 | } |
| 4460 | sl->info.res.status = appctx->appctx->ctx.hlua_apphttp.status; |
| 4461 | |
| 4462 | /* Get the array associated to the field "response" in the object AppletHTTP. */ |
| 4463 | lua_pushvalue(L, 0); |
| 4464 | if (lua_getfield(L, 1, "response") != LUA_TTABLE) { |
| 4465 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'] missing.\n", |
| 4466 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4467 | WILL_LJMP(lua_error(L)); |
| 4468 | } |
| 4469 | |
| 4470 | /* Browse the list of headers. */ |
| 4471 | lua_pushnil(L); |
| 4472 | while(lua_next(L, -2) != 0) { |
| 4473 | /* We expect a string as -2. */ |
| 4474 | if (lua_type(L, -2) != LUA_TSTRING) { |
| 4475 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response'][] element must be a string. got %s.\n", |
| 4476 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4477 | lua_typename(L, lua_type(L, -2))); |
| 4478 | WILL_LJMP(lua_error(L)); |
| 4479 | } |
| 4480 | name = lua_tolstring(L, -2, &nlen); |
| 4481 | |
| 4482 | /* We expect an array as -1. */ |
| 4483 | if (lua_type(L, -1) != LUA_TTABLE) { |
| 4484 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'] element must be an table. got %s.\n", |
| 4485 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4486 | name, |
| 4487 | lua_typename(L, lua_type(L, -1))); |
| 4488 | WILL_LJMP(lua_error(L)); |
| 4489 | } |
| 4490 | |
| 4491 | /* Browse the table who is on the top of the stack. */ |
| 4492 | lua_pushnil(L); |
| 4493 | while(lua_next(L, -2) != 0) { |
| 4494 | int id; |
| 4495 | |
| 4496 | /* We expect a number as -2. */ |
| 4497 | if (lua_type(L, -2) != LUA_TNUMBER) { |
| 4498 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][] element must be a number. got %s.\n", |
| 4499 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4500 | name, |
| 4501 | lua_typename(L, lua_type(L, -2))); |
| 4502 | WILL_LJMP(lua_error(L)); |
| 4503 | } |
| 4504 | id = lua_tointeger(L, -2); |
| 4505 | |
| 4506 | /* We expect a string as -2. */ |
| 4507 | if (lua_type(L, -1) != LUA_TSTRING) { |
| 4508 | hlua_pusherror(L, "Lua applet http '%s': AppletHTTP['response']['%s'][%d] element must be a string. got %s.\n", |
| 4509 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4510 | name, id, |
| 4511 | lua_typename(L, lua_type(L, -1))); |
| 4512 | WILL_LJMP(lua_error(L)); |
| 4513 | } |
| 4514 | value = lua_tolstring(L, -1, &vlen); |
| 4515 | |
| 4516 | /* Simple Protocol checks. */ |
| 4517 | if (isteqi(ist2(name, nlen), ist("transfer-encoding"))) |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 4518 | h1_parse_xfer_enc_header(&h1m, ist2(value, vlen)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4519 | else if (isteqi(ist2(name, nlen), ist("content-length"))) { |
| 4520 | struct ist v = ist2(value, vlen); |
| 4521 | int ret; |
| 4522 | |
| 4523 | ret = h1_parse_cont_len_header(&h1m, &v); |
| 4524 | if (ret < 0) { |
| 4525 | hlua_pusherror(L, "Lua applet http '%s': Invalid '%s' header.\n", |
| 4526 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4527 | name); |
| 4528 | WILL_LJMP(lua_error(L)); |
| 4529 | } |
| 4530 | else if (ret == 0) |
| 4531 | goto next; /* Skip it */ |
| 4532 | } |
| 4533 | |
| 4534 | /* Add a new header */ |
| 4535 | if (!htx_add_header(htx, ist2(name, nlen), ist2(value, vlen))) { |
| 4536 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header '%s' in the response.\n", |
| 4537 | appctx->appctx->rule->arg.hlua_rule->fcn.name, |
| 4538 | name); |
| 4539 | WILL_LJMP(lua_error(L)); |
| 4540 | } |
| 4541 | next: |
| 4542 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4543 | lua_pop(L, 1); |
| 4544 | } |
| 4545 | |
| 4546 | /* Remove the array from the stack, and get next element with a remaining string. */ |
| 4547 | lua_pop(L, 1); |
| 4548 | } |
| 4549 | |
| 4550 | if (h1m.flags & H1_MF_CHNK) |
| 4551 | h1m.flags &= ~H1_MF_CLEN; |
| 4552 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 4553 | h1m.flags |= H1_MF_XFER_LEN; |
| 4554 | |
| 4555 | /* Uset HTX start-line flags */ |
| 4556 | if (h1m.flags & H1_MF_XFER_ENC) |
| 4557 | flags |= HTX_SL_F_XFER_ENC; |
| 4558 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 4559 | flags |= HTX_SL_F_XFER_LEN; |
| 4560 | if (h1m.flags & H1_MF_CHNK) |
| 4561 | flags |= HTX_SL_F_CHNK; |
| 4562 | else if (h1m.flags & H1_MF_CLEN) |
| 4563 | flags |= HTX_SL_F_CLEN; |
| 4564 | if (h1m.body_len == 0) |
| 4565 | flags |= HTX_SL_F_BODYLESS; |
| 4566 | } |
| 4567 | sl->flags |= flags; |
| 4568 | |
| 4569 | /* If we dont have a content-length set, and the HTTP version is 1.1 |
| 4570 | * and the status code implies the presence of a message body, we must |
| 4571 | * announce a transfer encoding chunked. This is required by haproxy |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 4572 | * for the keepalive compliance. If the applet announces a transfer-encoding |
| 4573 | * chunked itself, don't do anything. |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4574 | */ |
| 4575 | if ((flags & (HTX_SL_F_VER_11|HTX_SL_F_XFER_LEN)) == HTX_SL_F_VER_11 && |
| 4576 | appctx->appctx->ctx.hlua_apphttp.status >= 200 && |
| 4577 | appctx->appctx->ctx.hlua_apphttp.status != 204 && |
| 4578 | appctx->appctx->ctx.hlua_apphttp.status != 304) { |
| 4579 | /* Add a new header */ |
| 4580 | sl->flags |= (HTX_SL_F_XFER_ENC|H1_MF_CHNK|H1_MF_XFER_LEN); |
| 4581 | if (!htx_add_header(htx, ist("transfer-encoding"), ist("chunked"))) { |
| 4582 | hlua_pusherror(L, "Lua applet http '%s': Failed to add header 'transfer-encoding' in the response.\n", |
| 4583 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4584 | WILL_LJMP(lua_error(L)); |
| 4585 | } |
| 4586 | } |
| 4587 | |
| 4588 | /* Finalize headers. */ |
| 4589 | if (!htx_add_endof(htx, HTX_BLK_EOH)) { |
| 4590 | hlua_pusherror(L, "Lua applet http '%s': Failed create the response.\n", |
| 4591 | appctx->appctx->rule->arg.hlua_rule->fcn.name); |
| 4592 | WILL_LJMP(lua_error(L)); |
| 4593 | } |
| 4594 | |
| 4595 | if (htx_used_space(htx) > b_size(&res->buf) - global.tune.maxrewrite) { |
| 4596 | b_reset(&res->buf); |
| 4597 | hlua_pusherror(L, "Lua: 'start_response': response header block too big"); |
| 4598 | WILL_LJMP(lua_error(L)); |
| 4599 | } |
| 4600 | |
| 4601 | htx_to_buf(htx, &res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 4602 | channel_add_input(res, htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4603 | |
| 4604 | /* Headers sent, set the flag. */ |
| 4605 | appctx->appctx->ctx.hlua_apphttp.flags |= APPLET_HDR_SENT; |
| 4606 | return 0; |
| 4607 | |
| 4608 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4609 | /* We will build the status line and the headers of the HTTP response. |
| 4610 | * We will try send at once if its not possible, we give back the hand |
| 4611 | * waiting for more room. |
| 4612 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4613 | __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] | 4614 | { |
| 4615 | struct hlua_appctx *appctx = MAY_LJMP(hlua_checkapplet_http(L, 1)); |
| 4616 | struct stream_interface *si = appctx->appctx->owner; |
| 4617 | struct channel *res = si_ic(si); |
| 4618 | |
| 4619 | if (co_data(res)) { |
| 4620 | si_rx_room_blk(si); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4621 | 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] | 4622 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4623 | return MAY_LJMP(hlua_applet_http_send_response(L)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4624 | } |
| 4625 | |
| 4626 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4627 | __LJMP static int hlua_applet_http_start_response(lua_State *L) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4628 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4629 | return MAY_LJMP(hlua_applet_http_start_response_yield(L, 0, 0)); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4630 | } |
| 4631 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4632 | /* |
| 4633 | * |
| 4634 | * |
| 4635 | * Class HTTP |
| 4636 | * |
| 4637 | * |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 4638 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4639 | |
| 4640 | /* Returns a struct hlua_txn if the stack entry "ud" is |
| 4641 | * a class stream, otherwise it throws an error. |
| 4642 | */ |
| 4643 | __LJMP static struct hlua_txn *hlua_checkhttp(lua_State *L, int ud) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4644 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4645 | return MAY_LJMP(hlua_checkudata(L, ud, class_http_ref)); |
| 4646 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4647 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4648 | /* This function creates and push in the stack a HTTP object |
| 4649 | * according with a current TXN. |
| 4650 | */ |
| 4651 | static int hlua_http_new(lua_State *L, struct hlua_txn *txn) |
| 4652 | { |
| 4653 | struct hlua_txn *htxn; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4654 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4655 | /* Check stack size. */ |
| 4656 | if (!lua_checkstack(L, 3)) |
| 4657 | return 0; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4658 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4659 | /* Create the object: obj[0] = userdata. |
| 4660 | * Note that the base of the Converters object is the |
| 4661 | * same than the TXN object. |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4662 | */ |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4663 | lua_newtable(L); |
| 4664 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
| 4665 | lua_rawseti(L, -2, 0); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4666 | |
| 4667 | htxn->s = txn->s; |
| 4668 | htxn->p = txn->p; |
Christopher Faulet | 256b69a | 2019-05-23 11:14:21 +0200 | [diff] [blame] | 4669 | htxn->dir = txn->dir; |
| 4670 | htxn->flags = txn->flags; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4671 | |
| 4672 | /* Pop a class stream metatable and affect it to the table. */ |
| 4673 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_http_ref); |
| 4674 | lua_setmetatable(L, -2); |
| 4675 | |
| 4676 | return 1; |
| 4677 | } |
| 4678 | |
| 4679 | /* This function creates ans returns an array of HTTP headers. |
| 4680 | * This function does not fails. It is used as wrapper with the |
| 4681 | * 2 following functions. |
| 4682 | */ |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4683 | __LJMP static int hlua_http_get_headers(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4684 | { |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4685 | struct htx *htx; |
| 4686 | int32_t pos; |
| 4687 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4688 | /* Create the table. */ |
| 4689 | lua_newtable(L); |
| 4690 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4691 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4692 | htx = htxbuf(&msg->chn->buf); |
| 4693 | for (pos = htx_get_first(htx); pos != -1; pos = htx_get_next(htx, pos)) { |
| 4694 | struct htx_blk *blk = htx_get_blk(htx, pos); |
| 4695 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 4696 | struct ist n, v; |
| 4697 | int len; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4698 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4699 | if (type == HTX_BLK_HDR) { |
| 4700 | n = htx_get_blk_name(htx,blk); |
| 4701 | v = htx_get_blk_value(htx, blk); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4702 | } |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4703 | else if (type == HTX_BLK_EOH) |
| 4704 | break; |
| 4705 | else |
| 4706 | continue; |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4707 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4708 | /* Check for existing entry: |
| 4709 | * assume that the table is on the top of the stack, and |
| 4710 | * push the key in the stack, the function lua_gettable() |
| 4711 | * perform the lookup. |
| 4712 | */ |
| 4713 | lua_pushlstring(L, n.ptr, n.len); |
| 4714 | lua_gettable(L, -2); |
Christopher Faulet | 724a12c | 2018-12-13 22:12:15 +0100 | [diff] [blame] | 4715 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4716 | switch (lua_type(L, -1)) { |
| 4717 | case LUA_TNIL: |
| 4718 | /* Table not found, create it. */ |
| 4719 | lua_pop(L, 1); /* remove the nil value. */ |
| 4720 | lua_pushlstring(L, n.ptr, n.len); /* push the header name as key. */ |
| 4721 | lua_newtable(L); /* create and push empty table. */ |
| 4722 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4723 | lua_rawseti(L, -2, 0); /* index header value (pop it). */ |
| 4724 | 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] | 4725 | break; |
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 | case LUA_TTABLE: |
| 4728 | /* Entry found: push the value in the table. */ |
| 4729 | len = lua_rawlen(L, -1); |
| 4730 | lua_pushlstring(L, v.ptr, v.len); /* push header value. */ |
| 4731 | lua_rawseti(L, -2, len+1); /* index header value (pop it). */ |
| 4732 | lua_pop(L, 1); /* remove the table (it is stored in the main table). */ |
| 4733 | break; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4734 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4735 | default: |
| 4736 | /* Other cases are errors. */ |
| 4737 | hlua_pusherror(L, "internal error during the parsing of headers."); |
| 4738 | WILL_LJMP(lua_error(L)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4739 | } |
| 4740 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4741 | return 1; |
| 4742 | } |
| 4743 | |
| 4744 | __LJMP static int hlua_http_req_get_headers(lua_State *L) |
| 4745 | { |
| 4746 | struct hlua_txn *htxn; |
| 4747 | |
| 4748 | MAY_LJMP(check_args(L, 1, "req_get_headers")); |
| 4749 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4750 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4751 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4752 | WILL_LJMP(lua_error(L)); |
| 4753 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4754 | return hlua_http_get_headers(L, &htxn->s->txn->req); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4755 | } |
| 4756 | |
| 4757 | __LJMP static int hlua_http_res_get_headers(lua_State *L) |
| 4758 | { |
| 4759 | struct hlua_txn *htxn; |
| 4760 | |
| 4761 | MAY_LJMP(check_args(L, 1, "res_get_headers")); |
| 4762 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4763 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4764 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4765 | WILL_LJMP(lua_error(L)); |
| 4766 | |
Christopher Faulet | 9d1332b | 2020-02-24 16:46:16 +0100 | [diff] [blame] | 4767 | return hlua_http_get_headers(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4768 | } |
| 4769 | |
| 4770 | /* This function replace full header, or just a value in |
| 4771 | * the request or in the response. It is a wrapper fir the |
| 4772 | * 4 following functions. |
| 4773 | */ |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4774 | __LJMP static inline int hlua_http_rep_hdr(lua_State *L, struct http_msg *msg, int full) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4775 | { |
| 4776 | size_t name_len; |
| 4777 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4778 | const char *reg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 4779 | const char *value = MAY_LJMP(luaL_checkstring(L, 4)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4780 | struct htx *htx; |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4781 | struct my_regex *re; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4782 | |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4783 | if (!(re = regex_comp(reg, 1, 1, NULL))) |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4784 | WILL_LJMP(luaL_argerror(L, 3, "invalid regex")); |
| 4785 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4786 | htx = htxbuf(&msg->chn->buf); |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4787 | http_replace_hdrs(chn_strm(msg->chn), htx, ist2(name, name_len), value, re, full); |
Dragan Dosen | 2674303 | 2019-04-30 15:54:36 +0200 | [diff] [blame] | 4788 | regex_free(re); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4789 | return 0; |
| 4790 | } |
| 4791 | |
| 4792 | __LJMP static int hlua_http_req_rep_hdr(lua_State *L) |
| 4793 | { |
| 4794 | struct hlua_txn *htxn; |
| 4795 | |
| 4796 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4797 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4798 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4799 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4800 | WILL_LJMP(lua_error(L)); |
| 4801 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4802 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4803 | } |
| 4804 | |
| 4805 | __LJMP static int hlua_http_res_rep_hdr(lua_State *L) |
| 4806 | { |
| 4807 | struct hlua_txn *htxn; |
| 4808 | |
| 4809 | MAY_LJMP(check_args(L, 4, "res_rep_hdr")); |
| 4810 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4811 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4812 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4813 | WILL_LJMP(lua_error(L)); |
| 4814 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4815 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 1)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4816 | } |
| 4817 | |
| 4818 | __LJMP static int hlua_http_req_rep_val(lua_State *L) |
| 4819 | { |
| 4820 | struct hlua_txn *htxn; |
| 4821 | |
| 4822 | MAY_LJMP(check_args(L, 4, "req_rep_hdr")); |
| 4823 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4824 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4825 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4826 | WILL_LJMP(lua_error(L)); |
| 4827 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4828 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->req, 0)); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 4829 | } |
| 4830 | |
| 4831 | __LJMP static int hlua_http_res_rep_val(lua_State *L) |
| 4832 | { |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4833 | struct hlua_txn *htxn; |
| 4834 | |
| 4835 | MAY_LJMP(check_args(L, 4, "res_rep_val")); |
| 4836 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4837 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4838 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4839 | WILL_LJMP(lua_error(L)); |
| 4840 | |
Christopher Faulet | d1914aa | 2020-02-24 16:52:46 +0100 | [diff] [blame] | 4841 | return MAY_LJMP(hlua_http_rep_hdr(L, &htxn->s->txn->rsp, 0)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4842 | } |
| 4843 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 4844 | /* This function deletes all the occurrences of an header. |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4845 | * It is a wrapper for the 2 following functions. |
| 4846 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4847 | __LJMP static inline int hlua_http_del_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4848 | { |
| 4849 | size_t len; |
| 4850 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4851 | struct htx *htx = htxbuf(&msg->chn->buf); |
| 4852 | struct http_hdr_ctx ctx; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4853 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4854 | ctx.blk = NULL; |
| 4855 | while (http_find_header(htx, ist2(name, len), &ctx, 1)) |
| 4856 | http_remove_header(htx, &ctx); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4857 | return 0; |
| 4858 | } |
| 4859 | |
| 4860 | __LJMP static int hlua_http_req_del_hdr(lua_State *L) |
| 4861 | { |
| 4862 | struct hlua_txn *htxn; |
| 4863 | |
| 4864 | MAY_LJMP(check_args(L, 2, "req_del_hdr")); |
| 4865 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4866 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4867 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4868 | WILL_LJMP(lua_error(L)); |
| 4869 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4870 | return hlua_http_del_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4871 | } |
| 4872 | |
| 4873 | __LJMP static int hlua_http_res_del_hdr(lua_State *L) |
| 4874 | { |
| 4875 | struct hlua_txn *htxn; |
| 4876 | |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4877 | MAY_LJMP(check_args(L, 2, "res_del_hdr")); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4878 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4879 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4880 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4881 | WILL_LJMP(lua_error(L)); |
| 4882 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4883 | return hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4884 | } |
| 4885 | |
| 4886 | /* This function adds an header. It is a wrapper used by |
| 4887 | * the 2 following functions. |
| 4888 | */ |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4889 | __LJMP static inline int hlua_http_add_hdr(lua_State *L, struct http_msg *msg) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4890 | { |
| 4891 | size_t name_len; |
| 4892 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
| 4893 | size_t value_len; |
| 4894 | const char *value = MAY_LJMP(luaL_checklstring(L, 3, &value_len)); |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4895 | struct htx *htx = htxbuf(&msg->chn->buf); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4896 | |
Christopher Faulet | a209796 | 2019-07-15 16:25:33 +0200 | [diff] [blame] | 4897 | lua_pushboolean(L, http_add_header(htx, ist2(name, name_len), |
| 4898 | ist2(value, value_len))); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4899 | return 0; |
| 4900 | } |
| 4901 | |
| 4902 | __LJMP static int hlua_http_req_add_hdr(lua_State *L) |
| 4903 | { |
| 4904 | struct hlua_txn *htxn; |
| 4905 | |
| 4906 | MAY_LJMP(check_args(L, 3, "req_add_hdr")); |
| 4907 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4908 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4909 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4910 | WILL_LJMP(lua_error(L)); |
| 4911 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4912 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4913 | } |
| 4914 | |
| 4915 | __LJMP static int hlua_http_res_add_hdr(lua_State *L) |
| 4916 | { |
| 4917 | struct hlua_txn *htxn; |
| 4918 | |
| 4919 | MAY_LJMP(check_args(L, 3, "res_add_hdr")); |
| 4920 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4921 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4922 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4923 | WILL_LJMP(lua_error(L)); |
| 4924 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4925 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4926 | } |
| 4927 | |
| 4928 | static int hlua_http_req_set_hdr(lua_State *L) |
| 4929 | { |
| 4930 | struct hlua_txn *htxn; |
| 4931 | |
| 4932 | MAY_LJMP(check_args(L, 3, "req_set_hdr")); |
| 4933 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4934 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4935 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4936 | WILL_LJMP(lua_error(L)); |
| 4937 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4938 | hlua_http_del_hdr(L, &htxn->s->txn->req); |
| 4939 | return hlua_http_add_hdr(L, &htxn->s->txn->req); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4940 | } |
| 4941 | |
| 4942 | static int hlua_http_res_set_hdr(lua_State *L) |
| 4943 | { |
| 4944 | struct hlua_txn *htxn; |
| 4945 | |
| 4946 | MAY_LJMP(check_args(L, 3, "res_set_hdr")); |
| 4947 | htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 4948 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4949 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4950 | WILL_LJMP(lua_error(L)); |
| 4951 | |
Christopher Faulet | d31c7b3 | 2020-02-25 09:37:57 +0100 | [diff] [blame] | 4952 | hlua_http_del_hdr(L, &htxn->s->txn->rsp); |
| 4953 | return hlua_http_add_hdr(L, &htxn->s->txn->rsp); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4954 | } |
| 4955 | |
| 4956 | /* This function set the method. */ |
| 4957 | static int hlua_http_req_set_meth(lua_State *L) |
| 4958 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4959 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4960 | size_t name_len; |
| 4961 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4962 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4963 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4964 | WILL_LJMP(lua_error(L)); |
| 4965 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4966 | 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] | 4967 | return 1; |
| 4968 | } |
| 4969 | |
| 4970 | /* This function set the method. */ |
| 4971 | static int hlua_http_req_set_path(lua_State *L) |
| 4972 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4973 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4974 | size_t name_len; |
| 4975 | 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] | 4976 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4977 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4978 | WILL_LJMP(lua_error(L)); |
| 4979 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 4980 | 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] | 4981 | return 1; |
| 4982 | } |
| 4983 | |
| 4984 | /* This function set the query-string. */ |
| 4985 | static int hlua_http_req_set_query(lua_State *L) |
| 4986 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 4987 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4988 | size_t name_len; |
| 4989 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4990 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 4991 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 4992 | WILL_LJMP(lua_error(L)); |
| 4993 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 4994 | /* Check length. */ |
| 4995 | if (name_len > trash.size - 1) { |
| 4996 | lua_pushboolean(L, 0); |
| 4997 | return 1; |
| 4998 | } |
| 4999 | |
| 5000 | /* Add the mark question as prefix. */ |
| 5001 | chunk_reset(&trash); |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5002 | trash.area[trash.data++] = '?'; |
| 5003 | memcpy(trash.area + trash.data, name, name_len); |
| 5004 | trash.data += name_len; |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5005 | |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 5006 | lua_pushboolean(L, |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5007 | 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] | 5008 | return 1; |
| 5009 | } |
| 5010 | |
| 5011 | /* This function set the uri. */ |
| 5012 | static int hlua_http_req_set_uri(lua_State *L) |
| 5013 | { |
Willy Tarreau | bcb39cc | 2015-04-06 11:21:44 +0200 | [diff] [blame] | 5014 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5015 | size_t name_len; |
| 5016 | const char *name = MAY_LJMP(luaL_checklstring(L, 2, &name_len)); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5017 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5018 | if (htxn->dir != SMP_OPT_DIR_REQ || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5019 | WILL_LJMP(lua_error(L)); |
| 5020 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5021 | 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] | 5022 | return 1; |
| 5023 | } |
| 5024 | |
Robin H. Johnson | 52f5db2 | 2017-01-01 13:10:52 -0800 | [diff] [blame] | 5025 | /* This function set the response code & optionally reason. */ |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5026 | static int hlua_http_res_set_status(lua_State *L) |
| 5027 | { |
| 5028 | struct hlua_txn *htxn = MAY_LJMP(hlua_checkhttp(L, 1)); |
| 5029 | unsigned int code = MAY_LJMP(luaL_checkinteger(L, 2)); |
Christopher Faulet | 96bff76 | 2019-12-17 13:46:18 +0100 | [diff] [blame] | 5030 | const char *str = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5031 | const struct ist reason = ist2(str, (str ? strlen(str) : 0)); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5032 | |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5033 | if (htxn->dir != SMP_OPT_DIR_RES || !IS_HTX_STRM(htxn->s)) |
Christopher Faulet | 84a6d5b | 2019-07-26 16:17:01 +0200 | [diff] [blame] | 5034 | WILL_LJMP(lua_error(L)); |
| 5035 | |
Christopher Faulet | fc9cfe4 | 2019-07-16 14:54:53 +0200 | [diff] [blame] | 5036 | http_res_set_status(code, reason, htxn->s); |
Thierry FOURNIER | 35d70ef | 2015-08-26 16:21:56 +0200 | [diff] [blame] | 5037 | return 0; |
| 5038 | } |
| 5039 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5040 | /* |
| 5041 | * |
| 5042 | * |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5043 | * Class TXN |
| 5044 | * |
| 5045 | * |
| 5046 | */ |
| 5047 | |
| 5048 | /* Returns a struct hlua_session if the stack entry "ud" is |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5049 | * a class stream, otherwise it throws an error. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5050 | */ |
| 5051 | __LJMP static struct hlua_txn *hlua_checktxn(lua_State *L, int ud) |
| 5052 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 5053 | return MAY_LJMP(hlua_checkudata(L, ud, class_txn_ref)); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5054 | } |
| 5055 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5056 | __LJMP static int hlua_set_var(lua_State *L) |
| 5057 | { |
| 5058 | struct hlua_txn *htxn; |
| 5059 | const char *name; |
| 5060 | size_t len; |
| 5061 | struct sample smp; |
| 5062 | |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5063 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 5064 | WILL_LJMP(luaL_error(L, "'set_var' needs between 3 and 4 arguments")); |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5065 | |
| 5066 | /* It is useles to retrieve the stream, but this function |
| 5067 | * runs only in a stream context. |
| 5068 | */ |
| 5069 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5070 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5071 | |
| 5072 | /* Converts the third argument in a sample. */ |
| 5073 | hlua_lua2smp(L, 3, &smp); |
| 5074 | |
| 5075 | /* Store the sample in a variable. */ |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5076 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 4e172c9 | 2020-05-19 13:49:42 +0200 | [diff] [blame] | 5077 | |
| 5078 | if (lua_gettop(L) == 4 && lua_toboolean(L, 4)) |
| 5079 | lua_pushboolean(L, vars_set_by_name_ifexist(name, len, &smp) != 0); |
| 5080 | else |
| 5081 | lua_pushboolean(L, vars_set_by_name(name, len, &smp) != 0); |
| 5082 | |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5083 | return 1; |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5084 | } |
| 5085 | |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5086 | __LJMP static int hlua_unset_var(lua_State *L) |
| 5087 | { |
| 5088 | struct hlua_txn *htxn; |
| 5089 | const char *name; |
| 5090 | size_t len; |
| 5091 | struct sample smp; |
| 5092 | |
| 5093 | MAY_LJMP(check_args(L, 2, "unset_var")); |
| 5094 | |
| 5095 | /* It is useles to retrieve the stream, but this function |
| 5096 | * runs only in a stream context. |
| 5097 | */ |
| 5098 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5099 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5100 | |
| 5101 | /* Unset the variable. */ |
| 5102 | smp_set_owner(&smp, htxn->p, htxn->s->sess, htxn->s, htxn->dir & SMP_OPT_DIR); |
Tim Duesterhus | 84ebc13 | 2020-05-19 13:49:41 +0200 | [diff] [blame] | 5103 | lua_pushboolean(L, vars_unset_by_name_ifexist(name, len, &smp) != 0); |
| 5104 | return 1; |
Christopher Faulet | 85d79c9 | 2016-11-09 16:54:56 +0100 | [diff] [blame] | 5105 | } |
| 5106 | |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5107 | __LJMP static int hlua_get_var(lua_State *L) |
| 5108 | { |
| 5109 | struct hlua_txn *htxn; |
| 5110 | const char *name; |
| 5111 | size_t len; |
| 5112 | struct sample smp; |
| 5113 | |
| 5114 | MAY_LJMP(check_args(L, 2, "get_var")); |
| 5115 | |
| 5116 | /* It is useles to retrieve the stream, but this function |
| 5117 | * runs only in a stream context. |
| 5118 | */ |
| 5119 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5120 | name = MAY_LJMP(luaL_checklstring(L, 2, &len)); |
| 5121 | |
Willy Tarreau | 7560dd4 | 2016-03-10 16:28:58 +0100 | [diff] [blame] | 5122 | 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] | 5123 | if (!vars_get_by_name(name, len, &smp)) { |
Thierry FOURNIER | 053ba8ad | 2015-06-08 13:05:33 +0200 | [diff] [blame] | 5124 | lua_pushnil(L); |
| 5125 | return 1; |
| 5126 | } |
| 5127 | |
| 5128 | return hlua_smp2lua(L, &smp); |
| 5129 | } |
| 5130 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5131 | __LJMP static int hlua_set_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5132 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5133 | struct hlua *hlua; |
| 5134 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5135 | MAY_LJMP(check_args(L, 2, "set_priv")); |
| 5136 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5137 | /* It is useles to retrieve the stream, but this function |
| 5138 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5139 | */ |
| 5140 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5141 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5142 | |
| 5143 | /* Remove previous value. */ |
Thierry FOURNIER | e068b60 | 2017-04-26 13:27:05 +0200 | [diff] [blame] | 5144 | luaL_unref(L, LUA_REGISTRYINDEX, hlua->Mref); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5145 | |
| 5146 | /* Get and store new value. */ |
| 5147 | lua_pushvalue(L, 2); /* Copy the element 2 at the top of the stack. */ |
| 5148 | hlua->Mref = luaL_ref(L, LUA_REGISTRYINDEX); /* pop the previously pushed value. */ |
| 5149 | |
| 5150 | return 0; |
| 5151 | } |
| 5152 | |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5153 | __LJMP static int hlua_get_priv(lua_State *L) |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5154 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5155 | struct hlua *hlua; |
| 5156 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5157 | MAY_LJMP(check_args(L, 1, "get_priv")); |
| 5158 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 5159 | /* It is useles to retrieve the stream, but this function |
| 5160 | * runs only in a stream context. |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5161 | */ |
| 5162 | MAY_LJMP(hlua_checktxn(L, 1)); |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5163 | hlua = hlua_gethlua(L); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 5164 | |
| 5165 | /* Push configuration index in the stack. */ |
| 5166 | lua_rawgeti(L, LUA_REGISTRYINDEX, hlua->Mref); |
| 5167 | |
| 5168 | return 1; |
| 5169 | } |
| 5170 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5171 | /* Create stack entry containing a class TXN. This function |
| 5172 | * return 0 if the stack does not contains free slots, |
| 5173 | * otherwise it returns 1. |
| 5174 | */ |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5175 | 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] | 5176 | { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5177 | struct hlua_txn *htxn; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5178 | |
| 5179 | /* Check stack size. */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5180 | if (!lua_checkstack(L, 3)) |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5181 | return 0; |
| 5182 | |
| 5183 | /* NOTE: The allocation never fails. The failure |
| 5184 | * throw an error, and the function never returns. |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 5185 | * if the throw is not available, the process is aborted. |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5186 | */ |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5187 | /* Create the object: obj[0] = userdata. */ |
| 5188 | lua_newtable(L); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5189 | htxn = lua_newuserdata(L, sizeof(*htxn)); |
Thierry FOURNIER | 2297bc2 | 2015-03-11 17:43:33 +0100 | [diff] [blame] | 5190 | lua_rawseti(L, -2, 0); |
| 5191 | |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5192 | htxn->s = s; |
| 5193 | htxn->p = p; |
Thierry FOURNIER | c4eebc8 | 2015-11-02 10:01:59 +0100 | [diff] [blame] | 5194 | htxn->dir = dir; |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5195 | htxn->flags = flags; |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5196 | |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5197 | /* Create the "f" field that contains a list of fetches. */ |
| 5198 | lua_pushstring(L, "f"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5199 | if (!hlua_fetches_new(L, htxn, HLUA_F_MAY_USE_HTTP)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5200 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5201 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5202 | |
| 5203 | /* Create the "sf" field that contains a list of stringsafe fetches. */ |
| 5204 | lua_pushstring(L, "sf"); |
Thierry FOURNIER | ca98866 | 2015-12-20 18:43:03 +0100 | [diff] [blame] | 5205 | 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] | 5206 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5207 | lua_rawset(L, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 5208 | |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5209 | /* Create the "c" field that contains a list of converters. */ |
| 5210 | lua_pushstring(L, "c"); |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5211 | if (!hlua_converters_new(L, htxn, 0)) |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5212 | return 0; |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5213 | lua_rawset(L, -3); |
Thierry FOURNIER | 2694a1a | 2015-03-11 20:13:36 +0100 | [diff] [blame] | 5214 | |
| 5215 | /* Create the "sc" field that contains a list of stringsafe converters. */ |
| 5216 | lua_pushstring(L, "sc"); |
Thierry FOURNIER | 7fa0549 | 2015-12-20 18:42:25 +0100 | [diff] [blame] | 5217 | if (!hlua_converters_new(L, htxn, HLUA_F_AS_STRING)) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +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 | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 5220 | |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5221 | /* Create the "req" field that contains the request channel object. */ |
| 5222 | lua_pushstring(L, "req"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5223 | if (!hlua_channel_new(L, &s->req)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5226 | |
| 5227 | /* Create the "res" field that contains the response channel object. */ |
| 5228 | lua_pushstring(L, "res"); |
Willy Tarreau | 2a71af4 | 2015-03-10 13:51:50 +0100 | [diff] [blame] | 5229 | if (!hlua_channel_new(L, &s->res)) |
Thierry FOURNIER | 397826a | 2015-03-11 19:39:09 +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 | 397826a | 2015-03-11 19:39:09 +0100 | [diff] [blame] | 5232 | |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5233 | /* Creates the HTTP object is the current proxy allows http. */ |
| 5234 | lua_pushstring(L, "http"); |
| 5235 | if (p->mode == PR_MODE_HTTP) { |
Willy Tarreau | de49138 | 2015-04-06 11:04:28 +0200 | [diff] [blame] | 5236 | if (!hlua_http_new(L, htxn)) |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5237 | return 0; |
| 5238 | } |
| 5239 | else |
| 5240 | lua_pushnil(L); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 5241 | lua_rawset(L, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 5242 | |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 5243 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5244 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_ref); |
| 5245 | lua_setmetatable(L, -2); |
| 5246 | |
| 5247 | return 1; |
| 5248 | } |
| 5249 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5250 | __LJMP static int hlua_txn_deflog(lua_State *L) |
| 5251 | { |
| 5252 | const char *msg; |
| 5253 | struct hlua_txn *htxn; |
| 5254 | |
| 5255 | MAY_LJMP(check_args(L, 2, "deflog")); |
| 5256 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5257 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5258 | |
| 5259 | hlua_sendlog(htxn->s->be, htxn->s->logs.level, msg); |
| 5260 | return 0; |
| 5261 | } |
| 5262 | |
| 5263 | __LJMP static int hlua_txn_log(lua_State *L) |
| 5264 | { |
| 5265 | int level; |
| 5266 | const char *msg; |
| 5267 | struct hlua_txn *htxn; |
| 5268 | |
| 5269 | MAY_LJMP(check_args(L, 3, "log")); |
| 5270 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5271 | level = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5272 | msg = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5273 | |
| 5274 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5275 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5276 | |
| 5277 | hlua_sendlog(htxn->s->be, level, msg); |
| 5278 | return 0; |
| 5279 | } |
| 5280 | |
| 5281 | __LJMP static int hlua_txn_log_debug(lua_State *L) |
| 5282 | { |
| 5283 | const char *msg; |
| 5284 | struct hlua_txn *htxn; |
| 5285 | |
| 5286 | MAY_LJMP(check_args(L, 2, "Debug")); |
| 5287 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5288 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5289 | hlua_sendlog(htxn->s->be, LOG_DEBUG, msg); |
| 5290 | return 0; |
| 5291 | } |
| 5292 | |
| 5293 | __LJMP static int hlua_txn_log_info(lua_State *L) |
| 5294 | { |
| 5295 | const char *msg; |
| 5296 | struct hlua_txn *htxn; |
| 5297 | |
| 5298 | MAY_LJMP(check_args(L, 2, "Info")); |
| 5299 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5300 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5301 | hlua_sendlog(htxn->s->be, LOG_INFO, msg); |
| 5302 | return 0; |
| 5303 | } |
| 5304 | |
| 5305 | __LJMP static int hlua_txn_log_warning(lua_State *L) |
| 5306 | { |
| 5307 | const char *msg; |
| 5308 | struct hlua_txn *htxn; |
| 5309 | |
| 5310 | MAY_LJMP(check_args(L, 2, "Warning")); |
| 5311 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5312 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5313 | hlua_sendlog(htxn->s->be, LOG_WARNING, msg); |
| 5314 | return 0; |
| 5315 | } |
| 5316 | |
| 5317 | __LJMP static int hlua_txn_log_alert(lua_State *L) |
| 5318 | { |
| 5319 | const char *msg; |
| 5320 | struct hlua_txn *htxn; |
| 5321 | |
| 5322 | MAY_LJMP(check_args(L, 2, "Alert")); |
| 5323 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5324 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5325 | hlua_sendlog(htxn->s->be, LOG_ALERT, msg); |
| 5326 | return 0; |
| 5327 | } |
| 5328 | |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5329 | __LJMP static int hlua_txn_set_loglevel(lua_State *L) |
| 5330 | { |
| 5331 | struct hlua_txn *htxn; |
| 5332 | int ll; |
| 5333 | |
| 5334 | MAY_LJMP(check_args(L, 2, "set_loglevel")); |
| 5335 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5336 | ll = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5337 | |
| 5338 | if (ll < 0 || ll > 7) |
| 5339 | WILL_LJMP(luaL_argerror(L, 2, "Bad log level. It must be between 0 and 7")); |
| 5340 | |
| 5341 | htxn->s->logs.level = ll; |
| 5342 | return 0; |
| 5343 | } |
| 5344 | |
| 5345 | __LJMP static int hlua_txn_set_tos(lua_State *L) |
| 5346 | { |
| 5347 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5348 | int tos; |
| 5349 | |
| 5350 | MAY_LJMP(check_args(L, 2, "set_tos")); |
| 5351 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5352 | tos = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5353 | |
Willy Tarreau | 1a18b54 | 2018-12-11 16:37:42 +0100 | [diff] [blame] | 5354 | conn_set_tos(objt_conn(htxn->s->sess->origin), tos); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5355 | return 0; |
| 5356 | } |
| 5357 | |
| 5358 | __LJMP static int hlua_txn_set_mark(lua_State *L) |
| 5359 | { |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5360 | struct hlua_txn *htxn; |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5361 | int mark; |
| 5362 | |
| 5363 | MAY_LJMP(check_args(L, 2, "set_mark")); |
| 5364 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5365 | mark = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5366 | |
Lukas Tribus | 579e3e3 | 2019-08-11 18:03:45 +0200 | [diff] [blame] | 5367 | conn_set_mark(objt_conn(htxn->s->sess->origin), mark); |
Thierry FOURNIER | 2cce353 | 2015-03-16 12:04:16 +0100 | [diff] [blame] | 5368 | return 0; |
| 5369 | } |
| 5370 | |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 5371 | __LJMP static int hlua_txn_set_priority_class(lua_State *L) |
| 5372 | { |
| 5373 | struct hlua_txn *htxn; |
| 5374 | |
| 5375 | MAY_LJMP(check_args(L, 2, "set_priority_class")); |
| 5376 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5377 | htxn->s->priority_class = queue_limit_class(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5378 | return 0; |
| 5379 | } |
| 5380 | |
| 5381 | __LJMP static int hlua_txn_set_priority_offset(lua_State *L) |
| 5382 | { |
| 5383 | struct hlua_txn *htxn; |
| 5384 | |
| 5385 | MAY_LJMP(check_args(L, 2, "set_priority_offset")); |
| 5386 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
| 5387 | htxn->s->priority_offset = queue_limit_offset(MAY_LJMP(luaL_checkinteger(L, 2))); |
| 5388 | return 0; |
| 5389 | } |
| 5390 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5391 | /* Forward the Reply object to the client. This function converts the reply in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 5392 | * HTX an push it to into the response channel. It is response to forward the |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5393 | * message and terminate the transaction. It returns 1 on success and 0 on |
| 5394 | * error. The Reply must be on top of the stack. |
| 5395 | */ |
| 5396 | __LJMP static int hlua_txn_forward_reply(lua_State *L, struct stream *s) |
| 5397 | { |
| 5398 | struct htx *htx; |
| 5399 | struct htx_sl *sl; |
| 5400 | struct h1m h1m; |
| 5401 | const char *status, *reason, *body; |
| 5402 | size_t status_len, reason_len, body_len; |
| 5403 | int ret, code, flags; |
| 5404 | |
| 5405 | code = 200; |
| 5406 | status = "200"; |
| 5407 | status_len = 3; |
| 5408 | ret = lua_getfield(L, -1, "status"); |
| 5409 | if (ret == LUA_TNUMBER) { |
| 5410 | code = lua_tointeger(L, -1); |
| 5411 | status = lua_tolstring(L, -1, &status_len); |
| 5412 | } |
| 5413 | lua_pop(L, 1); |
| 5414 | |
| 5415 | reason = http_get_reason(code); |
| 5416 | reason_len = strlen(reason); |
| 5417 | ret = lua_getfield(L, -1, "reason"); |
| 5418 | if (ret == LUA_TSTRING) |
| 5419 | reason = lua_tolstring(L, -1, &reason_len); |
| 5420 | lua_pop(L, 1); |
| 5421 | |
| 5422 | body = NULL; |
| 5423 | body_len = 0; |
| 5424 | ret = lua_getfield(L, -1, "body"); |
| 5425 | if (ret == LUA_TSTRING) |
| 5426 | body = lua_tolstring(L, -1, &body_len); |
| 5427 | lua_pop(L, 1); |
| 5428 | |
| 5429 | /* Prepare the response before inserting the headers */ |
| 5430 | h1m_init_res(&h1m); |
| 5431 | htx = htx_from_buf(&s->res.buf); |
| 5432 | channel_htx_truncate(&s->res, htx); |
| 5433 | if (s->txn->req.flags & HTTP_MSGF_VER_11) { |
| 5434 | flags = (HTX_SL_F_IS_RESP|HTX_SL_F_VER_11); |
| 5435 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.1"), |
| 5436 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5437 | } |
| 5438 | else { |
| 5439 | flags = HTX_SL_F_IS_RESP; |
| 5440 | sl = htx_add_stline(htx, HTX_BLK_RES_SL, flags, ist("HTTP/1.0"), |
| 5441 | ist2(status, status_len), ist2(reason, reason_len)); |
| 5442 | } |
| 5443 | if (!sl) |
| 5444 | goto fail; |
| 5445 | sl->info.res.status = code; |
| 5446 | |
| 5447 | /* Push in the stack the "headers" entry. */ |
| 5448 | ret = lua_getfield(L, -1, "headers"); |
| 5449 | if (ret != LUA_TTABLE) |
| 5450 | goto skip_headers; |
| 5451 | |
| 5452 | lua_pushnil(L); |
| 5453 | while (lua_next(L, -2) != 0) { |
| 5454 | struct ist name, value; |
| 5455 | const char *n, *v; |
| 5456 | size_t nlen, vlen; |
| 5457 | |
| 5458 | if (!lua_isstring(L, -2) || !lua_istable(L, -1)) { |
| 5459 | /* Skip element if the key is not a string or if the value is not a table */ |
| 5460 | goto next_hdr; |
| 5461 | } |
| 5462 | |
| 5463 | n = lua_tolstring(L, -2, &nlen); |
| 5464 | name = ist2(n, nlen); |
| 5465 | if (isteqi(name, ist("content-length"))) { |
| 5466 | /* Always skip content-length header. It will be added |
| 5467 | * later with the correct len |
| 5468 | */ |
| 5469 | goto next_hdr; |
| 5470 | } |
| 5471 | |
| 5472 | /* Loop on header's values */ |
| 5473 | lua_pushnil(L); |
| 5474 | while (lua_next(L, -2)) { |
| 5475 | if (!lua_isstring(L, -1)) { |
| 5476 | /* Skip the value if it is not a string */ |
| 5477 | goto next_value; |
| 5478 | } |
| 5479 | |
| 5480 | v = lua_tolstring(L, -1, &vlen); |
| 5481 | value = ist2(v, vlen); |
| 5482 | |
| 5483 | if (isteqi(name, ist("transfer-encoding"))) |
| 5484 | h1_parse_xfer_enc_header(&h1m, value); |
| 5485 | if (!htx_add_header(htx, ist2(n, nlen), ist2(v, vlen))) |
| 5486 | goto fail; |
| 5487 | |
| 5488 | next_value: |
| 5489 | lua_pop(L, 1); |
| 5490 | } |
| 5491 | |
| 5492 | next_hdr: |
| 5493 | lua_pop(L, 1); |
| 5494 | } |
| 5495 | skip_headers: |
| 5496 | lua_pop(L, 1); |
| 5497 | |
| 5498 | /* Update h1m flags: CLEN is set if CHNK is not present */ |
| 5499 | if (!(h1m.flags & H1_MF_CHNK)) { |
| 5500 | const char *clen = ultoa(body_len); |
| 5501 | |
| 5502 | h1m.flags |= H1_MF_CLEN; |
| 5503 | if (!htx_add_header(htx, ist("content-length"), ist(clen))) |
| 5504 | goto fail; |
| 5505 | } |
| 5506 | if (h1m.flags & (H1_MF_CLEN|H1_MF_CHNK)) |
| 5507 | h1m.flags |= H1_MF_XFER_LEN; |
| 5508 | |
| 5509 | /* Update HTX start-line flags */ |
| 5510 | if (h1m.flags & H1_MF_XFER_ENC) |
| 5511 | flags |= HTX_SL_F_XFER_ENC; |
| 5512 | if (h1m.flags & H1_MF_XFER_LEN) { |
| 5513 | flags |= HTX_SL_F_XFER_LEN; |
| 5514 | if (h1m.flags & H1_MF_CHNK) |
| 5515 | flags |= HTX_SL_F_CHNK; |
| 5516 | else if (h1m.flags & H1_MF_CLEN) |
| 5517 | flags |= HTX_SL_F_CLEN; |
| 5518 | if (h1m.body_len == 0) |
| 5519 | flags |= HTX_SL_F_BODYLESS; |
| 5520 | } |
| 5521 | sl->flags |= flags; |
| 5522 | |
| 5523 | |
| 5524 | if (!htx_add_endof(htx, HTX_BLK_EOH) || |
| 5525 | (body_len && !htx_add_data_atonce(htx, ist2(body, body_len))) || |
| 5526 | !htx_add_endof(htx, HTX_BLK_EOM)) |
| 5527 | goto fail; |
| 5528 | |
| 5529 | /* Now, forward the response and terminate the transaction */ |
| 5530 | s->txn->status = code; |
| 5531 | htx_to_buf(htx, &s->res.buf); |
| 5532 | if (!http_forward_proxy_resp(s, 1)) |
| 5533 | goto fail; |
| 5534 | |
| 5535 | return 1; |
| 5536 | |
| 5537 | fail: |
| 5538 | channel_htx_truncate(&s->res, htx); |
| 5539 | return 0; |
| 5540 | } |
| 5541 | |
| 5542 | /* Terminate a transaction if called from a lua action. For TCP streams, |
| 5543 | * processing is just aborted. Nothing is returned to the client and all |
| 5544 | * arguments are ignored. For HTTP streams, if a reply is passed as argument, it |
| 5545 | * is forwarded to the client before terminating the transaction. On success, |
| 5546 | * the function exits with ACT_RET_DONE code. If an error occurred, it exits |
| 5547 | * with ACT_RET_ERR code. If this function is not called from a lua action, it |
| 5548 | * just exits without any processing. |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5549 | */ |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5550 | __LJMP static int hlua_txn_done(lua_State *L) |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5551 | { |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5552 | struct hlua_txn *htxn; |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5553 | struct stream *s; |
| 5554 | int finst; |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5555 | |
Willy Tarreau | b2ccb56 | 2015-04-06 11:11:15 +0200 | [diff] [blame] | 5556 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5557 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5558 | /* If the flags NOTERM is set, we cannot terminate the session, so we |
| 5559 | * just end the execution of the current lua code. */ |
| 5560 | if (htxn->flags & HLUA_TXN_NOTERM) |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5561 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | ab00df6 | 2016-07-14 11:42:37 +0200 | [diff] [blame] | 5562 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5563 | s = htxn->s; |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5564 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5565 | struct channel *req = &s->req; |
| 5566 | struct channel *res = &s->res; |
Willy Tarreau | 8138967 | 2015-03-10 12:03:52 +0100 | [diff] [blame] | 5567 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5568 | channel_auto_read(req); |
| 5569 | channel_abort(req); |
| 5570 | channel_auto_close(req); |
| 5571 | channel_erase(req); |
| 5572 | |
| 5573 | res->wex = tick_add_ifset(now_ms, res->wto); |
| 5574 | channel_auto_read(res); |
| 5575 | channel_auto_close(res); |
| 5576 | channel_shutr_now(res); |
| 5577 | |
| 5578 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_D); |
| 5579 | goto done; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5580 | } |
Thierry FOURNIER | 10ec214 | 2015-08-24 17:23:45 +0200 | [diff] [blame] | 5581 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5582 | if (lua_gettop(L) == 1 || !lua_istable(L, 2)) { |
| 5583 | /* No reply or invalid reply */ |
| 5584 | s->txn->status = 0; |
| 5585 | http_reply_and_close(s, 0, NULL); |
| 5586 | } |
| 5587 | else { |
| 5588 | /* Remove extra args to have the reply on top of the stack */ |
| 5589 | if (lua_gettop(L) > 2) |
| 5590 | lua_pop(L, lua_gettop(L) - 2); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5591 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5592 | if (!hlua_txn_forward_reply(L, s)) { |
| 5593 | if (!(s->flags & SF_ERR_MASK)) |
| 5594 | s->flags |= SF_ERR_PRXCOND; |
| 5595 | lua_pushinteger(L, ACT_RET_ERR); |
| 5596 | WILL_LJMP(hlua_done(L)); |
| 5597 | return 0; /* Never reached */ |
| 5598 | } |
Christopher Faulet | 4d0e263 | 2019-07-16 10:52:40 +0200 | [diff] [blame] | 5599 | } |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5600 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5601 | finst = ((htxn->dir == SMP_OPT_DIR_REQ) ? SF_FINST_R : SF_FINST_H); |
| 5602 | if (htxn->dir == SMP_OPT_DIR_REQ) { |
| 5603 | /* let's log the request time */ |
| 5604 | s->logs.tv_request = now; |
| 5605 | if (s->sess->fe == s->be) /* report it if the request was intercepted by the frontend */ |
| 5606 | _HA_ATOMIC_ADD(&s->sess->fe->fe_counters.intercepted_req, 1); |
| 5607 | } |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5608 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5609 | done: |
| 5610 | if (!(s->flags & SF_ERR_MASK)) |
| 5611 | s->flags |= SF_ERR_LOCAL; |
| 5612 | if (!(s->flags & SF_FINST_MASK)) |
| 5613 | s->flags |= finst; |
Christopher Faulet | fe6a71b | 2019-07-26 16:40:24 +0200 | [diff] [blame] | 5614 | |
Christopher Faulet | 4ad7310 | 2020-03-05 11:07:31 +0100 | [diff] [blame] | 5615 | lua_pushinteger(L, ACT_RET_ABRT); |
Thierry FOURNIER | 4bb375c | 2015-08-26 08:42:21 +0200 | [diff] [blame] | 5616 | WILL_LJMP(hlua_done(L)); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5617 | return 0; |
| 5618 | } |
| 5619 | |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5620 | /* |
| 5621 | * |
| 5622 | * |
| 5623 | * Class REPLY |
| 5624 | * |
| 5625 | * |
| 5626 | */ |
| 5627 | |
| 5628 | /* Pushes the TXN reply onto the top of the stack. If the stask does not have a |
| 5629 | * free slots, the function fails and returns 0; |
| 5630 | */ |
| 5631 | static int hlua_txn_reply_new(lua_State *L) |
| 5632 | { |
| 5633 | struct hlua_txn *htxn; |
| 5634 | const char *reason, *body = NULL; |
| 5635 | int ret, status; |
| 5636 | |
| 5637 | htxn = MAY_LJMP(hlua_checktxn(L, 1)); |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 5638 | if (!IS_HTX_STRM(htxn->s)) { |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 5639 | hlua_pusherror(L, "txn object is not an HTTP transaction."); |
| 5640 | WILL_LJMP(lua_error(L)); |
| 5641 | } |
| 5642 | |
| 5643 | /* Default value */ |
| 5644 | status = 200; |
| 5645 | reason = http_get_reason(status); |
| 5646 | |
| 5647 | if (lua_istable(L, 2)) { |
| 5648 | /* load status and reason from the table argument at index 2 */ |
| 5649 | ret = lua_getfield(L, 2, "status"); |
| 5650 | if (ret == LUA_TNIL) |
| 5651 | goto reason; |
| 5652 | else if (ret != LUA_TNUMBER) { |
| 5653 | /* invalid status: ignore the reason */ |
| 5654 | goto body; |
| 5655 | } |
| 5656 | status = lua_tointeger(L, -1); |
| 5657 | |
| 5658 | reason: |
| 5659 | lua_pop(L, 1); /* restore the stack: remove status */ |
| 5660 | ret = lua_getfield(L, 2, "reason"); |
| 5661 | if (ret == LUA_TSTRING) |
| 5662 | reason = lua_tostring(L, -1); |
| 5663 | |
| 5664 | body: |
| 5665 | lua_pop(L, 1); /* restore the stack: remove invalid status or reason */ |
| 5666 | ret = lua_getfield(L, 2, "body"); |
| 5667 | if (ret == LUA_TSTRING) |
| 5668 | body = lua_tostring(L, -1); |
| 5669 | lua_pop(L, 1); /* restore the stack: remove body */ |
| 5670 | } |
| 5671 | |
| 5672 | /* Create the Reply table */ |
| 5673 | lua_newtable(L); |
| 5674 | |
| 5675 | /* Add status element */ |
| 5676 | lua_pushstring(L, "status"); |
| 5677 | lua_pushinteger(L, status); |
| 5678 | lua_settable(L, -3); |
| 5679 | |
| 5680 | /* Add reason element */ |
| 5681 | reason = http_get_reason(status); |
| 5682 | lua_pushstring(L, "reason"); |
| 5683 | lua_pushstring(L, reason); |
| 5684 | lua_settable(L, -3); |
| 5685 | |
| 5686 | /* Add body element, nil if undefined */ |
| 5687 | lua_pushstring(L, "body"); |
| 5688 | if (body) |
| 5689 | lua_pushstring(L, body); |
| 5690 | else |
| 5691 | lua_pushnil(L); |
| 5692 | lua_settable(L, -3); |
| 5693 | |
| 5694 | /* Add headers element */ |
| 5695 | lua_pushstring(L, "headers"); |
| 5696 | lua_newtable(L); |
| 5697 | |
| 5698 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5699 | if (lua_istable(L, 2)) { |
| 5700 | /* load headers from the table argument at index 2. If it is a table, copy it. */ |
| 5701 | ret = lua_getfield(L, 2, "headers"); |
| 5702 | if (ret == LUA_TTABLE) { |
| 5703 | /* stack: [ ... <headers:table>, <table> ] */ |
| 5704 | lua_pushnil(L); |
| 5705 | while (lua_next(L, -2) != 0) { |
| 5706 | /* stack: [ ... <headers:table>, <table>, k, v] */ |
| 5707 | if (!lua_isstring(L, -1) && !lua_istable(L, -1)) { |
| 5708 | /* invalid value type, skip it */ |
| 5709 | lua_pop(L, 1); |
| 5710 | continue; |
| 5711 | } |
| 5712 | |
| 5713 | |
| 5714 | /* Duplicate the key and swap it with the value. */ |
| 5715 | lua_pushvalue(L, -2); |
| 5716 | lua_insert(L, -2); |
| 5717 | /* stack: [ ... <headers:table>, <table>, k, k, v ] */ |
| 5718 | |
| 5719 | lua_newtable(L); |
| 5720 | lua_insert(L, -2); |
| 5721 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, v ] */ |
| 5722 | |
| 5723 | if (lua_isstring(L, -1)) { |
| 5724 | /* push the value in the inner table */ |
| 5725 | lua_rawseti(L, -2, 1); |
| 5726 | } |
| 5727 | else { /* table */ |
| 5728 | lua_pushnil(L); |
| 5729 | while (lua_next(L, -2) != 0) { |
| 5730 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2, v2 ] */ |
| 5731 | if (!lua_isstring(L, -1)) { |
| 5732 | /* invalid value type, skip it*/ |
| 5733 | lua_pop(L, 1); |
| 5734 | continue; |
| 5735 | } |
| 5736 | /* push the value in the inner table */ |
| 5737 | lua_rawseti(L, -4, lua_rawlen(L, -4) + 1); |
| 5738 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table>, <v:table>, k2 ] */ |
| 5739 | } |
| 5740 | lua_pop(L, 1); |
| 5741 | /* stack: [ ... <headers:table>, <table>, k, k, <inner:table> ] */ |
| 5742 | } |
| 5743 | |
| 5744 | /* push (k,v) on the stack in the headers table: |
| 5745 | * stack: [ ... <headers:table>, <table>, k, k, v ] |
| 5746 | */ |
| 5747 | lua_settable(L, -5); |
| 5748 | /* stack: [ ... <headers:table>, <table>, k ] */ |
| 5749 | } |
| 5750 | } |
| 5751 | lua_pop(L, 1); |
| 5752 | } |
| 5753 | /* stack: [ txn, <Arg:table>, <Reply:table>, "headers", <headers:table> ] */ |
| 5754 | lua_settable(L, -3); |
| 5755 | /* stack: [ txn, <Arg:table>, <Reply:table> ] */ |
| 5756 | |
| 5757 | /* Pop a class sesison metatable and affect it to the userdata. */ |
| 5758 | lua_rawgeti(L, LUA_REGISTRYINDEX, class_txn_reply_ref); |
| 5759 | lua_setmetatable(L, -2); |
| 5760 | return 1; |
| 5761 | } |
| 5762 | |
| 5763 | /* Set the reply status code, and optionally the reason. If no reason is |
| 5764 | * provided, the default one corresponding to the status code is used. |
| 5765 | */ |
| 5766 | __LJMP static int hlua_txn_reply_set_status(lua_State *L) |
| 5767 | { |
| 5768 | int status = MAY_LJMP(luaL_checkinteger(L, 2)); |
| 5769 | const char *reason = MAY_LJMP(luaL_optlstring(L, 3, NULL, NULL)); |
| 5770 | |
| 5771 | /* First argument (self) must be a table */ |
| 5772 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5773 | |
| 5774 | if (status < 100 || status > 599) { |
| 5775 | lua_pushboolean(L, 0); |
| 5776 | return 1; |
| 5777 | } |
| 5778 | if (!reason) |
| 5779 | reason = http_get_reason(status); |
| 5780 | |
| 5781 | lua_pushinteger(L, status); |
| 5782 | lua_setfield(L, 1, "status"); |
| 5783 | |
| 5784 | lua_pushstring(L, reason); |
| 5785 | lua_setfield(L, 1, "reason"); |
| 5786 | |
| 5787 | lua_pushboolean(L, 1); |
| 5788 | return 1; |
| 5789 | } |
| 5790 | |
| 5791 | /* Add a header into the reply object. Each header name is associated to an |
| 5792 | * array of values in the "headers" table. If the header name is not found, a |
| 5793 | * new entry is created. |
| 5794 | */ |
| 5795 | __LJMP static int hlua_txn_reply_add_header(lua_State *L) |
| 5796 | { |
| 5797 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5798 | const char *value = MAY_LJMP(luaL_checkstring(L, 3)); |
| 5799 | int ret; |
| 5800 | |
| 5801 | /* First argument (self) must be a table */ |
| 5802 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5803 | |
| 5804 | /* Push in the stack the "headers" entry. */ |
| 5805 | ret = lua_getfield(L, 1, "headers"); |
| 5806 | if (ret != LUA_TTABLE) { |
| 5807 | hlua_pusherror(L, "Reply['headers'] is expected to a an array. %s found", lua_typename(L, ret)); |
| 5808 | WILL_LJMP(lua_error(L)); |
| 5809 | } |
| 5810 | |
| 5811 | /* check if the header is already registered. If not, register it. */ |
| 5812 | ret = lua_getfield(L, -1, name); |
| 5813 | if (ret == LUA_TNIL) { |
| 5814 | /* Entry not found. */ |
| 5815 | lua_pop(L, 1); /* remove the nil. The "headers" table is the top of the stack. */ |
| 5816 | |
| 5817 | /* Insert the new header name in the array in the top of the stack. |
| 5818 | * It left the new array in the top of the stack. |
| 5819 | */ |
| 5820 | lua_newtable(L); |
| 5821 | lua_pushstring(L, name); |
| 5822 | lua_pushvalue(L, -2); |
| 5823 | lua_settable(L, -4); |
| 5824 | } |
| 5825 | else if (ret != LUA_TTABLE) { |
| 5826 | hlua_pusherror(L, "Reply['headers']['%s'] is expected to be an array. %s found", name, lua_typename(L, ret)); |
| 5827 | WILL_LJMP(lua_error(L)); |
| 5828 | } |
| 5829 | |
| 5830 | /* Now the top od thestack is an array of values. We push |
| 5831 | * the header value as new entry. |
| 5832 | */ |
| 5833 | lua_pushstring(L, value); |
| 5834 | ret = lua_rawlen(L, -2); |
| 5835 | lua_rawseti(L, -2, ret + 1); |
| 5836 | |
| 5837 | lua_pushboolean(L, 1); |
| 5838 | return 1; |
| 5839 | } |
| 5840 | |
| 5841 | /* Remove all occurrences of a given header name. */ |
| 5842 | __LJMP static int hlua_txn_reply_del_header(lua_State *L) |
| 5843 | { |
| 5844 | const char *name = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5845 | int ret; |
| 5846 | |
| 5847 | /* First argument (self) must be a table */ |
| 5848 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5849 | |
| 5850 | /* Push in the stack the "headers" entry. */ |
| 5851 | ret = lua_getfield(L, 1, "headers"); |
| 5852 | if (ret != LUA_TTABLE) { |
| 5853 | hlua_pusherror(L, "Reply['headers'] is expected to be an array. %s found", lua_typename(L, ret)); |
| 5854 | WILL_LJMP(lua_error(L)); |
| 5855 | } |
| 5856 | |
| 5857 | lua_pushstring(L, name); |
| 5858 | lua_pushnil(L); |
| 5859 | lua_settable(L, -3); |
| 5860 | |
| 5861 | lua_pushboolean(L, 1); |
| 5862 | return 1; |
| 5863 | } |
| 5864 | |
| 5865 | /* Set the reply's body. Overwrite any existing entry. */ |
| 5866 | __LJMP static int hlua_txn_reply_set_body(lua_State *L) |
| 5867 | { |
| 5868 | const char *payload = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5869 | |
| 5870 | /* First argument (self) must be a table */ |
| 5871 | luaL_checktype(L, 1, LUA_TTABLE); |
| 5872 | |
| 5873 | lua_pushstring(L, payload); |
| 5874 | lua_setfield(L, 1, "body"); |
| 5875 | |
| 5876 | lua_pushboolean(L, 1); |
| 5877 | return 1; |
| 5878 | } |
| 5879 | |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 5880 | __LJMP static int hlua_log(lua_State *L) |
| 5881 | { |
| 5882 | int level; |
| 5883 | const char *msg; |
| 5884 | |
| 5885 | MAY_LJMP(check_args(L, 2, "log")); |
| 5886 | level = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 5887 | msg = MAY_LJMP(luaL_checkstring(L, 2)); |
| 5888 | |
| 5889 | if (level < 0 || level >= NB_LOG_LEVELS) |
| 5890 | WILL_LJMP(luaL_argerror(L, 1, "Invalid loglevel.")); |
| 5891 | |
| 5892 | hlua_sendlog(NULL, level, msg); |
| 5893 | return 0; |
| 5894 | } |
| 5895 | |
| 5896 | __LJMP static int hlua_log_debug(lua_State *L) |
| 5897 | { |
| 5898 | const char *msg; |
| 5899 | |
| 5900 | MAY_LJMP(check_args(L, 1, "debug")); |
| 5901 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5902 | hlua_sendlog(NULL, LOG_DEBUG, msg); |
| 5903 | return 0; |
| 5904 | } |
| 5905 | |
| 5906 | __LJMP static int hlua_log_info(lua_State *L) |
| 5907 | { |
| 5908 | const char *msg; |
| 5909 | |
| 5910 | MAY_LJMP(check_args(L, 1, "info")); |
| 5911 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5912 | hlua_sendlog(NULL, LOG_INFO, msg); |
| 5913 | return 0; |
| 5914 | } |
| 5915 | |
| 5916 | __LJMP static int hlua_log_warning(lua_State *L) |
| 5917 | { |
| 5918 | const char *msg; |
| 5919 | |
| 5920 | MAY_LJMP(check_args(L, 1, "warning")); |
| 5921 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5922 | hlua_sendlog(NULL, LOG_WARNING, msg); |
| 5923 | return 0; |
| 5924 | } |
| 5925 | |
| 5926 | __LJMP static int hlua_log_alert(lua_State *L) |
| 5927 | { |
| 5928 | const char *msg; |
| 5929 | |
| 5930 | MAY_LJMP(check_args(L, 1, "alert")); |
| 5931 | msg = MAY_LJMP(luaL_checkstring(L, 1)); |
| 5932 | hlua_sendlog(NULL, LOG_ALERT, msg); |
Thierry FOURNIER | 893bfa3 | 2015-02-17 18:42:34 +0100 | [diff] [blame] | 5933 | return 0; |
| 5934 | } |
| 5935 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5936 | __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] | 5937 | { |
| 5938 | int wakeup_ms = lua_tointeger(L, -1); |
| 5939 | if (now_ms < wakeup_ms) |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5940 | 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] | 5941 | return 0; |
| 5942 | } |
| 5943 | |
| 5944 | __LJMP static int hlua_sleep(lua_State *L) |
| 5945 | { |
| 5946 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5947 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5948 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5949 | MAY_LJMP(check_args(L, 1, "sleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5950 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5951 | delay = MAY_LJMP(luaL_checkinteger(L, 1)) * 1000; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5952 | wakeup_ms = tick_add(now_ms, delay); |
| 5953 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5954 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5955 | 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] | 5956 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5957 | } |
| 5958 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5959 | __LJMP static int hlua_msleep(lua_State *L) |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5960 | { |
| 5961 | unsigned int delay; |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5962 | unsigned int wakeup_ms; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5963 | |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5964 | MAY_LJMP(check_args(L, 1, "msleep")); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5965 | |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5966 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | d44731f | 2015-03-04 15:51:09 +0100 | [diff] [blame] | 5967 | wakeup_ms = tick_add(now_ms, delay); |
| 5968 | lua_pushinteger(L, wakeup_ms); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5969 | |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5970 | 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] | 5971 | return 0; |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 5972 | } |
| 5973 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5974 | /* This functionis an LUA binding. it permits to give back |
| 5975 | * the hand at the HAProxy scheduler. It is used when the |
| 5976 | * LUA processing consumes a lot of time. |
| 5977 | */ |
Thierry FOURNIER | f90838b | 2015-03-06 13:48:32 +0100 | [diff] [blame] | 5978 | __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] | 5979 | { |
| 5980 | return 0; |
| 5981 | } |
| 5982 | |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5983 | __LJMP static int hlua_yield(lua_State *L) |
| 5984 | { |
Willy Tarreau | 9635e03 | 2018-10-16 17:52:55 +0200 | [diff] [blame] | 5985 | 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] | 5986 | return 0; |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 5987 | } |
| 5988 | |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5989 | /* This function change the nice of the currently executed |
| 5990 | * task. It is used set low or high priority at the current |
| 5991 | * task. |
| 5992 | */ |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 5993 | __LJMP static int hlua_set_nice(lua_State *L) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5994 | { |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5995 | struct hlua *hlua; |
| 5996 | int nice; |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 5997 | |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 5998 | MAY_LJMP(check_args(L, 1, "set_nice")); |
| 5999 | hlua = hlua_gethlua(L); |
| 6000 | nice = MAY_LJMP(luaL_checkinteger(L, 1)); |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6001 | |
| 6002 | /* If he task is not set, I'm in a start mode. */ |
| 6003 | if (!hlua || !hlua->task) |
| 6004 | return 0; |
| 6005 | |
| 6006 | if (nice < -1024) |
| 6007 | nice = -1024; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 6008 | else if (nice > 1024) |
Thierry FOURNIER | 37196f4 | 2015-02-16 19:34:56 +0100 | [diff] [blame] | 6009 | nice = 1024; |
| 6010 | |
| 6011 | hlua->task->nice = nice; |
| 6012 | return 0; |
| 6013 | } |
| 6014 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6015 | /* 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] | 6016 | * HAProxy task subsystem when the task is awaked. The LUA runtime can |
| 6017 | * return an E_AGAIN signal, the emmiter of this signal must set a |
| 6018 | * signal to wake the task. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6019 | * |
| 6020 | * Task wrapper are longjmp safe because the only one Lua code |
| 6021 | * executed is the safe hlua_ctx_resume(); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6022 | */ |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6023 | 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] | 6024 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6025 | struct hlua *hlua = context; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6026 | enum hlua_exec status; |
| 6027 | |
Christopher Faulet | 5bc9972 | 2018-04-25 10:34:45 +0200 | [diff] [blame] | 6028 | if (task->thread_mask == MAX_THREADS_MASK) |
| 6029 | task_set_affinity(task, tid_bit); |
| 6030 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6031 | /* If it is the first call to the task, we must initialize the |
| 6032 | * execution timeouts. |
| 6033 | */ |
| 6034 | if (!HLUA_IS_RUNNING(hlua)) |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6035 | hlua->max_time = hlua_timeout_task; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6036 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6037 | /* Execute the Lua code. */ |
| 6038 | status = hlua_ctx_resume(hlua, 1); |
| 6039 | |
| 6040 | switch (status) { |
| 6041 | /* finished or yield */ |
| 6042 | case HLUA_E_OK: |
| 6043 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6044 | task_destroy(task); |
Tim Duesterhus | cd235c6 | 2018-04-24 13:56:01 +0200 | [diff] [blame] | 6045 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6046 | break; |
| 6047 | |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6048 | case HLUA_E_AGAIN: /* co process or timeout wake me later. */ |
Thierry FOURNIER | cb14688 | 2017-12-10 17:10:57 +0100 | [diff] [blame] | 6049 | notification_gc(&hlua->com); |
PiBa-NL | fe971b3 | 2018-05-02 22:27:14 +0200 | [diff] [blame] | 6050 | task->expire = hlua->wake_time; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6051 | break; |
| 6052 | |
| 6053 | /* finished with error. */ |
| 6054 | case HLUA_E_ERRMSG: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6055 | SEND_ERR(NULL, "Lua task: %s.\n", lua_tostring(hlua->T, -1)); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6056 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6057 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6058 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6059 | break; |
| 6060 | |
| 6061 | case HLUA_E_ERR: |
| 6062 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6063 | SEND_ERR(NULL, "Lua task: unknown error.\n"); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6064 | hlua_ctx_destroy(hlua); |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6065 | task_destroy(task); |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6066 | task = NULL; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6067 | break; |
| 6068 | } |
Emeric Brun | 253e53e | 2017-10-17 18:58:40 +0200 | [diff] [blame] | 6069 | return task; |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6070 | } |
| 6071 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6072 | /* This function is an LUA binding that register LUA function to be |
| 6073 | * executed after the HAProxy configuration parsing and before the |
| 6074 | * HAProxy scheduler starts. This function expect only one LUA |
| 6075 | * argument that is a function. This function returns nothing, but |
| 6076 | * throws if an error is encountered. |
| 6077 | */ |
| 6078 | __LJMP static int hlua_register_init(lua_State *L) |
| 6079 | { |
| 6080 | struct hlua_init_function *init; |
| 6081 | int ref; |
| 6082 | |
| 6083 | MAY_LJMP(check_args(L, 1, "register_init")); |
| 6084 | |
| 6085 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6086 | |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6087 | init = calloc(1, sizeof(*init)); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6088 | if (!init) |
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 | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 6090 | |
| 6091 | init->function_ref = ref; |
| 6092 | LIST_ADDQ(&hlua_init_functions, &init->l); |
| 6093 | return 0; |
| 6094 | } |
| 6095 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6096 | /* This functio is an LUA binding. It permits to register a task |
| 6097 | * executed in parallel of the main HAroxy activity. The task is |
| 6098 | * created and it is set in the HAProxy scheduler. It can be called |
| 6099 | * from the "init" section, "post init" or during the runtime. |
| 6100 | * |
| 6101 | * Lua prototype: |
| 6102 | * |
| 6103 | * <none> core.register_task(<function>) |
| 6104 | */ |
| 6105 | static int hlua_register_task(lua_State *L) |
| 6106 | { |
| 6107 | struct hlua *hlua; |
| 6108 | struct task *task; |
| 6109 | int ref; |
| 6110 | |
| 6111 | MAY_LJMP(check_args(L, 1, "register_task")); |
| 6112 | |
| 6113 | ref = MAY_LJMP(hlua_checkfunction(L, 1)); |
| 6114 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6115 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6116 | if (!hlua) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6117 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6118 | |
Emeric Brun | c60def8 | 2017-09-27 14:59:38 +0200 | [diff] [blame] | 6119 | task = task_new(MAX_THREADS_MASK); |
Willy Tarreau | e09101e | 2018-10-16 17:37:12 +0200 | [diff] [blame] | 6120 | if (!task) |
| 6121 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
| 6122 | |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6123 | task->context = hlua; |
| 6124 | task->process = hlua_process_task; |
| 6125 | |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6126 | if (!hlua_ctx_init(hlua, task, 1)) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6127 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 6128 | |
| 6129 | /* Restore the function in the stack. */ |
| 6130 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ref); |
| 6131 | hlua->nargs = 0; |
| 6132 | |
| 6133 | /* Schedule task. */ |
| 6134 | task_schedule(task, now_ms); |
| 6135 | |
| 6136 | return 0; |
| 6137 | } |
| 6138 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6139 | /* Wrapper called by HAProxy to execute an LUA converter. This wrapper |
| 6140 | * doesn't allow "yield" functions because the HAProxy engine cannot |
| 6141 | * resume converters. |
| 6142 | */ |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6143 | 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] | 6144 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6145 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6146 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6147 | const char *error; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6148 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6149 | if (!stream) |
| 6150 | return 0; |
| 6151 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6152 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6153 | * Lua context can be not initialized. This behavior |
| 6154 | * permits to save performances because a systematic |
| 6155 | * Lua initialization cause 5% performances loss. |
| 6156 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6157 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6158 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6159 | if (!stream->hlua) { |
| 6160 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6161 | return 0; |
| 6162 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6163 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6164 | SEND_ERR(stream->be, "Lua converter '%s': can't initialize Lua context.\n", fcn->name); |
| 6165 | return 0; |
| 6166 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6167 | } |
| 6168 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6169 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6170 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6171 | |
| 6172 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6173 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6174 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6175 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6176 | else |
| 6177 | error = "critical error"; |
| 6178 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", fcn->name, error); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6179 | return 0; |
| 6180 | } |
| 6181 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6182 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6183 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6184 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6185 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6186 | return 0; |
| 6187 | } |
| 6188 | |
| 6189 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6190 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6191 | |
| 6192 | /* convert input sample and pust-it in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6193 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6194 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6195 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6196 | return 0; |
| 6197 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6198 | hlua_smp2lua(stream->hlua->T, smp); |
| 6199 | stream->hlua->nargs = 1; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6200 | |
| 6201 | /* push keywords in the stack. */ |
| 6202 | if (arg_p) { |
| 6203 | for (; arg_p->type != ARGT_STOP; arg_p++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6204 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6205 | SEND_ERR(stream->be, "Lua converter '%s': full stack.\n", fcn->name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6206 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6207 | return 0; |
| 6208 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6209 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6210 | stream->hlua->nargs++; |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6211 | } |
| 6212 | } |
| 6213 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6214 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6215 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6216 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6217 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6218 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6219 | } |
| 6220 | |
| 6221 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6222 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6223 | /* finished. */ |
| 6224 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6225 | /* If the stack is empty, the function fails. */ |
| 6226 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6227 | return 0; |
| 6228 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6229 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6230 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6231 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6232 | return 1; |
| 6233 | |
| 6234 | /* yield. */ |
| 6235 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6236 | 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] | 6237 | return 0; |
| 6238 | |
| 6239 | /* finished with error. */ |
| 6240 | case HLUA_E_ERRMSG: |
| 6241 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6242 | SEND_ERR(stream->be, "Lua converter '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6243 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6244 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6245 | return 0; |
| 6246 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6247 | case HLUA_E_ETMOUT: |
| 6248 | SEND_ERR(stream->be, "Lua converter '%s': execution timeout.\n", fcn->name); |
| 6249 | return 0; |
| 6250 | |
| 6251 | case HLUA_E_NOMEM: |
| 6252 | SEND_ERR(stream->be, "Lua converter '%s': out of memory error.\n", fcn->name); |
| 6253 | return 0; |
| 6254 | |
| 6255 | case HLUA_E_YIELD: |
| 6256 | SEND_ERR(stream->be, "Lua converter '%s': yield functions like core.tcp() or core.sleep() are not allowed.\n", fcn->name); |
| 6257 | return 0; |
| 6258 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6259 | case HLUA_E_ERR: |
| 6260 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6261 | 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] | 6262 | |
| 6263 | default: |
| 6264 | return 0; |
| 6265 | } |
| 6266 | } |
| 6267 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6268 | /* Wrapper called by HAProxy to execute a sample-fetch. this wrapper |
| 6269 | * doesn't allow "yield" functions because the HAProxy engine cannot |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6270 | * resume sample-fetches. This function will be called by the sample |
| 6271 | * fetch engine to call lua-based fetch operations. |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6272 | */ |
Thierry FOURNIER | 0786d05 | 2015-05-11 15:42:45 +0200 | [diff] [blame] | 6273 | static int hlua_sample_fetch_wrapper(const struct arg *arg_p, struct sample *smp, |
| 6274 | const char *kw, void *private) |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6275 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 6276 | struct hlua_function *fcn = private; |
Thierry FOURNIER | 0a9a2b8 | 2015-05-11 15:20:49 +0200 | [diff] [blame] | 6277 | struct stream *stream = smp->strm; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6278 | const char *error; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6279 | unsigned int hflags = HLUA_TXN_NOTERM; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6280 | |
Willy Tarreau | be508f1 | 2016-03-10 11:47:01 +0100 | [diff] [blame] | 6281 | if (!stream) |
| 6282 | return 0; |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 6283 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6284 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6285 | * Lua context can be not initialized. This behavior |
| 6286 | * permits to save performances because a systematic |
| 6287 | * Lua initialization cause 5% performances loss. |
| 6288 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6289 | if (!stream->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6290 | stream->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6291 | if (!stream->hlua) { |
| 6292 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6293 | return 0; |
| 6294 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6295 | if (!hlua_ctx_init(stream->hlua, stream->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6296 | SEND_ERR(stream->be, "Lua sample-fetch '%s': can't initialize Lua context.\n", fcn->name); |
| 6297 | return 0; |
| 6298 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6299 | } |
| 6300 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6301 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6302 | if (!HLUA_IS_RUNNING(stream->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6303 | |
| 6304 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6305 | if (!SET_SAFE_LJMP(stream->hlua->T)) { |
| 6306 | if (lua_type(stream->hlua->T, -1) == LUA_TSTRING) |
| 6307 | error = lua_tostring(stream->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6308 | else |
| 6309 | error = "critical error"; |
| 6310 | 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] | 6311 | return 0; |
| 6312 | } |
| 6313 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6314 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6315 | if (!lua_checkstack(stream->hlua->T, 2)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6316 | 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] | 6317 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6318 | return 0; |
| 6319 | } |
| 6320 | |
| 6321 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6322 | lua_rawgeti(stream->hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6323 | |
| 6324 | /* push arguments in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6325 | 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] | 6326 | 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] | 6327 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6328 | return 0; |
| 6329 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6330 | stream->hlua->nargs = 1; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6331 | |
| 6332 | /* push keywords in the stack. */ |
| 6333 | for (; arg_p && arg_p->type != ARGT_STOP; arg_p++) { |
| 6334 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6335 | if (!lua_checkstack(stream->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6336 | 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] | 6337 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6338 | return 0; |
| 6339 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6340 | hlua_arg2lua(stream->hlua->T, arg_p); |
| 6341 | stream->hlua->nargs++; |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6342 | } |
| 6343 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6344 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6345 | stream->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6346 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6347 | /* At this point the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6348 | RESET_SAFE_LJMP(stream->hlua->T); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6349 | } |
| 6350 | |
| 6351 | /* Execute the function. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6352 | switch (hlua_ctx_resume(stream->hlua, 0)) { |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6353 | /* finished. */ |
| 6354 | case HLUA_E_OK: |
Thierry FOURNIER | fd80df1 | 2017-05-12 16:32:20 +0200 | [diff] [blame] | 6355 | /* If the stack is empty, the function fails. */ |
| 6356 | if (lua_gettop(stream->hlua->T) <= 0) |
| 6357 | return 0; |
| 6358 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6359 | /* Convert the returned value in sample. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6360 | hlua_lua2smp(stream->hlua->T, -1, smp); |
| 6361 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6362 | |
| 6363 | /* Set the end of execution flag. */ |
| 6364 | smp->flags &= ~SMP_F_MAY_CHANGE; |
| 6365 | return 1; |
| 6366 | |
| 6367 | /* yield. */ |
| 6368 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6369 | 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] | 6370 | return 0; |
| 6371 | |
| 6372 | /* finished with error. */ |
| 6373 | case HLUA_E_ERRMSG: |
| 6374 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6375 | SEND_ERR(smp->px, "Lua sample-fetch '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6376 | fcn->name, lua_tostring(stream->hlua->T, -1)); |
| 6377 | lua_pop(stream->hlua->T, 1); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6378 | return 0; |
| 6379 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6380 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6381 | SEND_ERR(smp->px, "Lua sample-fetch '%s': execution timeout.\n", fcn->name); |
| 6382 | return 0; |
| 6383 | |
| 6384 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6385 | SEND_ERR(smp->px, "Lua sample-fetch '%s': out of memory error.\n", fcn->name); |
| 6386 | return 0; |
| 6387 | |
| 6388 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6389 | SEND_ERR(smp->px, "Lua sample-fetch '%s': yield not allowed.\n", fcn->name); |
| 6390 | return 0; |
| 6391 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6392 | case HLUA_E_ERR: |
| 6393 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6394 | 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] | 6395 | |
| 6396 | default: |
| 6397 | return 0; |
| 6398 | } |
| 6399 | } |
| 6400 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6401 | /* This function is an LUA binding used for registering |
| 6402 | * "sample-conv" functions. It expects a converter name used |
| 6403 | * in the haproxy configuration file, and an LUA function. |
| 6404 | */ |
| 6405 | __LJMP static int hlua_register_converters(lua_State *L) |
| 6406 | { |
| 6407 | struct sample_conv_kw_list *sck; |
| 6408 | const char *name; |
| 6409 | int ref; |
| 6410 | int len; |
| 6411 | struct hlua_function *fcn; |
| 6412 | |
| 6413 | MAY_LJMP(check_args(L, 2, "register_converters")); |
| 6414 | |
| 6415 | /* First argument : converter name. */ |
| 6416 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6417 | |
| 6418 | /* Second argument : lua function. */ |
| 6419 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6420 | |
| 6421 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6422 | sck = calloc(1, sizeof(*sck) + sizeof(struct sample_conv) * 2); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6423 | if (!sck) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6424 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6425 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6426 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6427 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6428 | |
| 6429 | /* Fill fcn. */ |
| 6430 | fcn->name = strdup(name); |
| 6431 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6432 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6433 | fcn->function_ref = ref; |
| 6434 | |
| 6435 | /* List head */ |
| 6436 | sck->list.n = sck->list.p = NULL; |
| 6437 | |
| 6438 | /* converter keyword. */ |
| 6439 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6440 | sck->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6441 | if (!sck->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6442 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6443 | |
| 6444 | snprintf((char *)sck->kw[0].kw, len, "lua.%s", name); |
| 6445 | sck->kw[0].process = hlua_sample_conv_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6446 | 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] | 6447 | sck->kw[0].val_args = NULL; |
| 6448 | sck->kw[0].in_type = SMP_T_STR; |
| 6449 | sck->kw[0].out_type = SMP_T_STR; |
| 6450 | sck->kw[0].private = fcn; |
| 6451 | |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 6452 | /* Register this new converter */ |
| 6453 | sample_register_convs(sck); |
| 6454 | |
| 6455 | return 0; |
| 6456 | } |
| 6457 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6458 | /* This function is an LUA binding used for registering |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6459 | * "sample-fetch" functions. It expects a converter name used |
| 6460 | * in the haproxy configuration file, and an LUA function. |
| 6461 | */ |
| 6462 | __LJMP static int hlua_register_fetches(lua_State *L) |
| 6463 | { |
| 6464 | const char *name; |
| 6465 | int ref; |
| 6466 | int len; |
| 6467 | struct sample_fetch_kw_list *sfk; |
| 6468 | struct hlua_function *fcn; |
| 6469 | |
| 6470 | MAY_LJMP(check_args(L, 2, "register_fetches")); |
| 6471 | |
| 6472 | /* First argument : sample-fetch name. */ |
| 6473 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 6474 | |
| 6475 | /* Second argument : lua function. */ |
| 6476 | ref = MAY_LJMP(hlua_checkfunction(L, 2)); |
| 6477 | |
| 6478 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6479 | sfk = calloc(1, sizeof(*sfk) + sizeof(struct sample_fetch) * 2); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6480 | if (!sfk) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6481 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6482 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6483 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6484 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6485 | |
| 6486 | /* Fill fcn. */ |
| 6487 | fcn->name = strdup(name); |
| 6488 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6489 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6490 | fcn->function_ref = ref; |
| 6491 | |
| 6492 | /* List head */ |
| 6493 | sfk->list.n = sfk->list.p = NULL; |
| 6494 | |
| 6495 | /* sample-fetch keyword. */ |
| 6496 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 6497 | sfk->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6498 | if (!sfk->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 6499 | return luaL_error(L, "Lua out of memory error."); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6500 | |
| 6501 | snprintf((char *)sfk->kw[0].kw, len, "lua.%s", name); |
| 6502 | sfk->kw[0].process = hlua_sample_fetch_wrapper; |
David Carlier | 0c437f4 | 2016-04-27 16:21:56 +0100 | [diff] [blame] | 6503 | 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] | 6504 | sfk->kw[0].val_args = NULL; |
| 6505 | sfk->kw[0].out_type = SMP_T_STR; |
| 6506 | sfk->kw[0].use = SMP_USE_HTTP_ANY; |
| 6507 | sfk->kw[0].val = 0; |
| 6508 | sfk->kw[0].private = fcn; |
| 6509 | |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 6510 | /* Register this new fetch. */ |
| 6511 | sample_register_fetches(sfk); |
| 6512 | |
| 6513 | return 0; |
| 6514 | } |
| 6515 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6516 | /* This function is a lua binding to set the wake_time. |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6517 | */ |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 6518 | __LJMP static int hlua_set_wake_time(lua_State *L) |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6519 | { |
| 6520 | struct hlua *hlua = hlua_gethlua(L); |
| 6521 | unsigned int delay; |
| 6522 | unsigned int wakeup_ms; |
| 6523 | |
| 6524 | MAY_LJMP(check_args(L, 1, "wake_time")); |
| 6525 | |
| 6526 | delay = MAY_LJMP(luaL_checkinteger(L, 1)); |
| 6527 | wakeup_ms = tick_add(now_ms, delay); |
| 6528 | hlua->wake_time = wakeup_ms; |
| 6529 | return 0; |
| 6530 | } |
| 6531 | |
| 6532 | |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6533 | /* This function is a wrapper to execute each LUA function declared as an action |
| 6534 | * wrapper during the initialisation period. This function may return any |
| 6535 | * ACT_RET_* value. On error ACT_RET_CONT is returned and the action is |
| 6536 | * ignored. If the lua action yields, ACT_RET_YIELD is returned. On success, the |
| 6537 | * return value is the first element on the stack. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6538 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6539 | 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] | 6540 | struct session *sess, struct stream *s, int flags) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6541 | { |
| 6542 | char **arg; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6543 | unsigned int hflags = 0; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6544 | int dir, act_ret = ACT_RET_CONT; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6545 | const char *error; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6546 | |
| 6547 | switch (rule->from) { |
Christopher Faulet | d8f0e07 | 2020-02-25 09:45:51 +0100 | [diff] [blame] | 6548 | case ACT_F_TCP_REQ_CNT: dir = SMP_OPT_DIR_REQ; break; |
| 6549 | case ACT_F_TCP_RES_CNT: dir = SMP_OPT_DIR_RES; break; |
| 6550 | case ACT_F_HTTP_REQ: dir = SMP_OPT_DIR_REQ; break; |
| 6551 | case ACT_F_HTTP_RES: dir = SMP_OPT_DIR_RES; break; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6552 | default: |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6553 | SEND_ERR(px, "Lua: internal error while execute action.\n"); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6554 | goto end; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6555 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6556 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6557 | /* In the execution wrappers linked with a stream, the |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6558 | * Lua context can be not initialized. This behavior |
| 6559 | * permits to save performances because a systematic |
| 6560 | * Lua initialization cause 5% performances loss. |
| 6561 | */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6562 | if (!s->hlua) { |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6563 | s->hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6564 | if (!s->hlua) { |
| 6565 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6566 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6567 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6568 | } |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6569 | if (!hlua_ctx_init(s->hlua, s->task, 0)) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6570 | SEND_ERR(px, "Lua action '%s': can't initialize Lua context.\n", |
| 6571 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6572 | goto end; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6573 | } |
Thierry FOURNIER | 05ac424 | 2015-02-27 18:37:27 +0100 | [diff] [blame] | 6574 | } |
| 6575 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6576 | /* If it is the first run, initialize the data for the call. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6577 | if (!HLUA_IS_RUNNING(s->hlua)) { |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6578 | |
| 6579 | /* The following Lua calls can fail. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6580 | if (!SET_SAFE_LJMP(s->hlua->T)) { |
| 6581 | if (lua_type(s->hlua->T, -1) == LUA_TSTRING) |
| 6582 | error = lua_tostring(s->hlua->T, -1); |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6583 | else |
| 6584 | error = "critical error"; |
| 6585 | SEND_ERR(px, "Lua function '%s': %s.\n", |
| 6586 | rule->arg.hlua_rule->fcn.name, error); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6587 | goto end; |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6588 | } |
| 6589 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6590 | /* Check stack available size. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6591 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6592 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6593 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6594 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6595 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6596 | } |
| 6597 | |
| 6598 | /* Restore the function in the stack. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6599 | 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] | 6600 | |
Willy Tarreau | 87b0966 | 2015-04-03 00:22:06 +0200 | [diff] [blame] | 6601 | /* Create and and push object stream in the stack. */ |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6602 | if (!hlua_txn_new(s->hlua->T, s, px, dir, hflags)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6603 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6604 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6605 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6606 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6607 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6608 | s->hlua->nargs = 1; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6609 | |
| 6610 | /* push keywords in the stack. */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6611 | for (arg = rule->arg.hlua_rule->args; arg && *arg; arg++) { |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6612 | if (!lua_checkstack(s->hlua->T, 1)) { |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6613 | SEND_ERR(px, "Lua function '%s': full stack.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6614 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6615 | RESET_SAFE_LJMP(s->hlua->T); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6616 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6617 | } |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6618 | lua_pushstring(s->hlua->T, *arg); |
| 6619 | s->hlua->nargs++; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6620 | } |
| 6621 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6622 | /* Now the execution is safe. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6623 | RESET_SAFE_LJMP(s->hlua->T); |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 6624 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 6625 | /* We must initialize the execution timeouts. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6626 | s->hlua->max_time = hlua_timeout_session; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6627 | } |
| 6628 | |
Christopher Faulet | 23308eb | 2020-06-02 18:46:07 +0200 | [diff] [blame] | 6629 | /* Always reset the analyse expiration timeout for the corresponding |
| 6630 | * channel in case the lua script yield, to be sure to not keep an |
| 6631 | * expired timeout. |
| 6632 | */ |
| 6633 | if (dir == SMP_OPT_DIR_REQ) |
| 6634 | s->req.analyse_exp = TICK_ETERNITY; |
| 6635 | else |
| 6636 | s->res.analyse_exp = TICK_ETERNITY; |
| 6637 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6638 | /* Execute the function. */ |
Christopher Faulet | 105ba6c | 2019-12-18 14:41:51 +0100 | [diff] [blame] | 6639 | switch (hlua_ctx_resume(s->hlua, !(flags & ACT_OPT_FINAL))) { |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6640 | /* finished. */ |
| 6641 | case HLUA_E_OK: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6642 | /* Catch the return value */ |
| 6643 | if (lua_gettop(s->hlua->T) > 0) |
| 6644 | act_ret = lua_tointeger(s->hlua->T, -1); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6645 | |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 6646 | /* Set timeout in the required channel. */ |
| 6647 | if (act_ret == ACT_RET_YIELD && s->hlua->wake_time != TICK_ETERNITY) { |
| 6648 | if (dir == SMP_OPT_DIR_REQ) |
| 6649 | s->req.analyse_exp = s->hlua->wake_time; |
| 6650 | else |
| 6651 | s->res.analyse_exp = s->hlua->wake_time; |
| 6652 | } |
| 6653 | goto end; |
| 6654 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6655 | /* yield. */ |
| 6656 | case HLUA_E_AGAIN: |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6657 | /* Set timeout in the required channel. */ |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6658 | if (s->hlua->wake_time != TICK_ETERNITY) { |
Christopher Faulet | 81921b1 | 2019-08-14 23:19:45 +0200 | [diff] [blame] | 6659 | if (dir == SMP_OPT_DIR_REQ) |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6660 | s->req.analyse_exp = s->hlua->wake_time; |
Christopher Faulet | bfab2dd | 2019-07-26 15:09:53 +0200 | [diff] [blame] | 6661 | else |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6662 | s->res.analyse_exp = s->hlua->wake_time; |
Thierry FOURNIER | c42c1ae | 2015-03-03 17:17:55 +0100 | [diff] [blame] | 6663 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6664 | /* Some actions can be wake up when a "write" event |
| 6665 | * is detected on a response channel. This is useful |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 6666 | * only for actions targeted on the requests. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6667 | */ |
Christopher Faulet | 51fa358 | 2019-07-26 14:54:52 +0200 | [diff] [blame] | 6668 | if (HLUA_IS_WAKERESWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6669 | s->res.flags |= CF_WAKE_WRITE; |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6670 | if (HLUA_IS_WAKEREQWR(s->hlua)) |
Willy Tarreau | 22ec1ea | 2014-11-27 20:45:39 +0100 | [diff] [blame] | 6671 | s->req.flags |= CF_WAKE_WRITE; |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6672 | act_ret = ACT_RET_YIELD; |
| 6673 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6674 | |
| 6675 | /* finished with error. */ |
| 6676 | case HLUA_E_ERRMSG: |
| 6677 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6678 | SEND_ERR(px, "Lua function '%s': %s.\n", |
Thierry FOURNIER | 2c8b54e | 2016-12-17 12:45:32 +0100 | [diff] [blame] | 6679 | rule->arg.hlua_rule->fcn.name, lua_tostring(s->hlua->T, -1)); |
| 6680 | lua_pop(s->hlua->T, 1); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6681 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6682 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6683 | case HLUA_E_ETMOUT: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6684 | SEND_ERR(px, "Lua function '%s': execution timeout.\n", rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6685 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6686 | |
| 6687 | case HLUA_E_NOMEM: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6688 | SEND_ERR(px, "Lua function '%s': out of memory error.\n", rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6689 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6690 | |
| 6691 | case HLUA_E_YIELD: |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6692 | SEND_ERR(px, "Lua function '%s': aborting Lua processing on expired timeout.\n", |
| 6693 | rule->arg.hlua_rule->fcn.name); |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6694 | goto end; |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6695 | |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6696 | case HLUA_E_ERR: |
| 6697 | /* Display log. */ |
Thierry FOURNIER | 23bc375 | 2015-09-11 19:15:43 +0200 | [diff] [blame] | 6698 | SEND_ERR(px, "Lua function '%s' return an unknown error.\n", |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 6699 | rule->arg.hlua_rule->fcn.name); |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6700 | |
| 6701 | default: |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6702 | goto end; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6703 | } |
Christopher Faulet | 7716cdf | 2020-01-29 11:53:30 +0100 | [diff] [blame] | 6704 | |
| 6705 | end: |
| 6706 | return act_ret; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 6707 | } |
| 6708 | |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6709 | 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] | 6710 | { |
Olivier Houchard | 9f6af33 | 2018-05-25 14:04:04 +0200 | [diff] [blame] | 6711 | struct appctx *ctx = context; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6712 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6713 | appctx_wakeup(ctx); |
Willy Tarreau | d958741 | 2017-08-23 16:07:33 +0200 | [diff] [blame] | 6714 | t->expire = TICK_ETERNITY; |
Willy Tarreau | d1aa41f | 2017-07-21 16:41:56 +0200 | [diff] [blame] | 6715 | return t; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6716 | } |
| 6717 | |
| 6718 | static int hlua_applet_tcp_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6719 | { |
| 6720 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6721 | struct hlua *hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6722 | struct task *task; |
| 6723 | char **arg; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6724 | const char *error; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6725 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6726 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6727 | if (!hlua) { |
| 6728 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6729 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6730 | return 0; |
| 6731 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6732 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6733 | ctx->ctx.hlua_apptcp.hlua = hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6734 | ctx->ctx.hlua_apptcp.flags = 0; |
| 6735 | |
| 6736 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6737 | task = task_new(tid_bit); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6738 | if (!task) { |
| 6739 | SEND_ERR(px, "Lua applet tcp '%s': out of memory.\n", |
| 6740 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6741 | return 0; |
| 6742 | } |
| 6743 | task->nice = 0; |
| 6744 | task->context = ctx; |
| 6745 | task->process = hlua_applet_wakeup; |
| 6746 | ctx->ctx.hlua_apptcp.task = task; |
| 6747 | |
| 6748 | /* In the execution wrappers linked with a stream, the |
| 6749 | * Lua context can be not initialized. This behavior |
| 6750 | * permits to save performances because a systematic |
| 6751 | * Lua initialization cause 5% performances loss. |
| 6752 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6753 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6754 | SEND_ERR(px, "Lua applet tcp '%s': can't initialize Lua context.\n", |
| 6755 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6756 | return 0; |
| 6757 | } |
| 6758 | |
| 6759 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6760 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6761 | |
| 6762 | /* The following Lua calls can fail. */ |
| 6763 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6764 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6765 | error = lua_tostring(hlua->T, -1); |
| 6766 | else |
| 6767 | error = "critical error"; |
| 6768 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6769 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6770 | return 0; |
| 6771 | } |
| 6772 | |
| 6773 | /* Check stack available size. */ |
| 6774 | if (!lua_checkstack(hlua->T, 1)) { |
| 6775 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6776 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6777 | RESET_SAFE_LJMP(hlua->T); |
| 6778 | return 0; |
| 6779 | } |
| 6780 | |
| 6781 | /* Restore the function in the stack. */ |
| 6782 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6783 | |
| 6784 | /* Create and and push object stream in the stack. */ |
| 6785 | if (!hlua_applet_tcp_new(hlua->T, ctx)) { |
| 6786 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6787 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6788 | RESET_SAFE_LJMP(hlua->T); |
| 6789 | return 0; |
| 6790 | } |
| 6791 | hlua->nargs = 1; |
| 6792 | |
| 6793 | /* push keywords in the stack. */ |
| 6794 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6795 | if (!lua_checkstack(hlua->T, 1)) { |
| 6796 | SEND_ERR(px, "Lua applet tcp '%s': full stack.\n", |
| 6797 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6798 | RESET_SAFE_LJMP(hlua->T); |
| 6799 | return 0; |
| 6800 | } |
| 6801 | lua_pushstring(hlua->T, *arg); |
| 6802 | hlua->nargs++; |
| 6803 | } |
| 6804 | |
| 6805 | RESET_SAFE_LJMP(hlua->T); |
| 6806 | |
| 6807 | /* Wakeup the applet ASAP. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 6808 | si_cant_get(si); |
Willy Tarreau | 3367d41 | 2018-11-15 10:57:41 +0100 | [diff] [blame] | 6809 | si_rx_endp_more(si); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6810 | |
| 6811 | return 1; |
| 6812 | } |
| 6813 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 6814 | void hlua_applet_tcp_fct(struct appctx *ctx) |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6815 | { |
| 6816 | struct stream_interface *si = ctx->owner; |
| 6817 | struct stream *strm = si_strm(si); |
| 6818 | struct channel *res = si_ic(si); |
| 6819 | struct act_rule *rule = ctx->rule; |
| 6820 | struct proxy *px = strm->be; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6821 | struct hlua *hlua = ctx->ctx.hlua_apptcp.hlua; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6822 | |
| 6823 | /* The applet execution is already done. */ |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6824 | if (ctx->ctx.hlua_apptcp.flags & APPLET_DONE) { |
| 6825 | /* eat the whole request */ |
| 6826 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6827 | return; |
Olivier Houchard | 594c8c5 | 2018-08-28 14:41:31 +0200 | [diff] [blame] | 6828 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6829 | |
| 6830 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 6831 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 6832 | return; |
| 6833 | |
| 6834 | /* Execute the function. */ |
| 6835 | switch (hlua_ctx_resume(hlua, 1)) { |
| 6836 | /* finished. */ |
| 6837 | case HLUA_E_OK: |
| 6838 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6839 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6840 | /* eat the whole request */ |
Willy Tarreau | a79021a | 2018-06-15 18:07:57 +0200 | [diff] [blame] | 6841 | co_skip(si_oc(si), co_data(si_oc(si))); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6842 | res->flags |= CF_READ_NULL; |
| 6843 | si_shutr(si); |
| 6844 | return; |
| 6845 | |
| 6846 | /* yield. */ |
| 6847 | case HLUA_E_AGAIN: |
Thierry Fournier | 0164f20 | 2016-02-20 17:47:43 +0100 | [diff] [blame] | 6848 | if (hlua->wake_time != TICK_ETERNITY) |
| 6849 | task_schedule(ctx->ctx.hlua_apptcp.task, hlua->wake_time); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6850 | return; |
| 6851 | |
| 6852 | /* finished with error. */ |
| 6853 | case HLUA_E_ERRMSG: |
| 6854 | /* Display log. */ |
| 6855 | SEND_ERR(px, "Lua applet tcp '%s': %s.\n", |
| 6856 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 6857 | lua_pop(hlua->T, 1); |
| 6858 | goto error; |
| 6859 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 6860 | case HLUA_E_ETMOUT: |
| 6861 | SEND_ERR(px, "Lua applet tcp '%s': execution timeout.\n", |
| 6862 | rule->arg.hlua_rule->fcn.name); |
| 6863 | goto error; |
| 6864 | |
| 6865 | case HLUA_E_NOMEM: |
| 6866 | SEND_ERR(px, "Lua applet tcp '%s': out of memory error.\n", |
| 6867 | rule->arg.hlua_rule->fcn.name); |
| 6868 | goto error; |
| 6869 | |
| 6870 | case HLUA_E_YIELD: /* unexpected */ |
| 6871 | SEND_ERR(px, "Lua applet tcp '%s': yield not allowed.\n", |
| 6872 | rule->arg.hlua_rule->fcn.name); |
| 6873 | goto error; |
| 6874 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6875 | case HLUA_E_ERR: |
| 6876 | /* Display log. */ |
| 6877 | SEND_ERR(px, "Lua applet tcp '%s' return an unknown error.\n", |
| 6878 | rule->arg.hlua_rule->fcn.name); |
| 6879 | goto error; |
| 6880 | |
| 6881 | default: |
| 6882 | goto error; |
| 6883 | } |
| 6884 | |
| 6885 | error: |
| 6886 | |
| 6887 | /* For all other cases, just close the stream. */ |
| 6888 | si_shutw(si); |
| 6889 | si_shutr(si); |
| 6890 | ctx->ctx.hlua_apptcp.flags |= APPLET_DONE; |
| 6891 | } |
| 6892 | |
| 6893 | static void hlua_applet_tcp_release(struct appctx *ctx) |
| 6894 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 6895 | task_destroy(ctx->ctx.hlua_apptcp.task); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6896 | ctx->ctx.hlua_apptcp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6897 | hlua_ctx_destroy(ctx->ctx.hlua_apptcp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6898 | ctx->ctx.hlua_apptcp.hlua = NULL; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 6899 | } |
| 6900 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6901 | /* The function returns 1 if the initialisation is complete, 0 if |
| 6902 | * an errors occurs and -1 if more data are required for initializing |
| 6903 | * the applet. |
| 6904 | */ |
| 6905 | static int hlua_applet_http_init(struct appctx *ctx, struct proxy *px, struct stream *strm) |
| 6906 | { |
| 6907 | struct stream_interface *si = ctx->owner; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6908 | struct http_txn *txn; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6909 | struct hlua *hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6910 | char **arg; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6911 | struct task *task; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6912 | const char *error; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6913 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6914 | txn = strm->txn; |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 6915 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6916 | if (!hlua) { |
| 6917 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6918 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6919 | return 0; |
| 6920 | } |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6921 | HLUA_INIT(hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 6922 | ctx->ctx.hlua_apphttp.hlua = hlua; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6923 | ctx->ctx.hlua_apphttp.left_bytes = -1; |
| 6924 | ctx->ctx.hlua_apphttp.flags = 0; |
| 6925 | |
Thierry FOURNIER | d93ea2b | 2015-12-20 19:14:52 +0100 | [diff] [blame] | 6926 | if (txn->req.flags & HTTP_MSGF_VER_11) |
| 6927 | ctx->ctx.hlua_apphttp.flags |= APPLET_HTTP11; |
| 6928 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6929 | /* Create task used by signal to wakeup applets. */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 6930 | task = task_new(tid_bit); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6931 | if (!task) { |
| 6932 | SEND_ERR(px, "Lua applet http '%s': out of memory.\n", |
| 6933 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6934 | return 0; |
| 6935 | } |
| 6936 | task->nice = 0; |
| 6937 | task->context = ctx; |
| 6938 | task->process = hlua_applet_wakeup; |
| 6939 | ctx->ctx.hlua_apphttp.task = task; |
| 6940 | |
| 6941 | /* In the execution wrappers linked with a stream, the |
| 6942 | * Lua context can be not initialized. This behavior |
| 6943 | * permits to save performances because a systematic |
| 6944 | * Lua initialization cause 5% performances loss. |
| 6945 | */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 6946 | if (!hlua_ctx_init(hlua, task, 0)) { |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6947 | SEND_ERR(px, "Lua applet http '%s': can't initialize Lua context.\n", |
| 6948 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6949 | return 0; |
| 6950 | } |
| 6951 | |
| 6952 | /* Set timeout according with the applet configuration. */ |
Thierry FOURNIER | 10770fa | 2015-09-29 01:59:42 +0200 | [diff] [blame] | 6953 | hlua->max_time = ctx->applet->timeout; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6954 | |
| 6955 | /* The following Lua calls can fail. */ |
| 6956 | if (!SET_SAFE_LJMP(hlua->T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 6957 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 6958 | error = lua_tostring(hlua->T, -1); |
| 6959 | else |
| 6960 | error = "critical error"; |
| 6961 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 6962 | ctx->rule->arg.hlua_rule->fcn.name, error); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6963 | return 0; |
| 6964 | } |
| 6965 | |
| 6966 | /* Check stack available size. */ |
| 6967 | if (!lua_checkstack(hlua->T, 1)) { |
| 6968 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6969 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6970 | RESET_SAFE_LJMP(hlua->T); |
| 6971 | return 0; |
| 6972 | } |
| 6973 | |
| 6974 | /* Restore the function in the stack. */ |
| 6975 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, ctx->rule->arg.hlua_rule->fcn.function_ref); |
| 6976 | |
| 6977 | /* Create and and push object stream in the stack. */ |
| 6978 | if (!hlua_applet_http_new(hlua->T, ctx)) { |
| 6979 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6980 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6981 | RESET_SAFE_LJMP(hlua->T); |
| 6982 | return 0; |
| 6983 | } |
| 6984 | hlua->nargs = 1; |
| 6985 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 6986 | /* push keywords in the stack. */ |
| 6987 | for (arg = ctx->rule->arg.hlua_rule->args; arg && *arg; arg++) { |
| 6988 | if (!lua_checkstack(hlua->T, 1)) { |
| 6989 | SEND_ERR(px, "Lua applet http '%s': full stack.\n", |
| 6990 | ctx->rule->arg.hlua_rule->fcn.name); |
| 6991 | RESET_SAFE_LJMP(hlua->T); |
| 6992 | return 0; |
| 6993 | } |
| 6994 | lua_pushstring(hlua->T, *arg); |
| 6995 | hlua->nargs++; |
| 6996 | } |
| 6997 | |
| 6998 | RESET_SAFE_LJMP(hlua->T); |
| 6999 | |
| 7000 | /* Wakeup the applet when data is ready for read. */ |
Willy Tarreau | 0cd3bd6 | 2018-11-06 18:46:37 +0100 | [diff] [blame] | 7001 | si_cant_get(si); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7002 | |
| 7003 | return 1; |
| 7004 | } |
| 7005 | |
Willy Tarreau | 60409db | 2019-08-21 14:14:50 +0200 | [diff] [blame] | 7006 | void hlua_applet_http_fct(struct appctx *ctx) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7007 | { |
| 7008 | struct stream_interface *si = ctx->owner; |
| 7009 | struct stream *strm = si_strm(si); |
| 7010 | struct channel *req = si_oc(si); |
| 7011 | struct channel *res = si_ic(si); |
| 7012 | struct act_rule *rule = ctx->rule; |
| 7013 | struct proxy *px = strm->be; |
| 7014 | struct hlua *hlua = ctx->ctx.hlua_apphttp.hlua; |
| 7015 | struct htx *req_htx, *res_htx; |
| 7016 | |
| 7017 | res_htx = htx_from_buf(&res->buf); |
| 7018 | |
| 7019 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7020 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7021 | goto out; |
| 7022 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7023 | /* Check if the input buffer is available. */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7024 | if (!b_size(&res->buf)) { |
| 7025 | si_rx_room_blk(si); |
| 7026 | goto out; |
| 7027 | } |
| 7028 | /* check that the output is not closed */ |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7029 | if (res->flags & (CF_SHUTW|CF_SHUTW_NOW|CF_SHUTR)) |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7030 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7031 | |
| 7032 | /* Set the currently running flag. */ |
| 7033 | if (!HLUA_IS_RUNNING(hlua) && |
| 7034 | !(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7035 | struct htx_blk *blk; |
| 7036 | size_t count = co_data(req); |
| 7037 | |
| 7038 | if (!count) { |
| 7039 | si_cant_get(si); |
| 7040 | goto out; |
| 7041 | } |
| 7042 | |
| 7043 | /* We need to flush the request header. This left the body for |
| 7044 | * the Lua. |
| 7045 | */ |
| 7046 | req_htx = htx_from_buf(&req->buf); |
Christopher Faulet | a3f1550 | 2019-05-13 15:27:23 +0200 | [diff] [blame] | 7047 | blk = htx_get_first_blk(req_htx); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7048 | while (count && blk) { |
| 7049 | enum htx_blk_type type = htx_get_blk_type(blk); |
| 7050 | uint32_t sz = htx_get_blksz(blk); |
| 7051 | |
| 7052 | if (sz > count) { |
| 7053 | si_cant_get(si); |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7054 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7055 | goto out; |
| 7056 | } |
| 7057 | |
| 7058 | count -= sz; |
| 7059 | co_set_data(req, co_data(req) - sz); |
| 7060 | blk = htx_remove_blk(req_htx, blk); |
| 7061 | |
| 7062 | if (type == HTX_BLK_EOH) |
| 7063 | break; |
| 7064 | } |
Christopher Faulet | 0ae79d0 | 2019-02-27 21:36:59 +0100 | [diff] [blame] | 7065 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7066 | } |
| 7067 | |
| 7068 | /* Executes The applet if it is not done. */ |
| 7069 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_DONE)) { |
| 7070 | |
| 7071 | /* Execute the function. */ |
| 7072 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7073 | /* finished. */ |
| 7074 | case HLUA_E_OK: |
| 7075 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7076 | break; |
| 7077 | |
| 7078 | /* yield. */ |
| 7079 | case HLUA_E_AGAIN: |
| 7080 | if (hlua->wake_time != TICK_ETERNITY) |
| 7081 | task_schedule(ctx->ctx.hlua_apphttp.task, hlua->wake_time); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7082 | goto out; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7083 | |
| 7084 | /* finished with error. */ |
| 7085 | case HLUA_E_ERRMSG: |
| 7086 | /* Display log. */ |
| 7087 | SEND_ERR(px, "Lua applet http '%s': %s.\n", |
| 7088 | rule->arg.hlua_rule->fcn.name, lua_tostring(hlua->T, -1)); |
| 7089 | lua_pop(hlua->T, 1); |
| 7090 | goto error; |
| 7091 | |
| 7092 | case HLUA_E_ETMOUT: |
| 7093 | SEND_ERR(px, "Lua applet http '%s': execution timeout.\n", |
| 7094 | rule->arg.hlua_rule->fcn.name); |
| 7095 | goto error; |
| 7096 | |
| 7097 | case HLUA_E_NOMEM: |
| 7098 | SEND_ERR(px, "Lua applet http '%s': out of memory error.\n", |
| 7099 | rule->arg.hlua_rule->fcn.name); |
| 7100 | goto error; |
| 7101 | |
| 7102 | case HLUA_E_YIELD: /* unexpected */ |
| 7103 | SEND_ERR(px, "Lua applet http '%s': yield not allowed.\n", |
| 7104 | rule->arg.hlua_rule->fcn.name); |
| 7105 | goto error; |
| 7106 | |
| 7107 | case HLUA_E_ERR: |
| 7108 | /* Display log. */ |
| 7109 | SEND_ERR(px, "Lua applet http '%s' return an unknown error.\n", |
| 7110 | rule->arg.hlua_rule->fcn.name); |
| 7111 | goto error; |
| 7112 | |
| 7113 | default: |
| 7114 | goto error; |
| 7115 | } |
| 7116 | } |
| 7117 | |
| 7118 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7119 | if (ctx->ctx.hlua_apphttp.flags & APPLET_RSP_SENT) |
| 7120 | goto done; |
| 7121 | |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7122 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) |
| 7123 | goto error; |
| 7124 | |
Christopher Faulet | 54b5e21 | 2019-06-04 10:08:28 +0200 | [diff] [blame] | 7125 | /* Don't add TLR because mux-h1 will take care of it */ |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7126 | if (!htx_add_endof(res_htx, HTX_BLK_EOM)) { |
| 7127 | si_rx_room_blk(si); |
| 7128 | goto out; |
| 7129 | } |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7130 | channel_add_input(res, 1); |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7131 | strm->txn->status = ctx->ctx.hlua_apphttp.status; |
| 7132 | ctx->ctx.hlua_apphttp.flags |= APPLET_RSP_SENT; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7133 | } |
| 7134 | |
| 7135 | done: |
| 7136 | if (ctx->ctx.hlua_apphttp.flags & APPLET_DONE) { |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7137 | if (!(res->flags & CF_SHUTR)) { |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7138 | res->flags |= CF_READ_NULL; |
Christopher Faulet | 56a3d6e | 2019-02-27 22:06:23 +0100 | [diff] [blame] | 7139 | si_shutr(si); |
| 7140 | } |
| 7141 | |
| 7142 | /* eat the whole request */ |
| 7143 | if (co_data(req)) { |
| 7144 | req_htx = htx_from_buf(&req->buf); |
| 7145 | co_htx_skip(req, req_htx, co_data(req)); |
| 7146 | htx_to_buf(req_htx, &req->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7147 | } |
| 7148 | } |
| 7149 | |
| 7150 | out: |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7151 | htx_to_buf(res_htx, &res->buf); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7152 | return; |
| 7153 | |
| 7154 | error: |
| 7155 | |
| 7156 | /* If we are in HTTP mode, and we are not send any |
| 7157 | * data, return a 500 server error in best effort: |
| 7158 | * if there is no room available in the buffer, |
| 7159 | * just close the connection. |
| 7160 | */ |
| 7161 | if (!(ctx->ctx.hlua_apphttp.flags & APPLET_HDR_SENT)) { |
Christopher Faulet | f734638 | 2019-07-17 22:02:08 +0200 | [diff] [blame] | 7162 | struct buffer *err = &http_err_chunks[HTTP_ERR_500]; |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7163 | |
| 7164 | channel_erase(res); |
| 7165 | res->buf.data = b_data(err); |
| 7166 | memcpy(res->buf.area, b_head(err), b_data(err)); |
| 7167 | res_htx = htx_from_buf(&res->buf); |
Christopher Faulet | f6cce3f | 2019-02-27 21:20:09 +0100 | [diff] [blame] | 7168 | channel_add_input(res, res_htx->data); |
Christopher Faulet | 9c832fc | 2018-12-14 13:34:05 +0100 | [diff] [blame] | 7169 | } |
| 7170 | if (!(strm->flags & SF_ERR_MASK)) |
| 7171 | strm->flags |= SF_ERR_RESOURCE; |
| 7172 | ctx->ctx.hlua_apphttp.flags |= APPLET_DONE; |
| 7173 | goto done; |
| 7174 | } |
| 7175 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7176 | static void hlua_applet_http_release(struct appctx *ctx) |
| 7177 | { |
Olivier Houchard | 3f795f7 | 2019-04-17 22:51:06 +0200 | [diff] [blame] | 7178 | task_destroy(ctx->ctx.hlua_apphttp.task); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7179 | ctx->ctx.hlua_apphttp.task = NULL; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7180 | hlua_ctx_destroy(ctx->ctx.hlua_apphttp.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7181 | ctx->ctx.hlua_apphttp.hlua = NULL; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7182 | } |
| 7183 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7184 | /* global {tcp|http}-request parser. Return ACT_RET_PRS_OK in |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7185 | * success case, else return ACT_RET_PRS_ERR. |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7186 | * |
| 7187 | * This function can fail with an abort() due to an Lua critical error. |
| 7188 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7189 | * tolerated. |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7190 | */ |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7191 | static enum act_parse_ret action_register_lua(const char **args, int *cur_arg, struct proxy *px, |
| 7192 | struct act_rule *rule, char **err) |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7193 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7194 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7195 | int i; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7196 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7197 | /* Memory for the rule. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7198 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7199 | if (!rule->arg.hlua_rule) { |
| 7200 | memprintf(err, "out of memory error"); |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7201 | return ACT_RET_PRS_ERR; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7202 | } |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7203 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7204 | /* Memory for arguments. */ |
| 7205 | rule->arg.hlua_rule->args = calloc(fcn->nargs + 1, sizeof(char *)); |
| 7206 | if (!rule->arg.hlua_rule->args) { |
| 7207 | memprintf(err, "out of memory error"); |
| 7208 | return ACT_RET_PRS_ERR; |
| 7209 | } |
| 7210 | |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7211 | /* Reference the Lua function and store the reference. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7212 | rule->arg.hlua_rule->fcn = *fcn; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7213 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7214 | /* Expect some arguments */ |
| 7215 | for (i = 0; i < fcn->nargs; i++) { |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7216 | if (*args[*cur_arg] == '\0') { |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7217 | memprintf(err, "expect %d arguments", fcn->nargs); |
| 7218 | return ACT_RET_PRS_ERR; |
| 7219 | } |
Thierry FOURNIER | 1725c2e | 2019-01-06 19:38:49 +0100 | [diff] [blame] | 7220 | rule->arg.hlua_rule->args[i] = strdup(args[*cur_arg]); |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7221 | if (!rule->arg.hlua_rule->args[i]) { |
| 7222 | memprintf(err, "out of memory error"); |
| 7223 | return ACT_RET_PRS_ERR; |
| 7224 | } |
| 7225 | (*cur_arg)++; |
| 7226 | } |
| 7227 | rule->arg.hlua_rule->args[i] = NULL; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7228 | |
Thierry FOURNIER | 4214873 | 2015-09-02 17:17:33 +0200 | [diff] [blame] | 7229 | rule->action = ACT_CUSTOM; |
Thierry FOURNIER | 4dc15d1 | 2015-08-06 18:25:56 +0200 | [diff] [blame] | 7230 | rule->action_ptr = hlua_action; |
Thierry FOURNIER | afa8049 | 2015-08-19 09:04:15 +0200 | [diff] [blame] | 7231 | return ACT_RET_PRS_OK; |
Thierry FOURNIER | 258d8aa | 2015-02-16 20:23:40 +0100 | [diff] [blame] | 7232 | } |
| 7233 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7234 | static enum act_parse_ret action_register_service_http(const char **args, int *cur_arg, struct proxy *px, |
| 7235 | struct act_rule *rule, char **err) |
| 7236 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7237 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7238 | |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7239 | /* HTTP applets are forbidden in tcp-request rules. |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7240 | * HTTP applet request requires everything initialized by |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7241 | * "http_process_request" (analyzer flag AN_REQ_HTTP_INNER). |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7242 | * The applet will be immediately initialized, but its before |
Thierry FOURNIER | 718e2a7 | 2015-12-20 20:13:14 +0100 | [diff] [blame] | 7243 | * the call of this analyzer. |
| 7244 | */ |
| 7245 | if (rule->from != ACT_F_HTTP_REQ) { |
| 7246 | memprintf(err, "HTTP applets are forbidden from 'tcp-request' rulesets"); |
| 7247 | return ACT_RET_PRS_ERR; |
| 7248 | } |
| 7249 | |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7250 | /* Memory for the rule. */ |
| 7251 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7252 | if (!rule->arg.hlua_rule) { |
| 7253 | memprintf(err, "out of memory error"); |
| 7254 | return ACT_RET_PRS_ERR; |
| 7255 | } |
| 7256 | |
| 7257 | /* Reference the Lua function and store the reference. */ |
| 7258 | rule->arg.hlua_rule->fcn = *fcn; |
| 7259 | |
| 7260 | /* TODO: later accept arguments. */ |
| 7261 | rule->arg.hlua_rule->args = NULL; |
| 7262 | |
| 7263 | /* Add applet pointer in the rule. */ |
| 7264 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7265 | rule->applet.name = fcn->name; |
| 7266 | rule->applet.init = hlua_applet_http_init; |
| 7267 | rule->applet.fct = hlua_applet_http_fct; |
| 7268 | rule->applet.release = hlua_applet_http_release; |
| 7269 | rule->applet.timeout = hlua_timeout_applet; |
| 7270 | |
| 7271 | return ACT_RET_PRS_OK; |
| 7272 | } |
| 7273 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7274 | /* This function is an LUA binding used for registering |
| 7275 | * "sample-conv" functions. It expects a converter name used |
| 7276 | * in the haproxy configuration file, and an LUA function. |
| 7277 | */ |
| 7278 | __LJMP static int hlua_register_action(lua_State *L) |
| 7279 | { |
| 7280 | struct action_kw_list *akl; |
| 7281 | const char *name; |
| 7282 | int ref; |
| 7283 | int len; |
| 7284 | struct hlua_function *fcn; |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7285 | int nargs; |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7286 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7287 | /* Initialise the number of expected arguments at 0. */ |
| 7288 | nargs = 0; |
| 7289 | |
| 7290 | if (lua_gettop(L) < 3 || lua_gettop(L) > 4) |
| 7291 | 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] | 7292 | |
| 7293 | /* First argument : converter name. */ |
| 7294 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7295 | |
| 7296 | /* Second argument : environment. */ |
| 7297 | if (lua_type(L, 2) != LUA_TTABLE) |
| 7298 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7299 | |
| 7300 | /* Third argument : lua function. */ |
| 7301 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7302 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7303 | /* 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] | 7304 | if (lua_gettop(L) >= 4) |
| 7305 | nargs = MAY_LJMP(luaL_checkinteger(L, 4)); |
| 7306 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7307 | /* browse the second argument as an array. */ |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7308 | lua_pushnil(L); |
| 7309 | while (lua_next(L, 2) != 0) { |
| 7310 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7311 | WILL_LJMP(luaL_error(L, "register_action: second argument must be a table of strings")); |
| 7312 | |
| 7313 | /* Check required environment. Only accepted "http" or "tcp". */ |
| 7314 | /* Allocate and fill the sample fetch keyword struct. */ |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7315 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7316 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7317 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7318 | fcn = calloc(1, sizeof(*fcn)); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7319 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7320 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7321 | |
| 7322 | /* Fill fcn. */ |
| 7323 | fcn->name = strdup(name); |
| 7324 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7325 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7326 | fcn->function_ref = ref; |
| 7327 | |
Thierry FOURNIER / OZON.IO | 4b123be | 2016-12-09 18:03:31 +0100 | [diff] [blame] | 7328 | /* Set the expected number od arguments. */ |
| 7329 | fcn->nargs = nargs; |
| 7330 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7331 | /* List head */ |
| 7332 | akl->list.n = akl->list.p = NULL; |
| 7333 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7334 | /* action keyword. */ |
| 7335 | len = strlen("lua.") + strlen(name) + 1; |
Thierry FOURNIER | 3c7a77c | 2015-09-26 00:51:16 +0200 | [diff] [blame] | 7336 | akl->kw[0].kw = calloc(1, len); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7337 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7338 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7339 | |
| 7340 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7341 | |
| 7342 | akl->kw[0].match_pfx = 0; |
| 7343 | akl->kw[0].private = fcn; |
| 7344 | akl->kw[0].parse = action_register_lua; |
| 7345 | |
| 7346 | /* select the action registering point. */ |
| 7347 | if (strcmp(lua_tostring(L, -1), "tcp-req") == 0) |
| 7348 | tcp_req_cont_keywords_register(akl); |
| 7349 | else if (strcmp(lua_tostring(L, -1), "tcp-res") == 0) |
| 7350 | tcp_res_cont_keywords_register(akl); |
| 7351 | else if (strcmp(lua_tostring(L, -1), "http-req") == 0) |
| 7352 | http_req_keywords_register(akl); |
| 7353 | else if (strcmp(lua_tostring(L, -1), "http-res") == 0) |
| 7354 | http_res_keywords_register(akl); |
| 7355 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7356 | WILL_LJMP(luaL_error(L, "Lua action environment '%s' is unknown. " |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7357 | "'tcp-req', 'tcp-res', 'http-req' or 'http-res' " |
| 7358 | "are expected.", lua_tostring(L, -1))); |
| 7359 | |
| 7360 | /* pop the environment string. */ |
| 7361 | lua_pop(L, 1); |
| 7362 | } |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7363 | return ACT_RET_PRS_OK; |
| 7364 | } |
| 7365 | |
| 7366 | static enum act_parse_ret action_register_service_tcp(const char **args, int *cur_arg, struct proxy *px, |
| 7367 | struct act_rule *rule, char **err) |
| 7368 | { |
Vincent Bernat | 3c2f2f2 | 2016-04-03 13:48:42 +0200 | [diff] [blame] | 7369 | struct hlua_function *fcn = rule->kw->private; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7370 | |
Christopher Faulet | 280f85b | 2019-07-15 15:02:04 +0200 | [diff] [blame] | 7371 | if (px->mode == PR_MODE_HTTP) { |
| 7372 | memprintf(err, "Lua TCP services cannot be used on HTTP proxies"); |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7373 | return ACT_RET_PRS_ERR; |
| 7374 | } |
| 7375 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7376 | /* Memory for the rule. */ |
| 7377 | rule->arg.hlua_rule = calloc(1, sizeof(*rule->arg.hlua_rule)); |
| 7378 | if (!rule->arg.hlua_rule) { |
| 7379 | memprintf(err, "out of memory error"); |
| 7380 | return ACT_RET_PRS_ERR; |
| 7381 | } |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7382 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7383 | /* Reference the Lua function and store the reference. */ |
| 7384 | rule->arg.hlua_rule->fcn = *fcn; |
| 7385 | |
| 7386 | /* TODO: later accept arguments. */ |
| 7387 | rule->arg.hlua_rule->args = NULL; |
| 7388 | |
| 7389 | /* Add applet pointer in the rule. */ |
| 7390 | rule->applet.obj_type = OBJ_TYPE_APPLET; |
| 7391 | rule->applet.name = fcn->name; |
| 7392 | rule->applet.init = hlua_applet_tcp_init; |
| 7393 | rule->applet.fct = hlua_applet_tcp_fct; |
| 7394 | rule->applet.release = hlua_applet_tcp_release; |
| 7395 | rule->applet.timeout = hlua_timeout_applet; |
| 7396 | |
| 7397 | return 0; |
| 7398 | } |
| 7399 | |
| 7400 | /* This function is an LUA binding used for registering |
| 7401 | * "sample-conv" functions. It expects a converter name used |
| 7402 | * in the haproxy configuration file, and an LUA function. |
| 7403 | */ |
| 7404 | __LJMP static int hlua_register_service(lua_State *L) |
| 7405 | { |
| 7406 | struct action_kw_list *akl; |
| 7407 | const char *name; |
| 7408 | const char *env; |
| 7409 | int ref; |
| 7410 | int len; |
| 7411 | struct hlua_function *fcn; |
| 7412 | |
| 7413 | MAY_LJMP(check_args(L, 3, "register_service")); |
| 7414 | |
| 7415 | /* First argument : converter name. */ |
| 7416 | name = MAY_LJMP(luaL_checkstring(L, 1)); |
| 7417 | |
| 7418 | /* Second argument : environment. */ |
| 7419 | env = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7420 | |
| 7421 | /* Third argument : lua function. */ |
| 7422 | ref = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7423 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7424 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7425 | akl = calloc(1, sizeof(*akl) + sizeof(struct action_kw) * 2); |
| 7426 | if (!akl) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7427 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7428 | fcn = calloc(1, sizeof(*fcn)); |
| 7429 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7430 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7431 | |
| 7432 | /* Fill fcn. */ |
| 7433 | len = strlen("<lua.>") + strlen(name) + 1; |
| 7434 | fcn->name = calloc(1, len); |
| 7435 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7436 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7437 | snprintf((char *)fcn->name, len, "<lua.%s>", name); |
| 7438 | fcn->function_ref = ref; |
| 7439 | |
| 7440 | /* List head */ |
| 7441 | akl->list.n = akl->list.p = NULL; |
| 7442 | |
| 7443 | /* converter keyword. */ |
| 7444 | len = strlen("lua.") + strlen(name) + 1; |
| 7445 | akl->kw[0].kw = calloc(1, len); |
| 7446 | if (!akl->kw[0].kw) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7447 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7448 | |
| 7449 | snprintf((char *)akl->kw[0].kw, len, "lua.%s", name); |
| 7450 | |
Thierry FOURNIER / OZON.IO | 02564fd | 2016-11-12 11:07:05 +0100 | [diff] [blame] | 7451 | /* Check required environment. Only accepted "http" or "tcp". */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7452 | if (strcmp(env, "tcp") == 0) |
| 7453 | akl->kw[0].parse = action_register_service_tcp; |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 7454 | else if (strcmp(env, "http") == 0) |
| 7455 | akl->kw[0].parse = action_register_service_http; |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7456 | else |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7457 | WILL_LJMP(luaL_error(L, "Lua service environment '%s' is unknown. " |
Eric Salama | fe7456f | 2017-12-21 14:30:07 +0100 | [diff] [blame] | 7458 | "'tcp' or 'http' are expected.", env)); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7459 | |
| 7460 | akl->kw[0].match_pfx = 0; |
| 7461 | akl->kw[0].private = fcn; |
| 7462 | |
| 7463 | /* End of array. */ |
| 7464 | memset(&akl->kw[1], 0, sizeof(*akl->kw)); |
| 7465 | |
| 7466 | /* Register this new converter */ |
| 7467 | service_keywords_register(akl); |
| 7468 | |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 7469 | return 0; |
| 7470 | } |
| 7471 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7472 | /* This function initialises Lua cli handler. It copies the |
| 7473 | * arguments in the Lua stack and create channel IO objects. |
| 7474 | */ |
Aurélien Nephtali | abbf607 | 2018-04-18 13:26:46 +0200 | [diff] [blame] | 7475 | 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] | 7476 | { |
| 7477 | struct hlua *hlua; |
| 7478 | struct hlua_function *fcn; |
| 7479 | int i; |
| 7480 | const char *error; |
| 7481 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7482 | fcn = private; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7483 | appctx->ctx.hlua_cli.fcn = private; |
| 7484 | |
Willy Tarreau | bafbe01 | 2017-11-24 17:34:44 +0100 | [diff] [blame] | 7485 | hlua = pool_alloc(pool_head_hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7486 | if (!hlua) { |
| 7487 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7488 | return 1; |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7489 | } |
| 7490 | HLUA_INIT(hlua); |
| 7491 | appctx->ctx.hlua_cli.hlua = hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7492 | |
| 7493 | /* Create task used by signal to wakeup applets. |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7494 | * We use the same wakeup function than the Lua applet_tcp and |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7495 | * applet_http. It is absolutely compatible. |
| 7496 | */ |
Willy Tarreau | 5f4a47b | 2017-10-31 15:59:32 +0100 | [diff] [blame] | 7497 | appctx->ctx.hlua_cli.task = task_new(tid_bit); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7498 | if (!appctx->ctx.hlua_cli.task) { |
Thierry FOURNIER | ffbf569 | 2016-12-16 11:14:06 +0100 | [diff] [blame] | 7499 | SEND_ERR(NULL, "Lua cli '%s': out of memory.\n", fcn->name); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7500 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7501 | } |
| 7502 | appctx->ctx.hlua_cli.task->nice = 0; |
| 7503 | appctx->ctx.hlua_cli.task->context = appctx; |
| 7504 | appctx->ctx.hlua_cli.task->process = hlua_applet_wakeup; |
| 7505 | |
| 7506 | /* Initialises the Lua context */ |
Thierry FOURNIER | bf90ce1 | 2019-01-06 19:04:24 +0100 | [diff] [blame] | 7507 | 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] | 7508 | 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] | 7509 | goto error; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7510 | } |
| 7511 | |
| 7512 | /* The following Lua calls can fail. */ |
| 7513 | if (!SET_SAFE_LJMP(hlua->T)) { |
| 7514 | if (lua_type(hlua->T, -1) == LUA_TSTRING) |
| 7515 | error = lua_tostring(hlua->T, -1); |
| 7516 | else |
| 7517 | error = "critical error"; |
| 7518 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", fcn->name, error); |
| 7519 | goto error; |
| 7520 | } |
| 7521 | |
| 7522 | /* Check stack available size. */ |
| 7523 | if (!lua_checkstack(hlua->T, 2)) { |
| 7524 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7525 | goto error; |
| 7526 | } |
| 7527 | |
| 7528 | /* Restore the function in the stack. */ |
| 7529 | lua_rawgeti(hlua->T, LUA_REGISTRYINDEX, fcn->function_ref); |
| 7530 | |
| 7531 | /* Once the arguments parsed, the CLI is like an AppletTCP, |
| 7532 | * so push AppletTCP in the stack. |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7533 | */ |
| 7534 | if (!hlua_applet_tcp_new(hlua->T, appctx)) { |
| 7535 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7536 | goto error; |
| 7537 | } |
| 7538 | hlua->nargs = 1; |
| 7539 | |
| 7540 | /* push keywords in the stack. */ |
| 7541 | for (i = 0; *args[i]; i++) { |
| 7542 | /* Check stack available size. */ |
| 7543 | if (!lua_checkstack(hlua->T, 1)) { |
| 7544 | SEND_ERR(NULL, "Lua cli '%s': full stack.\n", fcn->name); |
| 7545 | goto error; |
| 7546 | } |
| 7547 | lua_pushstring(hlua->T, args[i]); |
| 7548 | hlua->nargs++; |
| 7549 | } |
| 7550 | |
| 7551 | /* We must initialize the execution timeouts. */ |
| 7552 | hlua->max_time = hlua_timeout_session; |
| 7553 | |
| 7554 | /* At this point the execution is safe. */ |
| 7555 | RESET_SAFE_LJMP(hlua->T); |
| 7556 | |
| 7557 | /* It's ok */ |
| 7558 | return 0; |
| 7559 | |
| 7560 | /* It's not ok. */ |
| 7561 | error: |
| 7562 | RESET_SAFE_LJMP(hlua->T); |
| 7563 | hlua_ctx_destroy(hlua); |
Thierry FOURNIER | 1be3415 | 2016-12-17 12:09:51 +0100 | [diff] [blame] | 7564 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7565 | return 1; |
| 7566 | } |
| 7567 | |
| 7568 | static int hlua_cli_io_handler_fct(struct appctx *appctx) |
| 7569 | { |
| 7570 | struct hlua *hlua; |
| 7571 | struct stream_interface *si; |
| 7572 | struct hlua_function *fcn; |
| 7573 | |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7574 | hlua = appctx->ctx.hlua_cli.hlua; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7575 | si = appctx->owner; |
Willy Tarreau | 8ae4f75 | 2016-12-14 15:41:45 +0100 | [diff] [blame] | 7576 | fcn = appctx->ctx.hlua_cli.fcn; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7577 | |
| 7578 | /* If the stream is disconnect or closed, ldo nothing. */ |
| 7579 | if (unlikely(si->state == SI_ST_DIS || si->state == SI_ST_CLO)) |
| 7580 | return 1; |
| 7581 | |
| 7582 | /* Execute the function. */ |
| 7583 | switch (hlua_ctx_resume(hlua, 1)) { |
| 7584 | |
| 7585 | /* finished. */ |
| 7586 | case HLUA_E_OK: |
| 7587 | return 1; |
| 7588 | |
| 7589 | /* yield. */ |
| 7590 | case HLUA_E_AGAIN: |
| 7591 | /* We want write. */ |
| 7592 | if (HLUA_IS_WAKERESWR(hlua)) |
Willy Tarreau | db39843 | 2018-11-15 11:08:52 +0100 | [diff] [blame] | 7593 | si_rx_room_blk(si); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7594 | /* Set the timeout. */ |
| 7595 | if (hlua->wake_time != TICK_ETERNITY) |
| 7596 | task_schedule(hlua->task, hlua->wake_time); |
| 7597 | return 0; |
| 7598 | |
| 7599 | /* finished with error. */ |
| 7600 | case HLUA_E_ERRMSG: |
| 7601 | /* Display log. */ |
| 7602 | SEND_ERR(NULL, "Lua cli '%s': %s.\n", |
| 7603 | fcn->name, lua_tostring(hlua->T, -1)); |
| 7604 | lua_pop(hlua->T, 1); |
| 7605 | return 1; |
| 7606 | |
Thierry Fournier | d5b073c | 2018-05-21 19:42:47 +0200 | [diff] [blame] | 7607 | case HLUA_E_ETMOUT: |
| 7608 | SEND_ERR(NULL, "Lua converter '%s': execution timeout.\n", |
| 7609 | fcn->name); |
| 7610 | return 1; |
| 7611 | |
| 7612 | case HLUA_E_NOMEM: |
| 7613 | SEND_ERR(NULL, "Lua converter '%s': out of memory error.\n", |
| 7614 | fcn->name); |
| 7615 | return 1; |
| 7616 | |
| 7617 | case HLUA_E_YIELD: /* unexpected */ |
| 7618 | SEND_ERR(NULL, "Lua converter '%s': yield not allowed.\n", |
| 7619 | fcn->name); |
| 7620 | return 1; |
| 7621 | |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7622 | case HLUA_E_ERR: |
| 7623 | /* Display log. */ |
| 7624 | SEND_ERR(NULL, "Lua cli '%s' return an unknown error.\n", |
| 7625 | fcn->name); |
| 7626 | return 1; |
| 7627 | |
| 7628 | default: |
| 7629 | return 1; |
| 7630 | } |
| 7631 | |
| 7632 | return 1; |
| 7633 | } |
| 7634 | |
| 7635 | static void hlua_cli_io_release_fct(struct appctx *appctx) |
| 7636 | { |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7637 | hlua_ctx_destroy(appctx->ctx.hlua_cli.hlua); |
Thierry FOURNIER | ebed6e9 | 2016-12-16 11:54:07 +0100 | [diff] [blame] | 7638 | appctx->ctx.hlua_cli.hlua = NULL; |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7639 | } |
| 7640 | |
| 7641 | /* This function is an LUA binding used for registering |
| 7642 | * new keywords in the cli. It expects a list of keywords |
| 7643 | * which are the "path". It is limited to 5 keywords. A |
| 7644 | * description of the command, a function to be executed |
| 7645 | * for the parsing and a function for io handlers. |
| 7646 | */ |
| 7647 | __LJMP static int hlua_register_cli(lua_State *L) |
| 7648 | { |
| 7649 | struct cli_kw_list *cli_kws; |
| 7650 | const char *message; |
| 7651 | int ref_io; |
| 7652 | int len; |
| 7653 | struct hlua_function *fcn; |
| 7654 | int index; |
| 7655 | int i; |
| 7656 | |
| 7657 | MAY_LJMP(check_args(L, 3, "register_cli")); |
| 7658 | |
| 7659 | /* First argument : an array of maximum 5 keywords. */ |
| 7660 | if (!lua_istable(L, 1)) |
| 7661 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table")); |
| 7662 | |
| 7663 | /* Second argument : string with contextual message. */ |
| 7664 | message = MAY_LJMP(luaL_checkstring(L, 2)); |
| 7665 | |
| 7666 | /* Third and fourth argument : lua function. */ |
| 7667 | ref_io = MAY_LJMP(hlua_checkfunction(L, 3)); |
| 7668 | |
| 7669 | /* Allocate and fill the sample fetch keyword struct. */ |
| 7670 | cli_kws = calloc(1, sizeof(*cli_kws) + sizeof(struct cli_kw) * 2); |
| 7671 | if (!cli_kws) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7672 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7673 | fcn = calloc(1, sizeof(*fcn)); |
| 7674 | if (!fcn) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7675 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7676 | |
| 7677 | /* Fill path. */ |
| 7678 | index = 0; |
| 7679 | lua_pushnil(L); |
| 7680 | while(lua_next(L, 1) != 0) { |
| 7681 | if (index >= 5) |
| 7682 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table with a maximum of 5 entries")); |
| 7683 | if (lua_type(L, -1) != LUA_TSTRING) |
| 7684 | WILL_LJMP(luaL_argerror(L, 1, "1st argument must be a table filled with strings")); |
| 7685 | cli_kws->kw[0].str_kw[index] = strdup(lua_tostring(L, -1)); |
| 7686 | if (!cli_kws->kw[0].str_kw[index]) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7687 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7688 | index++; |
| 7689 | lua_pop(L, 1); |
| 7690 | } |
| 7691 | |
| 7692 | /* Copy help message. */ |
| 7693 | cli_kws->kw[0].usage = strdup(message); |
| 7694 | if (!cli_kws->kw[0].usage) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7695 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7696 | |
| 7697 | /* Fill fcn io handler. */ |
| 7698 | len = strlen("<lua.cli>") + 1; |
| 7699 | for (i = 0; i < index; i++) |
| 7700 | len += strlen(cli_kws->kw[0].str_kw[i]) + 1; |
| 7701 | fcn->name = calloc(1, len); |
| 7702 | if (!fcn->name) |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7703 | WILL_LJMP(luaL_error(L, "Lua out of memory error.")); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 7704 | strncat((char *)fcn->name, "<lua.cli", len); |
| 7705 | for (i = 0; i < index; i++) { |
| 7706 | strncat((char *)fcn->name, ".", len); |
| 7707 | strncat((char *)fcn->name, cli_kws->kw[0].str_kw[i], len); |
| 7708 | } |
| 7709 | strncat((char *)fcn->name, ">", len); |
| 7710 | fcn->function_ref = ref_io; |
| 7711 | |
| 7712 | /* Fill last entries. */ |
| 7713 | cli_kws->kw[0].private = fcn; |
| 7714 | cli_kws->kw[0].parse = hlua_cli_parse_fct; |
| 7715 | cli_kws->kw[0].io_handler = hlua_cli_io_handler_fct; |
| 7716 | cli_kws->kw[0].io_release = hlua_cli_io_release_fct; |
| 7717 | |
| 7718 | /* Register this new converter */ |
| 7719 | cli_register_kw(cli_kws); |
| 7720 | |
| 7721 | return 0; |
| 7722 | } |
| 7723 | |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7724 | static int hlua_read_timeout(char **args, int section_type, struct proxy *curpx, |
| 7725 | struct proxy *defpx, const char *file, int line, |
| 7726 | char **err, unsigned int *timeout) |
| 7727 | { |
| 7728 | const char *error; |
| 7729 | |
| 7730 | error = parse_time_err(args[1], timeout, TIME_UNIT_MS); |
Willy Tarreau | 9faebe3 | 2019-06-07 19:00:37 +0200 | [diff] [blame] | 7731 | if (error == PARSE_TIME_OVER) { |
| 7732 | memprintf(err, "timer overflow in argument <%s> to <%s> (maximum value is 2147483647 ms or ~24.8 days)", |
| 7733 | args[1], args[0]); |
| 7734 | return -1; |
| 7735 | } |
| 7736 | else if (error == PARSE_TIME_UNDER) { |
| 7737 | memprintf(err, "timer underflow in argument <%s> to <%s> (minimum non-null value is 1 ms)", |
| 7738 | args[1], args[0]); |
| 7739 | return -1; |
| 7740 | } |
| 7741 | else if (error) { |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7742 | memprintf(err, "%s: invalid timeout", args[0]); |
| 7743 | return -1; |
| 7744 | } |
| 7745 | return 0; |
| 7746 | } |
| 7747 | |
| 7748 | static int hlua_session_timeout(char **args, int section_type, struct proxy *curpx, |
| 7749 | struct proxy *defpx, const char *file, int line, |
| 7750 | char **err) |
| 7751 | { |
| 7752 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7753 | file, line, err, &hlua_timeout_session); |
| 7754 | } |
| 7755 | |
| 7756 | static int hlua_task_timeout(char **args, int section_type, struct proxy *curpx, |
| 7757 | struct proxy *defpx, const char *file, int line, |
| 7758 | char **err) |
| 7759 | { |
| 7760 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7761 | file, line, err, &hlua_timeout_task); |
| 7762 | } |
| 7763 | |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 7764 | static int hlua_applet_timeout(char **args, int section_type, struct proxy *curpx, |
| 7765 | struct proxy *defpx, const char *file, int line, |
| 7766 | char **err) |
| 7767 | { |
| 7768 | return hlua_read_timeout(args, section_type, curpx, defpx, |
| 7769 | file, line, err, &hlua_timeout_applet); |
| 7770 | } |
| 7771 | |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7772 | static int hlua_forced_yield(char **args, int section_type, struct proxy *curpx, |
| 7773 | struct proxy *defpx, const char *file, int line, |
| 7774 | char **err) |
| 7775 | { |
| 7776 | char *error; |
| 7777 | |
| 7778 | hlua_nb_instruction = strtoll(args[1], &error, 10); |
| 7779 | if (*error != '\0') { |
| 7780 | memprintf(err, "%s: invalid number", args[0]); |
| 7781 | return -1; |
| 7782 | } |
| 7783 | return 0; |
| 7784 | } |
| 7785 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7786 | static int hlua_parse_maxmem(char **args, int section_type, struct proxy *curpx, |
| 7787 | struct proxy *defpx, const char *file, int line, |
| 7788 | char **err) |
| 7789 | { |
| 7790 | char *error; |
| 7791 | |
| 7792 | if (*(args[1]) == 0) { |
| 7793 | memprintf(err, "'%s' expects an integer argument (Lua memory size in MB).\n", args[0]); |
| 7794 | return -1; |
| 7795 | } |
| 7796 | hlua_global_allocator.limit = strtoll(args[1], &error, 10) * 1024L * 1024L; |
| 7797 | if (*error != '\0') { |
| 7798 | memprintf(err, "%s: invalid number %s (error at '%c')", args[0], args[1], *error); |
| 7799 | return -1; |
| 7800 | } |
| 7801 | return 0; |
| 7802 | } |
| 7803 | |
| 7804 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7805 | /* This function is called by the main configuration key "lua-load". It loads and |
| 7806 | * execute an lua file during the parsing of the HAProxy configuration file. It is |
| 7807 | * the main lua entry point. |
| 7808 | * |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 7809 | * This function runs with the HAProxy keywords API. It returns -1 if an error |
| 7810 | * occurs, otherwise it returns 0. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7811 | * |
| 7812 | * In some error case, LUA set an error message in top of the stack. This function |
| 7813 | * 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] | 7814 | * |
| 7815 | * This function can fail with an abort() due to an Lua critical error. |
| 7816 | * We are in the configuration parsing process of HAProxy, this abort() is |
| 7817 | * tolerated. |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7818 | */ |
| 7819 | static int hlua_load(char **args, int section_type, struct proxy *curpx, |
| 7820 | struct proxy *defpx, const char *file, int line, |
| 7821 | char **err) |
| 7822 | { |
| 7823 | int error; |
| 7824 | |
| 7825 | /* Just load and compile the file. */ |
| 7826 | error = luaL_loadfile(gL.T, args[1]); |
| 7827 | if (error) { |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7828 | 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] | 7829 | lua_pop(gL.T, 1); |
| 7830 | return -1; |
| 7831 | } |
| 7832 | |
| 7833 | /* If no syntax error where detected, execute the code. */ |
| 7834 | error = lua_pcall(gL.T, 0, LUA_MULTRET, 0); |
| 7835 | switch (error) { |
| 7836 | case LUA_OK: |
| 7837 | break; |
| 7838 | case LUA_ERRRUN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7839 | memprintf(err, "Lua runtime error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7840 | lua_pop(gL.T, 1); |
| 7841 | return -1; |
| 7842 | case LUA_ERRMEM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7843 | memprintf(err, "Lua out of memory error.n"); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7844 | return -1; |
| 7845 | case LUA_ERRERR: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7846 | 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] | 7847 | lua_pop(gL.T, 1); |
| 7848 | return -1; |
| 7849 | case LUA_ERRGCMM: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7850 | 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] | 7851 | lua_pop(gL.T, 1); |
| 7852 | return -1; |
| 7853 | default: |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 7854 | memprintf(err, "Lua unknown error: %s\n", lua_tostring(gL.T, -1)); |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7855 | lua_pop(gL.T, 1); |
| 7856 | return -1; |
| 7857 | } |
| 7858 | |
| 7859 | return 0; |
| 7860 | } |
| 7861 | |
Tim Duesterhus | c9fc9f2 | 2020-01-12 13:55:39 +0100 | [diff] [blame] | 7862 | /* Prepend the given <path> followed by a semicolon to the `package.<type>` variable |
| 7863 | * in the given <ctx>. |
| 7864 | */ |
| 7865 | static int hlua_prepend_path(struct hlua ctx, char *type, char *path) |
| 7866 | { |
| 7867 | lua_getglobal(ctx.T, "package"); /* push package variable */ |
| 7868 | lua_pushstring(ctx.T, path); /* push given path */ |
| 7869 | lua_pushstring(ctx.T, ";"); /* push semicolon */ |
| 7870 | lua_getfield(ctx.T, -3, type); /* push old path */ |
| 7871 | lua_concat(ctx.T, 3); /* concatenate to new path */ |
| 7872 | lua_setfield(ctx.T, -2, type); /* store new path */ |
| 7873 | lua_pop(ctx.T, 1); /* pop package variable */ |
| 7874 | |
| 7875 | return 0; |
| 7876 | } |
| 7877 | |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7878 | static int hlua_config_prepend_path(char **args, int section_type, struct proxy *curpx, |
| 7879 | struct proxy *defpx, const char *file, int line, |
| 7880 | char **err) |
| 7881 | { |
| 7882 | char *path; |
| 7883 | char *type = "path"; |
| 7884 | if (too_many_args(2, args, err, NULL)) { |
| 7885 | return -1; |
| 7886 | } |
| 7887 | |
| 7888 | if (!(*args[1])) { |
| 7889 | memprintf(err, "'%s' expects to receive a <path> as argument", args[0]); |
| 7890 | return -1; |
| 7891 | } |
| 7892 | path = args[1]; |
| 7893 | |
| 7894 | if (*args[2]) { |
| 7895 | if (strcmp(args[2], "path") != 0 && strcmp(args[2], "cpath") != 0) { |
| 7896 | memprintf(err, "'%s' expects <type> to either be 'path' or 'cpath'", args[0]); |
| 7897 | return -1; |
| 7898 | } |
| 7899 | type = args[2]; |
| 7900 | } |
| 7901 | |
| 7902 | return hlua_prepend_path(gL, type, path); |
| 7903 | } |
| 7904 | |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7905 | /* configuration keywords declaration */ |
| 7906 | static struct cfg_kw_list cfg_kws = {{ },{ |
Tim Duesterhus | dd74b5f | 2020-01-12 13:55:40 +0100 | [diff] [blame] | 7907 | { CFG_GLOBAL, "lua-prepend-path", hlua_config_prepend_path }, |
Thierry FOURNIER | bd41349 | 2015-03-03 16:52:26 +0100 | [diff] [blame] | 7908 | { CFG_GLOBAL, "lua-load", hlua_load }, |
| 7909 | { CFG_GLOBAL, "tune.lua.session-timeout", hlua_session_timeout }, |
| 7910 | { CFG_GLOBAL, "tune.lua.task-timeout", hlua_task_timeout }, |
Thierry FOURNIER | 56da101 | 2015-10-01 08:42:31 +0200 | [diff] [blame] | 7911 | { CFG_GLOBAL, "tune.lua.service-timeout", hlua_applet_timeout }, |
Thierry FOURNIER | ee9f802 | 2015-03-03 17:37:37 +0100 | [diff] [blame] | 7912 | { CFG_GLOBAL, "tune.lua.forced-yield", hlua_forced_yield }, |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7913 | { CFG_GLOBAL, "tune.lua.maxmem", hlua_parse_maxmem }, |
Thierry FOURNIER | 6c9b52c | 2015-01-23 15:57:06 +0100 | [diff] [blame] | 7914 | { 0, NULL, NULL }, |
| 7915 | }}; |
| 7916 | |
Willy Tarreau | 0108d90 | 2018-11-25 19:14:37 +0100 | [diff] [blame] | 7917 | INITCALL1(STG_REGISTER, cfg_register_keywords, &cfg_kws); |
| 7918 | |
Christopher Faulet | afd8f10 | 2018-11-08 11:34:21 +0100 | [diff] [blame] | 7919 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 7920 | /* This function can fail with an abort() due to an Lua critical error. |
| 7921 | * We are in the initialisation process of HAProxy, this abort() is |
| 7922 | * tolerated. |
| 7923 | */ |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7924 | int hlua_post_init() |
| 7925 | { |
| 7926 | struct hlua_init_function *init; |
| 7927 | const char *msg; |
| 7928 | enum hlua_exec ret; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 7929 | const char *error; |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7930 | |
Ilya Shipitsin | 856aabc | 2020-04-16 23:51:34 +0500 | [diff] [blame] | 7931 | /* Call post initialisation function in safe environment. */ |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7932 | if (!SET_SAFE_LJMP(gL.T)) { |
| 7933 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 7934 | error = lua_tostring(gL.T, -1); |
| 7935 | else |
| 7936 | error = "critical error"; |
| 7937 | fprintf(stderr, "Lua post-init: %s.\n", error); |
| 7938 | exit(1); |
| 7939 | } |
Frédéric Lécaille | 54f2bcf | 2018-08-29 13:46:24 +0200 | [diff] [blame] | 7940 | |
| 7941 | #if USE_OPENSSL |
| 7942 | /* Initialize SSL server. */ |
| 7943 | if (socket_ssl.xprt->prepare_srv) { |
| 7944 | int saved_used_backed = global.ssl_used_backend; |
| 7945 | // don't affect maxconn automatic computation |
| 7946 | socket_ssl.xprt->prepare_srv(&socket_ssl); |
| 7947 | global.ssl_used_backend = saved_used_backed; |
| 7948 | } |
| 7949 | #endif |
| 7950 | |
Thierry Fournier | 3d4a675 | 2016-02-19 20:53:30 +0100 | [diff] [blame] | 7951 | hlua_fcn_post_init(gL.T); |
| 7952 | RESET_SAFE_LJMP(gL.T); |
| 7953 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7954 | list_for_each_entry(init, &hlua_init_functions, l) { |
| 7955 | lua_rawgeti(gL.T, LUA_REGISTRYINDEX, init->function_ref); |
| 7956 | ret = hlua_ctx_resume(&gL, 0); |
| 7957 | switch (ret) { |
| 7958 | case HLUA_E_OK: |
| 7959 | lua_pop(gL.T, -1); |
| 7960 | return 1; |
| 7961 | case HLUA_E_AGAIN: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7962 | ha_alert("Lua init: yield not allowed.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7963 | return 0; |
| 7964 | case HLUA_E_ERRMSG: |
| 7965 | msg = lua_tostring(gL.T, -1); |
Christopher Faulet | 767a84b | 2017-11-24 16:50:31 +0100 | [diff] [blame] | 7966 | ha_alert("lua init: %s.\n", msg); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7967 | return 0; |
| 7968 | case HLUA_E_ERR: |
| 7969 | default: |
Thierry FOURNIER | 2986c0d | 2018-02-25 14:32:36 +0100 | [diff] [blame] | 7970 | ha_alert("Lua init: unknown runtime error.\n"); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 7971 | return 0; |
| 7972 | } |
| 7973 | } |
| 7974 | return 1; |
| 7975 | } |
| 7976 | |
Willy Tarreau | 32f61e2 | 2015-03-18 17:54:59 +0100 | [diff] [blame] | 7977 | /* The memory allocator used by the Lua stack. <ud> is a pointer to the |
| 7978 | * allocator's context. <ptr> is the pointer to alloc/free/realloc. <osize> |
| 7979 | * is the previously allocated size or the kind of object in case of a new |
| 7980 | * allocation. <nsize> is the requested new size. |
| 7981 | */ |
| 7982 | static void *hlua_alloc(void *ud, void *ptr, size_t osize, size_t nsize) |
| 7983 | { |
| 7984 | struct hlua_mem_allocator *zone = ud; |
| 7985 | |
| 7986 | if (nsize == 0) { |
| 7987 | /* it's a free */ |
| 7988 | if (ptr) |
| 7989 | zone->allocated -= osize; |
| 7990 | free(ptr); |
| 7991 | return NULL; |
| 7992 | } |
| 7993 | |
| 7994 | if (!ptr) { |
| 7995 | /* it's a new allocation */ |
| 7996 | if (zone->limit && zone->allocated + nsize > zone->limit) |
| 7997 | return NULL; |
| 7998 | |
| 7999 | ptr = malloc(nsize); |
| 8000 | if (ptr) |
| 8001 | zone->allocated += nsize; |
| 8002 | return ptr; |
| 8003 | } |
| 8004 | |
| 8005 | /* it's a realloc */ |
| 8006 | if (zone->limit && zone->allocated + nsize - osize > zone->limit) |
| 8007 | return NULL; |
| 8008 | |
| 8009 | ptr = realloc(ptr, nsize); |
| 8010 | if (ptr) |
| 8011 | zone->allocated += nsize - osize; |
| 8012 | return ptr; |
| 8013 | } |
| 8014 | |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8015 | /* Ithis function can fail with an abort() due to an Lua critical error. |
| 8016 | * We are in the initialisation process of HAProxy, this abort() is |
| 8017 | * tolerated. |
| 8018 | */ |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8019 | void hlua_init(void) |
| 8020 | { |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8021 | int i; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8022 | int idx; |
| 8023 | struct sample_fetch *sf; |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8024 | struct sample_conv *sc; |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8025 | char *p; |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8026 | const char *error_msg; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8027 | #ifdef USE_OPENSSL |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8028 | struct srv_kw *kw; |
| 8029 | int tmp_error; |
| 8030 | char *error; |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8031 | char *args[] = { /* SSL client configuration. */ |
| 8032 | "ssl", |
| 8033 | "verify", |
| 8034 | "none", |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8035 | NULL |
| 8036 | }; |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8037 | #endif |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8038 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8039 | /* Init main lua stack. */ |
| 8040 | gL.Mref = LUA_REFNIL; |
Thierry FOURNIER | a097fdf | 2015-03-03 15:17:35 +0100 | [diff] [blame] | 8041 | gL.flags = 0; |
Thierry FOURNIER | 9ff7e6e | 2015-01-23 11:08:20 +0100 | [diff] [blame] | 8042 | LIST_INIT(&gL.com); |
Willy Tarreau | 42ef75f | 2017-04-12 21:40:29 +0200 | [diff] [blame] | 8043 | gL.T = lua_newstate(hlua_alloc, &hlua_global_allocator); |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8044 | hlua_sethlua(&gL); |
| 8045 | gL.Tref = LUA_REFNIL; |
| 8046 | gL.task = NULL; |
| 8047 | |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8048 | /* From this point, until the end of the initialisation function, |
Thierry FOURNIER | babae28 | 2015-09-17 11:36:37 +0200 | [diff] [blame] | 8049 | * the Lua function can fail with an abort. We are in the initialisation |
| 8050 | * process of HAProxy, this abort() is tolerated. |
| 8051 | */ |
| 8052 | |
Thierry FOURNIER | 380d093 | 2015-01-23 14:27:52 +0100 | [diff] [blame] | 8053 | /* Initialise lua. */ |
| 8054 | luaL_openlibs(gL.T); |
Tim Duesterhus | 541fe1e | 2020-01-12 13:55:41 +0100 | [diff] [blame] | 8055 | #define HLUA_PREPEND_PATH_TOSTRING1(x) #x |
| 8056 | #define HLUA_PREPEND_PATH_TOSTRING(x) HLUA_PREPEND_PATH_TOSTRING1(x) |
| 8057 | #ifdef HLUA_PREPEND_PATH |
| 8058 | hlua_prepend_path(gL, "path", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_PATH)); |
| 8059 | #endif |
| 8060 | #ifdef HLUA_PREPEND_CPATH |
| 8061 | hlua_prepend_path(gL, "cpath", HLUA_PREPEND_PATH_TOSTRING(HLUA_PREPEND_CPATH)); |
| 8062 | #endif |
| 8063 | #undef HLUA_PREPEND_PATH_TOSTRING |
| 8064 | #undef HLUA_PREPEND_PATH_TOSTRING1 |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8065 | |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8066 | /* Set safe environment for the initialisation. */ |
| 8067 | if (!SET_SAFE_LJMP(gL.T)) { |
Thierry Fournier | fd107a2 | 2016-02-19 19:57:23 +0100 | [diff] [blame] | 8068 | if (lua_type(gL.T, -1) == LUA_TSTRING) |
| 8069 | error_msg = lua_tostring(gL.T, -1); |
| 8070 | else |
| 8071 | error_msg = "critical error"; |
| 8072 | fprintf(stderr, "Lua init: %s.\n", error_msg); |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8073 | exit(1); |
| 8074 | } |
| 8075 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8076 | /* |
| 8077 | * |
| 8078 | * Create "core" object. |
| 8079 | * |
| 8080 | */ |
| 8081 | |
Thierry FOURNIER | a2d8c65 | 2015-03-11 17:29:39 +0100 | [diff] [blame] | 8082 | /* This table entry is the object "core" base. */ |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8083 | lua_newtable(gL.T); |
| 8084 | |
| 8085 | /* Push the loglevel constants. */ |
Willy Tarreau | 80f5fae | 2015-02-27 16:38:20 +0100 | [diff] [blame] | 8086 | for (i = 0; i < NB_LOG_LEVELS; i++) |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8087 | hlua_class_const_int(gL.T, log_levels[i], i); |
| 8088 | |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8089 | /* Register special functions. */ |
| 8090 | hlua_class_function(gL.T, "register_init", hlua_register_init); |
Thierry FOURNIER | 24f3353 | 2015-01-23 12:13:00 +0100 | [diff] [blame] | 8091 | hlua_class_function(gL.T, "register_task", hlua_register_task); |
Thierry FOURNIER | fa0e5dd | 2015-02-16 20:19:18 +0100 | [diff] [blame] | 8092 | hlua_class_function(gL.T, "register_fetches", hlua_register_fetches); |
Thierry FOURNIER | 9be813f | 2015-02-16 20:21:12 +0100 | [diff] [blame] | 8093 | hlua_class_function(gL.T, "register_converters", hlua_register_converters); |
Thierry FOURNIER | 8255a75 | 2015-09-23 21:03:35 +0200 | [diff] [blame] | 8094 | hlua_class_function(gL.T, "register_action", hlua_register_action); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8095 | hlua_class_function(gL.T, "register_service", hlua_register_service); |
Thierry FOURNIER / OZON.IO | a44fdd9 | 2016-11-13 13:19:20 +0100 | [diff] [blame] | 8096 | hlua_class_function(gL.T, "register_cli", hlua_register_cli); |
Thierry FOURNIER | 13416fe | 2015-02-17 15:01:59 +0100 | [diff] [blame] | 8097 | hlua_class_function(gL.T, "yield", hlua_yield); |
Willy Tarreau | 5955166 | 2015-03-10 14:23:13 +0100 | [diff] [blame] | 8098 | hlua_class_function(gL.T, "set_nice", hlua_set_nice); |
Thierry FOURNIER | 5b8608f | 2015-02-16 19:43:25 +0100 | [diff] [blame] | 8099 | hlua_class_function(gL.T, "sleep", hlua_sleep); |
| 8100 | hlua_class_function(gL.T, "msleep", hlua_msleep); |
Thierry FOURNIER | 83758bb | 2015-02-04 13:21:04 +0100 | [diff] [blame] | 8101 | hlua_class_function(gL.T, "add_acl", hlua_add_acl); |
| 8102 | hlua_class_function(gL.T, "del_acl", hlua_del_acl); |
| 8103 | hlua_class_function(gL.T, "set_map", hlua_set_map); |
| 8104 | hlua_class_function(gL.T, "del_map", hlua_del_map); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8105 | hlua_class_function(gL.T, "tcp", hlua_socket_new); |
Thierry FOURNIER | c798b5d | 2015-03-17 01:09:57 +0100 | [diff] [blame] | 8106 | hlua_class_function(gL.T, "log", hlua_log); |
| 8107 | hlua_class_function(gL.T, "Debug", hlua_log_debug); |
| 8108 | hlua_class_function(gL.T, "Info", hlua_log_info); |
| 8109 | hlua_class_function(gL.T, "Warning", hlua_log_warning); |
| 8110 | hlua_class_function(gL.T, "Alert", hlua_log_alert); |
Thierry FOURNIER | 0a99b89 | 2015-08-26 00:14:17 +0200 | [diff] [blame] | 8111 | hlua_class_function(gL.T, "done", hlua_done); |
Thierry Fournier | fb0b546 | 2016-01-21 09:28:58 +0100 | [diff] [blame] | 8112 | hlua_fcn_reg_core_fcn(gL.T); |
Thierry FOURNIER | a4a0f3d | 2015-01-23 12:08:30 +0100 | [diff] [blame] | 8113 | |
Thierry FOURNIER | 2ba18a2 | 2015-01-23 14:07:08 +0100 | [diff] [blame] | 8114 | lua_setglobal(gL.T, "core"); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8115 | |
| 8116 | /* |
| 8117 | * |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8118 | * Create "act" object. |
| 8119 | * |
| 8120 | */ |
| 8121 | |
| 8122 | /* This table entry is the object "act" base. */ |
| 8123 | lua_newtable(gL.T); |
| 8124 | |
| 8125 | /* push action return constants */ |
| 8126 | hlua_class_const_int(gL.T, "CONTINUE", ACT_RET_CONT); |
| 8127 | hlua_class_const_int(gL.T, "STOP", ACT_RET_STOP); |
| 8128 | hlua_class_const_int(gL.T, "YIELD", ACT_RET_YIELD); |
| 8129 | hlua_class_const_int(gL.T, "ERROR", ACT_RET_ERR); |
| 8130 | hlua_class_const_int(gL.T, "DONE", ACT_RET_DONE); |
| 8131 | hlua_class_const_int(gL.T, "DENY", ACT_RET_DENY); |
| 8132 | hlua_class_const_int(gL.T, "ABORT", ACT_RET_ABRT); |
| 8133 | hlua_class_const_int(gL.T, "INVALID", ACT_RET_INV); |
| 8134 | |
Christopher Faulet | 501465d | 2020-02-26 14:54:16 +0100 | [diff] [blame] | 8135 | hlua_class_function(gL.T, "wake_time", hlua_set_wake_time); |
Christopher Faulet | 2c2c2e3 | 2020-01-31 19:07:52 +0100 | [diff] [blame] | 8136 | |
Christopher Faulet | 0f3c890 | 2020-01-31 18:57:12 +0100 | [diff] [blame] | 8137 | lua_setglobal(gL.T, "act"); |
| 8138 | |
| 8139 | /* |
| 8140 | * |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8141 | * Register class Map |
| 8142 | * |
| 8143 | */ |
| 8144 | |
| 8145 | /* This table entry is the object "Map" base. */ |
| 8146 | lua_newtable(gL.T); |
| 8147 | |
| 8148 | /* register pattern types. */ |
| 8149 | for (i=0; i<PAT_MATCH_NUM; i++) |
| 8150 | hlua_class_const_int(gL.T, pat_match_names[i], i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8151 | for (i=0; i<PAT_MATCH_NUM; i++) { |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8152 | snprintf(trash.area, trash.size, "_%s", pat_match_names[i]); |
| 8153 | hlua_class_const_int(gL.T, trash.area, i); |
Thierry FOURNIER | 4dc7197 | 2017-01-28 08:33:08 +0100 | [diff] [blame] | 8154 | } |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8155 | |
| 8156 | /* register constructor. */ |
| 8157 | hlua_class_function(gL.T, "new", hlua_map_new); |
| 8158 | |
| 8159 | /* Create and fill the metatable. */ |
| 8160 | lua_newtable(gL.T); |
| 8161 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8162 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8163 | lua_pushstring(gL.T, "__index"); |
| 8164 | lua_newtable(gL.T); |
| 8165 | |
| 8166 | /* Register . */ |
| 8167 | hlua_class_function(gL.T, "lookup", hlua_map_lookup); |
| 8168 | hlua_class_function(gL.T, "slookup", hlua_map_slookup); |
| 8169 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8170 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8171 | |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8172 | /* Register previous table in the registry with reference and named entry. |
| 8173 | * The function hlua_register_metatable() pops the stack, so we |
| 8174 | * previously create a copy of the table. |
| 8175 | */ |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8176 | 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] | 8177 | class_map_ref = hlua_register_metatable(gL.T, CLASS_MAP); |
Thierry FOURNIER | 3def393 | 2015-04-07 11:27:54 +0200 | [diff] [blame] | 8178 | |
| 8179 | /* Assign the metatable to the mai Map object. */ |
| 8180 | lua_setmetatable(gL.T, -2); |
| 8181 | |
| 8182 | /* Set a name to the table. */ |
| 8183 | lua_setglobal(gL.T, "Map"); |
| 8184 | |
| 8185 | /* |
| 8186 | * |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8187 | * Register class Channel |
| 8188 | * |
| 8189 | */ |
| 8190 | |
| 8191 | /* Create and fill the metatable. */ |
| 8192 | lua_newtable(gL.T); |
| 8193 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8194 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8195 | lua_pushstring(gL.T, "__index"); |
| 8196 | lua_newtable(gL.T); |
| 8197 | |
| 8198 | /* Register . */ |
| 8199 | hlua_class_function(gL.T, "get", hlua_channel_get); |
| 8200 | hlua_class_function(gL.T, "dup", hlua_channel_dup); |
| 8201 | hlua_class_function(gL.T, "getline", hlua_channel_getline); |
| 8202 | hlua_class_function(gL.T, "set", hlua_channel_set); |
| 8203 | hlua_class_function(gL.T, "append", hlua_channel_append); |
| 8204 | hlua_class_function(gL.T, "send", hlua_channel_send); |
| 8205 | hlua_class_function(gL.T, "forward", hlua_channel_forward); |
| 8206 | hlua_class_function(gL.T, "get_in_len", hlua_channel_get_in_len); |
| 8207 | 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] | 8208 | hlua_class_function(gL.T, "is_full", hlua_channel_is_full); |
Christopher Faulet | 2ac9ba2 | 2020-02-25 10:15:50 +0100 | [diff] [blame] | 8209 | hlua_class_function(gL.T, "is_resp", hlua_channel_is_resp); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8210 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8211 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8212 | |
| 8213 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8214 | class_channel_ref = hlua_register_metatable(gL.T, CLASS_CHANNEL); |
Thierry FOURNIER | 5a6d3fd | 2015-02-09 16:38:34 +0100 | [diff] [blame] | 8215 | |
| 8216 | /* |
| 8217 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8218 | * Register class Fetches |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8219 | * |
| 8220 | */ |
| 8221 | |
| 8222 | /* Create and fill the metatable. */ |
| 8223 | lua_newtable(gL.T); |
| 8224 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8225 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8226 | lua_pushstring(gL.T, "__index"); |
| 8227 | lua_newtable(gL.T); |
| 8228 | |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8229 | /* Browse existing fetches and create the associated |
| 8230 | * object method. |
| 8231 | */ |
| 8232 | sf = NULL; |
| 8233 | while ((sf = sample_fetch_getnext(sf, &idx)) != NULL) { |
| 8234 | |
| 8235 | /* Dont register the keywork if the arguments check function are |
| 8236 | * not safe during the runtime. |
| 8237 | */ |
| 8238 | if ((sf->val_args != NULL) && |
| 8239 | (sf->val_args != val_payload_lv) && |
| 8240 | (sf->val_args != val_hdr)) |
| 8241 | continue; |
| 8242 | |
| 8243 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8244 | * by an underscore. |
| 8245 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8246 | strncpy(trash.area, sf->kw, trash.size); |
| 8247 | trash.area[trash.size - 1] = '\0'; |
| 8248 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8249 | if (*p == '.' || *p == '-' || *p == '+') |
| 8250 | *p = '_'; |
| 8251 | |
| 8252 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8253 | lua_pushstring(gL.T, trash.area); |
Willy Tarreau | 2ec2274 | 2015-03-10 14:27:20 +0100 | [diff] [blame] | 8254 | lua_pushlightuserdata(gL.T, sf); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8255 | lua_pushcclosure(gL.T, hlua_run_sample_fetch, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8256 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | d0fa538 | 2015-02-16 20:14:51 +0100 | [diff] [blame] | 8257 | } |
| 8258 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8259 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8260 | |
| 8261 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8262 | class_fetches_ref = hlua_register_metatable(gL.T, CLASS_FETCHES); |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8263 | |
| 8264 | /* |
| 8265 | * |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8266 | * Register class Converters |
| 8267 | * |
| 8268 | */ |
| 8269 | |
| 8270 | /* Create and fill the metatable. */ |
| 8271 | lua_newtable(gL.T); |
| 8272 | |
| 8273 | /* Create and fill the __index entry. */ |
| 8274 | lua_pushstring(gL.T, "__index"); |
| 8275 | lua_newtable(gL.T); |
| 8276 | |
| 8277 | /* Browse existing converters and create the associated |
| 8278 | * object method. |
| 8279 | */ |
| 8280 | sc = NULL; |
| 8281 | while ((sc = sample_conv_getnext(sc, &idx)) != NULL) { |
| 8282 | /* Dont register the keywork if the arguments check function are |
| 8283 | * not safe during the runtime. |
| 8284 | */ |
| 8285 | if (sc->val_args != NULL) |
| 8286 | continue; |
| 8287 | |
| 8288 | /* gL.Tua doesn't support '.' and '-' in the function names, replace it |
| 8289 | * by an underscore. |
| 8290 | */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8291 | strncpy(trash.area, sc->kw, trash.size); |
| 8292 | trash.area[trash.size - 1] = '\0'; |
| 8293 | for (p = trash.area; *p; p++) |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8294 | if (*p == '.' || *p == '-' || *p == '+') |
| 8295 | *p = '_'; |
| 8296 | |
| 8297 | /* Register the function. */ |
Willy Tarreau | 843b7cb | 2018-07-13 10:54:26 +0200 | [diff] [blame] | 8298 | lua_pushstring(gL.T, trash.area); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8299 | lua_pushlightuserdata(gL.T, sc); |
| 8300 | lua_pushcclosure(gL.T, hlua_run_sample_conv, 1); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8301 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8302 | } |
| 8303 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8304 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8305 | |
| 8306 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8307 | class_converters_ref = hlua_register_metatable(gL.T, CLASS_CONVERTERS); |
Thierry FOURNIER | 594afe7 | 2015-03-10 23:58:30 +0100 | [diff] [blame] | 8308 | |
| 8309 | /* |
| 8310 | * |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8311 | * Register class HTTP |
| 8312 | * |
| 8313 | */ |
| 8314 | |
| 8315 | /* Create and fill the metatable. */ |
| 8316 | lua_newtable(gL.T); |
| 8317 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8318 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8319 | lua_pushstring(gL.T, "__index"); |
| 8320 | lua_newtable(gL.T); |
| 8321 | |
| 8322 | /* Register Lua functions. */ |
| 8323 | hlua_class_function(gL.T, "req_get_headers",hlua_http_req_get_headers); |
| 8324 | hlua_class_function(gL.T, "req_del_header", hlua_http_req_del_hdr); |
| 8325 | hlua_class_function(gL.T, "req_rep_header", hlua_http_req_rep_hdr); |
| 8326 | hlua_class_function(gL.T, "req_rep_value", hlua_http_req_rep_val); |
| 8327 | hlua_class_function(gL.T, "req_add_header", hlua_http_req_add_hdr); |
| 8328 | hlua_class_function(gL.T, "req_set_header", hlua_http_req_set_hdr); |
| 8329 | hlua_class_function(gL.T, "req_set_method", hlua_http_req_set_meth); |
| 8330 | hlua_class_function(gL.T, "req_set_path", hlua_http_req_set_path); |
| 8331 | hlua_class_function(gL.T, "req_set_query", hlua_http_req_set_query); |
| 8332 | hlua_class_function(gL.T, "req_set_uri", hlua_http_req_set_uri); |
| 8333 | |
| 8334 | hlua_class_function(gL.T, "res_get_headers",hlua_http_res_get_headers); |
| 8335 | hlua_class_function(gL.T, "res_del_header", hlua_http_res_del_hdr); |
| 8336 | hlua_class_function(gL.T, "res_rep_header", hlua_http_res_rep_hdr); |
| 8337 | hlua_class_function(gL.T, "res_rep_value", hlua_http_res_rep_val); |
| 8338 | hlua_class_function(gL.T, "res_add_header", hlua_http_res_add_hdr); |
| 8339 | 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] | 8340 | 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] | 8341 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8342 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8343 | |
| 8344 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8345 | class_http_ref = hlua_register_metatable(gL.T, CLASS_HTTP); |
Thierry FOURNIER | 08504f4 | 2015-03-16 14:17:08 +0100 | [diff] [blame] | 8346 | |
| 8347 | /* |
| 8348 | * |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8349 | * Register class AppletTCP |
| 8350 | * |
| 8351 | */ |
| 8352 | |
| 8353 | /* Create and fill the metatable. */ |
| 8354 | lua_newtable(gL.T); |
| 8355 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8356 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8357 | lua_pushstring(gL.T, "__index"); |
| 8358 | lua_newtable(gL.T); |
| 8359 | |
| 8360 | /* Register Lua functions. */ |
Thierry FOURNIER / OZON.IO | 3e1d791 | 2016-12-12 12:29:34 +0100 | [diff] [blame] | 8361 | hlua_class_function(gL.T, "getline", hlua_applet_tcp_getline); |
| 8362 | hlua_class_function(gL.T, "receive", hlua_applet_tcp_recv); |
| 8363 | hlua_class_function(gL.T, "send", hlua_applet_tcp_send); |
| 8364 | hlua_class_function(gL.T, "set_priv", hlua_applet_tcp_set_priv); |
| 8365 | 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] | 8366 | hlua_class_function(gL.T, "set_var", hlua_applet_tcp_set_var); |
| 8367 | hlua_class_function(gL.T, "unset_var", hlua_applet_tcp_unset_var); |
| 8368 | hlua_class_function(gL.T, "get_var", hlua_applet_tcp_get_var); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8369 | |
| 8370 | lua_settable(gL.T, -3); |
| 8371 | |
| 8372 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8373 | class_applet_tcp_ref = hlua_register_metatable(gL.T, CLASS_APPLET_TCP); |
Thierry FOURNIER | f0a64b6 | 2015-09-19 12:36:17 +0200 | [diff] [blame] | 8374 | |
| 8375 | /* |
| 8376 | * |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8377 | * Register class AppletHTTP |
| 8378 | * |
| 8379 | */ |
| 8380 | |
| 8381 | /* Create and fill the metatable. */ |
| 8382 | lua_newtable(gL.T); |
| 8383 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8384 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8385 | lua_pushstring(gL.T, "__index"); |
| 8386 | lua_newtable(gL.T); |
| 8387 | |
| 8388 | /* Register Lua functions. */ |
Thierry FOURNIER | 8db004c | 2015-12-25 01:33:18 +0100 | [diff] [blame] | 8389 | hlua_class_function(gL.T, "set_priv", hlua_applet_http_set_priv); |
| 8390 | 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] | 8391 | hlua_class_function(gL.T, "set_var", hlua_applet_http_set_var); |
| 8392 | hlua_class_function(gL.T, "unset_var", hlua_applet_http_unset_var); |
| 8393 | hlua_class_function(gL.T, "get_var", hlua_applet_http_get_var); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8394 | hlua_class_function(gL.T, "getline", hlua_applet_http_getline); |
| 8395 | hlua_class_function(gL.T, "receive", hlua_applet_http_recv); |
| 8396 | hlua_class_function(gL.T, "send", hlua_applet_http_send); |
| 8397 | hlua_class_function(gL.T, "add_header", hlua_applet_http_addheader); |
| 8398 | hlua_class_function(gL.T, "set_status", hlua_applet_http_status); |
| 8399 | hlua_class_function(gL.T, "start_response", hlua_applet_http_start_response); |
| 8400 | |
| 8401 | lua_settable(gL.T, -3); |
| 8402 | |
| 8403 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8404 | class_applet_http_ref = hlua_register_metatable(gL.T, CLASS_APPLET_HTTP); |
Thierry FOURNIER | a30b5db | 2015-09-18 09:04:27 +0200 | [diff] [blame] | 8405 | |
| 8406 | /* |
| 8407 | * |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8408 | * Register class TXN |
| 8409 | * |
| 8410 | */ |
| 8411 | |
| 8412 | /* Create and fill the metatable. */ |
| 8413 | lua_newtable(gL.T); |
| 8414 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8415 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | bb53c7b | 2015-03-11 18:28:02 +0100 | [diff] [blame] | 8416 | lua_pushstring(gL.T, "__index"); |
| 8417 | lua_newtable(gL.T); |
| 8418 | |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8419 | /* Register Lua functions. */ |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8420 | hlua_class_function(gL.T, "set_priv", hlua_set_priv); |
| 8421 | hlua_class_function(gL.T, "get_priv", hlua_get_priv); |
| 8422 | hlua_class_function(gL.T, "set_var", hlua_set_var); |
| 8423 | hlua_class_function(gL.T, "unset_var", hlua_unset_var); |
| 8424 | hlua_class_function(gL.T, "get_var", hlua_get_var); |
| 8425 | hlua_class_function(gL.T, "done", hlua_txn_done); |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8426 | hlua_class_function(gL.T, "reply", hlua_txn_reply_new); |
Patrick Hemmer | 268a707 | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8427 | hlua_class_function(gL.T, "set_loglevel", hlua_txn_set_loglevel); |
| 8428 | hlua_class_function(gL.T, "set_tos", hlua_txn_set_tos); |
| 8429 | hlua_class_function(gL.T, "set_mark", hlua_txn_set_mark); |
| 8430 | hlua_class_function(gL.T, "set_priority_class", hlua_txn_set_priority_class); |
| 8431 | hlua_class_function(gL.T, "set_priority_offset", hlua_txn_set_priority_offset); |
| 8432 | hlua_class_function(gL.T, "deflog", hlua_txn_deflog); |
| 8433 | hlua_class_function(gL.T, "log", hlua_txn_log); |
| 8434 | hlua_class_function(gL.T, "Debug", hlua_txn_log_debug); |
| 8435 | hlua_class_function(gL.T, "Info", hlua_txn_log_info); |
| 8436 | hlua_class_function(gL.T, "Warning", hlua_txn_log_warning); |
| 8437 | hlua_class_function(gL.T, "Alert", hlua_txn_log_alert); |
Thierry FOURNIER | 05c0b8a | 2015-02-25 11:43:21 +0100 | [diff] [blame] | 8438 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8439 | lua_rawset(gL.T, -3); |
Thierry FOURNIER | 65f34c6 | 2015-02-16 20:11:43 +0100 | [diff] [blame] | 8440 | |
| 8441 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8442 | class_txn_ref = hlua_register_metatable(gL.T, CLASS_TXN); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8443 | |
| 8444 | /* |
| 8445 | * |
Christopher Faulet | 700d9e8 | 2020-01-31 12:21:52 +0100 | [diff] [blame] | 8446 | * Register class reply |
| 8447 | * |
| 8448 | */ |
| 8449 | lua_newtable(gL.T); |
| 8450 | lua_pushstring(gL.T, "__index"); |
| 8451 | lua_newtable(gL.T); |
| 8452 | hlua_class_function(gL.T, "set_status", hlua_txn_reply_set_status); |
| 8453 | hlua_class_function(gL.T, "add_header", hlua_txn_reply_add_header); |
| 8454 | hlua_class_function(gL.T, "del_header", hlua_txn_reply_del_header); |
| 8455 | hlua_class_function(gL.T, "set_body", hlua_txn_reply_set_body); |
| 8456 | lua_settable(gL.T, -3); /* Sets the __index entry. */ |
| 8457 | class_txn_reply_ref = luaL_ref(gL.T, LUA_REGISTRYINDEX); |
| 8458 | |
| 8459 | |
| 8460 | /* |
| 8461 | * |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8462 | * Register class Socket |
| 8463 | * |
| 8464 | */ |
| 8465 | |
| 8466 | /* Create and fill the metatable. */ |
| 8467 | lua_newtable(gL.T); |
| 8468 | |
Ilya Shipitsin | d425950 | 2020-04-08 01:07:56 +0500 | [diff] [blame] | 8469 | /* Create and fill the __index entry. */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8470 | lua_pushstring(gL.T, "__index"); |
| 8471 | lua_newtable(gL.T); |
| 8472 | |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8473 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8474 | hlua_class_function(gL.T, "connect_ssl", hlua_socket_connect_ssl); |
Baptiste Assmann | 84bb493 | 2015-03-02 21:40:06 +0100 | [diff] [blame] | 8475 | #endif |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8476 | hlua_class_function(gL.T, "connect", hlua_socket_connect); |
| 8477 | hlua_class_function(gL.T, "send", hlua_socket_send); |
| 8478 | hlua_class_function(gL.T, "receive", hlua_socket_receive); |
| 8479 | hlua_class_function(gL.T, "close", hlua_socket_close); |
| 8480 | hlua_class_function(gL.T, "getpeername", hlua_socket_getpeername); |
| 8481 | hlua_class_function(gL.T, "getsockname", hlua_socket_getsockname); |
| 8482 | hlua_class_function(gL.T, "setoption", hlua_socket_setoption); |
| 8483 | hlua_class_function(gL.T, "settimeout", hlua_socket_settimeout); |
| 8484 | |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8485 | 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] | 8486 | |
| 8487 | /* Register the garbage collector entry. */ |
| 8488 | lua_pushstring(gL.T, "__gc"); |
| 8489 | lua_pushcclosure(gL.T, hlua_socket_gc, 0); |
Thierry FOURNIER | 84e73c8 | 2015-09-25 22:13:32 +0200 | [diff] [blame] | 8490 | 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] | 8491 | |
| 8492 | /* Register previous table in the registry with reference and named entry. */ |
Thierry Fournier | 45e78d7 | 2016-02-19 18:34:46 +0100 | [diff] [blame] | 8493 | class_socket_ref = hlua_register_metatable(gL.T, CLASS_SOCKET); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8494 | |
| 8495 | /* Proxy and server configuration initialisation. */ |
| 8496 | memset(&socket_proxy, 0, sizeof(socket_proxy)); |
| 8497 | init_new_proxy(&socket_proxy); |
| 8498 | socket_proxy.parent = NULL; |
| 8499 | socket_proxy.last_change = now.tv_sec; |
| 8500 | socket_proxy.id = "LUA-SOCKET"; |
| 8501 | socket_proxy.cap = PR_CAP_FE | PR_CAP_BE; |
| 8502 | socket_proxy.maxconn = 0; |
| 8503 | socket_proxy.accept = NULL; |
| 8504 | socket_proxy.options2 |= PR_O2_INDEPSTR; |
| 8505 | socket_proxy.srv = NULL; |
| 8506 | socket_proxy.conn_retries = 0; |
| 8507 | socket_proxy.timeout.connect = 5000; /* By default the timeout connection is 5s. */ |
| 8508 | |
| 8509 | /* Init TCP server: unchanged parameters */ |
| 8510 | memset(&socket_tcp, 0, sizeof(socket_tcp)); |
| 8511 | socket_tcp.next = NULL; |
| 8512 | socket_tcp.proxy = &socket_proxy; |
| 8513 | socket_tcp.obj_type = OBJ_TYPE_SERVER; |
| 8514 | LIST_INIT(&socket_tcp.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8515 | socket_tcp.pendconns = EB_ROOT; |
Christopher Faulet | 40a007c | 2017-07-03 15:41:01 +0200 | [diff] [blame] | 8516 | socket_tcp.idle_conns = NULL; |
| 8517 | socket_tcp.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8518 | socket_tcp.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8519 | socket_tcp.last_change = 0; |
| 8520 | socket_tcp.id = "LUA-TCP-CONN"; |
| 8521 | socket_tcp.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8522 | socket_tcp.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8523 | socket_tcp.pp_opts = 0; /* Remove proxy protocol. */ |
| 8524 | |
| 8525 | /* XXX: Copy default parameter from default server, |
| 8526 | * but the default server is not initialized. |
| 8527 | */ |
| 8528 | socket_tcp.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8529 | socket_tcp.minconn = socket_proxy.defsrv.minconn; |
| 8530 | socket_tcp.maxconn = socket_proxy.defsrv.maxconn; |
| 8531 | socket_tcp.slowstart = socket_proxy.defsrv.slowstart; |
| 8532 | socket_tcp.onerror = socket_proxy.defsrv.onerror; |
| 8533 | socket_tcp.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8534 | socket_tcp.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8535 | socket_tcp.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8536 | socket_tcp.uweight = socket_proxy.defsrv.iweight; |
| 8537 | socket_tcp.iweight = socket_proxy.defsrv.iweight; |
| 8538 | |
| 8539 | socket_tcp.check.status = HCHK_STATUS_INI; |
| 8540 | socket_tcp.check.rise = socket_proxy.defsrv.check.rise; |
| 8541 | socket_tcp.check.fall = socket_proxy.defsrv.check.fall; |
| 8542 | socket_tcp.check.health = socket_tcp.check.rise; /* socket, but will fall down at first failure */ |
| 8543 | socket_tcp.check.server = &socket_tcp; |
| 8544 | |
| 8545 | socket_tcp.agent.status = HCHK_STATUS_INI; |
| 8546 | socket_tcp.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8547 | socket_tcp.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8548 | socket_tcp.agent.health = socket_tcp.agent.rise; /* socket, but will fall down at first failure */ |
| 8549 | socket_tcp.agent.server = &socket_tcp; |
| 8550 | |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8551 | socket_tcp.xprt = xprt_get(XPRT_RAW); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8552 | |
| 8553 | #ifdef USE_OPENSSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8554 | /* Init TCP server: unchanged parameters */ |
| 8555 | memset(&socket_ssl, 0, sizeof(socket_ssl)); |
| 8556 | socket_ssl.next = NULL; |
| 8557 | socket_ssl.proxy = &socket_proxy; |
| 8558 | socket_ssl.obj_type = OBJ_TYPE_SERVER; |
| 8559 | LIST_INIT(&socket_ssl.actconns); |
Patrick Hemmer | 0355dab | 2018-05-11 12:52:31 -0400 | [diff] [blame] | 8560 | socket_ssl.pendconns = EB_ROOT; |
Willy Tarreau | b784b35 | 2019-02-07 14:48:24 +0100 | [diff] [blame] | 8561 | socket_ssl.idle_conns = NULL; |
| 8562 | socket_ssl.safe_conns = NULL; |
Emeric Brun | 52a91d3 | 2017-08-31 14:41:55 +0200 | [diff] [blame] | 8563 | socket_ssl.next_state = SRV_ST_RUNNING; /* early server setup */ |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8564 | socket_ssl.last_change = 0; |
| 8565 | socket_ssl.id = "LUA-SSL-CONN"; |
| 8566 | socket_ssl.check.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8567 | socket_ssl.agent.state &= ~CHK_ST_ENABLED; /* Disable health checks. */ |
| 8568 | socket_ssl.pp_opts = 0; /* Remove proxy protocol. */ |
| 8569 | |
| 8570 | /* XXX: Copy default parameter from default server, |
| 8571 | * but the default server is not initialized. |
| 8572 | */ |
| 8573 | socket_ssl.maxqueue = socket_proxy.defsrv.maxqueue; |
| 8574 | socket_ssl.minconn = socket_proxy.defsrv.minconn; |
| 8575 | socket_ssl.maxconn = socket_proxy.defsrv.maxconn; |
| 8576 | socket_ssl.slowstart = socket_proxy.defsrv.slowstart; |
| 8577 | socket_ssl.onerror = socket_proxy.defsrv.onerror; |
| 8578 | socket_ssl.onmarkeddown = socket_proxy.defsrv.onmarkeddown; |
| 8579 | socket_ssl.onmarkedup = socket_proxy.defsrv.onmarkedup; |
| 8580 | socket_ssl.consecutive_errors_limit = socket_proxy.defsrv.consecutive_errors_limit; |
| 8581 | socket_ssl.uweight = socket_proxy.defsrv.iweight; |
| 8582 | socket_ssl.iweight = socket_proxy.defsrv.iweight; |
| 8583 | |
| 8584 | socket_ssl.check.status = HCHK_STATUS_INI; |
| 8585 | socket_ssl.check.rise = socket_proxy.defsrv.check.rise; |
| 8586 | socket_ssl.check.fall = socket_proxy.defsrv.check.fall; |
| 8587 | socket_ssl.check.health = socket_ssl.check.rise; /* socket, but will fall down at first failure */ |
| 8588 | socket_ssl.check.server = &socket_ssl; |
| 8589 | |
| 8590 | socket_ssl.agent.status = HCHK_STATUS_INI; |
| 8591 | socket_ssl.agent.rise = socket_proxy.defsrv.agent.rise; |
| 8592 | socket_ssl.agent.fall = socket_proxy.defsrv.agent.fall; |
| 8593 | socket_ssl.agent.health = socket_ssl.agent.rise; /* socket, but will fall down at first failure */ |
| 8594 | socket_ssl.agent.server = &socket_ssl; |
| 8595 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8596 | socket_ssl.use_ssl = 1; |
Willy Tarreau | a261e9b | 2016-12-22 20:44:00 +0100 | [diff] [blame] | 8597 | socket_ssl.xprt = xprt_get(XPRT_SSL); |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8598 | |
Thierry FOURNIER | 36d1374 | 2015-03-17 16:48:53 +0100 | [diff] [blame] | 8599 | for (idx = 0; args[idx] != NULL; idx++) { |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8600 | if ((kw = srv_find_kw(args[idx])) != NULL) { /* Maybe it's registered server keyword */ |
| 8601 | /* |
| 8602 | * |
| 8603 | * If the keyword is not known, we can search in the registered |
Joseph Herlant | b8f9c5e | 2018-11-15 10:06:08 -0800 | [diff] [blame] | 8604 | * server keywords. This is useful to configure special SSL |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8605 | * features like client certificates and ssl_verify. |
| 8606 | * |
| 8607 | */ |
| 8608 | tmp_error = kw->parse(args, &idx, &socket_proxy, &socket_ssl, &error); |
| 8609 | if (tmp_error != 0) { |
| 8610 | fprintf(stderr, "INTERNAL ERROR: %s\n", error); |
| 8611 | abort(); /* This must be never arrives because the command line |
| 8612 | not editable by the user. */ |
| 8613 | } |
| 8614 | idx += kw->skip; |
| 8615 | } |
| 8616 | } |
Thierry FOURNIER | 7e7ac32 | 2015-02-16 19:27:16 +0100 | [diff] [blame] | 8617 | #endif |
Thierry Fournier | 75933d4 | 2016-01-21 09:30:18 +0100 | [diff] [blame] | 8618 | |
| 8619 | RESET_SAFE_LJMP(gL.T); |
Thierry FOURNIER | 6f1fd48 | 2015-01-23 14:06:13 +0100 | [diff] [blame] | 8620 | } |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8621 | |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8622 | static void hlua_register_build_options(void) |
| 8623 | { |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8624 | char *ptr = NULL; |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8625 | |
Willy Tarreau | bb57d94 | 2016-12-21 19:04:56 +0100 | [diff] [blame] | 8626 | memprintf(&ptr, "Built with Lua version : %s", LUA_RELEASE); |
| 8627 | hap_register_build_opts(ptr, 1); |
| 8628 | } |
Willy Tarreau | 8071338 | 2018-11-26 10:19:54 +0100 | [diff] [blame] | 8629 | |
| 8630 | INITCALL0(STG_REGISTER, hlua_register_build_options); |